OOP: Lab 1 Exercises

Author: Ewan Klein
Version: 1.6
Date: 2009-01-13

Contents

Reading

These exercises assume that you have read Chapter 1 of Head First Java (HFJ).

1. HelloWorld

Exercise 1

Make a copy for yourself of the HelloWorld.java file that was discussed in the lecture. Check that you can compile the file:

% javac HelloWorld.java

and that after compiling, you can run it:

% java HelloWorld
hello, world
|-|3LL0, \/\/0RLD

Exercise 2

Assuming that worked OK, you are going to edit the file, and re-compile it, and run it again. Here's the task. First off, get rid of the second System.out.println() statement. Now, inside the main() method, declare and assign values to three String variables, one corresponding to the string "hello", a second corresponding to the comma, and the third corresponding to "world". For example, the first declaration might be something like the following:

String h = "hello";

Next, change the argument to the first System.out.println() statement so that it instead of being a literal string, it consists of the concatenation (using the + operator) of the three variables that you have defined. Finally, add some comments (using // as the comment character) to your file. Check that your modified file still compiles and produces "hello, world" as output.

2. while loops

Exercise 3

Below is the Loopy.java code from HFJ.

public class Loopy {
    public static void main (String[] args){
	int x = 1;
	System.out.println("Before the Loop");
	while (x < 4) {
	    System.out.println("In the loop");
	    System.out.println("Value of x is " + x);
	    x = x + 1;
	}	
	System.out.println("This is after the loop");
    }
}

Download a copy of this file to your own filespace, compile it with the command javac Loopy.java, and then run it with the command java Loopy. You should get the following output displayed on your terminal:

Before the Loop
In the loop
Value of x is 1
In the loop
Value of x is 2
In the loop
Value of x is 3
This is after the loop

Make a copy of the file with the name Loopy1.java, and modify it so that it produces the following output:

Before the Loop, value of x is 2

In the loop
Value of x is 2

In the loop
Value of x is 3

In the loop
Value of x is 4

This is after the loop

Hint

When you first compile your code, you might get the following error message:

Loopy1.java:1: class Loopy is public, should be declared in a file named Loopy.java
public class Loopy {
       ^
1 error

Since you have changed the name of the file to Loopy1.java, you also have to change the name in the class declaration to match.

Hint

To produce a newline, insert the characters '\n' into a string. E.g. the following declaration will be rendered on the terminal as two newlines followed by "hello":

String new = "\n\nhello";

Exercise 4

Have a look at DoYouWrong.java:

public class DoYouWrong {
    public static void main (String[] args){
	int x = 0;
	System.out.print("Oh baby")
	while (x == 3) {
	    System.out.println("DoYou");
	    x = y + 1;
		}	
	System.out.println("WannaDance?");
    }
}

It is somewhat similar to the DooBee example on p.13 of HFJ, but it has some problems. First, it doesn't compile. Second, the customer's specification (cf. http://www.youtube.com/watch?v=FO6uzDn-xnE) of intended output is the following:

Oh baby
DoYouDoYouDoYouDoYouWannaDance?

Make a copy of DoYouWrong.java under the name DoYou.java and fix it up to produce the correct output.


Date: Tue 13 Jan 2009 10:04:09 GMT

OOP Informatics Home