150 likes | 164 Views
Review for exam 2. CS 101 Spring 2005 Aaron Bloomfield. What’s on the exam. Creating class (chapter 4) Examples we’ve seen ColoredRectangle Car Rational Circle Decisions (chapter 5) If, if-else, if-else-if Switch Iteration (chapter 6) You just have to be able to analyze while loops.
E N D
Review for exam 2 CS 101 Spring 2005 Aaron Bloomfield
What’s on the exam • Creating class (chapter 4) • Examples we’ve seen • ColoredRectangle • Car • Rational • Circle • Decisions (chapter 5) • If, if-else, if-else-if • Switch • Iteration (chapter 6) • You just have to be able to analyze while loops
The Tune class • Used to represent a song from a CD collection • Properties: • artist • title • album • length • These will be implemented as instance variables • private String artist • private String title • private String album • private int length
Tune behaviors • Creating a new Tune object • Accessors • Mutators • Checking if two tunes have the same artist or are on the same album
Exercise 1: Default constructor • The default constructor initializes the Tune to “Mary Had a Little Lamb” • 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
Exercise 1: Method sameArtist() • This method will compare two objects to see if they have the same artist • 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
Exercise 1: Method sameArtist() • This method will compare two objects to see if they have the same artist • 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
t1 t2 Tune Tune “Neil Young” “Thomas Edison” - artist = - title = - album = - length = - artist = - title = - album = - length = “I Am the Ocean” “Mary had a little lamb” “Mirror Ball” “Mary had a little lamb” + Tune() + Tune (String, String, String, String) + boolean sameArtist (Tune that) + … + Tune() + Tune (String, String, String, String) + boolean sameArtist (Tune that) + … Exercise 2: Memory diagram • Give a memory diagram for the following: Tune t1 = new Tune(); Tune t2 new Tune( "Neil Young", "I Am the Ocean". "Mirror Ball", 428); “Thomas Edison” “Mary had a little lamb” “Mary had a little lamb” 15 “Neil Young” “I Am the Ocean” “Mirror Ball” 15
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
About the Tune class • We will be using that class on the exam…
// 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; }
// 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) ); } //... }
Quick survey • I feel I understand the Tune class… • Very well • With some review, I’ll be good • Not really • Not at all
Quick survey • How comfortable and prepared do you feel for the exam? • I’m gonna get a 100! • More or less comfortable • Uncomfortable • Exam? What exam?!?!?