CS110 Java Programming Assignment 3

Instructor: Trish Cornez

if - else Statements





PROGRAM 1:
Assume the term "twenties" implies that a number is in the range 20..29 inclusive. Write a program that prompts the user for three int values. Display true if one or more of the entered values are "twenties", otherwise display false.

Execution 1:
    Input integer 1: 23
    Input integer 2: 23
    Input integer 3: 22
    Result: true


Execution 2:
    Input integer 1: 12
    Input integer 2: 35
    Input integer 3: 49
    Result: false


Execution 3:
    Input integer 1: 1
    Input integer 2: 5
    Input integer 3: 21
    Result: true






PROGRAM 2:
When Computer Science students get together for a party, they like to eat pizza. A CS party is successfgul when the number of pizzas eaten is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of pizzas eaten.

Write a program that prompts the user for the number of pizzas eaten and if the party was held on the weekend. Display a true if the party was successful and false otherwise.

          TIP: Use the String method equalsIgnoreCase() to accept upper and lowercase answers.
                boolean isHappy;
                Scanner in = new Scanner(System.in);
                System.out.print("Are you happy? ");
                String answer = in.next();
                if (answer.equalsIgnoreCase("yes")){
                  isHappy = true;
                }
                else {
                  isHappy = false;
                }
          
              
Execution 1:
    Pizzas: 61
    Was it the weekend? no
    Successful: false


Execution 2:
    Pizzas: 39
    Was it the weekend? yes
    Successful: false


Execution 3:
    Pizzas: 40
    Was it the weekend? yes
    Successful: true






PROGRAM 3:
Write a program that prompts the user for three ints, a, b, and c. Display true if it is possible to add two of the ints to get the third.

Execution 1:
    Input integer 1: 2
    Input integer 2: 5
    Input integer 3: 3
    Result: true


Execution 2:
    Input integer 1: 9
    Input integer 2: 4
    Input integer 3: 5
    Result: true


Execution 3:
    Input integer 1: 9
    Input integer 2: 5
    Input integer 3: 5
    Result: false






PROGRAM 4:
Write a program that prompts the user for two int values. If the two values have the same remainder when divided by 5, then return the smaller of the two values. However, in all cases, if the two values are the same, return 0. Otherwise, return the largest of the values.

Execution 1:
    Input integer 1: 6
    Input integer 2: 2
    Result: 6


Execution 2:
    Input integer 1: 2
    Input integer 2: 7
    Result: 2


Execution 3:
    Input integer 1: 7
    Input integer 2: 7
    Result: 0






PROGRAM 5:
Write a program that will prompt the user for a purchase amount. If this bill amount is valid, then prompt the user again for the payment amount. If this is valid then compute the change to be returned. This amount should be displayed in pennies, nickels, dimes, quarters, single dollar bills, five dollar bills, ten dollar bills, and 20 dollar bills. If either of the input values are invalid, an error message should be displayed explaining precisly where the error is.
Do NOT display bills or coins that will not be returned.

Sample run number 1:
This program will compute the proper change from a purchase.

Please enter the purchase amount: $1.32
Please enter the payment amount: $20
The change is :
    1 TEN(S)
    1 FIVE(S)
    3 ONE(S)
    2 QUARTER(S)
    1 DIME(S)
    1 NICKEL(S)
    3 PENNIES


Sample run number 2:
This program will compute the proper change from a purchase.

Please enter the purchase amount: $-1
The purchase amount has an error!
The purchase amount must be at least 1 cent.



Sample run number 3:
This program will compute the proper change from a purchase.

Please enter the purchase amount: $7.32
Please enter the payment amount: $5
The payment amount has an error.
Payment amount must be at greater than or equal to the purchase amount.





PROGRAM 6:
Write an interactive program that plays the game of "Rock, paper, scissors."
In this game, a player will be prompted to enter (or dislay a hand symbol representing) either "rock," "paper," or "scissors." The winner is the one whose choice dominates the other.

Use a series of nested if statements to cover all possibilities.
The rules are:
  1. paper dominates (wraps) rock
  2. rock dominates (breaks) scissors
  3. scissors dominate (cut) paper.

EXAMPLE:

    WELCOME TO THE GAME OF ROCK PAPER OR SCISSORS!!

PLAYER, please choose ROCK (0), PAPER (1), or SCISSORS (2).
PLAYER: 0
    You selected ROCK.
    The computer has randomly selected PAPER.
    SORRY, YOU LOST!
    The computer won because PAPER dominates (wraps) ROCK.

Guidelines

  1. Use static final to declare the constants ROCK (0), PAPER (1), and SCISSORS (2).

  2. The opponent (the computer) must have a value from 0 to 2 generated by the program. Your program must be able to generate a random integer from 0 through 2. To do this, you will need to perform the steps below:
    Step 1: Specify a maximum value.
      static final int MAX = 3;


    Step 2: Use the following expression when you need to generate a random number from 0 to 2:
      int opponent = (int) (Math.random() * MAX);