Download Java Class Worksheet

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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 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 NetBeans, and create a new project called ZooProject. Select “General”
Categories, “Java Application” Projects. Make sure the Project Location is somewhere
appropriate on your H: drive.
2. Make sure “ZooProject” is selected in the Projects window. Create a “New File” (a “Java
Classes” categories, “Empty Java File” file types).
a. Class Name = Lion
b. This will create a Lion.java file.
c. Uncheck the box that asks about this being the “main”.
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. Build your project, correcting any errors and typos.
5. Make sure “ZooProject” is selected in the Projects window. Create a “New File” (a “Java
Classes” categories, “Empty Java File” file types).
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
7. Build your project again, correcting any errors.
8. Run your project. NetBeans will ask you where the main program lies – make sure
ZooProgram is selected. 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 NetBeans to creare a new project .
Used NetBeans 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
NetBeans.