Need help with my java lab!

using if, if/else, switch, and random

Page 1 of 1

3 Replies - 8254 Views - Last Post: 02 March 2009 - 09:38 AM Rate Topic: -----

#1 hell0k1tty   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-March 09

Need help with my java lab!

Posted 01 March 2009 - 11:30 PM

There are 3 different topics... Computing a Raise, Game Menu Chooser, and Rock paper scissors.


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 ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with my java lab!

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Need help with my java lab!

Posted 01 March 2009 - 11:33 PM

So where do you need help?

You've posted the assignment, & some code... what exactly is going on? Do you have a specific question about the code or about how to complete part of the assignment?
Was This Post Helpful? 0
  • +
  • -

#3 hell0k1tty   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-March 09

Re: Need help with my java lab!

Posted 01 March 2009 - 11:37 PM

View Postno2pencil, on 1 Mar, 2009 - 10:33 PM, said:

So where do you need help?

You've posted the assignment, & some code... what exactly is going on? Do you have a specific question about the code or about how to complete part of the assignment?


I'm not sure how to complete what they are asking. I did most of the game level one, but i couldn't figure out how to get it to say "you made an illegal choice" if you don't press 1, 2, or 3. This will also go for any other character entered. The other ones I am so lost on..

This post has been edited by hell0k1tty: 01 March 2009 - 11:38 PM

Was This Post Helpful? 0
  • +
  • -

#4 BigAnt   User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Need help with my java lab!

Posted 02 March 2009 - 09:38 AM

You use a switch for checking the char inputted, add a default case which will be everything that does not match the other cases.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1