Java-Classes Objects
Java-Classes Objects
Notes from
Well House
Consultants
These notes are written by Well House Consultants and distributed
under their Open Training Notes License. If a copy of this license is not
supplied at the end of these notes, please visit
http://www.wellho.net/net/whcotnl.html
for details.
Well House Consultants provides niche training, primarily but not exclusively in
Open Source programming languages. We offer public courses at our training centre
and private courses at your offices. We also make some of our training notes available
under our "Open Training Notes" license, such as we’re doing in this document here.
You are NOT allowed to charge (directly or indirectly) for the copying or distribu-
tion of these notes, nor are you allowed to charge for presentations making any use
of them.
If you would like us to attend a course (Java, Perl, Python, PHP, Tcl/Tk, MySQL
or Linux) presented by the author of these notes, please see our public course
schedule at
http://www.wellho.net/course/index.html
If you have a group of 4 or more trainees who require the same course at the same
time, it will cost you less to have us run a private course for you. Please visit our onsite
training page at
http://www.wellho.net/course/otc.html
which will give you details and costing information
Objects and
Classes
In Java, we place all our code in "classes". Each class contains one or
more named blocks of code known as methods, and the group of methods
provides all the functionality that's required to handle data (or objects) of
a particular type.
Naming conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Java would be very limited if data could only be held in the eight different primitive
types of variables. Although they can form the fundamental building blocks, you
really want something much more. You want to be able to define your own data type,
create variables to contain data of that type, and then perform operations on the vari-
ables, secure in the knowledge that all of the operations are sensible for that type of
data.
2.1 Using an instance of a class
You declare a variable to be not an int or a double but, perhaps, a "Film":
Film Kids;
There you go, a variable that can contain a film, and the name of the variable is
"Kids".
Just like the declaration
int number;
tells Java that number is an int, but doesn't set any value into it, so the same thing
applies to our Kids variable. Let's put something into our Kids variable, which we'll
do by running a method that can be used for any variable of type Film.
Kids = new Film("Shrek",133);
"Create an object of type Film; the two parameters are ’Shrek’ and ’133’, and store
the Film into the variable called ’Kids’."
There's going to be a certain number of things that we can do on a variable of type
Film,1 and if we want to do those things we need to do them by method calls. For
example:
System.out.println("The kids are watching "+ Kids.getcalled());
will output
The kids are watching Shrek
This evening, the adults are going to watch "Road to Perdition" and the children
will be watching "Shrek". Let's put both films into a class, and see how much earlier
the adults will be through:
1
and we need it defined somewhere, but that's a story later in this module
Film Kids;
Film Adults;
Looks good. We've used two objects of type Film, set them up, called information
back, etc. As programmers of this application, we needed to know what methods we
could call in the Film class, and what the parameters were to the methods, but we
didn't need to know how the class works internally.
Chances are that the Film class is also in use by some other programmers and
applications. Good; there's no point in rewriting code, and there's no point in
everyone having to learn the low-level details about how to handle films. Provided
that the Film class was written and tested well and does what's needed, it's a building
block that has a very wide use.
Some detail of using an object
"new " is a reserved word in Java. When our program says "new Film " it's an
instruction for Java to set up the memory to hold everything about a film, and to pass
back some sort of variable1 through which the information can be later accessed.
The getcalled and getminutes methods are names that just happen to be
defined in class Film as methods that can be called on an object of type Film, and
the syntax for the call is objectname.method(). If the method has any extra
parameters, they'll be passed in within the round brackets as you can see (for example)
in the println calls. println is a method in just the same way that getcalled
is, it's just that println is a part of the standard library and getcalled isn't.
1
sometimes referred to as an instance
String called;
int minutes;
1
getminutes returns an int, setminutes is specified as void to state that it has nothing to return, etc.
Exercise
You're going to be working with vehicles (cars, buses, train carriages etc.). Create a class of type Vehicle with three parameters
– a String for the owner's name, the number of passengers it can take, and the maximum speed it can travel. Also write some get
methods to let you retrieve data.
Write a test program in which you create three Vehicles – a car with a maximum capacity of five and a top speed of 70 m.p.h, a
railway carriage with a maximum capacity of 45 and a top speed of 120 miles per hour, and a bus with a capacity of 51 and a top
speed of 55 m.p.h.
You have 30 people to convey 100 miles. How long is it going to take you to do that with your train carriage, and how long would
it take you with your bus?
An example
We've modified our earlier example to include all the extra facilities that we've
talked about since the last exercise – static methods and variables, this, overloading,
and direct access to variables. Here's the object class (now called "Film2"):
String called;
public int minutes;
static int count = 0;
1
that word "public" that we've been using is more than enough!
Film2 Kids;
Film2 Adults;
}
}
And here's the result from running it:
Exercise
Modify your vehicle class so that you can call a method to ask how many seats (in total) you have created, and test that method
by adding a total seat report to the test harness.
Add in a second constructor that takes no parameters at all; if you call up a vehicle and don't specify anything, it's to be a car
with four seats, top speed 70 m.p.h. and have an "unknown" owner.
Alternative:
Take the example classes Animal and Ourmenag off the course server, compile them, study them, test them, and modify them
as follows:
a) Add in a constructor which takes two int parameters - an age and a convertion factor. Assume a breed of "unknown" if this
constructor is used
b) Define an extra inimal at the end of the zoo array which is 3 years old and has an ageing factor of 5. You do not know its breed.
c) Add a static variable (and method to access it) that keeps count of the number of animals with an effective age of 18 or more
... these are known as "adult animals".
d) Modify the test program to report on the number of adult animals in addition to the existing reports it generates.
License
These notes are distributed under the Well House Consultants
Open Training Notes License. Basically, if you distribute it and use it
for free, we’ll let you have it for free. If you charge for its distribution of
use, we’ll charge.
Please send any amendments and corrections to these notes to the Copyright
holder - under the spirit of the Open Distribution license, we will incorporate suit-
able changes into future releases for the use of the community.
If you are charged for this material, or for presentation of a course (Other than by
Well House Consultants) using this material, please let us know. It is a violation of
the license under which this notes are distributed for such a charge to be made,
except by the Copyright Holder.
If you would like Well House Consultants to use this material to present a training
course for your organisation, or if you wish to attend a public course is one is avail-
able, please contact us or see our web site - http://www.wellho.net - for further
details.
Change log
Original Version, Well House Consultants, 2004
License Ends.