In: Computer Science
Computing A Raise
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).
// ************************************************************ |
|
import java.util.Scanner; import java.text.NumberFormat;
public class Salary {
public static void main (String[] args) {
}
double currentSalary; double raise; double newSalary; String rating;
// employee's current salary // amount of the raise // new salary for the employee // 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.next();
// 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();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CODE TO COPY
NOTE: YOU CAN REPLACE rating.equals() TO rating.equalsIgnoreCase() INORDER TO AVOID CASE SENSITIVE
//
************************************************************
// 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 = 0; // amount of the
raise
double newSalary = 0; // 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.next();
// Compute the raise using if
...
if (rating.equals("Excellent"))
{
raise = 6;
// raise =
currentSalary*(raise/100);
newSalary =
currentSalary + raise;
} else if (rating.equals("Good"))
{
raise = 4;
// raise =
currentSalary*(raise/100);
newSalary =
currentSalary + raise;
} else if (rating.equals("Poor"))
{
raise =
1.5;
// raise =
currentSalary*(raise/100);
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
//
************************************************************
// 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 = 0; // amount of the
raise
double newSalary = 0; // 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.next();
// Compute the raise using if
...
if (rating.equals("Excellent")) {
// if the string entered is Excellent then raise is 6
raise = 6; //set
raise as 6
// raise =
currentSalary*(raise/100); //uncomment this if you want to raise by
percentage
newSalary =
currentSalary + raise; // by default it raises just by adding
} else if (rating.equals("Good")) {
// if entered string is Good
raise = 4; //set
raise as 4
// raise =
currentSalary*(raise/100);
newSalary =
currentSalary + raise; // by default it raises just by adding
} else if (rating.equals("Poor")) {
// if entered string is Poor
raise = 1.5;/ /
set raise a s1.5
// raise =
currentSalary*(raise/100); //uncomment this if you want to raise by
percentage
newSalary =
currentSalary + raise; // by default it raises just by adding
}
// 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();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
OUTPUT
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IF YOU HAVE ANY PROBLEM REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL HELP YOU///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////