Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.Code the following: a. 10% is stored in a variable called discount when the cu

ID: 3593499 • Letter: 1

Question

1.Code the following:

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

b. Code a do-while loop that keeps printing the message “CREDIT DENIED!” as long as creditScore is not greater than or equal to 700, yrsOnJob is not greater than or equal to 2, and annual salary is not greater than or equal to 30000. You don’t need to use negations. Assume creditScore, yrsOnJob, annualSalary, and input (for the Scanner class) are already declared. Prompt for the values as integers.

c. Re-code 1b using a while loop. Prime creditScore, yrsOnJob, and annualSalary so the loop is entered the first time.

d. Assume time is entered on the hour. Assume that time is already declared. Code a fall-through switch statement that will print the following messages:

When the value in time is 900: “Take an extra 20% off!”

When the value in time is 1000: “Take an extra 20% off!”

When the value in time is 1100: “Take an extra 20% off!”

When the value in time is 1200: “Take an extra 20% off!”

When the value in time is any other value:

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

f. Code a for loop with the switch structure from 1d and allow 3 attempts. Prompt for the user to enter an on-the-hour military time. When the time entered is in the correct range for the morning sale make sure you exit the loop (not the program) after the message is printed. Print the message “No more attempts left!” if it is the last attempt for an invalid entry.

g. Re-code the following as a do-while loop with a switch statement. Assume choice is declared and has been initialized to 1, so it can enter the loop. Assume continue and itemQty are already declared.

while(choice > 0 && choice < 4)

            System.out.printf(“%nEnter a choice: “

+ “%n1. Tootsie Roll Pops @ $1.25 each”

+ “%n2. Snickers @ $0.98 each”

+ “%n3. York’s Peppermint Patty @ $1.00 each”

+ “%n4. Exit”);

            choice = input.nextInt();

            if(choice == 1)

            {

                        System.out.printf(“%nHow many tootsie roll pops? ”);

                        itemQty = input.nextInt();                           

            }//END if choice = 1

            if(choice == 2)

            {

                        System.out.printf(“%nHow many snickers? ”);

                        itemQty = input.nextInt();                           

            }//END if choice = 2

            if(choice == 3)

            {

                        System.out.printf(“%nHow many peppermint patties? ”);         

                        itemQty = input.nextInt();                           

            }//END if choice = 3             

           

            if(choice < 1 || choice > 4)

            input.nextLine();

System.out.printf(“%nInvalid choice! Try again? Enter “

+ “’Y’ or ’N’: ”);

                        continue = input.nextLine().charAt(0);      

                        if(Character.toUpperCase(continue) == ‘Y’)

                        {

                                    choice = 1;

                        }//END if continue = ‘Y’

            }//END if choice < 1 OR > 4

}//END while choice > 0 OR < 4

Explanation / Answer

import java.util.Scanner;

public class ControlStruct {

public static void main(String[] args) {

//Answer a start

boolean military=true;

double discount=0;

discount=military? 10:0;

System.out.println(discount);

military=false;

discount=military? 10:0;

System.out.println(discount);

//Answer a ends

//Answer b starts

Scanner sc=new Scanner(System.in);

int creditScore=0;

int yrsOnJob=0;

double annualSalary=0;

do{

System.out.println("Enter credit score:");

creditScore=sc.nextInt();

System.out.println("Enter Years On Job:");

yrsOnJob=sc.nextInt();

System.out.println("Enter Annual Salary:");

annualSalary=sc.nextDouble();

if(creditScore<700 || yrsOnJob<2 || annualSalary<30000){

System.out.println("CREDIT DENIED!");

}

else{

break;

}

}

while(true);

//Answer b ends

//Answer c starts

while(true){

System.out.println("Enter credit score:");

creditScore=sc.nextInt();

System.out.println("Enter Years On Job:");

yrsOnJob=sc.nextInt();

System.out.println("Enter Annual Salary:");

annualSalary=sc.nextDouble();

if(creditScore<700 || yrsOnJob<2 || annualSalary<30000){

System.out.println("CREDIT DENIED!");

}

else{

break;

}

}

//Answer c ends

//Answer d starts

int time=900;

switch (time) {

case 900:

System.out.println("Take an extra 20% off!");

break;

case 1000:

System.out.println("Take an extra 20% off!");

break;

case 1100:

System.out.println("Take an extra 20% off!");

break;

case 1200:

System.out.println("Take an extra 20% off!");

break;

default:

break;

}

//Answer d ends

}

}

Answers:

a.

discount=military? 10:0;

b.

do{

System.out.println("Enter credit score:");

creditScore=sc.nextInt();

System.out.println("Enter Years On Job:");

yrsOnJob=sc.nextInt();

System.out.println("Enter Annual Salary:");

annualSalary=sc.nextDouble();

if(creditScore<700 || yrsOnJob<2 || annualSalary<30000){

System.out.println("CREDIT DENIED!");

}

else{

break;

}

}

while(true);

c.

while(true){

System.out.println("Enter credit score:");

creditScore=sc.nextInt();

System.out.println("Enter Years On Job:");

yrsOnJob=sc.nextInt();

System.out.println("Enter Annual Salary:");

annualSalary=sc.nextDouble();

if(creditScore<700 || yrsOnJob<2 || annualSalary<30000){

System.out.println("CREDIT DENIED!");

}

else{

break;

}

}

d.

switch (time) {

case 900:

System.out.println("Take an extra 20% off!");

break;

case 1000:

System.out.println("Take an extra 20% off!");

break;

case 1100:

System.out.println("Take an extra 20% off!");

break;

case 1200:

System.out.println("Take an extra 20% off!");

break;

default:

break;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote