In: Computer Science
Write a JAVA program in eclipse (write comment in every line) that asks the user to enter two numbers and an operator (+, -, *, / or %), obtain them from the user and prints their sum, product, difference, quotient or remainder depending on the operator. You need to use OOP techniques (objects, classes, methods….). This program must be place in a loop that runs 3 times.
code:
import java.util.Scanner;
//importing package
public class Main
//initialising the class named Main
{
int add(int num1, int num2)
//method to perform addition
{
return num1+num2;
// returning the answer of addition
}
int sub(int num1, int num2)
//method to perform subtration
{
return num1-num2;
// returning the answer of subtraction
}
int mul(int num1, int num2)
//method to perform multiplication
{
return num1*num2;
// returning the answer of multiplication
}
int div(int num1, int num2)
//method to perform division
{
return num1/num2;
// returning the answer of division
}
int modu(int num1, int num2)
//method to perform Modulus(reminder)
{
return num1%num2;
// returning the answer of modulus
}
public static void main(String[] args)
//main method here the execution starts
{
Scanner in = new Scanner(System.in);
// Input two numbers from user
System.out.println("Enter first number :");
int num1 = in.nextInt();
//first input
System.out.println("Enter second number :");
int num2 = in.nextInt();
//second input
Main obj = new Main();
//creating object for Main class
System.out.println("Addition: "+obj.add(num1,num2));
//This will call the add method
System.out.println("Substaction: "+obj.sub(num1,num2));
//This will call the sub method//This will call the mul
method
System.out.println("Multiplicat/ion: "+obj.mul(num1,num2));
//This will call the mul method
System.out.println("Division: "+obj.div(num1,num2));
//This will call the div method
System.out.println("Reminder(modulus):
"+obj.modu(num1,num2));
//This will call the modu method
}
//end of main method
}
//end of class Main
Screenshots:
Thank you.