Sie sind auf Seite 1von 3

Java Object & Class Worksheet

Chapter 4
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 Inforation
Java Classes the basic Java class loos lie the following:
public class ClassName {
(The contents of the class go here fields, methods)
} //ClassName
! comment is placed after the closing brace for the class. "his is very good practice. !s you
write longer programs you will have many closing braces. #rite a short comment stating what
the closing brace closes, so you can more easily eep trac of how your code is nested.
Java !ethods functionality the class obeys. "he basic Java method loos lie the following
(this method returns nothing):
public void methodName () {
(The contents of the method go here)
$ %% methodName
&otice the comment after the closing brace.
What "ou #eed to $o
#e will build a Java program concerning a 'oo.
(. )pen &et*eans, and create a new project called %oo&roject. +elect ,-eneral.
/ategories, ,Java !pplication. 0rojects. 1ae sure the 0roject 2ocation is somewhere
appropriate on your 3: drive.
4. 1ae sure ,'oo0roject. is selected in the 0rojects window. /reate a ,&ew 5ile. (a ,Java
/lasses. categories, ,6mpty Java 5ile. file types).
a. /lass &ame 7 2ion
b. "his will create a file.
c. 8nchec the bo9 that ass about this being the ,main..
:. !dd appropriate fields and methods to to build a woring Java class. 3ere is a
public class 2ion
private int age<
public void roar()
+ystem.out.println(,=oar> =oar>.)<
$ %% roar()
$ %%2ion
?. *uild your project, correcting any errors and typos.
@. 1ae sure ,'oo0roject. is selected in the 0rojects window. /reate a ,&ew 5ile. (a ,Java
/lasses. categories, ,6mpty Java 5ile. file types).
a. /lass &ame 7 'oo0rogram
b. "his will create a ' file.
A. !dd the following code to '
public class 'oo0rogram ;
public static void main (+tring BC args) ;
2ion leo 7 new 2ion ()<
$ %% main ()
$ %% 'oo0rogram
D. *uild your project again, correcting any errors.
E. =un your project. &et*eans will as you where the main program lies mae sure
'oo0rogram is selected. #hat is the outputF
G. !dd 4 or : more animals to your Hoo.
(I. Jn each of your Java files add a comment with your name.
((. *uild and run your project.
What "ou $id and 'opefull( Learned
Jn this worsheet, you performed the following steps:
8sed &et*eans to creare a new project .
8sed &et*eans to create a file called
8sed the editor to put appropriate Java statements into to mae it into a
woring Java class
/ompiled this class and corrected any typing errors you might have made.
8sed the editor to create a program (%oo&rogra) in the file called %oo&rogra)java.
"his program created and used a Lion object, called leo
/reated more classes.
1odified 'oo0rogram to use your other classes.
Kou have learned the following information about Java:
Java programs are divided into separate classes.
6ach class is stored in its own individual file.
! Java program can mae use of many different classes.
/lass, method and object names are subject to a number of restrictions.
! program may use a class to create an object.
)nce an object is created, the program may use any of the methods which belong to the
class of the object
! program may create objects of many different classes.
!ll the necessary file creation, editing, compilation and running can be performed inside