Homework #2
Due: Wednesday, January 21, 2003 by 5:00 p.m. You may turn your written
assignment during lecture that day, or you may turn it in at the front
desk of the CSE main office in the Allan Center before the office closes
at 5:00 pm. The required Java files should be turned in electronically
by that time. No late assignments will be accepted.
1. Imagine that you are starting to design an Instant Messaging
System that people will use to communicate with their friends on-line.
In order to build the system, you will need objects that represent "buddies",
the people that you want to be able to contact quickly.
a. List three properties that you might want a Buddy object
to have. (There are numerous useful answers for this; just think about
the basic items of information you'd like to have available about a
Buddy when you're on-line.)
b. For each of the three properties, describe the type of the
property that you might use when actually implementing this in Java.
In order to describe the type, you can use one of the standard simple
Java types like int or double, or you can provide a reference type name
(a class name) and describe the components of that type.
c. Give one sample value for each of the three properties described
above.
2. Consider the following set of properties for a Java class that
describes a building. Of the types and classes that we talked about in
lecture, which one would you pick for each property listed? Explain your
selection, including a reason why it is better than the other choices.
Select from int, double, boolean, char, String, or <some new class>.
Note that there are many possible answers for this question.
/**
* Describes a generic building.
*/
public class Building {
/** total floor space in square feet */
__________________floorSpace;
/** number of floors */
__________________floorCount;
/** address of the building */
__________________address;
/** owner of the building */
__________________owner;
/** is the owner an occupant of the building? */
__________________ownerOccupied;
/** number of connected telephone lines */
__________________phoneLineCount;
3. This question requires that you understand how to access the
documentation that is available for the standard Java libraries from Sun
and the special uwcse graphics library. In the navigation bar on the class
web site, there is a link to the "Java Libraries." That page
has links to "Sun's Java standard library documentation" and
"Java documentation for CSE 142/143 classes", specifically the
Graphics library.
a. In the lectures we have mentioned the String class several
times. This class is part of the java.lang package in Sun's Java standard
libraries, and is documented in the Java API documentation. Referring
to the documentation, what is the purpose of the String method "startsWith(String
prefix)"?
b. The documentation for the uwcse class libraries lists all
of the methods available for each class of object defined by the library.
Referring to the documentation for the Graphics library, what is the
purpose of the TextShape method "setFont(java.awt.Font newFont)"?
4. Download the file SkiResort.java.
Your task is to open the project using DrJava and modify the existing
class SkiResort to give it a few more capabilities.
a. Start DrJava. Open the SkiResort source file and compile
it. Create the javadoc documentation for this class by clicking on the
Javadoc button in DrJava. Look at the resulting documentation in your
browser (which should open automatically). According to the documentation,
what is the purpose of the SkiResort method "setTagLine(String
tag)"?
b. Referring to the documentation you created in step a, briefly
describe any three of the properties (the fields) and any three
of the responsibilities (the methods) implemented by the SkiResort
class. (added 1/15/04) Look at the source code and describe
any threeof the properites (the fields) in the SkiResort class.
c. After compiling the source file, create a new object from
the class definition by typing
SkiResort r = new SkiResort("Crystal");
in the DrJava interactions window.
You now have an object that you can refer to by the name r. It has
the properties and responsibilities of a SkiResort.
Ask your SkiResort to perform various responsibilities by typing in
commands in the DrJava interactions window. For example
r.getDepthInches()
r.getTagLine()
r.setTagLine("Fresh snow!")
r.getTagLine()
What is the value returned by getTempF before you have made any calls
to setTempF?
d. Notice that this class uses the US system of measurement
with degrees Fahrenheit and depths in inches. Your task is to implement
the following two methods:
getTempC which returns the temperature converted to the equivalent
degrees centigrade using the formula degC = (5.0/9.0)*(degF-32).
getDepthCentimeters which returns the snow depth converted to the equivalent
centimeters using the formula cm = 2.54*inches.
Add these two methods to the source file SkiResort.java using the DrJava
editor and recompile the class. Type
SkiResort r = new SkiResort("Crystal");
r.setTempF(32)
r.getTempC()
r.setTempF(41)
r.getTempC()
and various other combinations in the DrJava interactions window to
convince yourself that your conversions are working correctly.
Use this online turnin form to turn in your modified SkiResort.java
file when you are done.
|