Question

In: Computer Science

Description: You are to develop a Java program that prints out the multiplication or addition table...

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);

    }           

}

Solutions

Expert Solution

//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


Related Solutions

Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
/* Problem 1 * Write and run a java program that prints out two things you...
/* Problem 1 * Write and run a java program that prints out two things you have learned * so far in this class, and three things you hope to learn, all in different lines. */ You could write any two basic things in Java. /* Problem 2 * The formula for finding the area of a triangle is 1/2 (Base * height). * The formula for finding the perimeter of a rectangle is 2(length * width). * Write a...
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of...
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of each word A main(. . . ) function and the use of std::cin and std::cout to read in data and write out data as described below. Variables to hold the data read in using std::cin and a return statement. #include <iostream > int main(int argc, char *argv[]) { .... your code goes here }//main Example usage: >A01.exe Enter three 3-letter space separated words, then...
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
Description Write a program that prints out your name, the course ID of this class, what...
Description Write a program that prints out your name, the course ID of this class, what programming/computer courses you've taken. Ask the user for two numbers. Show the sum of the two numbers, the difference, the product and the quotient. For the difference subtract the second number from the first, for the quotient, use the first number as the numerator(dividend) and the second as the denominator(divisor). Sample Output: My name is Jianan Liu, I'm in course CS36. I've taken: C...
Q9) Write a function that asks the user for a number and prints out the multiplication...
Q9) Write a function that asks the user for a number and prints out the multiplication table for that number. Python3 Q10)Write a function that takes two integer inputs for width and length, and prints out a rectangle of stars. (Use * to represent a star). Q11)Write a program that reads in a string and prints whether it: [Hint: given a character like ‘H’ you can apply to it the method ‘H’.isalpha() to check if it is a letter or...
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication,...
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication, or modulo (%) to extract the bits. In this exercise use only logic bit-wise operations. Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you CANNOT use the arithmetic division by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT