Creating the project
Start your favorite IDE.
Create a project with a name of your choosing.
Start by creating a Java application (a class
with the class name
within the package
main method print something like
"Today it will snow!".
Creating a second class
Create a second class, called
within the same package.
Place is a subclass of
so you do not need to use
Place has no
Place has four
String, the name of the place
String, the state where the place is located
Stringas an argument and sets the name of a place
Stringas an argument and sets the state for the place
Be sure to define fields to contain the name and state for
Initialize both fields to
In the terminology of object-oriented programming, the “get” methods are known at accessor, or getter, methods and the the “get” methods are known at mutator, or setter, methods.
Using the new class
Go back to
Add code to create a variable
Set the name of
"Hazelwood" and the state to
System.out.println to print the value of
hazelwood. (The default
toString will be used.)
Go back to
Place and add a constructor that takes two arguments,
Strings corresponding to name and state, in that order.
TestDriver. Oops, looks like a problem with the
constructor used to create
hazelwood. Java provides its own
default constructor until you define one of your own. Then
Java takes the old one away. That is strange.
So, add a default constructor and, just for fun, call
to initialize both fields to
TestDriver is happy.
Use your new constructor to create a
frogLevel with name
"Frog Level" and state
And, again use
System.out.println to print
Examining the modifiers
If you are using NetBeans, select Place.java in the Projects tab and take a look at the Member View in the lower left corner.
Notice the little icons in front of constructor, method, and field names.
Change the access modifiers in front of the members (choices are
and see how the icons are changed.
toString method so that the
Place object will return values similar to
"Frog Level, NC".