CREATING AND
USING STUDENT OBJECT
AIM: To write a program
in Java to create and use objects of type Student to store the
details like Register Number, Name and Marks in Three Subjects and to
find the Total of Marks for 5 different students.
- Declare a Class named Student that define the student object with three data members namely RegNo, Name, and Marks. RegNo is of type int, Name is of type String and Marks is an array of int.
- Define methods setRegNo() and getRegNo() for storing and retrieving data from RegNo
- Define methods setName() and getName() for storing and retrieving data from Name
- Define methods setMarks() and getMarks() for storing and retrieving data from Marks array
- Declare 5 objects namely stud1, stud2, stud3, stud4 and stud5 of type Student in method main()
- Call the set methods and get methods for each and every object for storing and retrieving data from all five objects
PROGRAM:
Student.java
import java.io.*;
public class Student {
public DataInputStream din;
public int RegNo, Total;
public String Name;
public int Marks[] = new
int[3];
Student()
{
din = new
DataInputStream(System.in);
Total = 0;
}
public void setRegNo() throws
IOException
{
System.out.println("Enter
the Register Number : ");
RegNo =
Integer.parseInt(din.readLine());
}
public void getRegNo()
{
System.out.println("Register
Number : " + RegNo);
}
public void setName() throws
IOException
{
System.out.println("Enter
the Name : ");
Name = din.readLine();
}
public void getName()
{
System.out.println("Name
: " + Name);
}
public void setMarks() throws
IOException
{
System.out.println("Enter
the Three Marks (one by one) : ");
for(int i=0; i<3; i++)
{
Marks[i] =
Integer.parseInt(din.readLine());
Total = Total +
Marks[i];
}
}
public void getMarks()
{
System.out.println("Marks
: " + Marks[0] + " " + Marks[1] + " "+
Marks[2]);
System.out.println("Total
Marks : " + Total);
}
}
StudentObj.java
import java.io.IOException;
public class StudentObj
{
public static void
main(String[] args) throws IOException
{
Student stud1, stud2,
stud3, stud4, stud5;
stud1 = new Student();
System.out.print("\n");
System.out.println("Enter
the Details of 1st Student : ");
stud1.setRegNo();
stud1.setName();
stud1.setMarks();
stud2 = new Student();
System.out.print("\n");
System.out.println("Enter
the Details of 2nd Student : ");
stud2.setRegNo();
stud2.setName();
stud2.setMarks();
stud3 = new Student();
System.out.print("\n");
System.out.println("Enter
the Details of 3rd Student : ");
stud3.setRegNo();
stud3.setName();
stud3.setMarks();
stud4 = new Student();
System.out.print("\n");
System.out.println("Enter
the Details of 4th Student : ");
stud4.setRegNo();
stud4.setName();
stud4.setMarks();
stud5 = new Student();
System.out.print("\n");
System.out.println("Enter
the Details of 5th Student : ");
stud5.setRegNo();
stud5.setName();
stud5.setMarks();
System.out.print("\n");
System.out.println("Details
of Student-1");
stud1.getRegNo();
stud1.getName();
stud1.getMarks();
System.out.print("\n");
System.out.println("Details
of Student-2");
stud2.getRegNo();
stud2.getName();
stud2.getMarks();
System.out.print("\n");
System.out.println("Details
of Student-3");
stud3.getRegNo();
stud3.getName();
stud3.getMarks();
System.out.print("\n");
System.out.println("Details
of Student-4");
stud4.getRegNo();
stud4.getName();
stud4.getMarks();
System.out.print("\n");
System.out.println("Details
of Student-5");
stud5.getRegNo();
stud5.getName();
stud5.getMarks();
}
}
Sample Run:
Enter the Details of 1st
Student :
Enter the Register Number :
13508309
Enter the Name :
Anitha M
Enter the Three Marks (one by
one) :
60
59
53
Enter the Details of 2nd
Student :
Enter the Register Number :
13508311
Enter the Name :
Banu Priya M
Enter the Three Marks (one by
one) :
47
52
51
Enter the Details of 3rd
Student :
Enter the Register Number :
13508313
Enter the Name :
Dhivya R
Enter the Three Marks (one by
one) :
53
56
70
Enter the Details of 4th
Student :
Enter the Register Number :
13508321
Enter the Name :
Haripriya T
Enter the Three Marks (one by
one) :
56
59
33
Enter the Details of 5th
Student :
Enter the Register Number :
13508323
Enter the Name :
Iswariya Lakshmi M
Enter the Three Marks (one by
one) :
84
72
67
Details of Student-1
Register Number : 13508309
Name : Anitha M
Marks : 60 59 53
Total Marks : 172
Details of Student-2
Register Number : 13508311
Name : Banu Priya M
Marks : 47 52 51
Total Marks : 150
Details of Student-3
Register Number : 13508313
Name : Dhivya R
Marks : 53 56 70
Total Marks : 179
Details of Student-4
Register Number : 13508321
Name : Haripriya T
Marks : 56 59 33
Total Marks : 148
Details of Student-5
Register Number : 13508323
Name : Iswariya Lakshmi M
Marks : 84 72 67
Total Marks : 223
BUILD SUCCESSFUL (total time: 2
minutes 54 seconds)