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. The performance rating here is being 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.
Add the if.or 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.
// ***************************************************************
// 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.0; // 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.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();
}
}
Game Menu Chooser
File GameMenu.java contains a program that takes as input the user’s choice of difficulty level and display the choice. For example, if the user chooses 1, then your program should display “You picked the level Easy”.
Add the switch statement to the program to make it run as described above.
import java.util.Scanner;
public class GameMenu
{
public static void main(String[] args)
{
char choice;
Scanner scan = new Scanner(System.in);
System.out.println("Please choose a difficulty level");
System.out.println(" 1 - Easy");
System.out.println(" 2 - Normal");
System.out.println(" 3 - Hard");
System.out.println("Choice: ");
choice = scan.next().charAt(0);
System.out.print("You Chose: " + choice);
switch (choice)
{
case '1': System.out.println (" You picked the level Easy.");
break;
case '2': System.out.println (" You picked the level Normal.");
break;
case '3': System.out.println (" You picked the level Hard.");
break;
}
if (choice > 3)
choice = 0;
else
System.out.println(" You made an illegal choice.");
choice = 0;
// Display the user’s choice using a switch statement
// If the user made a choice that is not 1, 2, or 3,
// display “You made an illegal choice”.
}
}
Rock, Paper, Scissors
Program Rock.java contains a skeleton for the game Rock, Paper, Scissors. Save it to your directory. Add statements to the program as indicated by the comments so that the program asks the user to enter a play, generates a random play for the computer, compares them and announces the winner (and why). For example, one run of your program might look like this:
$ java Rock
Enter your play: R, P, or S
r
Computer play is S
Rock crushes scissors, you win!
Note that the user should be able to enter either upper or lower case r, p, and s. The user's play is stored as a string to make it easy to convert whatever is entered to upper case. Use a switch statement to convert the randomly generated integer for the computer's play to a string.
// ****************************************************************
// Rock.java
//
// Play Rock, Paper, Scissors with the user
//
// ****************************************************************
import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void main(String[] args)
{
String personPlay; //User's play -- "R", "P", or "S"
String computerPlay; //Computer's play -- "R", "P", or "S"
int computerInt; //Randomly generated number used to determine
//computer's play
Scanner scan = new Scanner(System.in);
Random generator = new Random();
//Generate computer's play (0,1,2)
//Translate computer's randomly generated play to string
switch (computerInt)
{
}
//Get player's play from input-- note that this is stored as a string
//Make player's play uppercase for ease of comparison
//Print computer's play
//See who won. Use nested ifs instead of &&.
if (personPlay.equals(computerPlay))
System.out.println("It's a tie!");
else if (personPlay.equals("R"))
if (computerPlay.equals("S"))
System.out.println("Rock crushes scissors. You win!!");
else
}
}
** Edit **

New Topic/Question
Reply



MultiQuote




|