Array of objects in Java

Arrays are defined as block of continous memory locations which are arranged in one after another. Elements of the Array can be accessed by the index number as:  a[2]

Syntax:

data_type[] array_name = new data_type[number of elements];

Example:

int[] name = new int[10];

Array of objects

Objects are instance of a class.

Object can easily be created using following syntax: class_name object_name = new class_name();

                                                                     Example: Student name = new Student();

but what if we need to create hundreds of objects of the same class. However this can be achieved using above procedure but imagine the the efforts and time required. To accomplish this task, Java introduces array of objects.

syntax: class_name[] object_name = new class_name[number of objects];

Example: Student name = new Student[5];

this code segment will create 5 objects called 'name' of type Student.

here is a beautiful example of apllication of array of objetcs: Students marksheet

Programs states:

Write a java program to accept details of students such as name, ID no, marks in Maths, Physics, and Chemistry. Display the students detail in descending order of their total marks. (Use array of objects)

import java.io.*;         //importing input-output files
class Student
{
   String name;                     //declaration of variables
   int id;
   int mathmarks;
   int phymarks;
   int chemmarks;
   int total;

   Student(String naam,int idno, int m,int p,int c)          //Initializing variables to user data
   {
          name=naam;
          id=idno;
          mathmarks=m;
          phymarks=p;
          chemmarks=c;
          total=m+p+c;
    }
    void display()                             //displaying information
    {
           System.out.println(name+"\t"+id+"\t"+mathmarks+"\t"+phymarks+"\t"+chemmarks+"\t"+total);
    }

}

class Studentexe                     //main class
{
  public static void main(String args[]) throws IOException                  //exception handling
  {
         System.out.println("Enter the numbers of students:");
         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
         int n=Integer.parseInt(in.readLine());

         Student[]  S=new Student[n];                      // array of objects declared and defined
         for(int i=0;i         {
            
               System.out.println("Enter the Details of Student no: "+(i+1));             //reading data form the user
               System.out.println("Name: ");
               String nm=in.readLine();
               System.out.println("ID no: ");
               int idno=Integer.parseInt(in.readLine());
               System.out.println("Maths marks: ");              
               int m=Integer.parseInt(in.readLine());
               System.out.println("Physics marks: ");
               int p=Integer.parseInt(in.readLine());
               System.out.println("Chem marks: ");
               int c=Integer.parseInt(in.readLine());
             
              S[i]=new Student(nm,idno,m,p,c);                           //calling Student constructor
            
          }
       
         Student temp;                           //swaping to achieve decsending order of total marks
         for(int a=0;a         {
                 for(int b=0;b                 {
                      if(S[b].total                      {
                           temp=S[b];
                           S[b]=S[b+1];
                            S[b+1]=temp;
                       }
                }
        }
        System.out.println("\nName"+"\t"+"ID"+"\t"+"Math"+"\t"+"Phy "+"\t"+"Chem"+"\t"+"Total");            //printing data on the output screen
        for(int i=0;i        {
               S[i].display();
        }

 

     }    //main ends here
}        //Studentexe class ends here

 

Note: 1) save the above code as 'Studentexe.java' while executing.

 


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments