Question:
We have the following code:
Make an edit to this code. What variable can we globalize to simplify the methods? You can also change the method definitions.
Note you don’t need to worry about how we came up with the roundUp() and roundDown() method definitions. Just know that they do the following:
● roundUp() : rounds up if decimal point is 5 or greater. For example, 10.5 will round to 11.
● roundDown() : rounds down regardless of the number. For example, 10.5 will round to 10.
Transcribed Image Text:
public class Rounding Calculator { } public static void main(String[] args) { double sum = add (10.5d, 9.0d, true); System.out.println("sum: " + sum); double subtraction = subtract (10.5d, 9.0d, true); System.out.println("subtraction: " + subtraction); double product = multiply(10.5d, 9.0d, true); System.out.println("product: + product); double division = divide (10.5, 9.0d, true); System.out.println("division: " + division); } public static double add(double x, double y, boolean roundup) { double sum = x + y; if (roundUp == true) { return roundUp(sum); } return roundDown (sum); } public static double subtract(double x, double y, boolean roundUp) { double subtraction = x - y; if (roundUp == true) { return roundUp(subtraction); } return roundDown (subtraction); } public static double multiply(double x, double y, boolean roundup) { double product = x = y; if (roundUp == true) { return roundUp(product); } public static double divide (double x, double y, boolean roundup) { double divide x / y; if (roundUp == true) { return roundup (divide); } return roundDown (divide); } public static double roundup (double number) { return Math.round(number); } public static double roundDown(double number) { return Math.floor(number);