Inheritance
Parent class /* This program demonstrate the use of inheritance It asks the user for a series of scores Then it calculate the user's grade and returns a letter grade * */ import java.util.*; public class GetGrade { static Scanner key = new Scanner(System.in); static double sum = 0.0; /* Gets the score from the user Caculates the sum in the loop Then returns the average * */ public static double getScore(int size) { for(int i=0; i= 90) { System.out.println("Well done!!! you got an A"); } else if(grade >= 80) { System.out.println("Good job!! you got a B"); } else if(grade >= 70) { System.out.println("You got a C"); } else if(grade >= 60) { System.out.println("You got a D"); } else { System.out.println("Try harder, you got a F"); } } } Child class /* The program demonstrate the use of inheritance * */ import java.util.*; public class GradeActivity extends GetGrade { public static void main(String[] args) { Scanner key = new Scanner(System.in); // Ask user for the size of the loop System.out.print("How many scores do you want to enter "); int size = key.nextInt(); /* Passes the size into getScore Assign the average to grade * */ double grade = getScore(size); // Pass grade into showGrade showGrade(grade); } } Output of the program How many scores do you want to enter 3 Enter your score: 60 Enter your score: 70 Enter your score: 90 You got a C
Parent class
/* This program demonstrate the use of inheritance It asks the user for a series of scores Then it calculate the user's grade and returns a letter grade * */ import java.util.*; public class GetGrade { static Scanner key = new Scanner(System.in); static double sum = 0.0; /* Gets the score from the user Caculates the sum in the loop Then returns the average * */ public static double getScore(int size) { for(int i=0; i= 90) { System.out.println("Well done!!! you got an A"); } else if(grade >= 80) { System.out.println("Good job!! you got a B"); } else if(grade >= 70) { System.out.println("You got a C"); } else if(grade >= 60) { System.out.println("You got a D"); } else { System.out.println("Try harder, you got a F"); } } }
Child class
/* The program demonstrate the use of inheritance * */ import java.util.*; public class GradeActivity extends GetGrade { public static void main(String[] args) { Scanner key = new Scanner(System.in); // Ask user for the size of the loop System.out.print("How many scores do you want to enter "); int size = key.nextInt(); /* Passes the size into getScore Assign the average to grade * */ double grade = getScore(size); // Pass grade into showGrade showGrade(grade); } }
Output of the program
How many scores do you want to enter 3 Enter your score: 60 Enter your score: 70 Enter your score: 90 You got a C
< Java > <Home><Java Programs>