SHOW:
|
|
- or go back to the newest paste.
| 1 | /************************************************************************************************** | |
| 2 | - | * Program Name: Programming Project 4.6: Arithmetic Table |
| 2 | + | * Program Name: Programming Project 4.6: Multiplication Table |
| 3 | * Author: Terry Weiss | |
| 4 | * Date Written: September 25, 2015 | |
| 5 | * Course/Section: CSC 111-003W | |
| 6 | * Program Description: | |
| 7 | * Design and implement an application that produces an arithmetic table, showing the | |
| 8 | * results of a user-specified operation on the integers 1 through a user-defined maximum. | |
| 9 | * | |
| 10 | * Edit: Removed requiring a space in `operator` in `makeTable()` and formatted it in instead. | |
| 11 | **************************************************************************************************/ | |
| 12 | ||
| 13 | import java.util.Scanner; | |
| 14 | - | public class ArithmeticTable |
| 14 | + | |
| 15 | ||
| 16 | public class ArithmeticTable2 | |
| 17 | {
| |
| 18 | public static String makeTable( String operator, int highestFactor ) | |
| 19 | {
| |
| 20 | int column, row, value; | |
| 21 | String table = ""; | |
| 22 | - | table += operator + "\t"; |
| 22 | + | |
| 23 | // Print the header row | |
| 24 | table += String.format(" %s\t", operator);
| |
| 25 | for (column = 1; column <= highestFactor; column++) | |
| 26 | {
| |
| 27 | table += String.format("%3d\t", column);
| |
| 28 | } | |
| 29 | table += "\n"; | |
| 30 | ||
| 31 | // Print table | |
| 32 | for (row = 1; row <= highestFactor; row++ ) | |
| 33 | {
| |
| 34 | table += String.format("%2d|\t", row); // Display row number
| |
| 35 | ||
| 36 | - | if (operator.equals(" x"))
|
| 36 | + | |
| 37 | {
| |
| 38 | if (operator.equals("x"))
| |
| 39 | {
| |
| 40 | - | else if (operator.equals(" +"))
|
| 40 | + | |
| 41 | } | |
| 42 | else if (operator.equals("+"))
| |
| 43 | {
| |
| 44 | - | else if (operator.equals(" -"))
|
| 44 | + | |
| 45 | } | |
| 46 | else if (operator.equals("-"))
| |
| 47 | {
| |
| 48 | value = row - column; | |
| 49 | } | |
| 50 | else // unsupported operation | |
| 51 | {
| |
| 52 | value = 0; | |
| 53 | } | |
| 54 | ||
| 55 | table += String.format("%3d\t", value);
| |
| 56 | } | |
| 57 | ||
| 58 | table += "\n"; | |
| 59 | } | |
| 60 | ||
| 61 | return table; | |
| 62 | } | |
| 63 | ||
| 64 | ||
| 65 | ||
| 66 | - | String[] operatorList = { "", " +", " -", " x" };
|
| 66 | + | |
| 67 | - | String[] operation = { "", "add", "subtract", "multiply" };
|
| 67 | + | |
| 68 | String[] operatorList = { "+", "-", "x" };
| |
| 69 | String[] operation = { "add", "subtract", "multiply" };
| |
| 70 | final int MIN_FACTOR = 1, MAX_FACTOR = 20; | |
| 71 | ||
| 72 | String number, choice; | |
| 73 | int highestFactor, menuChoice; | |
| 74 | Scanner user_input = new Scanner(System.in); | |
| 75 | ||
| 76 | do | |
| 77 | {
| |
| 78 | System.out.print( | |
| 79 | "\t1. Addition\n" | |
| 80 | + "\t2. Subtraction\n" | |
| 81 | + "\t3. Multiplication\n" | |
| 82 | + "Please enter which table you'd like to make: "); | |
| 83 | choice = user_input.next(); | |
| 84 | ||
| 85 | try {
| |
| 86 | menuChoice = Integer.parseInt(choice); | |
| 87 | } catch (NumberFormatException e) {
| |
| 88 | System.out.println("That's not a number!");
| |
| 89 | menuChoice = -1; | |
| 90 | } | |
| 91 | } while (menuChoice < 1 || menuChoice > 3); | |
| 92 | ||
| 93 | - | System.out.print("Please enter how high you'd like to " + operation[menuChoice]
|
| 93 | + | |
| 94 | {
| |
| 95 | System.out.print("Please enter how high you'd like to " + operation[menuChoice - 1]
| |
| 96 | + " (up to 20): "); | |
| 97 | number = user_input.next(); | |
| 98 | ||
| 99 | try {
| |
| 100 | highestFactor = Integer.parseInt(number); | |
| 101 | } catch (NumberFormatException e) {
| |
| 102 | System.out.print("That's not a number! ");
| |
| 103 | highestFactor = -1; | |
| 104 | } | |
| 105 | - | System.out.println(makeTable(operatorList[menuChoice], highestFactor)); |
| 105 | + | |
| 106 | ||
| 107 | System.out.println(makeTable(operatorList[menuChoice - 1], highestFactor)); | |
| 108 | } | |
| 109 | ||
| 110 | } |