470,831 Members | 1,983 Online

# Help with Salary Program

Help with following Programs:

Write two programs one where the performance rating here shoud be entered as a int where Excellent =1, Good= 2, Poor=3. 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.

// ************************************************** *************
// Salary.java
// Computes the amount of a raise and the new
// salary for an employee. The current salary
// and a performance rating (an int: 1 = "Excellent",
// 2 = "Good" or 3 = "Poor") are input.
// ************************************************** *************

import cs1.Keyboard;
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
int rating; // performance rating

System.out.print ("Enter the current salary: ");
System.out.print ("Enter the performance rating (1 = Excellent, 2 = Good, or 3 = Poor): ");

// 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();
}
}

I tried putting the following (below) after the comment// Compute the raise using if ... but it gave an error that said raise might not have been initialized.
if (rating == 1)
{
raise=(currentSalary * 0.06);
newSalary= currentSalary + raise;
}

if (rating == 2)
{
raise = (currentSalary * 0.04);
newSalary= currentSalary + raise;
}

if (rating == 3)
{
raise = (currentSalary * 0.015);
newSalary= currentSalary + raise;
}

The other program perfromance rating should be entered as a String. The three possible ratings are "Excellent", "Good", and "Poor". 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. Note that you will have to use the equals method of the String class (not the relational operator ==) to compare two strings.

// ************************************************** *************
// Salary2.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 cs1.Keyboard;
import java.text.NumberFormat;
public class Salary2
{
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

System.out.print ("Enter the current salary: ");
System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");

// 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();

}
}

I Tried Putting the following(below) in the 2nd program after the coment // Compute the raise using if...but it gave an error that said raise and newSalary might not have been initialized.

String Excellent;
if (rating == Excellent)
raise=0.06;
String Good;
if (rating==Good)
raise=0.04;
String Poor;
if (rating==Poor)
raise=0.015;
newSalary = (currentSalary + (+raise));

Thanks for the help
Oct 6 '06 #1
1 10767
r035198x
13,262 8TB
Help with following Programs:

Write two programs one where the performance rating here shoud be entered as a int where Excellent =1, Good= 2, Poor=3. 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.

// ************************************************** *************
// Salary.java
// Computes the amount of a raise and the new
// salary for an employee. The current salary
// and a performance rating (an int: 1 = "Excellent",
// 2 = "Good" or 3 = "Poor") are input.
// ************************************************** *************

import cs1.Keyboard;
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
int rating; // performance rating

System.out.print ("Enter the current salary: ");
System.out.print ("Enter the performance rating (1 = Excellent, 2 = Good, or 3 = Poor): ");

// 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();
}
}

I tried putting the following (below) after the comment// Compute the raise using if ... but it gave an error that said raise might not have been initialized.
if (rating == 1)
{
raise=(currentSalary * 0.06);
newSalary= currentSalary + raise;
}

if (rating == 2)
{
raise = (currentSalary * 0.04);
newSalary= currentSalary + raise;
}

if (rating == 3)
{
raise = (currentSalary * 0.015);
newSalary= currentSalary + raise;
}

The other program perfromance rating should be entered as a String. The three possible ratings are "Excellent", "Good", and "Poor". 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. Note that you will have to use the equals method of the String class (not the relational operator ==) to compare two strings.

// ************************************************** *************
// Salary2.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 cs1.Keyboard;
import java.text.NumberFormat;
public class Salary2
{
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

System.out.print ("Enter the current salary: ");
System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");

// 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();

}
}

I Tried Putting the following(below) in the 2nd program after the coment // Compute the raise using if...but it gave an error that said raise and newSalary might not have been initialized.

String Excellent;
if (rating == Excellent)
raise=0.06;
String Good;
if (rating==Good)
raise=0.04;
String Poor;
if (rating==Poor)
raise=0.015;
newSalary = (currentSalary + (+raise));

Thanks for the help
You missed the part of your requirements that said
"Note that you will have to use the equals method of the String class (not the relational operator ==) to compare two strings."
Expand|Select|Wrap|Line Numbers
1. System.out.print ("Enter the current salary: ");
3.    System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
5.    if(rating.equalsIgnoreCase("Excellent")) {
6.        rating = currentSalary * 0.06;
7.    }
8.    else if(rating.equalsIgnoreCase("Good")) {
9.        rating = currentSalary * 0.04;
10.    }
11.    else if(rating.equalsIgnoreCase("Poor")) {
12.        rating = currentSalary * 0.015;
13.
14.    }
15. //etc
16.
Notice that I have used equalsIgnoreCase instead of just equals(It's your decision which one you want to use)
Also notice that I have used code tags for the code.
Oct 7 '06 #2