You are on page 1of 1

Answers 15-4

.H\WR([HUFLVHRQ/HVVRQ
1. double length = 44.0;
int width =13;
Rectangle myRect = new Rectangle(length, width);
a. Identify the class 5HFWDQJOH
b. Identify the object P\5HFW
c. What type of parameter(s) are passed to the constructor? GRXEOHLQW


2. Write out the signature for the constructor of the 5HFWDQJOH class from #1 above.
SXEOLF5HFWDQJOH GRXEOH[LQW\ 8VHDQ\QDPHV\RXOLNHLQWKHSODFHRI[DQG\

3. Suppose a constructor for the /XQFK class is as follows:


public Lunch(boolean diet, int cal)
{
diet_yes_no = diet;
calories = cal;
}

Write appropriate code that will create a /XQFK object called \XPP\. You should
tell the constructor that, yes, you are on a diet, and the number of calories should
be 900.
/XQFK\XPP\ QHZ/XQFK WUXH 

4. BankAccount account39 = new BankAccount(500.43);


a. Identify the class %DQN$FFRXQW
b. Identify the object DFFRXQW
c. What type of parameter(s) are passed to the constructor? GRXEOH


5. A class is like a FRRNLHFXWWHU. An object is like a FRRNLH.


Fill in the blanks above using the word “cookie” and “cookie cutter”.

6. What’s wrong (if anything) with the following constructor for the 6FKRRO class?
public void school(int d, String m)
{ … some code … }
YRLGVKRXOGEHRPLWWHGDQGVFKRROVKRXOGKDYHDFDSLWDO6

7. Which of the following is a correct association?


D 2QHFODVVPDQ\REMHFWV
b. One object, many classes

8. Which must exist first?


D7KH FODVV
b. The object

9. Is the following legal? If not, why? //This code is in PDLQ of 7HVWHU class
//Constructor int p = 3, q = 9;
public House(int j, boolean k) House myHouse = new House(p, q);
{ …some code… } 1RWKHVHFRQGSDUDPHWHUTVKRXOGEHDERROHDQ

You might also like