Question

Need some guidance with the following problems as I am new to Java programming. Any assistance...

Need some guidance with the following problems as I am new to Java programming. Any assistance is greatly appreciated.

Thanks

Using printf and specifiers for all print instructions:

1.            Code the following:

a.            10% is stored in a variable called discount when the customer is a student; otherwise, it stores 0%. Code this if … else control structure using the conditional operator (ternary operator). The variables discount and student have already been declared. Assume student is a boolean variable.

b.            Code a do-while loop that keeps printing the message “JACKPOT!” as long as reel1 and reel2 and reel3 equals “Ace”. Use equalsIgnoreCase() as the method to compare what is in the reel variables to “Ace”. Assume reel1, reel2, reel3, and input (for the Scanner class) are already declared.

c.             Re-code 1b using a while loop. Prime the reel variables so the loop is entered the first time.

d.            Code a fall-through switch statement that will print the following messages:

When the value in guess is 1: “Sorry, the coin is not here!”

When the value in guess is 2: “Sorry, the coin is not here!”

When the value in guess is 3: “Bazinga! Take the coin!”

When the value in guess is any other value: “The way you’re luck is running, if you were a politician you’d be honest!”

e.            Re-code 1d using double-selection ifs.

f.             Code a for loop that let’s contestants exit if they guess the correct number of pennies in a penny jar, but only allows 5 attempts. The error message, “Wrong guess! Try again!”, prints when the loop is entered and the guess is wrong. If the contestant enters the correct number of pennies and before exiting the loop, a congratulatory message prints, “Congratulations! You’ve won $100!”. The actual number of pennies is 1000 and what the contestant enters is stored in a variable called numberOfPennies. Assume numberOfPennies and the input variable for the Scanner class are already declared.    

g.            Re-code the following as a while loop with a switch statement. Assume go is a declared boolean variable that has been initialized to true.

do

{

                System.out.printf(“%nEnter the color of the traffic light: “

+ “%n%n1. Green”

+ “%n2. Yellow”

+ “%n3. Red%n”);

                trafficLight = input.nextInt();

                if(trafficLight == 1)

                {

                                System.out.printf(“%nStep on the gas pedal and go!%n”);

                                go = false;                          

                }

                if(trafficLight == 2)

                {

                                System.out.printf(“%nPress on the brake and prepare to stop!%n”);                        

                                go = false;

                }

                if(trafficLight == 3)

                {

                                System.out.printf(“%nSTOP!!!%n”);

                                go = false;                          

                }

                if(trafficLight < 1 || trafficLight > 3)

                {                             

                                System.out.printf(“%nInvalid choice! Try again!%n”);                     

                }

               

} while(go);         // END do-while when go is false

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you need more information:-

============================================

import java.util.Scanner;

public class Tester2 {

