brainfrz

Arithmetic Table

Sep 26th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. /**************************************************************************************************
  2.  *  Program Name:     Programming Project 4.6: Arithmetic 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.  
  11. import java.util.Scanner;
  12.  
  13.  
  14. public class ArithmeticTable
  15. {
  16.     public static String makeTable( String operator, int highestFactor )
  17.     {
  18.         int column, row, value;
  19.         String table = "";
  20.        
  21.         // Print the header row
  22.         table += operator + "\t";
  23.         for (column = 1; column <= highestFactor; column++)
  24.         {
  25.             table += String.format("%3d\t", column);
  26.         }
  27.         table += "\n";
  28.        
  29.         // Print table
  30.         for (row = 1; row <= highestFactor; row++ )
  31.         {
  32.             table += String.format("%2d|\t", row);   // Display row number
  33.            
  34.             for (column = 1; column <= highestFactor; column++)
  35.             {
  36.                 if (operator.equals(" x"))
  37.                 {
  38.                     value = row * column;
  39.                 }
  40.                 else if (operator.equals(" +"))
  41.                 {
  42.                     value = row + column;
  43.                 }
  44.                 else if (operator.equals(" -"))
  45.                 {
  46.                     value = row - column;
  47.                 }
  48.                 else // unsupported operation
  49.                 {
  50.                     value = 0;
  51.                 }
  52.  
  53.                 table += String.format("%3d\t", value);
  54.             }
  55.  
  56.             table += "\n";
  57.         }
  58.  
  59.         return table;
  60.     }
  61.    
  62.    
  63.    
  64.     public static void main( String[] args )
  65.     {
  66.         String[] operatorList = { "", " +", " -", " x" };
  67.         String[] operation = { "", "add", "subtract", "multiply" };
  68.         final int MIN_FACTOR = 1, MAX_FACTOR = 20;
  69.  
  70.         String number, choice;
  71.         int highestFactor, menuChoice;
  72.         Scanner user_input = new Scanner(System.in);
  73.        
  74.         do
  75.         {
  76.             System.out.print(
  77.                       "\t1. Addition\n"
  78.                     + "\t2. Subtraction\n"
  79.                     + "\t3. Multiplication\n"
  80.                     + "Please enter which table you'd like to make: ");
  81.             choice = user_input.next();
  82.            
  83.             try {
  84.                 menuChoice = Integer.parseInt(choice);
  85.             } catch (NumberFormatException e) {
  86.                 System.out.println("That's not a number!");
  87.                 menuChoice = -1;
  88.             }
  89.         } while (menuChoice < 1 || menuChoice > 3);
  90.        
  91.         do
  92.         {
  93.             System.out.print("Please enter how high you'd like to " + operation[menuChoice]
  94.                     + " (up to 20):  ");
  95.             number = user_input.next();
  96.  
  97.             try {
  98.                 highestFactor = Integer.parseInt(number);
  99.             } catch (NumberFormatException e) {
  100.                 System.out.print("That's not a number! ");
  101.                 highestFactor = -1;
  102.             }
  103.         } while (highestFactor < MIN_FACTOR || highestFactor > MAX_FACTOR);
  104.  
  105.         System.out.println(makeTable(operatorList[menuChoice], highestFactor));
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment