Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need the coding source to have an input file and outputfile. this is my input.txt* A 99 33 - 78 18 + 44 33

i need the coding source to have an input file and outputfile.

this is my input.txt*

A 99 33 - 78 18 + 44 33 Q

i have this but the output comes out blank

import java.io.*; import java.util.Scanner;

public class hw6 { public static void main(String[] args) throws IOException { File myfile = new File("homework6input.txt"); Scanner inputFile = new Scanner(myfile); PrintWriter outputFile = new PrintWriter("homework6output.txt"); menu(inputFile, outputFile); }

public static void menu(Scanner inputFile, PrintWriter outputFile) { String choice = " "; outputFile.println("A - average of two numbers"); outputFile.println("X - maximum of two numbers"); outputFile.println("M - minimum of two numbers"); outputFile.println("S - square of a number"); outputFile.println("Q - quit the program"); outputFile.println("Enter any of the letters mentioned above or arithmetic operators(+ - / * %)"); while (!choice.equalsIgnoreCase("Q")) { choice = inputFile.next(); switch (choice) { case "+": add(inputFile, outputFile); break; case "-": subtract(inputFile, outputFile); break; case "*": multiply(inputFile, outputFile); break; case "/": divide(inputFile, outputFile); break; case "%": modulo(inputFile, outputFile); break; case "A": average(inputFile, outputFile); break; case "X": maximum(inputFile, outputFile); break; case "M": minimum(inputFile, outputFile); break; case "S": square(inputFile, outputFile); break; case "Q": break; default: outputFile.println("Invalid choice of operation"); } } }

public static void add(Scanner inputFile, PrintWriter outputFile) { int n1, n2, ans; n1 = inputFile.nextInt(); n2 = inputFile.nextInt(); ans = n1 + n2; // write to output file outputFile.println("Operation: Addition"); outputFile.println("Number1: " + n1); outputFile.println("Number2: " + n2); outputFile.println("Sum: " + ans + " "); }

public static void subtract(Scanner inputFile, PrintWriter outputFile) { int n1, n2, ans; n1 = inputFile.nextInt(); n2 = inputFile.nextInt(); ans = n1 - n2; // write to output file outputFile.println("Operation: Subtraction"); outputFile.println("Number1: " + n1); outputFile.println("Number2: " + n2); outputFile.println("Difference: " + ans + " "); }

public static void multiply(Scanner inputFile, PrintWriter outputFile) { int n1, n2, ans; n1 = inputFile.nextInt(); n2 = inputFile.nextInt(); ans = n1 * n2; // write to output file outputFile.println("Operation: Multiplcation"); outputFile.println("Number1: " + n1); outputFile.println("Number2: " + n2); outputFile.println("Product: " + ans + " "); }

public static void divide(Scanner inputFile, PrintWriter outputFile) { int n1, n2, ans; n1 = inputFile.nextInt(); n2 = inputFile.nextInt(); ans = n1 / n2; // write to output file outputFile.println("Operation: Divison"); outputFile.println("Number1: " + n1); outputFile.println("Number2: " + n2); outputFile.println("Quotient: " + ans + " "); }

public static void modulo(Scanner inputFile, PrintWriter outputFile) { int n1, n2, ans; n1 = inputFile.nextInt(); n2 = inputFile.nextInt(); ans = n1 % n2; // write to output file outputFile.println("Operation: Modulo"); outputFile.println("Number1: " + n1); outputFile.println("Number2: " + n2); outputFile.println("Remainder: " + ans + " "); }

public static void average(Scanner inputFile, PrintWriter outputFile) { int n1, n2; double ans; n1 = inputFile.nextInt(); n2 = inputFile.nextInt(); ans = (n1 + n2) / 2.0; // write to output file outputFile.println("Operation: Average"); outputFile.println("Number1: " + n1); outputFile.println("Number2: " + n2); outputFile.println("Average: " + ans + " "); }

public static void maximum(Scanner inputFile, PrintWriter outputFile) { int n1, n2, ans; n1 = inputFile.nextInt(); n2 = inputFile.nextInt(); if (n1 > n2) ans = n1; else ans = n2; // write to output file outputFile.println("Operation: Maximum"); outputFile.println("Number1: " + n1); outputFile.println("Number2: " + n2); outputFile.println("Maximum: " + ans + " "); }

public static void minimum(Scanner inputFile, PrintWriter outputFile) { int n1, n2, ans; n1 = inputFile.nextInt(); n2 = inputFile.nextInt(); if (n1

public static void square(Scanner inputFile, PrintWriter outputFile) { int n1, ans; n1 = inputFile.nextInt(); ans = n1 * n1; // write to output file outputFile.println("Operation: Square"); outputFile.println("Number: " + n1); outputFile.println("Square: " + ans + " "); } }

image text in transcribed

Homework Topic 6 - Calculator: Write a program which allows the user to perform simple tasks on a calculator A series of methods allows the user to select operation to perform and then enter operands. an The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, I, or % A X M S Q representing the usual arithmetic operators representing the average of two numbers representing the maximum of two numbers representing the minimum of two numbers representing the square of a number indicating the user wants to quit the program The program reads the user's response into a variable of type char. Using a switch statement, the program determines which method to call to process the user's request. For example, if the user enters +, a method is called which prompts the user to enter two integers. The method then finds the sum of the two integers and the method prints the results of the operation. If the user enters x, a method asks for two integers and finds the larger of the two. If the user enters s, a method asks for one value and finds the square of that value. If the user enters Q, the program stops. For each calculation performed, the method prints the operation requested, the user's original input, and the result. Note: All output must be sent to a file Note: The program must be properly tested Note: The program will iterate until the user quits the program Sample Output: Operation: addition augend: 25 addend: 35 sum: 60 Required Submission: 1. The Java source code file (e.g., HW6.java) 2. The test cases input file (e.g., testCases.txt) 3. The program generated output file (e.g., output.txt)

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_2

Step: 3

blur-text-image_3

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

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions