Write a Java program to declare a Class named as Student which contains roll number,
name and course as instance variables and input_Student () and display_Student () as
instance methods. A derived class Exam is created from the class Student . The derived
class contains mark1, mark2, mark3 as instance variables representing the marks of three
subjects and input_Marks () and display_Result () as instance methods. Create an array of
objects of the Exam class and display the result of 5 students
class Student {
private int roll_number;
private String name;
private int course;
public void input_Student(int roll_number, String name, int course) {
this.roll_number = roll_number;
this.name = name;
this.course = course;
}
public void display_Student() {
System.out.println("* Roll number: " + roll_number);
System.out.println("* Name: " + name);
System.out.println("* Course: " + course);
}
}
class Exam extends Student {
private int mark1;
private int mark2;
private int mark3;
public void input_Marks(int mark1, int mark2, int mark3) {
this.mark1 = mark1;
this.mark2 = mark2;
this.mark3 = mark3;
}
public void display_Marks() {
System.out.println("* Mark 1: " + mark1);
System.out.println("* Mark 2: " + mark2);
System.out.println("* Mark 3: " + mark3);
}
}
public class Main {
public static void main(String[] args) {
Exam[] exams = new Exam[5];
exams[0] = new Exam();
exams[0].input_Student(5263, "John Kennedy", 1);
exams[0].input_Marks(95, 85, 80);
exams[1] = new Exam();
exams[1].input_Student(6374, "Martha Wales", 1);
exams[1].input_Marks(90, 70, 75);
exams[2] = new Exam();
exams[2].input_Student(2957, "Jim Parker", 2);
exams[2].input_Marks(75, 60, 80);
exams[3] = new Exam();
exams[3].input_Student(2456, "Lora Smith", 3);
exams[3].input_Marks(80, 80, 70);
exams[4] = new Exam();
exams[4].input_Student(2733, "Tom Gates", 2);
exams[4].input_Marks(70, 65, 65);
for (int i = 0; i < exams.length; i++) {
System.out.println("STUDENT # " + (i + 1) + ":");
exams[i].display_Student();
exams[i].display_Marks();
System.out.println();
}
}
}
Comments
Leave a comment