Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please modify the following program so that it does two things: 1) Modify so that it can be directed to an output file.

Can you please modify the following program so that it does two things:

1) Modify so that it can be directed to an output file. Note: PrintWriter object has already been created its called "outputFile". User prompts do not need to be directed to an output file.

Sample output:

Operation: addition

augend: 25

addend: 35

sum: 60

2) Call a separate Method for each operation. In the code below under the comment that reads //Case for arithmetic operations and their respective methods. I wrote a case for each operation. I have highlighted the cases in bold so that you know which ones to change to methods instead. I would like to change that and call a separate method for each operation.

THANK YOU!!

import java.util.Scanner; //Importing io package for file I/O. import java.io.*; //Declaring class. public class Calculator { //Throwing IOException to main method for File input and output. public static void main(String args[]) throws IOException { //Declaring and initializing variables needed for the program. boolean notDone = true; char choice; //Creating PrintWriter object for output. PrintWriter outputFile = new PrintWriter("CalculatorOutput.txt"); //Creating scanner object to read in data. Scanner sc = new Scanner(System.in); //Setting up do loop for user to have quitting option. do { // Calling for menu method that displays the menu. menu(); // Reading in the choice from the user. choice = sc.nextLine().charAt(0); // Switch case statements for method calls switch (choice) { case 'Q': case 'q': // Setting notDone so that loop can be exited. notDone = false; break; // Case for arithmetic operations and call for their respective methods. case '+': case '-': case '*': case '/': case '%': myOperation(choice); break; case 'a': case 'A': // Calling for average method myAvg(); break; case 'x': case 'X': // Calling for maximum method myMax(); break; case 'm': case 'M': // Call for minimum method myMin(); break; case 's': case 'S': mySqr(); break; } } // Running loop until notDone is true while (notDone == true); } /* Method menu(); * Input: * Read in by user. * Process: * Calls a method to display menu options. * Output: * Prints out the menu. * */ public static void menu() { System.out.println("Choose from Operations: "); System.out.println("+, -, *, /, or % -> for Arithmetic operations"); System.out.println("A -> for avergae of two numbers"); System.out.println("X -> for maximum of two numbers"); System.out.println("M -> for minimum of two numbers"); System.out.println("S -> for square of a number"); System.out.println("Q -> quit the program"); } /* Method myOperation(); * Input: * choice - The users response is turned into a variable of type char. * Process: * Using a switch statement, the program determines which method to call to process the user's request. * Output: * Based on the case entered by the user a calculation is performed and the result is printed. */ public static void myOperation(char choice) { // Defining the scanner. Scanner sc = new Scanner(System.in); //Taking two numbers as input. System.out.print("Enter two numbers First Number: "); int fNum = sc.nextInt(); System.out.print("Second Number: "); int sNum = sc.nextInt(); // Switch for the operations. switch (choice) { case '+': System.out.println("Operation: addition"); System.out.println("augend: " + fNum); System.out.println("addend: " + sNum); // Calculating the result int result = fNum + sNum; System.out.println("sum: " + result); break; case '-': System.out.println("Operation: subtration"); System.out.println("augend: " + fNum); System.out.println("addend: " + sNum); // Calculate the result result = fNum - sNum; System.out.println("difference: " + result); break; case '*': System.out.println("Operation: multiplication"); System.out.println("augend: " + fNum); System.out.println("addend: " + sNum); // Calculating the result. result = fNum + sNum; System.out.println("product: " + result); break; case '/': System.out.println("Operation: division"); System.out.println("augend: " + fNum); System.out.println("addend: " + sNum); // Calculating the result result = fNum + sNum; System.out.println("quotient: " + result); break; case '%': System.out.println("Operation: remainder"); System.out.println("augend: " + fNum); System.out.println("addend: " + sNum); // Calculating the result result = fNum + sNum; System.out.println("remainder: " + result); break; } } /* Method myAvg(); * Input: * Read in by user. * Process: * Calculates the average based on the numbers read in by user. * Output : * Displays the average of the values that were read in. * */ public static void myAvg() { // Defining the scanner. Scanner sc = new Scanner(System.in); // take two numbers as input. System.out.print("Enter two numbers First Number: "); int fNum = sc.nextInt(); System.out.print("Second Number: "); int sNum = sc.nextInt(); // Calculating the average. double avg = (fNum + sNum) / 2; // Displaying the result. System.out.println("Average of two numbers is: " + avg); } /* Method myMax(); * Input: * Read in by user. * Process: * Finds the maximum value by using an if statement and a relational operator to compare two values. * Output: * Displays Maximum Value. * */ public static void myMax() { // Defining the scanner. Scanner sc = new Scanner(System.in); // Taking two numbers as input. System.out.print("Enter two numbers First Number: "); int fNum = sc.nextInt(); System.out.print("Second Number: "); int sNum = sc.nextInt(); // Find the maximum int max = fNum; if (max < sNum) max = sNum; // Displaying the result. System.out.println("Maximum of two numbers is: " + max); } /* Method myMin(); * Input: * Read in by user. * Process: * Finds the minimum value by using an if statement to compare two values. * Output: * Displays the minimum value. * */ public static void myMin() { // Defining the scanner. Scanner sc = new Scanner(System.in); // Taking two numbers as input. System.out.print("Enter two numbers First Number: "); int fNum = sc.nextInt(); System.out.print("Second Number: "); int sNum = sc.nextInt(); // Finding the maximum. int min = fNum; if (min > sNum) min = sNum; // Displaying the result. System.out.println("Minimum of two numbers is: " + min); } /* Method mySqr(); * Input: * Read in by user * Process: * Finds the square of a number through an arithmetic expression. * Output: * Displays the square root of the value. * */ public static void mySqr() { // Defining the scanner. Scanner sc = new Scanner(System.in); // Taking a input from user. System.out.print("Enter a number: "); int num = sc.nextInt(); // Calculating the square int result = num * num; // Displaying the result. System.out.println("Square of the number is: " + result); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions