Question

In: Computer Science

Lesson is on Inheritance and overriding the ToString ( ) method in Java: The program should...

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

Solutions

Expert Solution

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


Related Solutions

JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to...
Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to do in the JAVA Language: Model your code from the Time Class Case Study in Chapter 8 of Java How to Program, Late Objects (11th Edition) by Dietel (except the displayTime method and the toUniversalString method). Use a default constructor. The set method will validate that the hourly employee’s rate of pay is not less than $15.00/hour and not greater than $30.00 and validate...
Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Lesson on Inheritance in Java: Using the payroll weekly paycheck calculator scenario as a model, your...
Lesson on Inheritance in Java: Using the payroll weekly paycheck calculator scenario as a model, your code will consist of a superclass (Employee), three subclasses (SalaryEmployee, HourlyEmployee, CommEmployee) and a test class (PayrollTest). - The superclass should 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. - Three subclasses should be created that all inherit from the superclass: one...
in java What object is passed to the start method in a JavaFX program? What should...
in java What object is passed to the start method in a JavaFX program? What should you do with it?
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT