Topic: VOID/NON-VOID METHODS Trying to make this program run like the SAMPLE RUN
ID: 3915986 • Letter: T
Question
Topic: VOID/NON-VOID METHODS
Trying to make this program run like the SAMPLE RUN BELOW
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 sure to write the problem using a void method called sumDifferenceProblem ). The actual calculation of the sum must be done by a non-void method called calculateSum ) and the actual calculation of the difference must be done by another non-void method call calculateDifference Keep the problem repeating using the prompt to continue method. SAMPLE RUN Enter Number1: 5.5 Enter Number2: 3.2 Sum is: 8.7 Difference 2.3 Continue (y/n) ?n Thank vou for using this program...Explanation / Answer
import java.util.Scanner; public class SumAndDifference { static void sumDifferenceProblem() { Scanner in = new Scanner(System.in); double n1, n2; char choice; while (true) { System.out.print("Enter Number1: "); n1 = in.nextDouble(); System.out.print("Enter Number2: "); n2 = in.nextDouble(); System.out.println("Sum is: " + calculateSum(n1, n2)); System.out.println("Difference is: " + calculateDifference(n1, n2)); System.out.print(" Continue(y/n)?"); choice = in.next().charAt(0); System.out.println(); if(choice == 'n') { break; } } System.out.println("Thank you for using this program..."); } static double calculateSum(double n1, double n2) { return n1 + n2; } static double calculateDifference(double n1, double n2) { return n1 - n2; } public static void main(String[] args) { sumDifferenceProblem(); } }