Your assignment is to implement a class called Employee (there is no main method
ID: 3733455 • Letter: Y
Question
Your assignment is to implement a class called Employee (there is no main method). An Employee has a name (a String), a salary (a double) and number of years of experience. (an integer). In addition, your class Employee must have a static instance variable to keep track of number of employees. The class Employee must include the following constructors and methods.
Method header
Method Description
Default constructor sets the name to "???”, the salary to 0.0 and years of experience to 0
Constructs an Employee given the name as a String, salary as double and years of experience as an integer
Constructs an Employee given a string and using methods in class String or Scanner to extract name, salary: years of experiences. For Example the string "Bond:70000:20"
Returns the name of the employee
Returns the employee's salary.
Returns the employee’s years of experience
This is a mutator method that can be used to change the name of employee
A method to be run annually; increments the years of experiences annually
This is a mutator method that can be used to change the number of years of experience.
Takes one parameter and raises the employee's salary by that percent
Compares the two employees number of years of experience and returns the Employee who is more experienced
Returns the number of employees
Returns a String with information on each Employee (etc. name, salary, and years of experiences). The salary needs to be formatted with currency format.
Save the Employee class in a file called Employee.java and use the test driver called Assignment6.java, which has the main method to create new Employee objects and to test your class. You need to complete this class. A sample output is shown below.
Sample output: User input is in RED
Enter name of the employee? Grace Hopper Enter salary for Grace Hopper? 89000 Enter number of years for Grace Hopper? 8
Command Options
-----------------------------------
'a': prints info
'b': raise salary
'c': increment the years of experience 'd': who has more experience
'e': number of employees 'f': years of experience
'?': Displays the help menu 'q': quits
Please enter a command or type ? a
Name: Grace Hopper Salary: $89,000.00 Years of Experience: 8
Name: Bond
Salary: $70,000.00 Years of Experience: 20
Please enter a command or type ? b Enter the amount of raise: 2
Name: Grace Hopper Salary: $90,780.00 Years of Experience: 8
Name: Bond
Salary: $71,400.00 Years of Experience: 20
Please enter a command or type ? c Year has been added.
Please enter a command or type ? a
Name: Grace Hopper Salary: $90,780.00 Years of Experience: 9
Name: Bond
Salary: $71,400.00 Years of Experience: 21
Please enter a command or type ? d Bond has more experience.
Please enter a command or type ? e They are 2 employees.
Please enter a command or type ? f Enter years of experience: 35
Please enter a command or type ? a Name: Grace Hopper
Salary: $90,780.00 Years of Experience: 35
Name: Bond
Salary: $71,400.00 Years of Experience: 21
Please enter a command or type ? d Grace Hopper has more experience.
Please enter a command or type ? q
Test Driver:
Method header
Method Description
public Employee( )
Default constructor sets the name to "???”, the salary to 0.0 and years of experience to 0
public Employee (String name, double salary, int years)
Constructs an Employee given the name as a String, salary as double and years of experience as an integer
public Employee (String str)
Constructs an Employee given a string and using methods in class String or Scanner to extract name, salary: years of experiences. For Example the string "Bond:70000:20"
public String getName( )
Returns the name of the employee
public double getSalary( )
Returns the employee's salary.
public int getYears()
Returns the employee’s years of experience
public String setName(String st)
This is a mutator method that can be used to change the name of employee
public void setYears()
A method to be run annually; increments the years of experiences annually
public void setYears(int numYears)
This is a mutator method that can be used to change the number of years of experience.
public void raiseSalary (double byPercent)
Takes one parameter and raises the employee's salary by that percent
public Employee moreExperience(Employee e)
Compares the two employees number of years of experience and returns the Employee who is more experienced
public static int getNumEmployees()
Returns the number of employees
public String toString( )
Returns a String with information on each Employee (etc. name, salary, and years of experiences). The salary needs to be formatted with currency format.
Explanation / Answer
Assignment6.java
import java.util.*;
import java.text.*;
public class Assignment6
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
String choice;
char command;
// Create a new Employee object
System.out.print("Enter name of the employee? ");
String name = console.nextLine();
System.out.print("Enter salary for " + name + "? ");
double salary = console.nextDouble();
System.out.print("Enter number of years for " + name + "? ");
int years = console.nextInt();
//use the constructors in class Employee
Employee e1 = new Employee(name, salary, years);
Employee e2 = new Employee("Bond:70000:20");
// print the menu
printMenu();
do
{
// ask a user to choose a command
System.out.print(" Please enter a command or type ?");
choice = console.next().toLowerCase();
command = choice.charAt(0);
switch (command)
{
case 'a': // print the info for each employee
// complete the code here
System.out.println(e1);
System.out.println(e2);
break;
case 'b': //raise employee's salary
System.out.print("Enter the amount of raise: ");
double raise = console.nextDouble();
//complete the code here
e1.raiseSalary(raise);
e2.raiseSalary(raise);
System.out.println(e1);
System.out.println(e2);
break;
case 'c': //increment the number of years by 1
e1.setYears();
e2.setYears();
System.out.println("Year has been added.");
break;
case 'd': //find who has more experience
Employee who = e1.moreExperience(e2);
System.out.print(who.getName() + " has more experience.");
break;
case 'e': //number of employees
//complete the code here
System.out.println("They are "+Employee.getNumEmployees()+" employees.");
break;
case 'f': //years of experience
System.out.print("Enter years of experience: ");
years = console.nextInt();
e1.setYears(years);
break;
case '?':
printMenu();
break;
case 'q':
break;
default:
System.out.println("Invalid input");
}
} while (command != 'q');
}// end main
public static void printMenu()
{
System.out.print(" Command Options "
+ "----------------------------------- "
+ "'a': prints info "
+ "'b': raise salary "
+ "'c': increment the years of experience "
+ "'d': who has more experience "
+ "'e': number of employees "
+ "'f': years of experience "
+ "'?': Displays the help menu "
+ "'q': quits ");
} // end of the printMenu method
}
Employee.java
public class Employee {
private String name;
private double salary;
private int years;
public static int count= 0;
public Employee(String name,double salary,int years) {
this.name = name;
this.salary=salary;
this.years=years;
count++;
}
public Employee( ) {
years = 0;
salary=0.0;
name="???";
count++;
}
public Employee (String str) {
String s[] = str.split(":");
name = s[0];
years =Integer.parseInt(s[2]);
count++;
salary=Double.parseDouble(s[1]);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getYears() {
return years;
}
public void setYears(int years) {
this.years = years;
}
public void raiseSalary (double
byPercent) {
salary = salary+(salary*byPercent)/100;
}
public Employee
moreExperience(Employee e) {
if(e.getYears()>years) {
return e;
}
return this;
}
public void setYears() {
years++;
}
public static int getNumEmployees() {
return count;
}
public String toString( ) {
return "Name: "+name+" Salary: $"+salary+" Years: "+years;
}
}
Output:
Enter name of the employee? Grace Hopper
Enter salary for Grace Hopper? 89000
Enter number of years for Grace Hopper? 8
Command Options
-----------------------------------
'a': prints info
'b': raise salary
'c': increment the years of experience
'd': who has more experience
'e': number of employees
'f': years of experience
'?': Displays the help menu
'q': quits
Please enter a command or type ?a
Name: Grace Hopper Salary: $89000.0 Years: 8
Name: Bond Salary: $70000.0 Years: 20
Please enter a command or type ?b
Enter the amount of raise: 2
Name: Grace Hopper Salary: $90780.0 Years: 8
Name: Bond Salary: $71400.0 Years: 20
Please enter a command or type ?c
Year has been added.
Please enter a command or type ?a
Name: Grace Hopper Salary: $90780.0 Years: 9
Name: Bond Salary: $71400.0 Years: 21
Please enter a command or type ?d
Bond has more experience.
Please enter a command or type ?e
They are 2 employees.
Please enter a command or type ?f
Enter years of experience: 35
Please enter a command or type ?a
Name: Grace Hopper Salary: $90780.0 Years: 35
Name: Bond Salary: $71400.0 Years: 21
Please enter a command or type ?q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.