Java Program Write a Java program that prompts the user to enter two integers fr
ID: 3792009 • Letter: J
Question
Java Program
Write a Java program that prompts the user to enter two integers from the user. Then pass each value to methods: addition, subtraction, multiplication and division. Create each method to perform the task its name implies. Return the result of the calculations and display the amounts with a literal that tells the user exactly what they are looking at.
Ability to:
Create and call methods.
Able to return values from a method.
Able to perform calculations.
Use the print statement for output.
Explanation / Answer
import java.io.*;
import java.util.*;
public class calculator
{
//main function that calls different methods
public static void main(String[] args)
{
int num1,num2;
Scanner s=new Scanner(System.in);
System.out.println("Enter two integers");
num1=s.nextInt();
num2=s.nextInt();
//prints the addition of two numbers
System.out.println("Addition="+addNumbers(num1,num2));
//prints the subtraction of two numbers
System.out.println("Subtraction="+subNumbers(num1,num2));
//prints the multiplication of two numbers
System.out.println("Multiplication="+mulNumbers(num1,num2));
//prints the division of two numbers
System.out.println("Division="+divNumbers(num1,num2));
}
//Method to perform Addition and return the result
public static int addNumbers(int num1,int num2)
{
int result=num1+num2;
return result;
}
//Method to perform Subtraction and return the result
static int subNumbers(int num1,int num2)
{
int result=num1-num2;
return result;
}
//Method to perform Multiplication and return the result
static int mulNumbers(int num1, int num2)
{
int result=num1*num2;
return result;
}
//Method to perform Division and return the result
static double divNumbers(int num1, int num2)
{
double result=num1/num2;
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.