Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Review for exam 2 CS 101 Spring 2005 Aaron Bloomfield.

Similar presentations


Presentation on theme: "1 Review for exam 2 CS 101 Spring 2005 Aaron Bloomfield."— Presentation transcript:

1 1 Review for exam 2 CS 101 Spring 2005 Aaron Bloomfield

2 2 What’s on the exam Creating class (chapter 4) Examples we’ve seen Examples we’ve seenColoredRectangleCarRationalCircle Decisions (chapter 5) If, if-else, if-else-if If, if-else, if-else-if Switch Switch Iteration (chapter 6) You just have to be able to analyze while loops You just have to be able to analyze while loops

3 3 The Tune class Used to represent a song from a CD collection Properties: artist artist title title album album length length These will be implemented as instance variables private String artist private String artist private String title private String title private String album private String album private int length private int length

4 4 Tune behaviors Creating a new Tune object AccessorsMutators Checking if two tunes have the same artist or are on the same album

5 5 Exercise 1: Default constructor The default constructor initializes the Tune to “Mary Had a Little Lamb” That’s the first audio track ever recorded That’s the first audio track ever recorded public Tune () { setArtist (“Thomas Edison”); setTitle (“Mary Had a Little Lamb”); setAlbum (“Mary Had a Little Lamb”); setLength (15); } Note we haven’t declared the mutators yet! But we assume they are there and that they exist But we assume they are there and that they exist

6 6 Exercise 1: Method sameArtist() This method will compare two objects to see if they have the same artist The two objects are: The two objects are: The current object that the method is executing from The object passed in as a parameter public boolean sameArtist (Tune that) { String thisArtist = getArtist(); String thatArtist = that.getArtist(); if ( thisArtist.equals(thatArtist) ) { return true; } else { return false; }} Again, we are assuming that the accessors exist

7 7 Exercise 1: Method sameArtist() This method will compare two objects to see if they have the same artist The two objects are: The two objects are: The current object that the method is executing from The object passed in as a parameter public boolean sameArtist (Tune that) { String thisArtist = getArtist(); String thatArtist = that.getArtist(); return thisArtist.equals(thatArtist); } Again, we are assuming that the accessors exist

8 8 Give a memory diagram for the following: Tune t1 = new Tune(); Tune t2 new Tune( "Neil Young", "I Am the Ocean". "Mirror Ball", 428); Exercise 2: Memory diagram t1 - artist = - title = - album = - length = Tune + Tune() + Tune (String, String, String, String) + boolean sameArtist (Tune that) + … “Thomas Edison” “Mary had a little lamb” 15 “Thomas Edison” “Mary had a little lamb” t2 - artist = - title = - album = - length = Tune + Tune() + Tune (String, String, String, String) + boolean sameArtist (Tune that) + … “Neil Young” “I Am the Ocean” “Mirror Ball” 15 “Neil Young” “I Am the Ocean” “Mirror Ball”

9 9 Exercise 3: Memory diagram Update your memory diagram from the second exercise so that it also include the initial activation record depiction of sameArtist() for the following invocation t1.sameArtist(t2) We’ll not be going over this question As we haven’t studied activation records As we haven’t studied activation records

10 10 About the Tune class We will be using that class on the exam…

11 11 // Representation of a musical track public class Tune { // instance variables for attributes private String artist; // Performer of the piece private String title; // Name of the track private int year; // Release year // default constructor public Tune() { setArtist("Thomas Alva Edison"); setTitle("Mary had a little lamb"); setYear(1877);} // mutators public void setArtist(String performer) { artist = performer; } public void setTitle(String track) { title = track; } public void setYear(int date) { year = date; }

12 12 // stringifier public String toString() { String performer = getArtist(); String track = getTitle(); int date = getYear(); String result = "Tune( " + performer + ", " + track + ", " + date + " )"; return result; } // demo public static void main(String[] args) { Tune tune1 = new Tune(); Tune tune2 = new Tune(); tune2.setArtist("Paul McCartney"); tune2.setYear(1972); System.out.println( "tune1 = " + tune1 ); System.out.println( "tune2 = " + tune2 ); System.out.println( "artists match: " + tune1.sameArtist(tune2) ); System.out.println( "titles match: " + tune1.sameTitle(tune2) ); }//...}

13 13 Quick survey I feel I understand the Tune class… I feel I understand the Tune class… a) Very well b) With some review, I’ll be good c) Not really d) Not at all

14 14 Quick survey How comfortable and prepared do you feel for the exam? How comfortable and prepared do you feel for the exam? a) I’m gonna get a 100! b) More or less comfortable c) Uncomfortable d) Exam? What exam?!?!?

15 15 Today’s demotivators


Download ppt "1 Review for exam 2 CS 101 Spring 2005 Aaron Bloomfield."

Similar presentations


Ads by Google