File Salary.java contains most of a program that takes as input an employee\'s s
ID: 3678420 • Letter: F
Question
File Salary.java contains most of a program that takes as input an employee's salary and a rating of the employee's performance and computes the raise for the employee. This is similar to question #3 in the pre-lab, except that the performance rating here is being entered as a String—the three possible ratings are "Excellent", "Good", and "Poor". As in the pre-lab, an employee who is rated excellent will receive a 6% raise, one rated good will receive a 4% raise, and one rated poor will receive a 1.5% raise.
Add the if... else... statements to program Salary to make it run as described above. Note that you will have to use the equals method of the String class (not the relational operator ==) to compare two strings (see Section 5.3, Comparing Data).
// ***************************************************************
// Salary.java
//
// Computes the amount of a raise and the new
// salary for an employee. The current salary
// and a performance rating (a String: "Excellent",
// "Good" or "Poor") are input.
// ***************************************************************
import java.util.Scanner;
import java.text.NumberFormat;
public class Salary
public static void main (String[] args)
{
double currentSalary; // employee's current salary
double raise; // amount of the raise
double newSalary; // new salary for the employee
String rating; // performance rating
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the current salary: ");
currentSalary = scan.nextDouble();
System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
rating = scan.nextLine();
// Compute the raise using if ...
newSalary = currentSalary + raise;
// Print the results
NumberFormat money = NumberFormat.getCurrencyInstance();
System.out.println();
System.out.println("Current Salary: " + money.format(currentSalary));
System.out.println("Amount of your raise: " + money.format(raise));
System.out.println("Your new salary: " + money.format(newSalary));
System.out.println();
}
}
Explanation / Answer
import java.util.Scanner;
public class Salary
{
public static void main (String[] args)
{
double currentSalary; // current annual salary
String rating; // performance rating
double raise = 0; // dollar amount of the raise
String Excellent = new String ("excellent");
String Good = new String ("good");
String Poor = new String ("poor");
Scanner scan = new Scanner(System.in);
// Get the current salary and performance rating
System.out.print ("Enter the current salary: ");
currentSalary = scan.nextDouble();
System.out.print ("Enter the performance rating: ");
rating = scan.next();
// Compute the raise -- Use if ... else ...
if (rating .equalsIgnoreCase(Excellent))
{raise = currentSalary + (currentSalary * .06);}
else
if (rating .equalsIgnoreCase(Good))
{raise = currentSalary + (currentSalary * .04);}
else
if (rating .equals(Poor))
{raise = currentSalary + (currentSalary * .015);}
// Print the results
System.out.println ("Amount of your raise: $" + (raise - currentSalary));
System.out.println ("Your new salary: $" + (currentSalary + (raise - currentSalary)));
}
}
sample output:
Enter the current salary :10000
Enter the performance rating:Excellent
Amount of ur raise :$6000.0
Your new Salary :$106000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.