/* ComplexNumWithGenTester.java ** ** Author: R. McCloskey ** Date: Feb. 2022 ** Adapted from ComplexNumTester ** ** Java application that is for the purpose of testing the ** ComplexNumberWithGen class. ** An input line can have any of six forms, as exemplified by the following: ** ** Form 1: Q (quit: terminates execution) ** Form 2: H (help: displays examples of commands) ** Form 3: 3 5 + 6 2 (computes (3+5i) + (6+2i)) ** Form 4: 3 5 - 6 2 (computes (3+5i) - (6+2i)) ** Form 5: 3 5 * 6 2 (computes (3+5i) * (6+2i)) ** Form 6: 3 5 / 6 2 (computes (3+5i) / (6+2i)) ** ** That is, an input of 'Q' (or 'q') causes the program to terminate, ** 'H' (or 'h') causes example commands to be displayed, ** an expression of the form 'a b $ c d', in which each of a, b, c, and ** d is a number and $ is one of +, -, *, or /, results in the specified ** arithmetic operation being applied to (a+bi) and (c+di) and the result ** displayed. */ import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class ComplexNumWithGenTester { public static void main(String[] args) throws FileNotFoundException { String QUIT = "Q"; String HELP = "H"; Scanner input; // for reading input data boolean echo; // true iff inputs are to be printed if (args.length == 0) { // no command line arg, so input is from keyboard echo = false; input = new Scanner(System.in); } else { // args[0] is name of input file echo = true; input = new Scanner(new File(args[0])); } println("Welcome to the Complex Number (with generators) Tester Program."); boolean keepGoing = true; while (keepGoing) { System.out.print("\n> "); String line = input.nextLine().trim(); if (echo) { println(line); } if (line.length() == 0) { // ignore a blank line } else if (line.toUpperCase().equals(QUIT)) { keepGoing = false; } else if (line.toUpperCase().equals(HELP)) { printHelp(); } else { // carry out the command, which should be of the form 'a b $ c d' try { doCommand(line); } catch (Exception e) { e.printStackTrace(System.out); } } } println("Goodbye."); } /* Carries out the specified command, which is expected to be of the form ** 'a b $ c d', where a, b, c, and d are real numbers and $ is an arithmetic ** operator (i.e., one of +, -, *, or /). */ private static void doCommand(String command) { String[] tokens = command.split("\\s+"); if (tokens.length != 5) { println("Ignoring unknown command"); } else { double a = Double.parseDouble(tokens[0]); double b = Double.parseDouble(tokens[1]); char operator = tokens[2].charAt(0); double c = Double.parseDouble(tokens[3]); double d = Double.parseDouble(tokens[4]); ComplexNumberWithGen x = new ComplexNumberWithGen(a, b); ComplexNumberWithGen y = new ComplexNumberWithGen(c, d); ComplexNumberWithGen z = applyGenerator(x, operator, y); System.out.println(z); } } // Symbolic constants used in the following methods. // ------------------------------------------------- private static final char ADD_OP = '+'; private static final char SUBTRACT_OP = '-'; private static final char MULTIPLY_OP = '*'; private static final char DIVIDE_OP = '/'; /* Applies the generator method corresponding to the given arithmetic ** operator to the two given complex numbers, returning the result. */ private static ComplexNumberWithGen applyGenerator(ComplexNumberWithGen x, char operator, ComplexNumberWithGen y) { if (operator == ADD_OP) { return x.plus(y); } else if (operator == SUBTRACT_OP) { return x.minus(y); } else if (operator == MULTIPLY_OP) { return x.times(y); } else if (operator == DIVIDE_OP) { return x.dividedBy(y); } else { throw new RuntimeException(operator + " is not a valid operator"); } } /* Prints an example of each kind of command. */ private static void printHelp() { println("Examples of commands:"); println("Q (quit)"); println("H (print this help info)"); println("2 6 + 3 5 (computes (2+6i) + (3+5i))"); println("2 6 - 3 5 (computes (2+6i) - (3+5i))"); println("2 6 * 3 5 (computes (2+6i) * (3+5i))"); println("2 6 / 3 5 (computes (2+6i) / (3+5i))"); } /* Surrogate for System.out.println(). */ private static void println(String s) { System.out.println(s); } }