2) You are creating a new way to do math that uses estimates instead of actual v
ID: 3602252 • Letter: 2
Question
2) You are creating a new way to do math that uses estimates instead of actual values. You are going to create a class called EstimateMath, and inside this class will be only two static methods estimateAdd and estimateSubtract. Both methods will take in two integers as parameters. Each number will be rounded to the nearest 10 and then the arithmetic will be done. Such as this method call: EstimateMath.estimateAdd(14,21) would return 30. Write a main method in a separate class file that tests each of these methods by asking the user to input 2 integers and gives the results. Repeat this in a loop until zeros are input.
Explanation / Answer
EstimateMath.java file
public class EstimateMath{
public static int estimateAdd(int a,int b){
//add two numbers
int c = a+b;
// if last digit is less than six, substract last digit
if((c%10)<6){
return c - (c%10);
}
else{
//add the remaining value to make the nearest 10.
return c + (10-(c%10));
}
}
public static int estimateSubstract(int a,int b){
int c = a-b;
// if last digit is less than six, substract last digit
if((c%10)<6){
return c - (c%10);
}
else{
//add the remaining value to make the nearest 10.
return c + (10-(c%10));
}
}
}
Main.java file
public class Main{
public static void main(String args[]){
System.out.println(EstimateMath.estimateAdd(14,20));
System.out.println(EstimateMath.estimateAdd(14,22));
System.out.println(EstimateMath.estimateSubstract(30,7));
System.out.println(EstimateMath.estimateSubstract(30,4));
}
}
/*
sample output
30
40
20
30
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.