Write another program called calculator that inherits from class average and als
ID: 3720572 • Letter: W
Question
Write another program called calculator that inherits from class average and also has methods that has: two methods multiply() to calculate the product of the numbers. One method will have two parameters which are both ints. The second method has three parameters: two ints and a Double. Another method “Power” that finds the power of a number using methods. For example, if the user types in: 5 and 3, the program should print out 125. 3. And another method “Factorial” which finds the factorial of a number using methods. Create a tester class to test all methods. Ask for user input.
Explanation / Answer
Average.java
public class Average{
public int multiply(int a, int b) {
return a * b;
}
public double multiply(int a, int b, double c) {
return a * b * c;
}
public double power(int a, int b) {
int result = 1;
for(int i = 0; i < b; i++)
result = multiply(result, a);
return result;
}
public int Factorial(int a) {
int result = 1;
for(int i = 1; i <= a; i++)
result = multiply(i, result);
return result;
}
}
Calculator.java
import java.util.Scanner;
public class Calculator extends Average{
public static void main(String[] args) {
Average avg = new Average();
Scanner in = new Scanner(System.in);
System.out.println("Enter the first value: ");
int a = in.nextInt();
System.out.println("Enter the second value: ");
int b = in.nextInt();
System.out.println("Power = " + avg.power(a, b));
System.out.println("Factorial = " + avg.Factorial(a));
}
}
**Comment for any further queries.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.