Lecture 7 - Implementing Classes

Lecture 7 - Implementing Classes - Lecture 7 Implementing...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 7 Implementing classes Inside the CD class when we design an instantiable class we must consider: what data the class needs to hold (the attributes); what operations (methods) are needed to access that data. the CD class will need to hold six items of data: the title (stored as a String); the artist (stored as a String); number of tracks (stored as an integer); playing time (stored as an integer); flag to indicate whether owned (stored as boolean); a comment (stored as a String). the methods are shown in following table Methods of the CD class UML Class Diagram for CD a class is represented by a box divided into three sections. name operations (methods) attributes Analysis of the CD class a CD object will need attributes to hold values for the title, artist, number of tracks, playing time. Additional attributes are required to indicate ownership and store an arbitrary comment the attributes of a class are accessible to all the methods - unlike local variables, which are accessible only to the methods in which they are declared. once the attributes are declared as private (as opposed to public ), they cannot be accessed by methods of other classes ; private String title; private String artist; private int numberOfTracks; private int playingTime; private boolean gotIt; private String comment; The constructor the method is declared as public so that they can be called by methods of other classes; the above constructor is a user-defined constructor; we are defining it so that when a new CD object is created (with the keyword new ) it does more than simply space reserved in memory; public CD(String theTitle, String theArtist, int tracks, int time) { title = theTitle; artist = theArtist; numberOfTracks = tracks; playingTime = time; gotIt = false; comment = "<no comment>"; } The constructor in this case 4 assignment statements are executed: the first assigns the value of the parameter theTitle to the title attribute; the second assigns the value of the parameter theArtist to the artist attribute; the third assigns the value of the parameter tracks to the numberOfTracks attribute, etc. the gotIt and comment attributes are initialised when the CD object is instantiated. Note if you don't define your own constructor then a "default" constructor is provided; this does nothing more than reserve space in memory for the new object; it is called by using the constructor name (which is the same as the class name) with empty brackets; once we have defined our own constructors, this default constructor is no longer automatically available; if we want it to be available then we have to re-define it explicitly: //an empty constructor public CD() { } The setComment and setOwn methods this method does not return a value, so its return type is void ; however, it does require a parameter of type String that it will assign to the comment attribute;...
View Full Document

Page1 / 7

Lecture 7 - Implementing Classes - Lecture 7 Implementing...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online