Create an Employee class. Might look like this: public class Employee { } Add th
ID: 3918925 • Letter: C
Question
Create an Employee class. Might look like this: public class Employee { }
Add three private instance variables: String name, int ID, int salary
Create getter (accessor) and setter (mutator) methods for each instance variable
Write a constructor
Write (override) the toString method. Format currency values throughout the class.
Write an equals method
import java.util.Scanner;
Write a public static void main(String[] args) { … } method to test.
The main method should prompt the user for the name, ID, and salary for the first employee. Use the scanner class for this, just as you did in the first assignment.
Using the name, ID and salary information collected from the user create the first Employee. Might look like this: Employee e1 = new Employee(name, ID, salary);
Print out the first employee. Might look like this: System.out.println(e1);
Prevent the user from entering non-integers for the ID and salary. (do… while, maybe).
Repeat steps 9, 10, 11 and 12 for a second employee. Cut, paste and modify
Write and test an equals method. Two employees are equal if and only if they have the same name, ID and salary.
Write a method which calculates the weekly pay of an employee based on how many hours they worked. Assume time and a half for OT and 2080 hours a week. Hence if an employee’s salary is $50,000 a year and they worked 36 hours that week they would be paid
50,000/2080 * 36 = $865.38. If that same employee worked 44 hours they would be paid (50,000/2080 * 40) + ((44 -40) * 50,000/2080 * 1.5) = $1,105.77
Prompt the user for the first employee’s hours that week and use the user input to test the weekly pay method. Using the same method you employed in assignment 1, prevent the user from entering a non-numeric value.
Explanation / Answer
import java.util.*;
import java.text.*;
public class Employee
{
private String name= new String();
private int ID;
private double salary;
public String setname(String n)
{
name=n;
}
public String getname()
{
return name;
}
public int setID(int n)
{
ID=n;
}
public int getID()
{
return ID;
}
public double setsalary(int n)
{
salary=n;
}
public double getsalary()
{
return salary;
}
Employee()
{
System.out.println("this is a constructor");
}
NumberFormat formatter = NumberFormat.getCurrencyInstance();
System.out.println(formatter.format(salary));
public void equals()
{
if(e1.name==e2.name && e1.ID==e2.ID && e1.salary==e2.salary)
System.out.println("the two employees are equal or same");
else
System.out.println("the two employees are not equal");
}
public void weeklypay(int x)
{
if(x<=40){
System.out.println((50000/2080)*x);}
else if(x>40){
System.out.println(((50000/2080)*40)+((x-40)*(50000/2080)*1.5));}
else{
System.out.println("empty");}
}
public static void main(String args[])
{
Employee e=new Employee(System.in);
System.out.println("enter your name");
name=e.nextLine();
System.out.println("enter employee id");
ID=e.nextInt();
if(ID<0)
System.out.println("enter correct values");
System.out.println("enter employee salary");
salary=e.nextDouble();
if(salary<0)
System.out.println("enter correct values");
Employee e1 = new Employee(name, ID, salary);
System.out.println(e1);
Employee e2 = new Employee(name, ID, salary);
System.out.println(e2);
equals();
System.out.println("enter employee1 working hrs");
int w1=e.nextInt();
if(wl<0)
System.out.println("wrong format of number");
weeklypay(wl);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.