SUMDIFF PROBLEM 2. Write a program that repeatedly prompts the user for two numb
ID: 3916000 • Letter: S
Question
SUMDIFF PROBLEM 2. Write a program that repeatedly prompts the user for two numbers and for output calculates and displays their sum and difference. Make the problem using a void method called sumDifferenceProblem). The actual calculation of the sum mustbe done by a non-void method called calculateSum ) and the actual calculatjon of the difference must be done by another non-void method call calculateDifference () Keep the problem repeating using the prompt to continue method sure to write SAMPLE RUN Enter Number1: 5.5 Enter Number2: 3.2 Sum is: 8.7 Difference 2.3 Continue (y/n)?nExplanation / Answer
import java.util.Scanner;
class Main {
public static void sumDifferenceProblem(){
char choice = 'y';
while (choice == 'y')
{
// declaring variables
double a, b;
Scanner sc = new Scanner(System.in);
// taking user input of 2 numbers
System.out.print("Enter Number1: ");
a = sc.nextDouble();
System.out.print("Enter Number2: ");
b = sc.nextDouble();
// calling methods and printing output
System.out.print("Sum is: "+calculateSum(a, b));
System.out.print(" Difference is: "+calculateDifference(a, b));
// asking for choice to continue
System.out.print(" Continue(y/n): ");
choice = sc.next().charAt(0);
}
}
// this returns sum
public static double calculateSum(double one, double two)
{
return one+two;
}
// THIS returns difference
public static double calculateDifference(double one, double two)
{
return one-two;
}
public static void main(String[] args) {
sumDifferenceProblem();
}
}
/*SAMPLE OUTPUT
Enter Number1: 5.5
Enter Number2: 3.2
Sum is: 8.7
Difference is: 2.3
Continue(y/n): y
Enter Number1: 1.2
Enter Number2: 3.4
Sum is: 4.6
Difference is: -2.2
Continue(y/n): n
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.