Internet Computing: CSM02

Lab. session 1: Basic Java

Exercise 1

You will use the Java SDK to run a simple program.

The SDK does not have an integrated development environment; so for the moment use a simple text editor to create the class files (Notepad or Wordpad will do).

Save the any files you create in your home directory.

First of all, use the text editor to enter the following class definition:

public class Circle {

// A class field

public static final double PI= 3.14159; // A useful constant

// A class method: just compute a value based on the arguments

public static double radiansToDegrees(double rads) {

return rads * 180 / PI;

}

// An instance field

public double r; // The radius of the circle

// Two instance methods: they operate on the instance fields

// of an object

public double area() { // Compute the area of the circle

return PI * r * r;

}

public double circumference() { // Compute the circumference

return 2 * PI * r;

}

}

Do get into the habit of indenting lines in a similar way to that used above. It does help to make the program more readable.

Create a folder for your Java exercises. Save the file as “Circle.java”

Now open a command prompt (Select Run on the main menu, type in cmd and press return). Change directory to the one you created above.

IMPORTANT!!!

At the start of the session, do not forget to set the Classpath system variable to include the path to where your Java files are stored. If the path is:

H:\YourPath\YourFolder

You need to type:

set classpath=%classpath%;H:\YourPath\YourFolder

For example, if you have stored the Java files in a folder called Java that is directly in your home directory, type:

set classpath=%classpath%;H:\Java

Before you can use the Circle class, you need to compile it into machine independent byte code so that the Java Virtual Machine can execute it. Do this by typing the following command:

javac Circle.java

If you get an error message, first check that you have typed in the program exactly as above. In particular, check that all the brackets are correctly in place.

If the command prompt returns, then type:

dir

and check to see that “Circle.class” is now present in your directory.

If so, congratulations, you have compiled your first Java program!

BUT, you can’t do anything with it! Somehow we need to establish a connection between the terminal and the byte code that now resides inside your computer.

In a rather similar way to C, we need to establish the main entry point for a program. In C, program execution starts with a function named main(). Analogously in Java, we need to define a class with a “main()” method. The main method will have the following signature:

public static void main(String[] args)

There are two “modifiers” associated with this declaration:

  • “public” is a statement about the accessibility of this method – anyone can use it.
  • “static” says that this is a “class” method – it is not associated with any instance variables, and hence does not need an instance of the its class to be created before it is invoked.

The void keyword specifies the type of the return value of the main method. In fact, this keyword specifies that there is no return value. This is important, as it is a reminder to you that you need to specify within the definition of the main method how the result(s) of executing the application are returned to the user.

The input to the main method is through the variable named args. This is of type String[]. That is, it is an array of strings. So, it may need to be converted into the data type that is actually operated on within the body of the method (for example, we shall see the string converted into a real number).

We could put the main method into the Circle class. However, it is good practice to get into the habit of writing reusable classes, and then define a single application that uses these classes to work together to achieve some goal that is interesting to a user. As discussed in last week’s lecture we will start off by defining a simple application that creates a circle object and then calculates its circumference and area:

public class MakeCircle {

public static void main(String[] args) {

int input = Integer.parseInt(args[0]);

Circle c = new Circle();

c.r = input;

double circum = c.circumference();

System.out.println(circum);

double a = c.area();

System.out.println(a);

}

}

This class contains a single method and it’s sole responsibility is to define an application that makes circles.

Save it in a file called MakeCircle.java

Now type at the command prompt:

javac MakeCircle.java

If your application successfully compiles, you will see the file MakeCircle.class appear in your directory.

Now you can interact with you beloved Circle class.

Try the following and also some additional values for the radius.

java MakeCircle 5

31.4159

78.53975

java MakeCircle 4

25.13272

50.26544

Now, modify the main method to make a new circle d that is equal to circle c (Circle d = c;)

Make the radius of d twice that of c (d.r = 2*c.r;)

Calculate and output the circumference and the area of circle d.

This should now give you an application that generates two sets of circumference and area values:

java MakeCircle 2

12.56636

12.56636

25.13272

50.26544

Finally, modify the main method to print out the radius of the original circle c. Run the application again.

Is this result surprising to you? Can you explain why you get the result you see?

Exercise 2

As discussed in the lectures, there are two main problems with these class definitions:

  1. We have to input data via the command line
  2. There is no control of access to the radius field, r.

Modify the class definitions for Circle and MakeCircle to:

a)Use dialog boxes to input and output data; and

b)Make the definition “robust” through the use of protected fields and methods

You can access the slides from the CSM02 web site to revise how to do this.

11/13/18