Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This program uses inheritance to define three classes GroupA, GroupB, and GroupC that extend the Student class. The Student class has a constructor that takes
This program uses inheritance to define three classes GroupA, GroupB, and GroupC that extend the Student class. The Student class has a constructor that takes an ID and a name, and methods to get the ID and name of a student. The getGroup method returns a string indicating the group of the student. The subclasses GroupA, GroupB, and GroupC override the getGroup method to return a string indicating their respective groups import java.util.Scanner; class Student { protected int id; protected String name; public Student(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } public String getGroup() { return "Unknown"; } } class GroupA extends Student { public GroupA(int id, String name) { super(id, name); } @Override public String getGroup() { return "Group A"; } } class GroupB extends Student { public GroupB(int id, String name) { super(id, name); } @Override public String getGroup() { return "Group B"; } } class GroupC extends Student { public GroupC(int id, String name) { super(id, name); } @Override public String getGroup() { return "Group C"; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Student[] students = new Student[40]; for (int i = 0; i < 40; i++) { System.out.print("Enter student id: "); int id = scanner.nextInt(); System.out.print("Enter student name: "); String name = scanner.next(); System.out.print("Enter student group (A, B, or C): "); String group = scanner.next(); switch (group) { case "A": students[i] = new GroupA(id, name); break; case "B": students[i] = new GroupB(id, name); break; case "C": students[i] = new GroupC(id, name); break; default: students[i] = new Student(id, name); } } System.out.println(" Classification of Students"); System.out.println("ID\tName\tGroup"); for (int i = 0; i < 40; i++) { System.out.println(students[i].getId() + "\t" + students[i].getName() + "\t" + students[i].getGroup()); } scanner.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started