1 / 15

Review for exam 2

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.

marialong
Download Presentation

Review for exam 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. Tune behaviors • Creating a new Tune object • Accessors • Mutators • Checking if two tunes have the same artist or are on the same album

  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 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

  6. 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

  7. 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

  8. 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

  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

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

  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. // 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. Quick survey • I feel I understand the Tune class… • Very well • With some review, I’ll be good • Not really • Not at all

  14. 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?!?!?

  15. Today’s demotivators

More Related