Java classes
Download
Report
Transcript Java classes
Java Classes
Using Java Classes
Introduction to UML
Java and object-orientation
Java is an object-oriented language
a Java program consists of one or more
classes, each of which contain attributes
and methods
Java programs create and use instances of
these classes called objects
we can use UML (Unified Modelling language)
to design and document our object-oriented
designs
UML class diagrams can easily be translated
into Java code
Why classes?
convenient way
of packaging
together related
data and
functionality
this is called
encapsulation
Example - Spike
Spike's data
colour of body and
features
position (x and y
coordinates)
height
Spike's functionality
construct a new Spike object and initialise its data
draw
move to a given position
get its current position (x or y coordinate)
change its height to a given value
Java classes
in Java we use the following terms:
attributes are usually private
attributes for the data
methods for the functionality
other objects can't read or change them
public methods are used to interact with the object
typically methods could:
read an attribute
set (change) an attribute
do something (eg paint) using attribute values
Why classes?
once we have defined a class
we can create one or more
instances (objects) of that
class
class is the blueprint
object is the actual
instance
each object has its own data
can be different to that of
other objects of the same
class
methods are used to interact
with individual objects
How to create and use objects
declare a variable of the desired class
create objects of that class using the
constructor and the keyword new
Spike spike1;
spike1 = new Spike();
use the name of the object to call its
methods
spike1.moveTo(50, 100);
spike1.drawSpike();
Other classes you’ve seen
Console
System.out
readString(), readInt()…. methods
print() and println() methods
You also used the String class
Take a look at your first Java project again.
UML class diagrams
UML – Universal Modelling
Language
standard notation for
designing and documenting
object-oriented systems
a UML class diagram is a box
with 3 compartments
top: class name
middle: attributes
bottom: methods
Spike
startX
startY
height
width
bodyColour
featuresColour
Spike
drawSpike
moveTo
getX
getY
setHeight
More detailed class diagrams
attribute visibility
+ for public
- for private
type of attribute, return
type of method
after :
type and name of
method parameters
in brackets after method
name
Spike
-startX:int
-startY:int
-height:int
-width:int
-bodyColour:Color
-featuresColour:Color
+Spike(x:int,y:int,h:int,body:Color)
+drawSpike(g:Graphics):void
+moveTo(x:int,y:int):void
+getX():int
+getY():int
+setHeight(newHeight:int):int
Using classes
very often in Object-Oriented programming we
will use classes written by someone else
(hopefully) these classes will be well designed,
tested and documented
we can use them with confidence
this is called software reuse
part of Java language
in libraries, like Console
written by our colleagues (Spike)
allows rapid building of robust systems
now we will go through another example of
using a library (someone else's) class
Cloneab le
Day
Day
-day:int
-month:int
-year:int
+Day()
+Day(yyyy:int,m:int,d:int)
+advance(n:int):void
+getDay():int
+getMonth():int
+getYear():int
+weekday():int
+daysBetween(b:Day):int
+toString():String
+clone():Object
-isValid():boolean
-toJulian():int
-fromJulian(j:int):void
part of corejava library
encapsulates all information
about a date
private attributes:
day
month
year
all integers
why not have a dayOfWeek
attribute?
Cloneab le
Day methods
Day
-day:int
-month:int
-year:int
+Day()
+Day(yyyy:int,m:int,d:int)
+advance(n:int):void
+getDay():int
+getMonth():int
+getYear():int
+weekday():int
+daysBetween(b:Day):int
+toString():String
+clone():Object
-isValid():boolean
-toJulian():int
-fromJulian(j:int):void
what do you think the other
methods do?
hint: it is good programming
practice to give methods
meaningful names!
can refer to documentation
for more information
hopefully the class developer
wrote meaningful comments!!
Constructors
all classes have a constructor method that is called
when an object is first created
all constructors must have the same name as the
class and are public
constructors never have a return type
Java provides a default constructor
creates space in memory for the object and all its
attributes
classes can provide their own version(s) to do any
initialisation steps required
which methods are the Day constructors and what do
you think they do?
Cloneab le
Day class
Day
-day:int
-month:int
-year:int
+Day()
+Day(yyyy:int,m:int,d:int)
+advance(n:int):void
+getDay():int
+getMonth():int
+getYear():int
+weekday():int
+daysBetween(b:Day):int
+toString():String
+clone():Object
-isValid():boolean
-toJulian():int
-fromJulian(j:int):void
two constructor methods:
public Day()
creates a Day object with today’s
date
public Day(int yyyy, int m, int d)
creates a Day object with given date
get- methods
often need a method to return the value of a
private attribute
conventional to use a get- method
way of reading the attribute without changing it
method getX( ) returns the attribute X
public so other classes can use it
return type is the same type as the attribute
Day has three get- methods
what do you think they do?
set- methods
set- methods are used to change the value of
given attribute
method setX(type a) sets the value of attribute
X to the new value a
public so other classes can use it
parameter type is the same type as the attribute
give write- access to a private attribute
return type is usually void
Day does not have any set- methods
why do you think this is?
toString() method
all Java classes have a default toString()
method
returns a String containing information about
the object
the default is
name of class @ hash code of the object
for example Day@1a16869
many classes override this method to return
something more meaningful
in this case Day[2004,9,22]
name of class, then year, month, day in square
brackets
Cloneab le
Day
Day
-day:int
-month:int
-year:int
+Day()
+Day(yyyy:int,m:int,d:int)
+advance(n:int):void
+getDay():int
+getMonth():int
+getYear():int
+weekday():int
+daysBetween(b:Day):int
+toString():String
+clone():Object
-isValid():boolean
-toJulian():int
-fromJulian(j:int):void
other methods of interest:
advance(int n) advances the date
by n days
int weekday()
not as easy as it looks – have to
consider end of months and years
returns the day of the week
0 for Sunday to 6 for Saturday
int daysBetween(Day b)
calculates and returns the number of
days between 2 Day objects
Information hiding
the logic within these methods is quite
complicated
fortunately we don't need to know how they
are implemented
as long as we know what they do, we can
confidently use them without knowing how
they do it
in OO terms this is called information
hiding
Using the Day class
we'd now like to write a program that uses
some of the features of the Day class
before we write our program we need to
think about what it will do
Scenario - a small application program is
required which can
ask the user for a number and tell them what date
it will be in that many day's time
How can we do this?
could create a Day object
what date should we represent?
today's date
use default constructor
how can we get input from the user?
it encapsulates all the information about a date
use Console.readInt() method
how can we advance the date?
use the Day object's advance() method
luckily we don't need to write it ourselves!!!
How can we do this?
how can we output the results?
use the Day object's toString() method, and print the
returned String to the console
how should we structure the program?
put code in one class
DayInterface.java
small, so put all code in main() method
import corejava.* to access Console and Day
classes
DayInterface.java
import corejava.*;
public class DayInterface
{
public static void main(String[] args)
{
Day theDate;
int amount;
theDate = new Day();
amount = Console.readInt("How many days from now? ");
theDate.advance(amount);
System.out.println(amount + " days from today is " +
theDate.toString());
}
}
Output
How many days from now? 250
250 days from today is Day[2005,7,26]
Summary
have looked at classes
why we use them - encapsulation
what they are made of (attributes and methods)
how we can document them with UML class diagrams
how we can use library classes in our own programs
information hiding – we don't need to know how they work!
tutorial
design some class diagrams
write some more applications using other people's
classes