+ 1
this is my first attempt in creating a java program with a purpose. it is a primitive calculator. I need your guidance to improv
2 Answers
+ 25
//here are some improvements âș
//u can use 1 object of scanner class , to take multiple inputs
import java.util.Scanner;
public class Calc
{
public static void main(String[] args) {
Addition a = new Addition();
Subtraction s = new Subtraction();
Multiplication multi = new Multiplication();
Division d = new Division();
Scanner m = new Scanner(System.in);
double x,y,z=0;
char c;
while(true)
{
System.out.println("Enter 2 numbers:");
x = m.nextDouble();
y = m.nextDouble();
System.out.println("Enter operator:");
c = m.next().charAt(0);
if(c == '+')
z = a.add(x,y);
else if(c == '-')
z = s.sub(x,y);
else if(c == '*'||c=='Ă')
z = multi.mul(x,y);
else if(c == '/'||c=='Ă·')
z = d.div(x,y);
else{
System.out.println("Your choice is not is available");
System.exit(0);}
System.out.println(x+" "+c+" "+y+" = "+ z);
}
}
}
public class Addition
{
double add(double x,double y)
{
return x+y;
}
}
public class Subtraction
{
double sub(double x,double y)
{
return x-y;
}
}
public class Multiplication
{
double mul(double x,double y)
{
return x*y;
}
}
public class Division
{
double div(double x,double y)
{
return x/y;
}
}