In: Computer Science
Lesson is on Inheritance and overriding the ToString ( ) method in Java:
The program should consist of 3 classes and will calculate an hourly paycheck.
- The superclass will contain fields for common user data for an employee (first name and last name) and a toString method that returns the formatted output of the names to the calling method.
- The subclass will calculate a paycheck for an hourly employee and will inherit from the superclass.
- The testclass will interface with the user.
- The superclass will be used to set the first and last name of the employee.
- The subclass should have additional instance variables for the hours worked, the hourly rate of pay and the weekly pay that they calculate.
- The subclass should have a toString method that prints the calculated weekly paycheck from the class plus calls the toString method of the superclass to obtain the formatted first and last name.
- Create a basic driver class to supply input from the user into the program and print the results. (The results must be correct.)
Here is the grading rubric:
Create a superclass that sets the first name and last name as private instance variables and has a toString method that returns a formatted string of the employee’s first and last name
Create a subclass that inherits the superclass, calculates a weekly paycheck for an hourly employee and contains a toString method that calls the superclass toString method plus adds the weekly paycheck amount to the formatted output supplied by the superclass
The subclass contains a constructor that calls the super class constructor
The subclass overrides the toString method in the superclass and adds the weekly paycheck amount to the string it returns
The test class code should create object, call methods to calculate the weekly paycheck and print the results
Check out the solution and do COMMENT for any queries or modifications.
_____________________________
// imported for Scanner class
import java.util.*;
class SuperClass {
// private class variables
private String first_name;
private String last_name;
// constructor - sets the given values to class variables
SuperClass(String fname, String lname) {
this.first_name = fname;
this.last_name = lname;
}
// toString()
public String toString() {
return "\nFirst Name : " + this.first_name + "\nLast Name : " +
this.last_name;
}
}
// subclass inherits superclass using 'extends' keyword
class SubClass extends SuperClass {
// class variables
private double hrs_worked;
private double hrs_rate;
// constructor
SubClass(String fname, String lname, double wrk, double rate)
{
// super class constructor
super(fname, lname);
this.hrs_worked = wrk;
this.hrs_rate = rate;
}
// method to find the weekly pay
double WeeklyPay() {
double weekly_pay;
weekly_pay = this.hrs_worked * this.hrs_rate;
// returns the weekly pay
return weekly_pay;
}
// overrides the super class toString()
public String toString() {
// calls the super class toString() and adds the weeklypay() method
to get the weekly pay amount
return super.toString() + "\nWeekly Pay : " + WeeklyPay();
}
}
// main class
public class MyClass {
public static void main(String args[]) {
// declarations
String fname, lname;
double hrs_worked, hrs_rate;
// create a scanner class object to get the user input
Scanner sc = new Scanner(System.in);
// get the user input
System.out.print("Input Employee's First name : ");
fname = sc.nextLine();
System.out.print("Input Employee's Last name : ");
lname = sc.nextLine();
System.out.print("Input number of hours worked : ");
hrs_worked = sc.nextDouble();
System.out.print("Input Hourly pay rate : ");
hrs_rate = sc.nextDouble();
// object instantiation and pass the arguments
SubClass object1 = new SubClass(fname, lname, hrs_worked,
hrs_rate);
System.out.println("\n--------------------------------------------");
// print the object - prints the returned result of
toString()
System.out.print(object1);
}
}
___________________
__________
Output