Question
Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours, which represents the hours worked, and wage, which represents the
Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours, which represents the hours worked, and wage, which represents the employee's pay per hour. (Both are doubles.) Create a constructor that takes the arguments first name, last name, social security number, hourly wage, and the number of hours worked. Also create accessors, mutators, an earnings method that returns the money earned by the employee this week, and a toString method that returns information about the employee in the form of a String. The setWage method should ensure that the wage is nonnegative, and the setHours method should ensure that the value of hours is between 0 and 168 (the number of hours in a week). Create a Driver class with a main method that prompts the user to enter a first name, last name, social security number, hours, and wage for an employee. Then, the program should create an HourlyEmployee object and use its toString method to print information about it. SAMPLE RUN #1: java Driver
This is what I have so far with my code and results from code:
import java.util.*;
//class Employee class Employee { //private members private String firstName; private String lastName; private String socialSecurityNumber;
//constructor public Employee(String firstName, String lastName, String socialSecurityNumber ) { this.firstName = firstName; this.lastName = lastName; this.socialSecurityNumber = socialSecurityNumber; }
//set method to set empNumber public void setSocialSecurityNumber(String socialSecurityNumber) { this.socialSecurityNumber = socialSecurityNumber; }
//set method to set firstName public void setFirstName(String firstName) { this.firstName = firstName; }
//set method to set lastName public void setLastName(String lastName) { this.lastName = lastName; }
//get method to return socialSecurityNumber public String getSocialSecurityNumber() { return this.socialSecurityNumber; }
//get method to return firstName public String getFirstName() { return this.firstName; }
//get method to return lastName public String getLastName() { return this.lastName; } }
class HourlyEmployee extends Employee { //private members private double hours; private double wage;
//constructor public HourlyEmployee(String firstName, String lastName, String socialSecurityNumber, double hours, double wage) { super(firstName, lastName, socialSecurityNumber); this.hours = hours; this.wage = wage; }
//get method to return hours public double getHours() { return this.hours = hours; }
//set method to set hours public void setHours(double hours) { if(hours>=0 && hours
//get method to return wage public double getWage() { return this.wage = wage; }
//set method to set wage public void setWage(double wage) { if(wage>=0) this.wage = wage; else this.wage = 0; }
//get method to return earning public double getEarning() { return this.wage*this.hours; }
//method to return String public String toString() { String e = String.format("%.2f", getEarning()); return "hourly employee: " + getFirstName() + " " + getLastName() + " " + "social security number: " + getSocialSecurityNumber() + " " + "hours: " + getHours() + " " + "wage: " + getWage() + " " + "earnings: " + e; } }
//class Driver class Driver { //main method public static void main(String[] args) { //object of Scanner class Scanner sc = new Scanner(System.in);
//read a Employee Name System.out.print("Enter first name:"); String fname = sc.nextLine(); System.out.print("Enter last name:"); String lname = sc.nextLine();
//read a Employee social-securiry-number System.out.print("Enter social security number:"); String number = sc.nextLine();
System.out.print("Enter hours worked:"); double hours = sc.nextDouble();
System.out.print("Enter wage:"); double wage = sc.nextDouble();
//create an object HourlyEmployee he = new HourlyEmployee(fname, lname, number, hours, wage); //print information System.out.print(he); } }
Given the following was entered from the keyboard: Failed 2 out of 2 test runs. Failed Test Run \#1 The contents of your standard output is incorrect. Highlight: Expected Result: Your Code's Actual Result: Enter.first. name:Bobbid Enter.first.name:Bobbid Enter-1ast. name: Bentond Enter.last.name:BentondStep 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