CODE:
import java.util.Scanner;
class STUDENT {
String name;
int[] marks = new int[3];
STUDENT(String name, int[] marks) {
this.name = name;
this.marks = marks;
}
void compute() {
int total = 0;
for (int m : marks) total += m;
double avg = total / 3.0;
System.out.println("Total: " + total);
System.out.println("Average: " + avg);
}
void display() {
System.out.println("Name: " + name);
}
}
class ScienceStudent extends STUDENT {
int practicalMarks;
ScienceStudent(String name, int[] marks, int practicalMarks) {
super(name, marks);
this.practicalMarks = practicalMarks;
}
@Override
void compute() {
int total = practicalMarks;
for (int m : marks) total += m;
double avg = total / 4.0;
System.out.println("Total (with practical): " + total);
System.out.println("Average: " + avg);
}
void displayPracticalMarks() {
System.out.println("Practical Marks: " + practicalMarks);
}
}
class ArtsStudent extends STUDENT {
String electiveSubject;
ArtsStudent(String name, int[] marks, String electiveSubject) {
super(name, marks);
this.electiveSubject = electiveSubject;
}
void displayElective() {
System.out.println("Elective Subject: " + electiveSubject);
}
}
public class ProblemOne {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name for general student: ");
String n1 = sc.nextLine();
int[] m1 = new int[3];
System.out.println("Enter 3 marks:");
for (int i = 0; i < 3; i++) m1[i] = sc.nextInt();
STUDENT s1 = new STUDENT(n1, m1);
sc.nextLine();
System.out.print("Enter name for science student: ");
String n2 = sc.nextLine();
int[] m2 = new int[3];
System.out.println("Enter 3 marks:");
for (int i = 0; i < 3; i++) m2[i] = sc.nextInt();
System.out.print("Enter practical marks: ");
int p = sc.nextInt();
ScienceStudent s2 = new ScienceStudent(n2, m2, p);
sc.nextLine();
System.out.print("Enter name for arts student: ");
String n3 = sc.nextLine();
int[] m3 = new int[3];
System.out.println("Enter 3 marks:");
for (int i = 0; i < 3; i++) m3[i] = sc.nextInt();
sc.nextLine();
System.out.print("Enter elective subject: ");
String e = sc.nextLine();
ArtsStudent s3 = new ArtsStudent(n3, m3, e);
STUDENT ref;
System.out.println("\n--- General Student ---");
ref = s1;
ref.display();
ref.compute();
System.out.println("\n--- Science Student ---");
ref = s2;
ref.display();
ref.compute();
s2.displayPracticalMarks();
System.out.println("\n--- Arts Student ---");
ref = s3;
ref.display();
ref.compute();
s3.displayElective();
}
}
OUTPUT:
Enter name for general student: Achu
Enter 3 marks:
23 35 56
Enter name for science student: SomeGuy
Enter 3 marks:
34 66 56
Enter practical marks: 23
Enter name for arts student: ArtsMan
Enter 3 marks:
23 46 90
Enter elective subject: Environmental science
--- General Student ---
Name: Achu
Total: 114
Average: 38.0
--- Science Student ---
Name: SomeGuy
Total (with practical): 179
Average: 44.75
Practical Marks: 23
--- Arts Student ---
Name: ArtsMan
Total: 159
Average: 53.0
Elective Subject: Environmental science