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...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following:...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following: a variable roof that holds type of roof (ex: convertible, hard-top,softtop) 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 its called add exception handling to the changespeed method to keep soeed under 65 implement the sound method to print "brooom" to the screen. create one constructor that accepts the...
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,...
1) Create a class called Employee that includes three instance variables — a first name (type...
1) 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. 2) Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating...
1) Create a class called Employee that includes three instance variables — a first name (type...
1) 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. 2)Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating five...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”);...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT