/* ComplexNumTester.java ** ** Author: R. McCloskey ** Date: Feb. 2022 ** Adapted from Fraction2Tester ** ** Java application that is for the purpose of testing the ComplexNumber class. ** An input line can have any of eight forms, as exemplified by the following: ** ** Form 1: Q (quit: terminates execution) ** Form 2: H (help: displays examples of commands) ** Form 3: C (displays the "current" complex number) ** Form 4: 3.2 8.7 (sets the current complex number to 3.2 + 8.7i) ** Form 5: + 5 9 (adds 5 + 9i to the current complex number) ** Form 6: - 13 4 (subtracts 13 + 4i from the current complex number) ** Form 7: * 2 -7 (multiplies the current complex number by 2 - 7i) ** Form 8: / 1 3 (divides the current complex number by 1 + 3i) ** ** That is, an input of 'Q' (or 'q') causes the program to terminate, ** 'C' (or 'c') causes the current complex number to be displayed, ** 'H' (or 'h') causes example commands to be displayed, ** a pair (a,b) of numbers results in the "current" complex number being ** set to a + bi. ** a pair (a,b) of numbers preceded by an operator (any of +, -, *, or /) ** results in the current complex number being modified in the specified ** way (using a mutator method) and its updated value displayed. */ import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class ComplexNumTester { private static ComplexNumber x; // the "current" complex number public static void main(String[] args) throws FileNotFoundException { String QUIT = "Q"; String HELP = "H"; String CURRENT = "C"; 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 Tester Program."); // initialize current complex number to 0 + 0i x = new ComplexNumber(); 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 if (line.toUpperCase().equals(CURRENT)) { println(x.toString()); } else { // carry out the command try { doCommand(line); } catch (Exception e) { e.printStackTrace(System.out); } } } println("Goodbye."); } /* Carries out the specified command. */ private static void doCommand(String command) { String[] tokens = command.split("\\s+"); if (tokens.length == 1 || tokens.length > 3) { println("Ignoring unknown command"); } else if (tokens.length == 2) { // Command should be a pair (a,b) of numbers. // Set x to a + bi. double a = Double.parseDouble(tokens[0]); double b = Double.parseDouble(tokens[1]); x = new ComplexNumber(a, b); println(x.toString()); } else { // tokens.length == 3 // tokens[0] should be an operator char operator = tokens[0].charAt(0); // tokens[1] and tokens[2] should be a pair (a,b) of numbers // describing complex number a + bi double a = Double.parseDouble(tokens[1]); double b = Double.parseDouble(tokens[2]); ComplexNumber y = new ComplexNumber(a,b); // Set x's new value to x op y by calling appropriate mutator method applyMutator(operator, x, y); println(x.toString()); } } // 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 to one given complex number the mutator method indicated by ** the given operator, passing the other given complex number as the ** argument to that method. */ private static void applyMutator(char operator, ComplexNumber left, ComplexNumber right) { if (operator == ADD_OP) { left.addTo(right); } else if (operator == SUBTRACT_OP) { left.subtractFrom(right); } else if (operator == MULTIPLY_OP) { left.multiplyBy(right); } else if (operator == DIVIDE_OP) { left.divideBy(right); } else { throw new IllegalArgumentException(operator + " is invalid 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("C (print current complex number)"); println("4.5 -2.8 (set current complex number to 4.5 - 2.8i)"); println("+ 1 5 (add 1 + 5i to current complex number)"); println("- 7 4 (subtract 7 + 4i from current complex number)"); println("* 2 9 (multiply current complex number by 2 + 9i)"); println("/ 11 -4 (divide current complex number by 11 - 4i)"); } /* Surrogate for System.out.println(). */ private static void println(String s) { System.out.println(s); } }