In: Computer Science
Write a short, cohesive java program in jGrasp that interacts with the user and does the following:
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.
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--->>>