Create Software Components  Using Java Level 2 Course Info Scheme Resources Tutorials Java Demos Utilities Links

 Class Exercises Variables & Constants Exercises

This exercise should be carried out after reading the Data Types & Variables Notes

There are four exercises, designed to familiarize you with a variety of data types, variable declaration and constants.

Exercise 1: Numeric Variables

1. Copy the code below into Notepad.

import java.awt.*;

public class Average {

public static void main (String[] args) {
int a;
int b;
float av;

a = 10;
b = 5;
av = (a + b)/2;

System.out.println ( "a is " + a);
System.out.println ( "b is " + b);
System.out.println ( "the average is " + av);

}
}

1. Compile and run the code. This is a Java application so you compile it as normal:- javac Average.java.  Then run it at the command prompt using the command:- java Average

2. Now add another variable named c and give it a value.  Change the code to display the value of c and the average of the three numbers.

3. Recompile the code and run it

Exercise 2: Numeric and String Variables

1. Copy the code below into Notepad.

//calculate area of rectangle
import java.awt.*;
import java.applet.Applet;

public class RectArea extends Applet {

public void paint (Graphics g){

String msg;
length = 100;
height = 50;

msg = "The area of my rectangle is ";

area = length * height;

g.drawRect(50, 10, length - 1, height - 1);

g.drawString (msg + area, 10, 80);
}
}

1. This code is not complete.  You need to add extra code to DECLARE all the variables.

2. Compile the code. This is a Java applet so you need to create a HTML file and then run the applet using the browser or appletviewer.   When you have finished It should look something like this.

Exercise 3: Constants

1. Copy the code below into Notepad.

//calculate circle area & circumference
import java.awt.*;
import java.applet.Applet;

public class CircleCalc extends Applet{

public void paint (Graphics g){

circleArea = 3.14 * diameter * diameter / 4;
circumference = 3.14 * diameter;

g.drawOval(60, 0, diameter, diameter);

g.drawString("The area of my circle is " + circleArea, 0, diameter + 30);
g.drawString("The circumference of my circle is " + circumference, 0,diameter + 50);

}
}

1. This code is not complete.  You need to add extra code to DECLARE all the variables, I would suggest you use int's and doubles..

2. Replace the literal value 3.14 with a constant called PI.

3. Compile the code. his is a Java applet so you need to create a HTML file and then run the applet using the browser or appletviewer.   When you have finished It should look something like this.

Exercise 4: Coding Practice

1. Create your own code that draws a circle and a rectangle on an applet window; something like this.

 Note: --  Details of the graphics methods are shown below. ```drawOval(int x, int y, int width, int height)```           Draws the outline of an oval. ```drawRect(int x, int y, int width, int height)```           Draws the outline of the specified rectangle. ```drawString(String str, int x, int y)```           Draws the text using this graphics context's current color. Note: that for the drawrect method, you must specify one pixel less than the desired width and height. This is because the painting system draws lines just below the specified rectangle, instead of within the specified rectangle. The same rule of specifying one less than the desired width applies to other `drawXxx` methods, such as `draw3DRect`. For the `fillXxx` methods, on the other hand, you specify exactly the desired width and height in pixels.

Have fun!

 Course Info Welcome Overview Assessment Qualification Scheme of WorkAssignments Resources Information Blackboard Learning Center Web Materials Reading Java Demos Utilities Links Lecture Materials Tutorials & Notes Quizzes

 Site Home Top Unit Home