Methods - Exercise 1
Implement a class Calculator with the method mentioned below.
Method Description
findAverage()
Calculate the average of three numbers
Return the average rounded off to two decimal digits
Test the functionalities using the provided Tester class.
Sample Input and Output
Hint: For round-off to two decimal digits:
double num1 = 65, num2 = 175;
double num3 = num1/num2;
System.out.println(Math.round(num3*100.0)/100.0);
class Calculator{
public double findAverage(int number1,int number2,int number3){
double sum=(number1+number2+number3);
double average=sum/3;
double roundoff=Math.round(average*100.0)/100.0;
return roundoff;
}
}
class Tester{
public static void main(String args[]){
Calculator calculator= new Calculator();
double x=calculator.findAverage(12,8,15);
System.out.println(x);
//InvokethemethodfindAverageoftheCalculatorclassanddisplaytheaverage}
}
}
No comments:
Post a Comment