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

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...
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.
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...
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the class are: NUM_EMPLOYEES: a constant integer variable to hold the number of employees. employeeId. An array of integers to hold employee identification numbers. hours. An array of integers to hold the number of hours worked by each employee payRate. An array of doubles to hold each employee’s hourly pay rate wages. An array of seven doubles to hold each employee’s gross wages The class...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and complexNum of type double. Your class shall have following member functions Complex(); Complex(double, double); Complex add(Complex num1, Complex num2); Complex subtract(Complex num1, Complex num2); string printComplexNumber(); Write a driver program to test your code. Please make sure your code is separated into Complex.h Containing definition of the class Complex.cpp Containing the implementation ComplexTestProgram.cpp Containing main program Use the following directive in class definition to...
***Define a class called Pizza that has member variables to track the type of pizza (either...
***Define a class called Pizza that has member variables to track the type of pizza (either deep dish, hand tossed, or pan) along with the size (either small, medium, or large) and the number of pepperoni or cheese toppings. You can use constants to represent the type and size. Include mutator and accessor functions for your class. Create a void function, outputDescription( ) , that outputs a textual description of the pizza object. Also include a function, computePrice( ) ,...
5. In the following code snippet, which member function of the class Car is called first?...
5. In the following code snippet, which member function of the class Car is called first? class Car { public: void start(); void accelerate(double acc_speed); void stop(); double get_speed() const; Car(); private: double speed; }; Car::Car() { speed = 0; } void Car::start() { accelerate(get_speed() + 10); } void Car::stop() { speed = 0; } void Car::accelerate(double acc_speed) { speed = speed + acc_speed; } double Car::get_speed() const { return speed; } int main() { Car c1; c1.start(); c1.accelerate(10); c1.get_speed();...
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds...
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds a part's serial number. manufactDate: A member that holds the date the part was manufactured. lotNum: An integer that holds the part's lot number. The class should have appropriate setter and getter functions. Next, design a stack class that can hold objects of the class described above. If you wish, you may use the linked list from program 5 as a basis for designing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT