Write a program in java that will have two classes i.e. Student and Test.
Write a program in java that will have two classes i.e. Student and Test. Student class will have three attributes (id, name, and program) and methods of constructors, getters, setters, print, etc. Test class will have the main method in which you are required to create four objects of the Student class and store them in an array. You have to write code for error exception handling as well in case the array index is to be accessed out of bounds.
Detail:
- Student class will have three attributes (id, name, and
program).
- Methods of Constructors i.e default and parameterized to
initialize the attribute.
1. Default
constructors: If an object of the Student class is
created without passing any parameter default constructor should be executed
and set the values of attributes (id, name, and program) to your own
student id, name and program.
2. Parameterized
Constructors: When parameters are provided the
parameterized constructor will initialize the data members with the given
values.
- If the first two characters of the program string for example BS
do not match the first two characters of Student id i. e BC457810241
then the error should be displayed as:
“Your program does
not match your student Id”
- It should have getter and setter functions to get or set the
values of any one attribute.
- print function of the Student class should display the student's
object data stored in the array list.
- Test class will have the main method in which you are required
to create four objects of the Student class and store them in an Array.
- The first object should be
without providing the parameters so it should call the default
constructor.
- Display the data of all four students using for loop with one more index i.e 0,1,2,3,4. On index 4, the Index Bounds Exception should be displayed.
Student.txt
//public class Student ()
public class Student {
private String id;
private String name;
private String program;
// default constructor
public Student () {
id = "BSc00000000";
name = "Ahmed";
program = "BSc Computer Science";
}
// parameters constructor
public Student (String id, String name, String program) {
this.id = id;
this.name = name;
this.program =program;
if(id.substring(0,2).equals(program.substring(0,2)) ) {
this.program = program;
}
else {
this.program = "Your Program does not match your student Id";
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProgram() {
return program;
}
public void setProgram(String program) {
this.program = program;
}
// print function
public void print (Student student) {
System. out.println("Id: " + student.getId() + "Name : " + student.getName() +"program: " + student.getProgram());
}
}
Test.txt
public class Test {
public static void main (String[] args) {
Student object[] = new Student [4];
object[0] = new Student();
object[1] = new Student ("BS0000001", "Ahmed Ali", "Ð’Sc Computer Science");
object[2] = new Student ("BC0000003", "Ali Noor", "BS IT");
object[3] = new Student ("BS0000002", "Asif", "MS Computer Science");
for (int i = 0; i < 4; i++) {
try {
Student student = new Student();
student.print (object[I]);
}
catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
}
No comments