   public static void main(String[] args) {

       /**
       * a. 10% is stored in a variable called discount when the customer is a
       * student; otherwise, it stores 0%. Code this if … else control structure using
       * the conditional operator (ternary operator). The variables discount and
       * student have already been declared. Assume student is a boolean variable.
       */
       boolean student = false; // This can be changed and tested;
       float discount = student ? (10 / 100) : 0; // If student true then it will be 10%, if not 0%
       System.out.println(discount);

       /**
       * b. Code a do-while loop that keeps printing the message “JACKPOT!” as long as
       * reel1 and reel2 and reel3 equals “Ace”. Use equalsIgnoreCase() as the method
       * to compare what is in the reel variables to “Ace”. Assume reel1, reel2,
       * reel3, and input (for the Scanner class) are already declared.
       */
       Scanner input = new Scanner(System.in);
       String reel1 = "", reel2 = "", reel3 = "";
       do {
           System.out.println("JACKPOT!");
           System.out.println("Enter reel1: ");
           reel1 = input.nextLine();// Reads input
           System.out.println("Enter reel2: ");
           reel2 = input.nextLine();// Reads input
           System.out.println("Enter reel3: ");
           reel3 = input.nextLine();// Reads input
       } while (!(reel1.equalsIgnoreCase("Ace") && reel2.equalsIgnoreCase("Ace") && reel3.equalsIgnoreCase("Ace")));

       /**
       * c. Re-code 1b using a while loop. Prime the reel variables so the loop is
       * entered the first time.
       */
       reel1 = reel2 = reel3 = "";
       while (!(reel1.equalsIgnoreCase("Ace") && reel2.equalsIgnoreCase("Ace") && reel3.equalsIgnoreCase("Ace"))) {
           System.out.println("JACKPOT!");
           System.out.println("Enter reel1: ");
           reel1 = input.nextLine();// Reads input
           System.out.println("Enter reel2: ");
           reel2 = input.nextLine();// Reads input
           System.out.println("Enter reel3: ");
           reel3 = input.nextLine();// Reads input
       }

       /**
       * d. Code a fall-through switch statement that will print the following
       * messages:
       *
       * When the value in guess is 1: “Sorry, the coin is not here!”
       *
       * When the value in guess is 2: “Sorry, the coin is not here!”
       *
       * When the value in guess is 3: “Bazinga! Take the coin!”
       *
       * When the value in guess is any other value: “The way you’re luck is running,
       * if you were a politician you’d be honest!”
       */
       int guess = 2;// This can be changed for testing
       switch (guess) {
       case 1:
           System.out.println("Sorry, the coin is not here!");
           break;
       case 2:
           System.out.println("Sorry, the coin is not here!");
           break;
       case 3:
           System.out.println("Bazinga! Take the coin!");
           break;
       default:
           System.out.println("The way you’re luck is running, if you were a politician you’d be honest!");
       }

       /**
       * e. Re-code 1d using double-selection ifs.
       */
       if (guess == 1) {
           System.out.println("Sorry, the coin is not here!");
       } else if (guess == 2) {
           System.out.println("Sorry, the coin is not here!");
       } else if (guess == 3) {
           System.out.println("Bazinga! Take the coin!");
       } else {
           System.out.println("The way you’re luck is running, if you were a politician you’d be honest!");
       }

       /**
       * f. Code a for loop that let’s contestants exit if they guess the correct
       * number of pennies in a penny jar, but only allows 5 attempts. The error
       * message, “Wrong guess! Try again!”, prints when the loop is entered and the
       * guess is wrong. If the contestant enters the correct number of pennies and
       * before exiting the loop, a congratulatory message prints, “Congratulations!
       * You’ve won $100!”. The actual number of pennies is 1000 and what the
       * contestant enters is stored in a variable called numberOfPennies. Assume
       * numberOfPennies and the input variable for the Scanner class are already
       * declared.
       */
       for (int i = 0; i < 5; i++) {
           System.out.println("Enter the guess: ");
           int numberOfPennies = input.nextInt();
           if (numberOfPennies == 1000) {
               System.out.println("You’ve won $100!");
               break;
           } else {
               System.out.println("Wrong guess! Try again!");
           }
       }

       /**
       * g. Re-code the following as a while loop with a switch statement. Assume go
       * is a declared boolean variable that has been initialized to true.
       */

       int trafficLight;
       boolean go = false;
       do {
           System.out.printf("%nEnter the color of the traffic light: "

                   + "%n%n1. Green"

                   + "%n2. Yellow"

                   + "%n3. Red%n");
           trafficLight = input.nextInt();
           switch (trafficLight) {
           case 1:
               System.out.printf("%nStep on the gas pedal and go!%n");
               go = false;
               break;
           case 2:
               System.out.printf("%nPress on the brake and prepare to stop!%n");
               go = false;
               break;
           case 3:
               System.out.printf("%nSTOP!!!%n");
               go = false;
               break;
           default:
               System.out.printf("%nInvalid choice! Try again!%n");
               break;
           }
       } while (go); // END do-while when go is false

   }

}

================================

===

====

===

============================

OUTPUT:-

