2017 universities Ranking in Pakistan

Here are overall top universities of Pakistan.Some universities are at the top place in engineering fields but others are at Applied science, BA and in IT programs

Binary Search in C++

Search found at index

Quick sorting program

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Tree traversal (preorder,inorder,postorder) using linklist (c++)

Tree traversal (preorder,inorder,postorder) using linklist (c++)

Sunday 16 July 2023

For Each Loop in Java: A Beginner's Guide

 The "for each loop in Java" is a powerful tool for iterating through arrays and collections. In this guide, we will learn how to use the for each loop, including its syntax, examples, and benefits.

For Each Loop in Java

In Java, the "for each" loop, also known as the enhanced for loop, is a simplified way to iterate over elements in an array or a collection. It is designed to make looping through elements more convenient and readable.

Syntax:

for (datatype variable : array/collection) {
    // code to be executed for each element
}

Example:

public class ForEachLoopExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};

        // Printing each element of the array using the for-each loop
        for (int num : numbers) {
            System.out.println(num);
        }
    }
}

Output:

1
2
3
4
5

Benefits of Using the For Each Loop:

  • Simplicity: The for-each loop simplifies the code and makes it easier to read and understand.
  • Readability: The for-each loop clearly expresses the intent of iterating over each element.
  • Avoiding Index Errors: It helps prevent index-related errors, such as going out of bounds or skipping elements.
  • Type Safety: The for-each loop ensures type safety by guaranteeing the loop variable's datatype matches the elements.

FAQs:

  1. Can I modify elements while using the for-each loop?
    No, the for-each loop is designed for read-only access to elements. You cannot modify elements using the for-each loop.
  2. Which data types can be used with the for-each loop?
    The for-each loop can be used with arrays and any data structure implementing the `Iterable` interface.
  3. Can I use the for-each loop if I need to access the index of each element?
    No, the for-each loop does not provide direct access to the index. Use a traditional for loop instead.
  4. Can I use the for-each loop with a null array or collection?
    No, the array or collection must have valid elements. Using a null array or collection will result in a `NullPointerException`.
  5. Can I break out of a for-each loop prematurely?
    No, the for-each loop does not provide a direct way to break out prematurely. Use a traditional for loop or other control structures if needed.

Monday 25 March 2019

LinkedList implementation in JAVA



import java.util.Scanner;

class Node


{


  int data;


  Node next;


}


//**********************LinkedList Class**********************//


public class LinkedList


{


 Node head;


     public void insert(int d)


      { 


           Node temp=new Node();


           temp.data=d;


          temp.next=null;


          if(head==null)

        {
           head=temp;
        }
        else
        {
           Node n;
           n=head;

             while(n.next!=null)

             {
               n=n.next;
              }

               n.next=temp;

         }

    }




   public void show()

    {
   Node s;
   s=head;
     while(s.next!=null)
            {
              System.out.println(s.data);
              s=s.next;
             }
               System.out.println(s.data);
      }

}


//*********************Driver Class***********************//



import java.util.Scanner;


public class Driver

    {
 public static void main(String []args)
  {
          Scanner s=new Scanner(System.in);
          LinkedList l=new LinkedList();
          int x;

           do{
                 System.out.println("Enter number in Linkedlist:");

   int n=s.nextInt();


   l.insert(n);


   System.out.println("Do you want to enter another number(1=yes): ");


   x=s.nextInt();


          }while(x==1);




               System.out.println("The numbers you entered in Linkedlist are:");


  l.show();


  }



}


import java.util.Scanner;


class Node


{


  int data;


  Node next;


}


//**********************LinkedList Class**********************//


public class LinkedList


{


 Node head;


 public void insert(int d)


 {


  Node temp=new Node();


  temp.data=d;


  temp.next=null;


  if(head==null)


  {


   head=temp;


  }


  else


  {


   Node n;


   n=head;


   while(n.next!=null)


   {


    n=n.next;


   }


   n.next=temp;


  }


 }




 public void show()


  {


   Node s;


   s=head;


   while(s.next!=null)


   {


   System.out.println(s.data);


   s=s.next;


   }


   System.out.println(s.data);


  }


}


//*********************Driver Class***********************//



import java.util.Scanner;


public class Driver


{




 public static void main(String []args)


  {




                Scanner s=new Scanner(System.in);


  LinkedList l=new LinkedList();




  int x;


 


                 do{


   System.out.println("Enter number in Linkedlist:");


   int n=s.nextInt();


   l.insert(n);


   System.out.println("Do you want to enter another number(1=yes): ");


   x=s.nextInt();


          }while(x==1);




               System.out.println("The numbers you entered in Linkedlist are:");


  l.show();

}
}

Wednesday 13 March 2019

Enum class in java (Simple program)


Days.java

public enum Days
{
Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
};

Test.java

public class Test
{
public static void main(String []args)
{
Days d;
Days enumarr[]=Days.values();
for(Days x:enumarr)
{
System.out.print(x.ordinal());  // it should print the constant value
System.out.print(" "+x);   // it should print all the days
System.out.print("\n");
}
System.out.println("Printing using Valueof()");
System.out.println(Days.valueOf("Friday"));        //it should print Friday
}
}

Three dimensional array in java using ForEachLoop



import java.util.*;
public class ForEachLoop
{
public static void main(String []args)
{

Scanner s=new Scanner(System.in);
System.out.println("Enter number of Pages:");
int p=s.nextInt();
System.out.println("Enter number of rows:");
int r=s.nextInt();
System.out.println("Enter number of colums:");
int c=s.nextInt();

int arr[][][]=new int[p][][];

for(int i=0;i<p;i++)
        {
arr[i]=new int[r][];

            for(int k=0;k<r;k++)
            {

                arr[i][k]=new int[c];
            }
        }


System.out.println("Enter numbers in Three dimentional Array:");

for(int j=0;j<arr.length;j++)
for(int i=0;i<arr[j].length;i++)
for(int k=0;k<arr[j][i].length;k++)
{
arr[j][i][k]=s.nextInt();
}


System.out.println("printing");

for(int x[][] : arr)
for(int i[]:x)
for(int j:i)
{
System.out.println(j);
}

}
}

Predefined Array-Copy method in JAVA


public class ArrayCopy
{
public static void main(String []args)
{
int arr[]=new int[]{1,2,3,4,5,6,7,8,9,10};
for(int i:arr)
{
System.out.print(i);
}
int arr1[]=new int[10];
System.arraycopy(arr,5,arr1,0,5);
for(int i:arr1)
{
System.out.println(i);
}
}
}

Tuesday 12 March 2019

User Define Class array Simple Java program


import java.util.Scanner;

public class ZamaCodes
{
public static void main(String[] args)
{
Employee E=new Employee();
Employee Earr[]=new Employee[5];
        System.out.println(E.getAge());
   
    for(int k=0;k<5;k++)
    {
         Earr[k]=new Employee();
    }

        Earr[0].setAge(10);
Earr[1].setAge(20);

       System.out.println(Earr[0].getAge());
       System.out.println(Earr[1].getAge());

}
}




public class Employee {
    
    private String name;
    private int age;
    private float salary;
    
    public Employee()
    {
        name=null;
        age=0;
        salary=0;
    }
    public Employee(String name,int age,float Salary)
    {
        this.name=name;
       this.age=age;
        this.salary=Salary;
    }
    
    
//**********************setters*************************//

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }
//**********************Getters*************************//
    public String getName() {
        return name;
    }

    public int getAge() {
        return this.age;
    }

    public float getSalary() {
        return salary;
    }

    public void aging()
    {
        this.age=this.age+1;
    }    
}

Find x position in Matrix (JAVA)

 Declare, Instantiate, initialize and print a 2Dimensional array of numbers as follows.                                   

 Given a matrix and a number x, find the position of x in the matrix if it is present in it else print “Not Found”.  



import java.util.Scanner;
public class ZamaCodes
{
public static void main(String []args)
{
int arr[][]=new int[4][];
arr[0]=new int[1];
arr[1]=new int[2];
arr[2]=new int[3];
arr[3]=new int[4];

Scanner s=new Scanner(System.in);
System.out.println("Enter the numbers");
for(int i=0;i<arr.length;i++)
for(int k=0;k<arr[i].length;k++)
{

arr[i][k]=s.nextInt();

}

System.out.println("Printing");
for(int v[] : arr)
for(int x : v)
{
System.out.println(x);
}

System.out.println("Enter the number you want to search in 2d array:");
int x;
x=s.nextInt();

for(int i=0;i<arr.length;i++)
for(int k=0;k<arr[i].length;k++)
if(x==arr[i][k])
{
System.out.println("Value found at index"+"ROW="+i+"Colum="+k);
}
else
{
System.out.println("Not Found");
}
}
}

Initialize 1-D array of 10 numbers and get minimum and maximum in JAVA


Declare and initialize a one-dimensional array of 10 numbers and get the minimum number of an array using methods.



import java.util.Scanner;
public class ZamaCodes
{
public static void main(String []args)
{
Scanner s=new Scanner(System.in);
int arr[]=new int[10];
System.out.println("Enter 10 numbers in an array:");
for(int i=0;i<10;i++)
{
arr[i]=s.nextInt();
}

Zamacodes q=new Zamacodes();
System.out.println("The minimum number in an array is "+q.min(arr));
System.out.println("The maximum number in an array is "+q.max(arr));

}

int min(int arr[])
{
int m=1;
for(int i=0;i<arr.length;i++)
{
if(arr[i]<m)
{
m=arr[i];
}
}
return m;
}

int max(int arr[])
{
int ma=1;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>ma)
{
ma=arr[i];
}
}
return ma;
}
}

Declaration of one dimensional array of numbers and characters in JAVA


-Declare and initialize two one dimensional arrays with 1) Numbers  2) Characters 1.1-Print the content of both arrays 1.2-Get the length of both arrays and print 


import java.util.Scanner;
public class SimpleJavaProgram
{
public static void main(String []args)
{
int arr[];


Scanner s=new Scanner(System.in);
int x;
System.out.println("How many elements you want to store in an array?");
x=s.nextInt();
arr=new int[x];
for(int i=0;i<arr.length;i++)
{
arr[i]=s.nextInt();
}
System.out.println("Print the values");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}

System.out.println("How many number you want to store in charactor array?");
int c=s.nextInt();
char ch[]=new char[c];

for(int i=0;i<ch.length;i++)
{
ch[i]=s.next().charAt(0);
}
for(int i=0;i<ch.length;i++)
{
System.out.println(ch[i]);
}
System.out.print(arr.length);
}
}

Wednesday 20 February 2019

Use of Objects in Java (Basic Program)



The below program we created three files named as Client, Student, and Marker. We declare the main method in Client class. The objective of this program is to show you the use of objects in java.
For Compilation Javac -d . Client.java  is used. And for execution Java Client is used.

Student.java


package Student;

public class Student
{
public int age;
public String name;
public int id;

}

Marker.java

package Marker;

public class Marker
{
    public String color;
public boolean type;
public int inkLevel;
}

Client.java

package Client;
import Marker.*;
import Student.*;
import java.util.Scanner;
public class Client
{
public static void main(String []args)
{
Scanner s=new Scanner(System.in);
int y;
int z;
System.out.println("How many students data you want to store:");
y=s.nextInt();
Student x[]=new Student[y];

for(int i=0;i<y;i++)
{
x[i]=new Student();
System.out.println("Enter your age:");
x[i].age=s.nextInt();
System.out.println("\n Enter your name:");
x[i].name=s.next();
System.out.println("\n Enter your Id:");
x[i].id=s.nextInt();
}
System.out.println("How many markers data you want to store:");
z=s.nextInt();
        Marker m[]=new Marker[z];

for(int i=0;i<z;i++)
{
m[i]=new Marker();
System.out.println("Enter Marker color:");
m[i].color=s.next();
System.out.println("Enter Marker type:");
m[i].type=s.nextBoolean();
System.out.println("Enter Marker Inklevel:");
m[i].inklevel=s.nextInt();
}
System.out.println("\n Printing Student data");
for(int i=0;i<y;i++)
{
System.out.println(x[i].id);
System.out.println(x[i].name);
System.out.println(x[i].age);
}
System.out.println("\n Printing Marker data");
for(int i=0;i<z;i++)
{
System.out.println(m[i].type);
System.out.println(m[i].color);
System.out.println(m[i].inklevel);
}
}
}

Wednesday 6 February 2019

Three Dimension Array in Java (Simple program)



import java.util.Scanner;

public class ThreeDArray
{
public static void main(String []args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter number of Pages:");
int p=s.nextInt();
System.out.println("Enter number of rows:");
int r=s.nextInt();
System.out.println("Enter number of colums:");
int c=s.nextInt();

int arr[][][]=new int[p][r][c];

System.out.println("Enter numbers in Three dimentional Array:");

for(int j=0;j<arr.length;j++)
for(int i=0;i<arr.length;i++)
for(int k=0;k<arr.length;k++)
{
arr[i][j][k]=s.nextInt();
}
System.out.println("printing");

for(int j=0;j<arr.length;j++)
for(int i=0;i<arr.length;i++)
for(int k=0;k<arr.length;k++)
{
System.out.println(arr[i][j][k]);
}
}
}