inheritance
Question:
Create a superclass, Student, and subclass, Grade. The superclass Student should have data members: name, ID, age, address.The superclass, Student should have at least one method: findGrade(double percent) . The purpose of the findGrade method is to take one parameter,percent (value between 0 and 100) and find grade.In the Student class this method should be empty as an abstract method. The Grade will inherit all data members of the Student class and override the method findGrade. Create a test class for above two classes. In the test class, create Grad object. For an object, provide percent and find grade and display the results of the findGrade method.
💡 Solutions (1)
Super Admin
AdminFeb 03, 2026 at 05:43
upvotes
abstract class Student { protected final String name; protected final String id; protected final int age; protected final String address; public Student(String name, String id, int age, String address) { this.name = name; this.id = id; this.age = age; this.address = address; } public abstract String findGrade(double percent); } class Grade extends Student { public Grade(String name, String id, int age, String address) { super(name, id, age, address); } @Override public String findGrade(double percent) { if (percent < 0 || percent > 100) { return "Invalid percent"; } if (percent >= 90) { return "A"; } if (percent >= 80) { return "B"; } if (percent >= 70) { return "C"; } if (percent >= 60) { return "D"; } return "F"; } } public class StudentGradeTest { public static void main(String[] args) { java.util.Scanner scanner = new java.util.Scanner(System.in); System.out.print("Enter name: "); String name = scanner.nextLine().trim(); System.out.print("Enter ID: "); String id = scanner.nextLine().trim(); System.out.print("Enter age: "); int age = Integer.parseInt(scanner.nextLine().trim()); System.out.print("Enter address: "); String address = scanner.nextLine().trim(); System.out.print("Enter percent (0-100): "); double percent = Double.parseDouble(scanner.nextLine().trim()); Grade grad = new Grade(name, id, age, address); String grade = grad.findGrade(percent); System.out.println("Name: " + grad.name); System.out.println("ID: " + grad.id); System.out.println("Age: " + grad.age); System.out.println("Address: " + grad.address); System.out.println("Percent: " + percent); System.out.println("Grade: " + grade); } }
Want to add your solution? Login to contribute!
💬 Comments (0)
Login to post comments
No comments yet
Be the first to comment!