Question

In: Computer Science

Write a short, java program that interacts with the user asking them to buy an xbox...

Write a short, java program that interacts with the user asking them to buy an xbox series x or PS5 and does the following:

  • create at least 2 double variables
  • create at least 1 constant
  • get at least 1 string input from the user
  • get at least 1 int/double input from the user
  • include both, incorporating the input you got from the user:
    • 1 multiway if-else with at least 3 "else if"
    • 1 nested if

Your program should combine all of these into one cohesive, interactive program with all the parts working together. In your program description, be sure to describe what your program does.

Solutions

Expert Solution

I have created A TestProgram class with double, int & String variable, taking inputs from user, if-else-if ladder and nested if as well with inline comments to explain the logic.

Do let me know in comments in case of any doubt.

import java.util.Scanner;

public class TestProgram {

    final double fixedXboxPrice = 5000.0, fixedPs5Price = 5100.0;
    String welcomeStr = "Welcome to Play Station & XBOX world";
    int qty;
    double finalPrice;


    public static void main(String[] args) {
        TestProgram obj = new TestProgram();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your name:");
        String name = sc.nextLine();
        System.out.println("Welcome "+name+"!");
        obj.startProgram();
    }

    public void startProgram() {
        Scanner sc = new Scanner(System.in);
        System.out.println(welcomeStr);
        // this loop will run until user selects option 3 from given menu
        while (true) {
            System.out.println("Choose the option from below menu:");
            System.out.println("1. XBOX Series");
            System.out.println("2. PS5");
            System.out.println("3. Exit");
            System.out.println("Enter 1 or 2 or 3:");
            int choice = sc.nextInt();
            if (choice == 1) {
                System.out.println("Enter quantity of XBOX");
                qty = sc.nextInt();
                calcDiscount(qty, fixedXboxPrice, choice);
            } else if (choice == 2) {
                System.out.println("Enter quantity of PS5");
                qty = sc.nextInt();
                calcDiscount(qty, fixedPs5Price, choice);
            } else if(choice == 3){
                System.exit(0);
            }else{
                System.out.println("Enter correct choice.");
            }
        }
    }

    //to demonstrate if-else-if ladder, let's provide discount to user based on below criteria and display final effective price
    // quantity <= 5 then discount of flat 10
    // quantity > 5 then 1% discount
    // quantity > 10 then 5% discount
    // quantity > 15 then 7% discount
    // quantity > 20 then 9 % discount and additional discount of flat 50 in case of ps5
    public void calcDiscount(int qty, double price, int choice) {

        if (qty > 20) {
            finalPrice = price - price * 0.09;
            if (choice == 2) // nested if
                finalPrice = finalPrice - 50;
        } else if (qty > 15) {
            finalPrice = price - price * 0.07;
        } else if (qty > 10) {
            finalPrice = price - price * 0.05;
        } else if (qty > 5) {
            finalPrice = price - price * 0.01;
        } else if (qty > 0) {
            finalPrice = finalPrice - 10;
        } else {
            System.out.println("Quantity entered must be at least 1.");
            return; // return control only. in this case no need to print price because qty is <= 0.
        }
        System.out.println("Final price after discount : "+finalPrice);
    }
}

Screenshot of code:

Output:


Related Solutions

Write a short, cohesive java program in jGrasp that interacts with the user and does the...
Write a short, cohesive java program in jGrasp that interacts with the user and does the following: create at least 2 double variables create at least 1 constant get at least 1 string input from the user get at least 1 int/double input from the user include both, incorporating the input you got from the user: 1 multiway if-else with at least 3 "else if" 1 nested if Your program should combine all of these into one cohesive, interactive program...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
Can you provide java code for a program that prompts the user by asking "How many...
Can you provide java code for a program that prompts the user by asking "How many one mile races have you ran?". After the user inputs how many one mile races have they run it then prompts the user to input how many seconds it took for them to finish each race. So for example, if the user ran 6 races then the user will have to input 6 race times. Is there a way when you prompt the user...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
1. Write a program that keeps asking the user for a password until they correctly name...
1. Write a program that keeps asking the user for a password until they correctly name it. Once they correctly enter the password, the program congratulates the user and tells them how many guesses it took. Be sure to be grammatically correct with the guess/guesses output. Call the program LastNamePassword. Example (user input in italics) What is the password? monkeys Incorrect. Guess again. dishwasher Incorrect. Guess again. aardvark Correct! You got the password, and it took you 3 guesses to...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT