Question

In: Computer Science

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 with all the parts working together. In your program description, be sure to describe what your program does.

Solutions

Expert Solution

The code for java program is below. Screenshot of output is also provided.It is very simple program as you ahve asked. all the variables and logic is self explanatory. code comments are also provided. Program satisfies all the conditions you have given. It is interactive and user friendly

###############################################################################

PROGRAM DESCRIPTION-->>>>>

This program has Three double variables, one integer constant, one string variable, and one integer variable. This Program uses Scanner to take input from user. Program asks you to enter your name in string variable. Then it shows an interactive and user friendly menu to perform different actions. It gives you 5 choices of actions. Addition,Subtraction,Multiplication,Average,and the last task is find if a number is prime or not. It asks you the choice.once you enter your choice in integer variable choice, It uses different if esle if statemnts to match your choice. For the first 4 choices it prompts you to enter two numbers, then perform the specific operation addition or multiplication. It uses constant variable for finding average. and after finding result prints your name and result on screen in user friendly way. In the last choice it uses nested if else to print if a number isprime or not.

######################################################################
CODE-->>>

import java.util.Scanner; //importing scanner to take input from user

public class MyClass {
        public static void main(String[] args)
        {
                double x,y,result; //double variables
                final int div=2; //int constant
                String name; //string variable
                int choice; //int variable
                Scanner scan=new Scanner(System.in); //creating object of scanner to take input
                
                System.out.println("Enter your Name : ");
                name=scan.next(); //taking input for name
                System.out.println("What do you want to do ?"); //prompting diffrent choices
                System.out.println("1. Press 1 for Adding two numbers");
                System.out.println("2. Press 2 for Multiplying two numbers");
                System.out.println("3. Press 3 for Subtracting two numbers");
                System.out.println("4. Press 4 to find Average of two numbers");
                System.out.println("5. Press 5 to know if a number is Prime");
                
                choice=scan.nextInt(); //entering choice
                if(choice==1) //if choice=1
                {
                        System.out.println("Enter first number : ");
                        x=scan.nextDouble(); //enter first number
                        System.out.println("Enter second number : ");
                        y=scan.nextDouble(); //enter second no
                        result=x+y; //adding both numbers
                        //printing name as well as result
                        System.out.println("Hey "+ name +", Addition of "+x+ " and "+y+" is "+result); 
                }
                
                else if(choice==2) //elseif
                {
                        System.out.println("Enter first number : ");
                        x=scan.nextDouble();
                        System.out.println("Enter second number : ");
                        y=scan.nextDouble();
                        result=x*y; //multiplying numbers
                        //printing name as well as result
                        System.out.println("Hey "+ name +", Multiplication of "+x+ " and "+y+" is "+result);
                }
                
                else if(choice==3)
                {
                        System.out.println("Enter first number : ");
                        x=scan.nextDouble();
                        System.out.println("Enter second number : ");
                        y=scan.nextDouble();
                        result=x-y; //subtracting numbers
                        //printing name as well as result
                        System.out.println("Hey "+ name +", Subtraction of "+x+ " and "+y+" is "+result);
                }
                
                else if(choice==4)
                {
                        System.out.println("Enter first number : ");
                        x=scan.nextDouble();
                        System.out.println("Enter second number : ");
                        y=scan.nextDouble();
                        result=(x+y)/div; //using constant variable dev to find average
                        //printing name as well as average
                        System.out.println("Hey "+ name +", Average of "+x+ " and "+y+" is "+result);
                }
                
                else if(choice==5)
                {
                        boolean prime=true; //boolean variable
                        System.out.println("Enter the number : ");
                        x=scan.nextDouble();
                        for(int i=2;i<=x/div;i++) //loop to find if a number is prime or not
                        {
                                if(x%i==0) //if the number x is divisible by i
                                {
                                        prime=false; //set prime to false
                                        break; //break from loop as the number is not prime
                                }
                        }
                        
                        //nested if else loops
                        if(prime)  //if number is prime
                                System.out.println("Hey "+ name +", " + x +" is Prime Number");
                        else //if number is not prime
                                System.out.println("Hey "+ name +", " + x +" is Not a Prime Number");
                }
                
                scan.close(); //closing scan
        }
}

###################################################################

OUTPUT--->>>


Related Solutions

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...
jgrasp environment, java write a complete program that prompts the user to enter their first name,...
jgrasp environment, java write a complete program that prompts the user to enter their first name, middle name, and last name (separately). print out thier name and initials, exactly as shown: your name is: John Paul Chavez your initials are: J. P. C. use string method chartAt() to extract the first (zero-th) character from a name(the name is a string type): username.charAt(0). thank you.
jgrasp environment, java write a complete program that calculates a restaurant bill. Prompt the user for...
jgrasp environment, java write a complete program that calculates a restaurant bill. Prompt the user for the check amount, then ask the user what type of tipp they would like to leave: the choices are 1 for good tip (20%), 2 for an average tip (15%), or 3 for poor tip (10%). Use their input and an if-else to calculate the tip amount. Then calculate and output the final bill: check+tip print out the bill, exactly as shown (print the...
Write a java program that does the following: a) The user will enter data such as...
Write a java program that does the following: a) The user will enter data such as client name and client balance. The user can stop inputting data by entering "stop". The program should store the user inputs as a data member of an array. The type of this array is a class named ClientData. Below the output should be displayed by the program. The parts in bold are inputs from the user and not hard coded in the program Client...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
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...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name, that does the following: Create two arrays that will hold related information. You can choose any information to store, but here are some examples: an array that holds a person's name and an array that hold's their phone number an array that holds a pet's name and an array that holds what type of animal that pet is an array that holds a student's...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT