In: Computer Science
/*Design and code a class Calculator that has the following * tow integer member variables, num1 and num2. * - a method to display the sum of the two integers * - a method to display the product * - a method to display the difference * - a method to display the quotient * - a method to display the modulo (num1%num2) * - a method toString() to return num1 and num2 in a string * - Test your class in a separate class Tester.
#Java Programming
class Calculator
{
public int num1,num2;
public Calculator(int num1,int num2)
{
this.num1=num1;
this.num2=num2;
}
public void displaySum()
{
System.out.println("Sum of "+num1+" and "+num2+" =
"+(num1+num2));
}
public void displayProduct()
{
System.out.println("Product of "+num1+" and "+num2+" =
"+(num1*num2));
}
public void displayDifference()
{
System.out.println("Difference of "+num1+" and "+num2+" =
"+(num1-num2));
}
public void displayQuotient()
{
if(num2==0)
System.out.println("Denominator canot be zero");
else
System.out.println("Quotient of "+num1+" / "+num2+" =
"+(num1/num2));
}
public void displayModulo()
{
if(num2==0)
System.out.println("Denominator canot be zero");
else
System.out.println("Modulo of "+num1+" and "+num2+" =
"+(num1%num2));
}
public String toString()
{
return "Number1 = "+num1+" Number2 = "+num2;
}
}
public class Tester
{
public static void main(String[] args)
{
Calculator c1 = new
Calculator(5,2);
System.out.println(c1.toString());
c1.displaySum();
c1.displayProduct();
c1.displayDifference();
c1.displayQuotient();
c1.displayModulo();
Calculator c2 = new
Calculator(2,3);
System.out.println(c2.toString());
c2.displaySum();
c2.displayProduct();
c2.displayDifference();
c2.displayQuotient();
c2.displayModulo();
Calculator c3 = new
Calculator(5,0);
System.out.println(c3.toString());
c3.displaySum();
c3.displayProduct();
c3.displayDifference();
c3.displayQuotient();
c3.displayModulo();
}
}