Practice code by myself
This is a Java project that implements a Calculator class with a findAverage method.
The findAverage method calculates the average of three numbers and returns the result rounded to two decimal digits.
To use the Calculator class, follow these steps:
- Create an instance of the
Calculatorclass. - Call the
findAveragemethod with three numbers as arguments. - The method will return the average rounded to two decimal digits.
doublenum1 = 65;doublenum2 = 175;doublenum3 = num1 / num2;- System.out.println(Math.round(num3 * 100.0) / 100.0);
import java.text.DecimalFormat;
class Calculator {
DecimalFormat df = new DecimalFormat("0.00");
public double findAverage(int number1, int number2, int number3) {
double avg = (double) (number1 + number2 + number3) / 3;
return Double.parseDouble(df.format(avg));
}
}
class Tester {
public static void main(String args[]) {
Calculator calculator = new Calculator();
double res = calculator.findAverage(12, 8, 15);
System.out.println(res);
}
}