Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called Calculator with the following methods sumRange takes 2 inte

ID: 3738769 • Letter: W

Question

Write a class called Calculator with the following methods sumRange takes 2 integers, a and b. Input a cannot be negative and must be less than b. The sum of all the integers from a to b inclusively, is returned. If the input fails to meet the specified criteria return 0 findGreatest takes 3 doubles and returns the double with the greatest value factorial takes an integer and returns a long that represents the factorial of the integer. 5! (say "five factorial") 1*2*3*4*5 120 An input of 0 or less should return 0 Write a testing class called CalculatorTester with the following static method selectionMenu takes a Scanner as input and prints the menu shown below. The methods scans the user input and returns what was scanned Enter your selection: 1: Factorial 2: Sum Range 3: FindGreatest 4: Quit Write a main method that does the following . Creates a Scanner and a Calculator .Calls the selectionMenu method to capture user selection Prompt the user for input depending on selection Uses if statements or a switch call the appropriate Calculator method and print the result If the user inputs an invalid selection, print ERROR Uses a loop to repeat this until the user selects quit Example output for input of 1,4 Enter your selection: 1: Factoria1 2: Sum Range 3: Find Greatest 4: Quit Enter an integer 24

Explanation / Answer

CalculatorTester.java

import java.util.Scanner;

public class CalculatorTester {

static Scanner scan = new Scanner(System.in);

public static void main(String[] args) {

int n = selectionMenu();

Calculator c = new Calculator();

while(n!=4) {

if(n==1) {

System.out.println("Enter the number:");

int a = scan.nextInt();

System.out.println("Factorial: "+c.factorial(a));

} else if(n==2){

System.out.println("Enter the first number: ");

int a = scan.nextInt();

System.out.println("Enter the second number: ");

int b = scan.nextInt();

System.out.println("Sum: "+c.sumRange(a, b));

} else if(n==3){

System.out.println("Enter the first number: ");

double a = scan.nextDouble();

System.out.println("Enter the second number: ");

double b = scan.nextDouble();

System.out.println("Enter the third number: ");

double d = scan.nextDouble();

System.out.println("Max: "+c.findGreatest(a, b, d));

} else {

System.out.println("ERROR. Invalid Input");

}

n = selectionMenu();

}

}

public static int selectionMenu(){

System.out.println("Enter your selection: 1. Factorial 2. Sum Range 3. Find Greatest 4. Quit ");

return scan.nextInt();

}

}

Calculator.java

public class Calculator {

public int sumRange(int a, int b) {

int sum = 0;

for(int i=a;i<=b;i++){

sum+=i;

}

return sum;

}

public double findGreatest(double a, double b, double c) {

if(a>=b && a>=c) {

return a;

} else if(b>=c){

return b;

} else {

return c;

}

}

public long factorial(int n) {

long p =1;

for(int i=2;i<=n;i++){

p*=i;

}

return p;

}

}

Output:

Enter your selection:
1. Factorial
2. Sum Range
3. Find Greatest
4. Quit

1
Enter the number:
5
Factorial: 120
Enter your selection:
1. Factorial
2. Sum Range
3. Find Greatest
4. Quit

2
Enter the first number:
2
Enter the second number:
10
Sum: 54
Enter your selection:
1. Factorial
2. Sum Range
3. Find Greatest
4. Quit

3
Enter the first number:
4
Enter the second number:
5
Enter the third number:
3
Max: 5.0
Enter your selection:
1. Factorial
2. Sum Range
3. Find Greatest
4. Quit

4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote