I can\'t seem to not get errors when I try to create a program in Java that has
ID: 3757623 • Letter: I
Question
I can't seem to not get errors when I try to create a program in Java that has the following functions:
add: which takes 2 input numbers and computes their sum
subtract: which takes 2 input numbers and performs their subtraction
multiply: which takes 2 input numbers and computes their product
divide: which takes 2 input numbers and performs their division
add3: which takes 3 input numbers and computes their sum
multiply3: which takes 3 input numbers and computes their product
All numbers can be considered doubles.
In the main program perform THREE mathematical equations using the above functions to demonstrate their use.
For example: double example1 = subtract(100, sum(multiply(2,4), divide(10,2) ) );
So create three equations (i.e. 3 lines of code) like the above.
Explanation / Answer
import java.util.Scanner;
public class Arithmatic
{
public static void main(String[] args)
{
int m, n, p, opt, add, sub, mul, add3, mul3;
double div;
Scanner s = new Scanner(System.in);
System.out.print("Enter first number:");
m = s.nextInt();
System.out.print("Enter second number:");
n = s.nextInt();
System.out.print("Enter third number:");
p = s.nextInt();
while(true)
{
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 for add 3 numbers");
System.out.println("Enter 6 for multiply 3 numbers");
System.out.println("Enter 7 to Exit");
opt = s.nextInt();
switch(opt)
{
case 1:
add = m + n;
System.out.println("Result:"+add);
break;
case 2:
sub = m - n;
System.out.println("Result:"+sub);
break;
case 3:
mul = m * n;
System.out.println("Result:"+mul);
break;
case 4:
div = (double)m / n;
System.out.println("Result:"+div);
break;
case 5:
add3 = m + n + p;
System.out.println("Result:"+add3);
break;
case 6:
mul3 = m * n * p;
System.out.println("Result:"+mul3);
break;
case 7:
System.exit(0);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.