Java Class Worksheet



Java Object & Class Worksheet

Learning Goal: Design some simple Java classes and see how they can be instantiated and used in a Java program. Java programs can be divided into multiple objects (classes), and each object has its own file.

Background Information

Java Classes – the basic Java class looks like the following:

public class ClassName {

(The contents of the class go here – fields, methods)

} //ClassName

A comment is placed after the closing brace for the class. This is very good practice. As you write longer programs you will have many closing braces. Write a short comment stating what the closing brace closes, so you can more easily keep track of how your code is nested.

Java Methods – functionality the class obeys. The basic Java method looks like the following (this method returns nothing):

public void methodName () {

(The contents of the method go here)

} // methodName

Notice the comment after the closing brace.

What You Need to Do

We will build a Java program concerning a Zoo.

1. Open BlueJ, and create a new project called ZooProject. Make sure the Project Location is somewhere appropriate on your drive.

2. Create a new class.

a. Class Name = Lion

b. This will create a Lion.java file.

c. Uncheck the box that asks about this being the “main” if it asks you at all.

3. Add appropriate fields and methods to Lion.java to build a working Java class. Here is a start:

public class Lion

{

private int age;

public void roar()

{

System.out.println(“Roar! Roar!”);

} // roar()

} //Lion

4. Compile your project, correcting any errors and typos.

5. Create a “New File”

a. Class Name = ZooProgram

b. This will create a ZooProgram.java file.

6. Add the following code to ZooProgram.java:

public class ZooProgram {

public static void main (String [] args) {

Lion leo = new Lion ();

leo.roar();

} // main ()

} // ZooProgram

Compile your project again, correcting any errors.

7. Run your project.

8. What is the output?

9. Add 2 or 3 more animals to your zoo.

10. In each of your Java files add a comment with your name.

11. Build and run your project.

What You Did and Hopefully Learned

In this worksheet, you performed the following steps:

• Used BlueJ to creare a new project.

• Used BlueJ to create a file called Lion.java.

• Used the editor to put appropriate Java statements into Lion.java to make it into a working Java class

• Compiled this class and corrected any typing errors you might have made.

• Used the editor to create a program (ZooProgram) in the file called ZooProgram.java. This program created and used a Lion object, called leo

• Created more classes.

• Modified ZooProgram to use your other classes.

You have learned the following information about Java:

• Java programs are divided into separate classes.

• Each class is stored in its own individual file.

• A Java program can make use of many different classes.

• Class, method and object names are subject to a number of restrictions.

• A program may use a class to create an object.

• Once an object is created, the program may use any of the methods which belong to the class of the object

• A program may create objects of many different classes.

• All the necessary file creation, editing, compilation and running can be performed inside BlueJ.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download