Lecture 7 - Implementing Classes

Lecture 7 - Implementing Classes - Inside the CD class when...

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

View Full Document Right Arrow Icon
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>"; }
Image of page 1

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

View Full Document Right Arrow Icon
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; the body of the method consists of a single line which assigns the value of commentIn to the comment attribute.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern