Can somebody provide me the solution's for below 3 question:

1) Construct a program that is capable of printing the multiplication table for two
variables. For example if the user enters x as 5, and y as 6, the multiplication table
that will be printed out is as follows:

1*6=6
2*6=12
3*6=18
4*6=24
5*6=30

Should the user enter x=4 and y=3, the multiplication table that will be generated is as
follows:

1*3=3
2*3=6
3*3=9
4*3=12

x and y are integer variables entered by the user during runtime.

2) Write a program to store the 5 names of students: Ali, Ahmad, Ah Chong, Muthu and Ramasamy in an array. By referencing the array, print out the second name that youtyped into the array earlier.

3) Modify the program in 2) by reversing the third name entered into the array.
Example, if the third name entered was "Ah Chong", the result should be "gnohC hA".

All the Question are based on array.

For Question 1, i use:

public class Question1 {

    /** Creates a new instance of Question1 */
    public Question1() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        int i;
        int result;
        int p = 1;
        int table[] = new int [10];

        do
        {
            System.out.println( "Please enter the first digit: ");


            System.out.println( "Please enter the second digit: ");

            for(i=0;i<10;i++) /* Fills the array */
            {
              if(p==0) /* Stratagy to exit the program Benins */
              {
                 System.out.println("The program is exited!\n");
                 break;

              } /* Stratagy to exit the program ends */


              table[i]=i+1; /* Fills the array with numbers 1 - 10 */

              result = p * table[i];

              System.out.printf( "%d x %d = %d", p, table[i], result );
            }




        }while(p != 0);

    }   

}

output:

C:\Documents and Settings\Nathan\Desktop\4PFJ2_041080361_TMA3\Question 1\src\Question1.java:52: cannot find symbol
symbol  : method printf(java.lang.String,int,int,int)
location: class java.io.PrintStream
              System.out.printf( "%d x %d = %d", p, table[i], result );
1 error
BUILD FAILED (total time: 2 seconds)

For Question 2, i use:

public class Question2 {

    /** Creates a new instance of Question2 */
    public Question2() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {


        String[] name = new String[5];

        name[0] = "Ali";
        name[1] = "Ahmad";
        name[2] = "Ah Chong";
        name[3] = "Muthu";
        name[4] = "Ramasamy";

        int answer;

        System.out.print("Please type a number < 1 for Ali, 2 for Ahmad, 3 for Ah Chong, 4 for Muthu, 5 for Ramasamy >: ");


        if(answer == 1)
        {
            System.out.print("The name you had type are " + name[0]);
        }
            else if(answer == 2)
            {
                System.out.print("The name you had type are " + name[1]);
            } 
            else if(answer == 3)
            {
                System.out.print("The name you had type are " + name[2]);
            }
            else if(answer == 4)
            {
                System.out.print("The name you had type are " + name[3]);
            } 
            else if(answer == 5)
            {
                System.out.print("The name you had type are " + name[4]);
            }  

    }




}

output:

C:\Documents and Settings\Nathan\Desktop\4PFJ2_041080361_TMA3\Question 2\src\Question2.java:41: variable answer might not have been initialized
        if(answer == 1)
1 error
BUILD FAILED (total time: 0 seconds)

While Question 3, i didn't do yet. For Question 3, our tutor told us to us to use something that call reverse buffer.

Pls Help and give comment:( :'(

Recommended Answers

All 4 Replies

Please post your code in code tags, following the rules that can be found in the stickies at the top of this forum.

:)

int answer;

System.out.print("Please type a number < 1 for Ali, 2 for Ahmad, 3 for Ah Chong, 4 for Muthu, 5 for Ramasamy >: ");


if(answer == 1)

Where, above, do you give "answer" a value?

The answer, you don't. I assume you want to read user input some time after the println and before the if?

commented: I think they outsmarted themselves with your signature ;) +29

And to follow up on what Masijade said, you can use the Scanner class to do so. Google for Scanner class documentation as well as the java sun tutorial on use of the Scanner class.

And to follow up on what Masijade said, you can use the Scanner class to do so. Google for Scanner class documentation as well as the java sun tutorial on use of the Scanner class.

Among other things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.