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

I need help to write this program! Writ e the program for a simple Calculator in

ID: 3661843 • Letter: I

Question

I need help to write this program!

Write the program for a simple Calculator in Java that will run correctly. The program should prompt the user to enter a number from 1 to 4 for the arithmetic operation to perform, then request two numbers, and finally display the numbers entered as well as the result on screen. You may write the entire program procedurally within a single main method, or take a more object-oriented approach. (Future exercises and assignments will require an object-oriented approach.) Note: Output from the program should have numbers formatted with 2 decimal places.

Example of using the program, bolded digits are user input

Please enter an option:

1 Addition

2 Subtraction

3 Multiplication

4 Division

4

Please enter number 1:1

Please enter number 2:2

1.00 / 2.00 is 0.50

Explanation / Answer

Good Wishes,

Program in java

Calculator_main class:

import java.io.IOException;
import java.util.Scanner;

public class Calculator_main {
public static void main(String args[])throws IOException
{
float[] a=new float[2];
int c;
Calculator c1=new Calculator();
Scanner s=new Scanner(System.in);
System.out.println("Please enter an option 1 Addition 2 Subtraction 3 Multiplication 4 Division ");
c=s.nextInt();
for(int i=0;i<2;i++)
{
System.out.println("Please enter number");
a[i]=s.nextFloat();
}
switch(c)
{
case 1:c1.add(a[0],a[1]);
break;
case 2:c1.sub(a[0],a[1]);
break;
case 3:c1.mul(a[0],a[1]);
break;
case 4:c1.div(a[0],a[1]);
break;
default:System.out.println("Sorry wrong option entered");
}
}
}

Calculator class:

import java.text.DecimalFormat;

public class Calculator
{
public void add(float a,float b)
{
float c;
DecimalFormat df = new DecimalFormat("#.00");
c= (a+b);
System.out.println(df.format(a)+"+"+df.format(b)+" is "+df.format(c));
}
public void sub(float a,float b)
{
float c;
DecimalFormat df = new DecimalFormat("#.00");
c= (a-b);
System.out.println(df.format(a)+"-"+df.format(b)+" is "+df.format(c));
}
public void mul(float a,float b)
{
float c;
DecimalFormat df = new DecimalFormat("#.00");
c= (a*b);
System.out.println(df.format(a)+"*"+df.format(b)+" is "+df.format(c));
}
public void div(float a,float b)
{
float c;
DecimalFormat df = new DecimalFormat("#.00");
c= (a/b);
System.out.println(df.format(a)+"/"+df.format(b)+" is "+df.format(c));
}
  
  
}

Explaination:

Here I wrote two classes namely Calculator_main class which contains the main() method and the other class is Calculator class.

I took a more object oriented approach.

In main class i.e Calculator_main class

First I created an object to Calculator class which is c1

I accepted user choice of which operation to be done in

1 Addition

2 Subtraction

3 Multiplication

4 Division

and stored the choice in c.

Then I accepted two numbers from user and stored them in array 'a'

Then I used switch case to check the with the choice and call the appropriate method in Calculator class using its object c1

Then the control goes to Calculator class and invokes the appropriate method and does the operation and prints the result as required.

In Calculator class we 4 method namely

add()- which takes two float values as input and does addition on them and prints the result in the required format.

sub()- which takes two float values as input and does subtraction on them and prints the result in the required format.

mul()- which takes two float values as input and does multiplication on them and prints the result in the required format.

div()- which takes two float values as input and does division on them and prints the result in the required format.

Hope this is clear.

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