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

Write a program that simulates a simple calculator. The calculator has the abili

ID: 3633344 • Letter: W

Question

Write a program that simulates a simple calculator. The calculator has the ability to have two real numbers inputted and then:

-multiply the two numbers
-divide the first number by the second number (be sure to check for division by 0 and provide an error message if this occurs)
- add the two numbers together
- subtract the second number from the first number

Example: User inputs 10 and 5, Output would be:

Multiplied = 50, Divided = 2, Added = 15, Subtracted = 5

Create a method for each of the four tasks. The main method should ask for the numbers and display the results of each of the four tasks. Have the program repeat until the user tells the program to stop.

**As well: write a personal Class called Calc.java so that i can make my own calculator objects! this class would contain 4 behaviors for each of the methods, and two data fields to hold the users input, it could also have more behaviors for input and output and a menu, but that's all up to you.

P.s. I am using java 1.4.2 and I cannot use any newer versions. I cannot use classes like scanner and such. Please ensure that the program works with java 1.4.2. Thank you responders

Explanation / Answer

//Calc.java

/**
* A simple calculator class
*/
public class Calc
{
    private double _num1;
    private double _num2;
   
    //Constructor
    public Calc()
    {
        set_num1(0);//Initializing number 1 to 0
        set_num2(0);//Initializing number 2 to 0
    }    

    /**
    * sets the number 1
    */
    public void set_num1(double _num1)
    {
        this._num1 = _num1;
    }

    /**
    * sets the number 2
    */
    public void set_num2(double _num2)
    {
        this._num2 = _num2;
    }
   
    //Multiplication function
    public double Multiply()
    {
        return _num1*_num2;
    }
   
    //Addition function
    public double Add()
    {
        return _num1+_num2;
    }
   
    //Subtraction function
    public double Subtract()
    {
        return _num1-_num2;
    }
   
    //Division function
    public double Divide()
    {
        //if denominator is zero, then it throws an error
        if(_num2 == 0)
        {
            System.out.println("Error : Division by Zero error");
            return -999;
        }
        return _num1/_num2;
    }
   
}

//TestCalc.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class TestCalc
{
    public static void main(String[] args) throws NumberFormatException, IOException
    {
        Calc calc = new Calc();
        InputStreamReader inp = new InputStreamReader(System.in);
        BufferedReader input=new BufferedReader(inp);
        while(true)
        {
            System.out.println("Enter Number 1 : ");
            double num1 = Double.parseDouble(input.readLine());
           
            System.out.println("Enter Number 2 : ");
            double num2 = Double.parseDouble(input.readLine());
            calc.set_num1(num1);
            calc.set_num2(num2);
            String output = "Multiplied = "+calc.Multiply()+
                          ", Divided = 2"+calc.Divide()+
                          ", Added = "+calc.Add()+", Subtracted = "+calc.Subtract();
            System.out.println(output);
           
            System.out.println("Do you want to perform another operation?(Y/N) ");
            String str = input.readLine();
            if(!str.toLowerCase().equals("y"))
            {
                break;
            }
        }
    }

}

Follow the same steps you followed for Cake program, for this also.

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