




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





1
2
Uris Hall G
3
exams serve two purposes:
4
lecture
5
instanceof , func8on equals
6
instanceof , func8on equals
I’m gonna assume
you can do this with
your eyes closed by
now
7
instanceof , func8on equals
8
(Fall’07) Question 1 (15 points). Write the body of the
following function recursively.
/** = n, but with its digits reversed.
Precondition: n >= 0.
e.g. n = 135720, value is "027531".
e.g. n = 12345, value is "54321".
e.g. n = 7, value is "7".
e.g. n = 0, value is "0".*/
public static String rev(int n) {
9
10
/** = n, but with its digits reversed.
Precondition: n >= 0.
e.g. n = 135720, value is "027531".
e.g. n = 12345, value is "54321".
e.g. n = 7, value is "7".
e.g. n = 0, value is "0".*/
public static String rev(int n) {
// base case:
//{n has only one digit}
// recursive case:
// {n has at least two digits}
11
12
instanceof , func8on equals
19
20
21
public class Movie {
private String title; // title of movie
private int length; // length in minutes
/** Constructor: document with title t
and len minutes long */
public Movie(String t, int len) {
title= t; length= len;
}
/** = title of this Movie */
public String getTitle()
{ return title; }
/** = length of document, in minutes */
public int getLength()
{ return length; }
/** = the popularity:
shorter means more popular */
public int popularity()
{ return 240 – length; }
}
public class Trailer extends Movie {
/** Constructor: a trailer of movie t.
Trailers are 1 minute long*/
public Trailer(String t)
{super(t, 1);}
}
public class Documentary extends Movie {
private String topic; // …
/** Constructor: instance with title t,
length n, and topic p */
public Documentary(String t, int n,
String p) {
super(t, n);
topic= p;
}
/** = "Documentary" */
public String DocumentaryType()
{ return "Documentary"; }
/** = popularity of this instance */
public int popularity()
{ return 200 - getLength(); }
}
public class Short extends Documentary {
/** Constructor: instance with title t,
length n, and topic p */
public Short(String t, int n, String p)
{ super(t, n, p); }
/** displays acknowledgement */
public String showAck()
{return "We thank our director“;}
/** = "Short Doc" */
public String DocumentaryType()
{ return "Short Doc"; }
}
22
(Fall’05) Ques7on 4 (30 points) For each pair of statements below,
write the value of d aier execu8on. If the statements lead to an error,
write “BAD” and briefly explain the error. (The ques8on con8nues on
the next page.)
Documentary e=
new Short("Man on Wire”, 5, "Bio");
boolean d=
"Short Doc” .equals(e.DocumentaryType());
23
(Fall’05) Ques7on 4 (30 points) For each pair of statements below,
write the value of d aier execu8on. If the statements lead to an error,
write “BAD” and briefly explain the error. (The ques8on con8nues on
the next page.)
Documentary e=
new Short("Man on Wire”, 5, "Bio");
boolean d=
"Short Doc” .equals(e.DocumentaryType());
24
True. method equals here is from the string object
Movie c=
new Documentary(null, 3, "Carter Peace Center");
int d= c.popularity();
25
public class Movie {
private String title; // title of movie
private int length; // length in minutes
/** Constructor: document with title t
and len minutes long */
public Movie(String t, int len) {
title= t; length= len;
}
/** = title of this Movie */
public String getTitle()
{ return title; }
/** = length of document, in minutes */
public int getLength()
{ return length; }
/** = the popularity:
shorter means more popular */
public int popularity()
{ return 240 – length; }
}
public class Trailer extends Movie {
/** Constructor: a trailer of movie t.
Trailers are 1 minute long*/
public Trailer(String t)
{super(t, 1);}
}
public class Documentary extends Movie {
private String topic; // …
/** Constructor: instance with title t,
length n, and topic p */
public Documentary(String t, int n,
String p) {
super(t, n);
topic= p;
}
/** = "Documentary" */
public String DocumentaryType()
{ return "Documentary"; }
/** = popularity of this instance */
public int popularity()
{ return 200 - getLength(); }
}
public class Short extends Documentary {
/** Constructor: instance with title t,
length n, and topic p */
public Short(String t, int n, String p)
{ super(t, n, p); }
/** displays acknowledgement */
public String showAck()
{return "We thank our director“;}
/** = "Short Doc" */
public String DocumentaryType()
{ return "Short Doc"; }
}
26
27
a
Animal
Cat Cat(String, int)
getNoise()
toString()
getWeight()
age
Animal(String, int)
isOlder(Animal)
QUESTION: Which method is called by
Animal t= new Cat(“A”,5); t.toString()?
A. the one in the hidden partition for
Object of a
B. the one in partition Animal of a
C. the one in partition Cat of a
D. None of these
Object
Animal
Cat
the class hierarchy:
Movie c=
new Documentary(null, 3, "Carter Peace Center");
int d= c.popularity();
class Documentary is called
Movie
Documentary Trailer
Short
28
Short b= (Short)(new Documentary("", 2, "WMD"));
int d= b.DocumentaryType().length();
29
Short b= (Short)(new Documentary("", 2, "WMD"));
int d= b.DocumentaryType().length();
Movie
Documentary Trailer
Short
to documentary.
30
(Fall’05) Ques7on 4 (24 points). (a) Write an instance method
equals(Object obj) for class Documentary
public class Documentary extends Movie {
/** = "obj is a Documentary with the same values
in its fields as this Documentary" */
public boolean equals(Object obj) {
37
public class Documentary extends Movie {
/** = "obj is a Documentary with the same values
in its fields as this Documentary" */
public boolean equals(Object obj) {
38
public class Documentary extends Movie {
/** = "obj is a Documentary with the same values
in its fields as this Documentary" */
public boolean equals(Object obj) {
return false;
39
public class Documentary extends Movie {
/** = "obj is a Documentary with the same values
in its fields as this Documentary" */
public boolean equals(Object obj) {
return false;
40
Don’t forget to cast.
This is a legal cast. (Why?)
public class Documentary extends Movie {
/** = "obj is a Documentary with the same values
in its fields as this Documentary" */
public boolean equals(Object obj) {
if (!(obj instanceof Documentary) {
return false;
Documentary docObj= (Documentary)obj;
return
getTitle().equals(docObj.getTitle()) &&
getLength() == docObj.getLength() &&
topic.equals(docObj.topic);
41
instanceof , func8on equals
42
/** representation of an animal */
public class Animal {
private int birthDate; // animal’s birth date
private String predator; // predator of this animal
private String prey; // class of animals this hunts
…
// move the animal to direction…
public void move(…){
…
}
// make the animal eat…
public void eat (…){
…
}
…
}
43
44
45
/** representation of an animal */
public abstract class Animal{
private int birthDate; // birth date
private String predator; // animal’s predator
private String prey; // What animal hunts
// Move the animal move in direction …
public abstract void move(…);
// Make the animal eat…
public abstract void eat (…);
46
47