Review for exam 2

Download Report

Transcript Review for exam 2

Review for exam 2
CS 101
Spring 2005
Aaron Bloomfield
1
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
2
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
3
Tune behaviors
Creating a new Tune object
Accessors
Mutators
Checking if two tunes have the same artist
or are on the same album
4
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
5
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
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();
return thisArtist.equals(thatArtist);
}
Again, we are assuming that the accessors exist
7
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);
t1
t2
Tune
- artist = “Thomas Edison”
- title = “Mary had a little lamb”
- album = “Mary had a little lamb”
- length = 15
+ Tune()
+ Tune (String, String, String, String)
+ boolean sameArtist (Tune that)
+…
“Thomas Edison”
“Mary had a little lamb”
“Mary had a little lamb”
Tune
- artist = “Neil Young”
- title = “I Am the Ocean”
- album = “Mirror Ball”
- length = 15
+ Tune()
+ Tune (String, String, String, String)
+ boolean sameArtist (Tune that)
+…
8
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
9
About the Tune class
We will be using that class on the exam…
10
// 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;
}
11
// 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(
System.out.println(
System.out.println(
System.out.println(
"tune1 = " + tune1 );
"tune2 = " + tune2 );
"artists match: " + tune1.sameArtist(tune2) );
"titles match: " + tune1.sameTitle(tune2) );
}
//...
}
12
Quick survey

a)
b)
c)
d)
I feel I understand the Tune class…
Very well
With some review, I’ll be good
Not really
Not at all
13
Quick survey

a)
b)
c)
d)
How comfortable and prepared do you feel for
the exam?
I’m gonna get a 100!
More or less comfortable
Uncomfortable
Exam? What exam?!?!?
14
Today’s demotivators
15