Saturday, May 2, 2009

Make Table


Problem Statement:
Write a program to generate a table of given number from command prompt.

Language:
Java

Platform:
Command Prompt

Command:
[Program] [Input Number]

Expected Input:
[Input Number]: Any Integer (positive or negative)

Expected Output:
Table of given integer from 1 to 10.

Example:
Interface:
C:\Program> java table 5
Output:
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50

5 comments:

  1. //Table creation program in java

    import java.io.*;
    class Tables{
    public static void main(String srgs[]){
    System.out.println("Enter any number : ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String num ="";
    try{
    num = br.readLine();
    System.out.println("Table for Number : " + num );
    for(int i = 1 ; i<=10 ; i++){
    System.out.println( num + "*" + i + " = " + Integer.parseInt(num)*i);

    }
    }
    catch (IOException ioe) {
    System.out.println("IO error trying to read your name!");
    System.exit(1);
    }


    }
    }

    //output
    Enter any number :
    5
    Table for Number : 5
    5*1 = 5
    5*2 = 10
    5*3 = 15
    5*4 = 20
    5*5 = 25
    5*6 = 30
    5*7 = 35
    5*8 = 40
    5*9 = 45
    5*10 = 50

    ReplyDelete
  2. Its a nice code and work well if user is careful. Good Job! However user input can be monitor at a better place.

    Couple of comment on the code:

    1. Input was expected from the command line, not on the xterm.
    2. Expected output symbol of multiplier was "X" and not "*". The program is correct but not perfect as it does not meet the specification.
    3. Check out the print statement while catching the exception.

    ReplyDelete
  3. import java.io.*;
    class Tables{
    public static void main(String srgs[]){
    System.out.println("Enter any number : ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String num ="";
    try{
    num = br.readLine();
    System.out.println("Table for Number : " + num );
    for(int i = 1 ; i<=10 ; i++){
    System.out.println( num + "X" + i + " = " + Integer.parseInt(num)*i);

    }
    }
    catch (IOException ioe) {
    ioe.printStackTrace();
    System.out.println("IO error trying to read your name!");
    System.exit(1);
    }


    }
    }

    ReplyDelete
  4. Can u explain me what is the difference b\w xterm and command line arguments

    ReplyDelete
  5. Missing Specifications (its important to understand because once you missed it, the person sitting next to you may point it out .. (like IBM Team :)))

    1. Printing Format:
    Expected printing format is as followes:
    >> 5 X 1 = 5
    Printing format from your program is this:
    >> 4X1 = 4
    --- now there is a difference. you need to understand the minute details for perfection or "the perfect"!

    2. Print Statement in catch block:
    >> System.out.println("IO error trying to read your name!");
    --- how did this name comes into picture?

    3. Input Fromat:
    Expected input format:
    $ java table 5
    Your program input format:
    $ java table
    Enter any number :
    4
    --- specification was to provide input along with the command line and your program is taking input during the execution. there is a disadvantage of providing input during the execution, an operator need to be present to execute the program. whereas if the command itself takes the input arguments then you can easily automate the program execution. (learning will be in comming sessions.)


    difference between xterm and command line arguments:

    > xterm is a standard input emulator. this is a interface window to interact with program, compining, bla bla ... this is just a software.
    > command line argument is related to a program. when you run any program on the terminal, you might have some optional switched to provide to the program from the command itsels. this is known as command line arguments.
    for example, look at the following command:
    $ java table 5
    here "java" is the Java program, "table" is the name of the class file and "5" is additional command line argument. this argument goes to the program directly without any manual intervention.

    let me know if you still have any doubt.

    ReplyDelete