Question

In: Computer Science

JAVA Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours,...

JAVA 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

Enter·first·name:Bobbi↵
Enter·last·name:Benton↵
Enter·social·security·number:765-42-0092↵
Enter·hours·worked:53↵
Enter·wage:15.8↵
hourly·employee:·Bobbi·Benton↵
social·security·number:·765-42-0092↵
hours:·53.0·↵
wage:·15.80·↵
earnings:·837.40↵

Solutions

Expert Solution

import java.util.*;
class Employee
{   
   private String firstName,lastName,ssn;   
   public Employee(String firstName,String lastName, String ssn) // constructor   
   {
   this.firstName = firstName;   
   this.lastName = lastName;
   this.ssn = ssn;
   }
   public String toString()
   {
       return "employee : "+firstName +" "+lastName +"\nsocial security number : "+ssn;
   }
  
}

class HourlyEmployee extends Employee
{
   private double wage;
   private double hours;
   public HourlyEmployee(String firstName,String lastName, String ssn, double wage,double hours)
   {
       super(firstName,lastName,ssn);
       this.wage = wage;
       this.hours = hours;
   }   
  
   public double earnings()
       {
           return wage * hours;
       }
   public void setWage(double wage)
           {   
               if(wage > 0)
               this.wage = wage;
               else   
               wage = 0;   
           }   
   public void setHours(double hours)
           {
               if(hours > 0 && hours <= 168)
               this.hours = hours;
               else
               hours = 0;
           }
       public String toString()   
       {
           return "hourly "+ super.toString()+"\nhours : "+hours+"\nwages : "+wage+ String.format("\nearnings :%.2f",earnings());
           }
          
  
}

class TestEmployee{
   public static void main (String[] args)   
   {   
   Scanner input = new Scanner(System.in);
   System.out.println("Enter first name:");
   String firstName = input.next();   
   System.out.println("Enter last name:");
   String lastName = input.next();   
   System.out.println("Enter social security number:");
   String ssn = input.next();
   System.out.println("Enter hours worked:");
   double hours = input.nextDouble();   
   System.out.println("Enter wage:");   
   double wage = input.nextDouble();   
   HourlyEmployee emp = new HourlyEmployee(firstName,lastName,ssn,hours,wage);
   System.out.println(emp);
   }
  
}

Output:

Enter first name:Mazie 
Enter last name:Payne 
Enter social security number:633-38-5740 
Enter hours worked:45
Enter wage:22.5
hourly employee : Mazie Payne
social security number : 633-38-5740
hours : 22.5
wages : 45.0
earnings :1012.50

Do ask if any doubt. Please upvote.


Related Solutions

in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
Create a class called employee which has the following instance variables: Employee ID number Salary Years...
Create a class called employee which has the following instance variables: Employee ID number Salary Years at the company Your class should have the following methods: New employee which reads in an employee’s ID number, salary and years of service Anniversary which will up the years of service by 1 You got a raise which will read in how much the raise was (a percent) and then calculate the new salary You get a bonus which gives a yearly bonus...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number)...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius). Include the following: (1) two...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
Programming Problem 2 - Cycle[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.[B] Edit your class Cycle by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT