Create a program in Java that has the following functions: add: which takes 2 in
ID: 675051 • Letter: C
Question
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 all mathematical equations using the above functions to demonstrate their use. For example: double example1 = subtract(100, sum(multiply(2,4), divide(10,2) ) );
Explanation / Answer
Source code is below :
/*
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 all mathematical equations using the above functions to demonstrate their use.
For example: double example1 = subtract(100, sum(multiply(2,4), divide(10,2) ) );
*/
class Test1
{
public double add(double arg1, double arg2)
{
return (arg1+arg2);
}
public double subtract(double arg1, double arg2)
{
return (arg1-arg2);
}
public double multiply(double arg1, double arg2)
{
return (arg1*arg2);
}
public double divide(double arg1, double arg2)
{
return (arg1/arg2);
}
public double add3(double arg1, double arg2, double arg3)
{
return (arg1+arg2+arg3);
}
public double subtract3(double arg1, double arg2, double arg3)
{
return (arg1-arg2-arg3);
}
public double multiply3(double arg1, double arg2, double arg3)
{
return (arg1*arg2*arg3);
}
}
public class Test
{
public static void main(String[] args)
{
Test1 t1 =new Test1();
double example1 = t1.subtract(100, t1.add(t1.multiply(2,4), t1.divide(10,2) ) );
System.out.println("Result is: "+example1);
}
}
Output is :
Result is : 70
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.