Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class Calculator. Calculator has the following: Two private instance va

ID: 2249095 • Letter: C

Question

Create a class Calculator. Calculator has the following:

Two private instance variables num1 and num2, both of type int.

A constructor that takes two parameters.

An instance method add, which adds num1 and num2 and returns the result.

Create a class CalculatorTester with a main() method. main() method does the following:

Create an object of class Calculator using any two values of your choice.

Call the instance method add() of the Calculator class and display the results of the addition.

(Add appropriate comments (class, methods and variables). Poinst are allocated for comments)

Explanation / Answer

import java.io.*;
import java.util.*;

class Calculator
{
       int num1, num2;
       Calculator(int x, int y) // Constructor
       {
          num1 = x;
          num2 = y;
       }

       int add ()             // creating a method
       {
          int Sum;
          Sum = num1 + num2;
          return Sum;        // returning the sum
       }
}

public CalculatorTester
{
public static void main (String args[])
{
     Calculator object = new Calculator(15,16);    // Creating an object of Calculator class
     System.out.println("Sum    is   ==> "+object.add()); // Print the result
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote