Search This Blog

Saturday 10 May 2014

Java program to create an array of objects to accept student information

import java.io.*;
public class ArrayObj

   
    public static void main(String args[])throws IOException
    {
        int n;
        DataInputStream in=new DataInputStream(System.in);
        System.out.println("Enter number of students:");
        n=Integer.parseInt(in.readLine());
        //step1 : first create array of n elements that holds object addresses.
        Student eObj[] = new Student[n];
       
       
        System.out.println("Enter the information of " +n+ "students:");
        //step2 : now create objects in a loop by calling constructor.
        for(int i=0; i<n; i++)
        {
            System.out.println("Enter Name,Regno,Course and age of students:");
            eObj[i] = new Student(in.readLine(), in.readLine(), in.readLine(), Integer.parseInt(in.readLine()));//this will call constructor.
        }
        System.out.println("Student Information:");
        for(int i=0;i<n;i++)
        {
            System.out.println("Information of Student " + (i+1));
            System.out.println("Name   :"+eObj[i].name);
            System.out.println("Regno  :"+eObj[i].regno);
            System.out.println("Course :"+eObj[i].course);
            System.out.println("Age    :"+eObj[i].age);
        }
           
    }
}

class Student
{
    String name,regno,course;
    int age;
    public Student(String name1,String regno1,String course1,int age1)
    {
        name=name1;
        regno=regno1;
        course=course1;
        age=age1;
    }
}

No comments :

Post a Comment

FREE Hit Counters