Download presentation
Presentation is loading. Please wait.
Published byPrudence Cole Modified over 9 years ago
1
Java Classes Using Java Classes Introduction to UML
2
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
3
Why classes? convenient way of packaging together related data and functionality this is called encapsulation Example - Spike
4
Spike's data colour of body and features position (x and y coordinates) height
5
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
6
Java classes in Java we use the following terms: attributes for the data methods for the functionality attributes are usually private 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
7
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
8
How to create and use objects declare a variable of the desired class Spike spike1; create objects of that class using the constructor and the keyword new spike1 = new Spike(); use the name of the object to call its methods spike1.moveTo(50, 100); spike1.drawSpike();
9
Other classes you’ve seen Console readString(), readInt()…. methods System.out print() and println() methods You also used the String class Take a look at your first Java project again.
10
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
11
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
12
Using classes very often in Object-Oriented programming we will use classes written by someone else part of Java language in libraries, like Console written by our colleagues (Spike) (hopefully) these classes will be well designed, tested and documented we can use them with confidence this is called software reuse allows rapid building of robust systems now we will go through another example of using a library (someone else's) class
13
Day part of corejava library encapsulates all information about a date private attributes: day month year all integers why not have a dayOfWeek attribute?
14
Day methods 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!!
15
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?
16
Day class 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
17
get- methods often need a method to return the value of a private attribute way of reading the attribute without changing it conventional to use a get- method 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?
18
set- methods set- methods are used to change the value of given attribute give write- access to a private 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 return type is usually void Day does not have any set- methods why do you think this is?
19
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
20
Day other methods of interest: advance(int n) advances the date by n days not as easy as it looks – have to consider end of months and years int weekday() 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
21
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
22
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
23
How can we do this? could create a Day object it encapsulates all the information about a date what date should we represent? today's date use default constructor how can we get input from the user? 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!!!
24
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
25
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()); }
26
Output How many days from now? 250 250 days from today is Day[2005,7,26]
27
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.