Homework 2
Date Due: Sunday, September 25th, 2:00 pm
To install: cs015_install hw2
To hand in: cs015_handin hw2
New Concepts Covered:
● Reassigning variables
● Instance variables vs. local variables
● Association vs. containment
● Packages
● Interfaces
● Flow of Control
Collaboration Policy Reminder
Per the collaboration policy:
You may discuss this homework with other students, but cannot take notes away from
these discussions and must write up solutions completely individually.
Getting Started
Install by typing cs015_install hw2 into the Terminal. This will create an empty directory in
/home//course/cs015/hw2/ . This is where you’ll need to put the PDF with
your answers. Please do not include any identifying information, such as name or login, on your
PDF, as we grade anonymously.
CS15 Initial Student Questionnaire
Please take a few minutes to fill out our initial student survey at:
https://docs.google.com/forms/d/e/1FAIpQLScWUWb7es_i3_8ioUa7q2Nl0kTRS9JM5WwGTtxq
CX1t8t2fg/viewform. This information will help us tailor the course to your needs.
HW2, 1
Assignment Specifications:
Please answer the following questions:
Question 1
For the following questions, refer to the following code. Please note that these variable names
(a, b, c, _a, _b ) are purely for the purpose of this particular question, and do not reflect
our general style guidelines.
public class IntegerHolder {
private int _a;
private int _b;
public IntegerHolder() {
int a = 1;
int b = 2;
int c = 3;
_a = a;
_b = b;
b = c;
a = b;
c = a;
System.out.println(“Printline 1:”);
System.out.println(a + b);
System.out.println(“Printline 2:”);
System.out.println(b – c);
System.out.println(“Printline 3:”);
System.out.println(_a);
System.out.println(“Printline 4:”);
System.out.println(_b);
this.update();
}
public void update() {
int temp = _b;
_b = _a;
_a = temp;
}
}
HW2, 2
a. What is the difference between an instance variable and a local variable? Give an
example of each type from IntegerHolder .
b. What will be printed after “Printline 1: ”? Try walking through each line of code from
the start and determining what the values of each variable are at each line.
c. What will be printed after “Printline 2: ”?
d. What will be printed after “Printline 3: ”?
e. What will be printed after “Printline 4: ”?
f. What are the final values of _a and _b ?
Question 2
In the CasinoGame class that follows, which instance variables are referencing an object that is
contained within CasinoGame , and which are referencing objects that are associated with
CasinoGame ?
public class CasinoGame {
private Table _table;
private Dealer _dealer;
private Deck _deck;
private Player _one;
private Player _two;
public CasinoGame(Player one, Player two) {
_table = new Table();
_dealer = new Dealer();
_deck = new Deck();
_one = one;
_two = two;
}
/* other methods elided */
}
Question 3
The following questions have to do with packages and imports. (Note that you will need to have
read through Lab 1 in order to complete this question.)
a. What is a Java package?
b. What is the notation for importing an entire package? In what situations should
you import an entire package?
c. Explain an advantage of importing a class rather than explicitly referencing it
with its path name.
HW2, 3
Question 4
Refer to the following code to answer this question. Note: the dashed lines demarcate separate
classes and interfaces.
public interface Cookable {
public void cook();
}
public class Ham implements Cookable {
public Ham() {
/* implementation elided */
}
@Override
public void cook() {
this.season();
this.bake();
}
public void season() {
/* implementation elided */
}
public void bake() {
/* implementation elided */
}
}
public class GreenEggs implements Cookable {
public GreenEggs() {
/* implementation elided */
}
@Override
public void cook() {
this.fry();
}
HW2, 4
public void fry(){
/* implementation elided */
}
}
public class Seuss {
/* constructor elided */
public void makeGreenEggsAndHam() {
GreenEggs greenEggs = new GreenEggs();
Ham ham = new Ham();
ham.cook();
greenEggs.cook();
}
public void experimentWithFood(Cookable cookable) {
cookable.bake();
cookable.fry();
cookable.season();
}
}
public class Fries {
/*constructor elided */
public void cook() {
this.fry();
}
public void fry() {
/*implementation elided*/
}
}
a. Examine the makeGreenEggsAndHam() method in the Seuss class. Will it work? If
not, explain why. If so, explain the difference between Ham and GreenEggs ’ cook
methods.
b. Examine the experimentWithFood() method in the Seuss class. Will it work?
HW2, 5
Explain why or why not.
c. i) Suppose the makeGreenEggsAndHam() method was modified as shown below.
Does this definition differ from the original definition in terms of functionality (does it do
anything different)? If so, how? What lines of code would need to be added or changed
to make the method below work like the original?
public void makeGreenEggsAndHam() {
GreenEggs greenEggs = new GreenEggs();
Ham ham = new Ham();
ham.bake();
greenEggs.fry();
}
ii) Suppose the declared types of both greenEggs and ham were changed to
Cookable . Would the code compile if the original makeGreenEggsAndHam() method
was used? Would the code compile if the above makeGreenEggsAndHam() method
was used? Explain your answers briefly.
d. Suppose the method below is written in the Seuss class. Will it work if an instance of
GreenEggs is passed through? Will it work if an instance of Ham is passed through?
Will it work if an instance of Fries is passed through? Explain your answers.
public void makeGourmetMeal(Cookable cookable) {
cookable.cook();
}
Question 5
Refer to the following code to answer the question. Write the text output that would be
printed to the terminal when the program is run, in the order that it would be printed.
Assume all the musicians’ methods are written and working, and that any elided implementation
details will not cause additional printed lines. Note the dashed lines demarcate separate
classes.
public class App {
public static void main (String[] args) {
Band theSeusses = new Band();
theSeusses.tune();
theSeusses.jam();
}
}
public class Band {
HW2, 6
private Guitarist _guitarist;
private Drummer _drummer;
private Bassist _bassist;
public Band() {
System.out.println(“Introducing The Seusses!”);
_guitarist = new Guitarist();
_drummer = new Drummer();
_bassist = new Bassist();
}
public void tune() {
System.out.println(“The Seusses are tuning!”);
_drummer.lookingForSticks();
_guitarist.lookingForPick();
_bassist.actuallyTuning();
System.out.println(“The Seusses are ready to rock!”);
}
public void jam() {
System.out.println(“The Seusses are jamming!”);
_drummer.playBeat();
_guitarist.playRiff();
_bassist.slapDatBass();
}
}
public class Guitarist {
public Guitarist() {
/* implementation elided */
}
public void playRiff() {
System.out.println(“Playing riff!”);
/* implementation elided */
}
public void lookingForPick() {
System.out.println(“Looking for pick!”);
/* implementation elided */
HW2, 7
}
}
public class Drummer {
public Drummer() {
/* implementation elided */
}
public void playBeat() {
System.out.println(“Playing beats!”);
/* implementation elided */
}
public void lookingForSticks() {
System.out.println(“Looking for sticks!”);
/* implementation elided */
}
}
public class Bassist {
public Bassist() {
/* implementation elided */
}
public void slapDatBass() {
System.out.println(“Slapping dat bass!”);
/* implementation elided */
}
public void actuallyTuning() {
System.out.println(“Actually tuning!”);
/* implementation elided */
}
}
HW2, 8
Handin Information
This assignment must be submitted no later than 2:00 pm on Sunday, September 25th. There
is no late handin for this assignment. Please remove any identifying information from your
handin. Remember to create a single PDF with your answers and place it in the correct directory
(/home//course/cs015/hw2), and when your PDF is in the right directory, run
cs015_handin hw2 in a terminal to submit. When you have successfully handed in the
assignment, a confirmation email will be sent to your CS department account
(@cs.brown.edu).
You must submit this electronically through the handin script! If you do not handin via
cs015_handin and instead email your handin to the HTAs (before the handin deadline), you
will lose 25% of the total available points. Emailed handins must be in a PDF file format to
receive any credit.
HW2, 9