Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************************************************
- * Program Name: Programming Project 4.6: Arithmetic Table
- * Author: Terry Weiss
- * Date Written: September 25, 2015
- * Course/Section: CSC 111-003W
- * Program Description:
- * Design and implement an application that produces an arithmetic table, showing the
- * results of a user-specified operation on the integers 1 through a user-defined maximum.
- **************************************************************************************************/
- import java.util.Scanner;
- public class ArithmeticTable
- {
- public static String makeTable( String operator, int highestFactor )
- {
- int column, row, value;
- String table = "";
- // Print the header row
- table += operator + "\t";
- for (column = 1; column <= highestFactor; column++)
- {
- table += String.format("%3d\t", column);
- }
- table += "\n";
- // Print table
- for (row = 1; row <= highestFactor; row++ )
- {
- table += String.format("%2d|\t", row); // Display row number
- for (column = 1; column <= highestFactor; column++)
- {
- if (operator.equals(" x"))
- {
- value = row * column;
- }
- else if (operator.equals(" +"))
- {
- value = row + column;
- }
- else if (operator.equals(" -"))
- {
- value = row - column;
- }
- else // unsupported operation
- {
- value = 0;
- }
- table += String.format("%3d\t", value);
- }
- table += "\n";
- }
- return table;
- }
- public static void main( String[] args )
- {
- String[] operatorList = { "", " +", " -", " x" };
- String[] operation = { "", "add", "subtract", "multiply" };
- final int MIN_FACTOR = 1, MAX_FACTOR = 20;
- String number, choice;
- int highestFactor, menuChoice;
- Scanner user_input = new Scanner(System.in);
- do
- {
- System.out.print(
- "\t1. Addition\n"
- + "\t2. Subtraction\n"
- + "\t3. Multiplication\n"
- + "Please enter which table you'd like to make: ");
- choice = user_input.next();
- try {
- menuChoice = Integer.parseInt(choice);
- } catch (NumberFormatException e) {
- System.out.println("That's not a number!");
- menuChoice = -1;
- }
- } while (menuChoice < 1 || menuChoice > 3);
- do
- {
- System.out.print("Please enter how high you'd like to " + operation[menuChoice]
- + " (up to 20): ");
- number = user_input.next();
- try {
- highestFactor = Integer.parseInt(number);
- } catch (NumberFormatException e) {
- System.out.print("That's not a number! ");
- highestFactor = -1;
- }
- } while (highestFactor < MIN_FACTOR || highestFactor > MAX_FACTOR);
- System.out.println(makeTable(operatorList[menuChoice], highestFactor));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment