Java Lab Excercises Updated 05 Oct 2009
Deitel Code examples are here. (login info given in class)
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.
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?
    
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(Color.blue); etc         
  // changes the current drawing colour to blue
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.
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)
- MLKJIHGFEDCBA
- NOPQRSTUVWXYZ
So A -> Z, N-> M, B-> Y, O->L etc
| Below 18.5 | Underweight | 
| 18.5 -24.9 | Normal | 
| 25 - 29.9 | Overweight | 
| 30 & Above | Obese |