CSE 142 Homework 2
April 15, 2003
Purpose: This homework has three
parts, with the following goals.
ท
The goal of the first part is to practice your modeling
and design skills.
ท
The second and third part should improve your
programming skills, including loops.
Turn-in and due date: The
due date is Tuesday April 22 at 9 pm for electronic turn-ins, and Wednesday
April 23 in lecture for paper copy turn-in. More below.
Time: Parts 1 and 2 should take 1-2 hours. Part 3 will
likely take several hours. Make sure you manage your time efficiently.
1
Modeling
Purpose: To practice your modeling skills.
Procedure: You need to create a high-level
design for a mobile phone. In the earlier media player assignment you were free
to express your design any way you wanted to. However, this time you should
closely follow the format of the sample
media player design provided as a solution to homework 1.
Turn-in: Please turn in a single document
using the web
turn-in form. The due date is Tuesday
April 22 at 9 pm. The best way to ensure that your TA can read your
document is to use a recent version of word, or a plain text editor.
Hints:
- Quality
is much more important than quantity. Be brief, yet concise.
- Dont
spend more than 1-2 hours on this; it is not intended to be tricky, just
as an exercise in thinking about what classes and objects a program might contain,
and their properties, responsibilities and interactions.
- The
design part of homework 1 was very hard because it was so open-ended.
Those of you who put effort into it have learned a lot about how difficult
it is to express a design well. This time your creativity in expressing
the design is very limited you have to follow our rules. Instead you
get a chance to concentrate on creating the design.
- When
grading, the TAs will take into account how well the format of your
solution conforms to the sample media player design.
2 Calculator
Extensions
Purpose: Practicing your programming skills
and introducing loops.
Procedure: Using either your own calculator
or the sample solution code for homework 1 (Calculator.java,
CalculatorTestHarness.java),
add the following functionality:
- A
function square to compute the square of a number.
- A
function cube to compute the cube of a number.
- A
function inverse to compute the inverse of a number given an
integer n, compute 1/n.
- A
function pi that returns the constant p.
- A
function exp to compute the exponential function over the integer
domain given an integer n, compute en.
- A
function factorial that computes the factorial of a number given
an integer n, compute n! = 1ื2ื3ื
ืืื
ืn.
The factorial of a negative number is not defined. The factorial of 0
is 0! = 1.
- A
function power to compute the n-th power of a number given a
integer n and a power m, compute nm.
You also need to modify the test harness provided with
homework 1 to test the new calculator functionality.
Turn-in: Turn in the new versions of Calculator.java
and CalculatorTestHarness.java using
the web form, before 9 pm Tuesday April 22. Also hand in the Turin-in
Receipt at the beginning of lecture Wednesday April 23.
Hints:
- You
need to use conditionals in the right places, to deal with negative, zero
or positive parameter values.
- You
need to use loops in two places.
-
You may have discovered that Java
has a Math library. You may not use that library for this
assignment.
3 The
Paper Street Soap Company
Purpose: Practice using the code constructs
you have learned about so far, particularly conditional statements.
Problem: The Paper Street Soap Company
sells handmade bars of soap, which are distributed in sheets of recycled paper.
- Both
the soap and the sheets of paper are of unpredictable sizes.
- The
soap-wrapping machine takes an unwrapped bar of finished soap, and the
first available piece of paper, and determines if the bar can be wrapped
with that paper.
- If
so, the paper is cut into two parts, one of them just large enough for
wrapping the bar; the wrapping is done, the bar is marked
"wrapped", and the left-over paper is available to try with
another bar.
Your eventual assignment is to write the software that controls the
operation of the soap-wrapping machine. Only a part of the code is
due on April 22. The remainder will be due the following week, as a part
of Homework 3.
Turn-in: Turn in Paper.java,
Soap.java and SoapPaperHarness.java using
the web turn-in form, before 9 pm Tuesday April 22. Also turn in the
Receipt at the beginning of lecture Wednesday April 23.
More Information:
-
The eventual solutions will have three classes Soap, Paper, and Wrapper, plus
test harnesses.
- The
soap class has three properties corresponding to the three dimensions of a
bar of soap, and a fourth property recording its wrapped/non-wrapped
status.
- The
paper class only has two properties corresponding to the two dimensions.
-
The paper class is also responsible for cutting a paper object by changing the
dimension properties and accounting for the remainder.
- The
wrapper class has one responsibility, which is to take a bar of soap and a
sheet of paper and attempt to wrap.
- The
wrapper is not supposed to work like an assembly line! It only takes one
bar of soap and one sheet of paper, attempts to wrap, and then completes.
To wrap another bar the wrapping method needs to be invoked again.
- Much
conditional fun here.
For the First Week's Assignment:
- Download the starter classes for
Paper.java and Soap.java. They
contain much information in the form of comments. Implement the
constructors and the "getter" methods. Download
Wrapper.java and study it, but don't
implement yet.
- Write a PaperSoapHarness.java class, which is designed to test thoroughly
the methods of Paper.java and Soap.java. The method of this class
should be called main and have exactly the same signature as the
main of CalculatorTestHarness.java. A basic test consists of
creating an object (Paper or Soap), then verifying that its dimensions are
correct. For example, if you create a piece of paper with new
Paper(10.0, 14.0), then your test method should verify that the length of the
new piece of paper is 14.0 and the width is 10.0. Do this for all
dimensions of the objects, for all combinations of sizes from .5 inch to 20
inches, [added 4/18] in increments of 1/4" (see Message
Board posting under "Testharness for Paper and Soap". The test should operate silently unless an error is found; in
that case, it should describe the error situation precisely, then continue
with the next test. Perform all of the Paper tests before performing any
of the Soap tests. At the end of all the Paper tests, print a line
stating how many tests succeeded and how many failed. Do the same for
the Soap tests.
Hints:
- The Soap and Paper classes you write for the first week can be continued
and expanded for the second week. The testing needed for the second week
will be different (details later).
- Start by printing off the starter classes, reading them top to bottom, and
sketching some code away from the computer.
- Don't begin working on the Wrapper class until you have the first week's
assignment complete. You won't be able to turn in Wrapper the first week
anyway.
- If there are things you don't understand about the problem itself, figure
them out before wasting time writing code that you might have to rewrite
anyway.
- If at first you don't see how to write loops to do all the required tests
-- forget about the loops (temporarily). Write code that tests one piece
of paper and/or one bar of soap. Then worry about the loops.