Question

In: Computer Science

/*Design and code a class Calculator that has the following * tow integer member variables, num1...

/*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

Solutions

Expert Solution

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();
   }
}


Related Solutions

write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
/*Design and code and test a class MaxMin that has the following properties * two integer...
/*Design and code and test a class MaxMin that has the following properties * two integer member variables, min and max where min is smaller or equal than max at all times * A default constructor that sets both min and max to 0 * A constructor that accepts one integer and sets both min and max to that integer * A constructor that accepts two integers and sets min the smallest and max the largest * * Setters and...
Write a code fragment in C language that tests the value of an integer num1. If...
Write a code fragment in C language that tests the value of an integer num1. If the value is 10, square num1. If it is 9, read a new value into num1. If it is 2 or 3, multiply num1 by 99 and print out the result. Implement your code using SWITCH statements
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
C++ PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName...
C++ PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData, which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool. It will...
Design a Ship class that has the following members: • A member variable for the name...
Design a Ship class that has the following members: • A member variable for the name of the ship (a string) • A member variable for the year that the ship was built (a string) • A constructor and appropriate accessors and mutators • A virtual print function that displays the ship’s name and the year it was built. Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the following members: • A...
Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT