CS107 - Lab 5

Java Programming (part 1: basics)

Yes, this is true!!! We get to do some real programming!!

In this lab you will start programming in Java. You will get used to using the basic operations and instructions in Java, compiling, running, and debugging.

You will most likely encounter many problems in this first lab. Before you raise your hand for help, try to understand what happens with your program. If it is a compile problem, read the messages on the screen; the compiler tries to give you a hint of what went wrong; can you figure out what it's telling you?

If your program compiles, but when run it does not give you quite the desired output, then we say that it has bugs. Try to debug it. Try to understand what happens, what are the instructions that are being executed, in what order. To debug, people use lots of "print" instructions. For instance, if something wrong happens in a loop, then print out the values of the variables that are relevant to the loop; this will help you understand how values change in each iteration.

Don't forget to pay attention to issues of programming style: indentation, comments, meaningful names, spaces (see handouts). If you develop a good programming style, programming will come easier to you (plus, I wont take points off). And it will be easier for others to help you.

As usual, work individually, and call me if you need help. You are encouraged to discuss ideas and techniques broadly with other class members, but not specifics. Discussions should be limited to questions that can be asked and answered without using any written medium (e.g. pencil and paper or email). You should at no point look at the screen of your colleagues.


  1. Write a Java program that reads from the user a value n and prints an n by n multiplication table. When run, your program should look as follows:
    Dear user, please enter a number, and I will compute a multiplication table for you: 7
    
    You typed in 7. Here is the multiplication table: 
    
     1  2  3  4  5  6  7
     2  4  6  8 10 12 17
     3  6  9 12 15 18 21
     4  8 12 16 20 24 28
     5 10 15 20 25 30 35
     6 12 18 24 30 36 42
     7 14 21 28 35 42 49
    
    Happy? (y/n)
    
    If the user answers n the program should print: Try again.. and terminate. If the user answers y the program should print: Great! now you can try the next problem :) and terminate.

    Note: Be careful with the formatting! It should look exactly as above. Assume the size of the table is always smaller than 10.

    Hint:


  2. Write a Java program that asks the user if he/she likes Java programming. If the user answers yes, then it displays a congratulating message (use your imagination), and terminates.

    Now, if the user answers no, then the fun starts. The program should ask Are you sure you don't like Java programming?. If the user answers no, then it should display Hope you'll get to like it soon.. Otherwise, if the user answers yes, it should ask Are you really sure you don't like Java programming?, and repeat. If the user keeps saying yes, then the program keeps asking Are you really really sure you don't like Java programming?, then Are you really really really sure you don't like Java programming?. And so on, every time printing one more really.

    This should stop when it reaches the point when it prints really 5 times. At that point, if the user still answers yes, it should print Too bad, and terminate.

    Do you like Java programming? [y/n]: n
    Are you sure you don't like Java programming? [y/n]: y
    Are you really sure you don't like Java programming? [y/n]: y
    Are you really really sure you don't like Java programming? [y/n]: n
    Hope you'll get to olike it soon..
    
    Do you like Java programming? [y/n]: n
    Are you sure you don't like Java programming? [y/n]: y
    Are you really sure you don't like Java programming? [y/n]: y
    Are you really really sure you don't like Java programming? [y/n]: y
    Are you really really really sure you don't like Java programming? [y/n]: y
    Are you really really really really sure you don't like Java programming? [y/n]: y
    Are you really really really really really sure you don't like Java programming? [y/n]: y
    Too bad..
    


What to turn in:

Send me only the .java files (not the entire folder), both in the same email, as attachments.