brainfrz icon

Arithmetic Table #2

brainfrz | PRO | 09/26/15 03:20:58 PM UTC | 0 ⭐ | 814 👁️ | Never ⏰ | []
Java |

3.6 KB

|

None

|

0 👍

/

0 👎

/**************************************************************************************************
 *  Program Name:     Programming Project 4.6: Multiplication 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.
 *
 * Edit: Removed requiring a space in `operator` in `makeTable()` and formatted it in instead.
 **************************************************************************************************/
 
import java.util.Scanner;
 
 
public class ArithmeticTable2
{
    public static String makeTable( String operator, int highestFactor )
    {
        int column, row, value;
        String table = "";
        
        // Print the header row
        table += String.format(" %s\t", operator);
        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 - 1]
                    + " (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 - 1], highestFactor));
    }
    
}

Comments