470,831 Members | 1,983 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,831 developers. It's quick & easy.

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: ");
currentSalary = Keyboard.readDouble();
System.out.print ("Enter the performance rating (1 = Excellent, 2 = Good, or 3 = Poor): ");
rating = Keyboard.readInt();

// 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: ");
currentSalary = Keyboard.readDouble();
System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
rating = Keyboard.readString();

// 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;
Excellent= Keyboard.readString();
if (rating == Excellent)
raise=0.06;
String Good;
Good= Keyboard.readString();
if (rating==Good)
raise=0.04;
Good= Keyboard.readString();
String Poor;
Poor= Keyboard.readString();
if (rating==Poor)
raise=0.015;
Poor= Keyboard.readString();
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: ");
currentSalary = Keyboard.readDouble();
System.out.print ("Enter the performance rating (1 = Excellent, 2 = Good, or 3 = Poor): ");
rating = Keyboard.readInt();

// 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: ");
currentSalary = Keyboard.readDouble();
System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
rating = Keyboard.readString();

// 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;
Excellent= Keyboard.readString();
if (rating == Excellent)
raise=0.06;
String Good;
Good= Keyboard.readString();
if (rating==Good)
raise=0.04;
Good= Keyboard.readString();
String Poor;
Poor= Keyboard.readString();
if (rating==Poor)
raise=0.015;
Poor= Keyboard.readString();
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: ");
  2.    currentSalary = Keyboard.readDouble();
  3.    System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
  4.    rating = Keyboard.readString();
  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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by msammart | last post: by
11 posts views Thread by 05l8kr | last post: by
1 post views Thread by punkybrewster | last post: by
10 posts views Thread by MichiganMan | last post: by
4 posts views Thread by danbuttercup | last post: by
1 post views Thread by Undermine | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.