Java Lab Excercises Updated 05 Oct 2009

Deitel Code examples are here. (login info given in class)

Exercise 1 Additon Applet

Get the source code for the Addition Applet and get it running.

You can get the source code by opening the class handout with Adobe Acrobat Reader - just copy the code and paste into the IDE (netbeanse, elipce, jbuilder etc) source code file.

You'll have to change the class name in your source code from Addition to what ever your source file is called.

Exercise 2.1 Addition V2 & draw some shapes 

Get the Addition applet running - 2nd version with the paint( ) method (the paint method is in the handout 1)

Try entering a pieces of text e.g. abc instead of a number and see what happens.
Try entering a floating point number e.g. 12.5 and see what your applet does.
A runtime error, an exception, occurs - on which line of your program is it happening?

Modify the paint( ) method on the g.drawString() line to use other methods from the Graphics class - look up the graphics class in the api. Draw circles, squares etc. For example to find drawString follow the links in the API:   java.awt ->  Graphics -> drawString. You'll change the colour of the background and what you've drawn in Ex 2.2 later.

Vary the size of, say, of a circle depending on what the user typed in (instead of adding the numbers)

Add an int primitive data type to the addition applet. Increment it each time the paint() method runs. Display this variable on the screen. Cover and un-cover your applet with another page. Does the counter increment?

Exercise 2.2 Changing Colour

Use the following method in your paint( ) method to change the background colour (default is lightgray) and to change the colour of what you've drawn in Ex 2.1:

setBackground (Color.white);      // changes the background colour of the Applet container
g.setColor(; etc          // changes the current drawing colour to blue

Exercise 2.3 Second TextField to draw shapes

Add a second textfield object to your applet and call it input1. So now you have two textfields input and input1.
You'll have to create the new input1 textfield object in the init() method and add code to the actionPerformed() method to retrieve user inputted text from it (use the TextField method getText()).
Use Graphics class method drawOval() to draw an oval where the user can vary the width and height by entering numbers into  the textfields input for the width and input1 for the height.

Exercise 3.1 Face

Create an applet that draws a face (using methods from the Graphics class).
When the user enters a number in a TextField the face will show different expressions depending on the number entered:
e.g. 1 = normal, 2 = happy, 3 = surprised, 4 = totally petrified..

Note you can use setColor to draw in specific colours and setting the drawing colour to the background colour can be used to 'undraw' previously drawn objects (use g.setColor(getBackground()); to retrieve the background colour)

Exercise 3.2 Placing An Applet on a web page

Identify the files in the project Ex 3.1 (.java, .html, .class) and find where your IDE has saved them. Copy the .html and .class file to a new directory on your f-drive so you can do the next step. If cannot find the HTML file then create one - see handout for an example of a simeple HIML file with an Applet tag in it.

Create a web page (if you don't already have one) and place the applet in Exercise 3.1 on it. Put your name somewhere on the web page. There are instructions on how to create a web page on the Maths and Computing web page, see Information|Web Pages. Also there's some info about student web page creation in the Sources folder. 

Exercise 3.3 Speed Conversion

Create an applet that allows a user to enter a speed in  miles per hour.
The applet calculates and diplays the equivalent speed in km/h (*1.15) and Knots (*1.85).
Also use drawRect to draw 3 bars on the applet for M/h, km/h and knots/h where each bar is proportional in lenght to the speed is is showing.

Exercise 3.4 Battery Status

Create an applet that displays a coloured bar to diplay a car battery's status. The battery voltage is entered into the applet as a floating point number.
Diplay a bar graph as follows:
    0-9.5 - Red  (battery dead)
    >9.6 <12 - Yellow (battery poor)
    >12  <14.4 - Green (battery good)
    >14.4 - Blue (Alternator faulty)

Exercise 4 Secret Text

Develop an applet or application that has a TextField  and  Label GUI components.
When a piece of text is entered the applet 'encrypts' the text by replacing each letter using the following mapping:

    So A -> Z,  N-> M,  B-> Y,  O->L etc

Display the encrypted text back in the TextField (so pressing enter should give you back the original text).
Display the length of the entered text using the Label.

Exercise 5 Missing Letter Game

Develop an applet or application that displays a word at random with a random letter(s) missing. The user has to guess the missing letter(s) by entering it/them into a text field(s). You canuse an array or vector to store some words internally in the program.

Exercise 6 BMI Servlet

Develop a JSP to allow a user to enter their height and weight in separate textfields. The JSP also has a submit button which activates a servlet when pressed. The servlet then displays the persons BMI. Body Mass Index formula: weight/height*height.  Also display a message based on the BMI index:
Below 18.5 Underweight
18.5 -24.9 Normal
25 - 29.9 Overweight
30 & Above Obese
Add a hit counter code to the servlet which increments eachtime the submit button is pressed.