0.0
JACKPOT!
Enter reel1:
d
Enter reel2:
s
Enter reel3:
d
JACKPOT!
Enter reel1:
Ace
Enter reel2:
Ace
Enter reel3:
Ace
JACKPOT!
Enter reel1:
Ace
Enter reel2:
Ace
Enter reel3:
Ace
Sorry, the coin is not here!
Sorry, the coin is not here!
Enter the guess:
1
Wrong guess! Try again!
Enter the guess:
2
Wrong guess! Try again!
Enter the guess:
4
Wrong guess! Try again!
Enter the guess:
5
Wrong guess! Try again!
Enter the guess:
1000
You’ve won $100!

Enter the color of the traffic light:

1. Green
2. Yellow
3. Red
1

Step on the gas pedal and go!

===

======

Thanks

Add a comment
Know the answer?
Add Answer to:
Need some guidance with the following problems as I am new to Java programming. Any assistance...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am almost done with this code, but for some reason I am still getting an...

    I am almost done with this code, but for some reason I am still getting an error message: Some of the instructions were: Using a loop, prompt for a currency to find. Inside the loop that manages the input, call the lookUpCurrency() method can pass the inputted currency code value. The lookUpCurrency() method needs to loop through the list of currencies and compare the passed currency code with the code in the array. If found, return true - otherwise return...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • Need help answering this java programming question. Thank you in advance! g. Re-code the following as...

    Need help answering this java programming question. Thank you in advance! g. Re-code the following as a do-while loop with a switch statement for the if-elses, Assume goingToMovies is declared and has been set to true, so it can enter the loop. Assume choice, movie, Cont and input are already declared. while(goingToMavies) Sustem.aut printf("%nChoose a number from 1 through 5 for the movie" + "you want to watch: ") choice inputnextint) iffchoice 1) movie "Shazam!" else iffchoice 2) movie "A...

  • d. Assumecalories is already declared as an integer.  Code a fall-through switchstatement by dividing calories by 100...

    d. Assumecalories is already declared as an integer.  Code a fall-through switchstatement by dividing calories by 100 in the controlling expression of the switch header, so the following messages print: When caloriesare 1200 through 1800:  “Diet is on target.” When caloriesare 2000 through 2550:  “Calorie intake ok if active.” When caloriesare any other value:  “Calorie intake is either insufficient or too much!” e. Re-code 1d using double-selection ifs.  You’ll use a conditional logical operator to join the sets of relational conditions (choose the right one).  ...

  • JAVA programming The task of parsing a file for correctness is an essential part of any...

    JAVA programming The task of parsing a file for correctness is an essential part of any programmer toolkit. The check for a balanced set of brackets in a file a Stack can be used and the following algorithm: • The source file is read character by character • Each time an opening '{' is read it is pushed onto the stack • Each time a closing '}' is read the stack is popped • If the stack is empty when...

  • Fundamental Programming Question 1 (a) Consider each of the following questions carefully. You are required to...

    Fundamental Programming Question 1 (a) Consider each of the following questions carefully. You are required to give the answer true or false and justify your answer. If it is true, explain why it is true i. Linked lists are statically allocated. [2 marks] ii. Automatic variables are destroyed (deallocated) by the C++ runtime system [2 marks] i. You cannot make a reference to unallocated memory when using a vector [2 marks] iv. The virtual keyword turns off runtime checking [2...

  • Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...

    Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the following data entry error conditions: A number less than 1 or greater than 12 has been entered for the month A negative integer has been entered for the year utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info import java.util.*; //Class definition public...

  • Please I need help with the following questions in JAVA programming language. Please comment the code...

    Please I need help with the following questions in JAVA programming language. Please comment the code to help me understand, thanks. Exercise 1: Inheritance 1. Firstly, create and compile a simple class called Parent. Give it the following behaviour: a. A default constructor that does nothing other than print out “Parent default constructor” using System.out b. A single method called getMessage which returns a String, e.g. “Parent message” 2. Next, create and compile a class called Child. Give it the...

  • It’s almost election day and the election officials need a program to help tally election results....

    It’s almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program’s job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT