In: Computer Science
Description: You are to develop a Java program that prints out the multiplication or addition table given the users start and end range and type of table. This time several classes will be used.
You are free to add more methods as you see fit – but all the methods listed below must be used in your solution.
For the class Table the following methods are required:
- Protected Constructor – stores the start and size of table, creates the correct size of array
- display() – displays with a good format (see A1a) a table
- For the classes AdditionTable and MultiplicationTable (separate files) the following methods should be made:
- Constructor – takes in start and end positions and calculates the table of values
- You will need to create an enumerated type (separate file) for the table types.
The driver (Main) class requires completion:
/**
* The driver program. Displays the specified arithmentic table with
* the specifified start/stop values.
*
*/
public final class Main
{
/**
* Disallow the creation of any Main objects.
*/
private Main()
{
}
/**
* The entry point to the program.
*
* @param argv the command line args.
* argv[0] - the type (+ or *)
* argv[1] - the start value (> 1 && < 100)
* argv[2] - the end value (> 1 && < 100)
*/
public static void main(final String[] argv)
{
final TableType type;
final int start;
final int stop;
final Table table;
if(argv.length != 3) {
usage();
}
type = getType(argv[0]);
start = getNumber(argv[1]);
stop = getNumber(argv[2]);
table = getTable(type, start, stop);
//YOUR CODE TO DISPLAY TABLE – HINT 1 LINE OF CODE!
}
/**
* Convert the supplied string into the appropriate TableType.
* If the string is not a valid type then exit the program.
*
* @param str the stringto convert
* @return the appropriate TableType
*/
public static TableType getType(final String str)
{
final TableType type;
if(str.equals("+"))
{
type = TableType.ADD;
}
else if(str.equals("*"))
{
type = TableType.MULT;
}
else
{
usage();
type = null;
}
return (type);
}
/**
* Convert the supplied string into an int.
* If the string is not a valid int then exit the program.
* To be valid the string must be an integer and be > 0 and < 100.
*
* @param str the string to convert
* @return the converted number
*/
public static int getNumber(final String str)
{
int val;
try
{
val = Integer.parseInt(str);
if(val < 1 || val > 100)
{
usage();
}
}
catch(final NumberFormatException ex)
{
usage();
val = 0;
}
return (val);
}
public static Table getTable(final TableType type,
final int width,
final int height)
{
//YOUR CODE GOES HERE
}
/**
* Display the usage message and exit the program.
*/
public static void usage()
{
System.err.println("Usage: Main <type> <start> <stop>");
System.err.println("\tWhere <type> is one of +, \"*\"");
System.err.println("\tand <start> is between 1 and 100");
System.err.println("\tand <stop> is between 1 and 100");
System.err.println("\tand start < stop");
System.exit(1);
}
}
//Java code
public abstract class Table { protected int start; protected int[] array; protected Table(int start, int size) { this.start= start; array = new int[size]; } public abstract void display(); }
//========================================
public class AdditionTable extends Table{ public AdditionTable(int start, int end) { super(start, end); } @Override public void display() { for (int i = start; i <array.length ; i++) { System.out.println(array.length+" + "+i+" = "+(array.length+i)); } } }
//==================================
public class MultiplicationTable extends Table { public MultiplicationTable(int start, int end) { super(start, end); } @Override public void display() { for (int i = start; i <array.length ; i++) { System.out.println(array.length+" * "+i+" = "+(array.length*i)); } } }
//======================================
public enum TableType { ADD,MULT }
//================================
/** * The driver program. Displays the specified arithmentic table with * the specifified start/stop values. * */ public final class Main { /** * Disallow the creation of any Main objects. */ private Main() { } /** * The entry point to the program. * * @param argv the command line args. * argv[0] - the type (+ or *) * argv[1] - the start value (> 1 && < 100) * argv[2] - the end value (> 1 && < 100) */ public static void main(final String[] argv) { final TableType type; final int start; final int stop; final Table table; if(argv.length != 3) { usage(); } type = getType(argv[0]); start = getNumber(argv[1]); stop = getNumber(argv[2]); table = getTable(type, start, stop); //YOUR CODE TO DISPLAY TABLE – HINT 1 LINE OF CODE! table.display(); } /** * Convert the supplied string into the appropriate TableType. * If the string is not a valid type then exit the program. * * @param str the stringto convert * @return the appropriate TableType */ public static TableType getType(final String str) { final TableType type; if(str.equals("+")) { type = TableType.ADD; } else if(str.equals("*")) { type = TableType.MULT; } else { usage(); type = null; } return (type); } /** * Convert the supplied string into an int. * If the string is not a valid int then exit the program. * To be valid the string must be an integer and be > 0 and < 100. * * @param str the string to convert * @return the converted number */ public static int getNumber(final String str) { int val; try { val = Integer.parseInt(str); if(val < 1 || val > 100) { usage(); } } catch(final NumberFormatException ex) { usage(); val = 0; } return (val); } public static Table getTable(final TableType type, final int width, final int height) { if(TableType.ADD == type) return new AdditionTable(width,height); else return new MultiplicationTable(width,height); } /** * Display the usage message and exit the program. */ public static void usage() { System.err.println("Usage: Main <type> <start> <stop>"); System.err.println("\tWhere <type> is one of +, \"*\""); System.err.println("\tand <start> is between 1 and 100"); System.err.println("\tand <stop> is between 1 and 100"); System.err.println("\tand start < stop"); System.exit(1); } }
//Output
//If you need any help regarding this solution ............ please leave a comment ........ thanks