| Revision History |
|---|
| None so far |
Due: 11:55 PM, September 17, 2016.
Homework for all problem sets will be submitted using the Canvas Assignments tool. Most homeworks will consist mainly of Scala files. For each assignment, you get to submit some text and some attachments. For many assignments, including this one, there will be no text (or just some pro forma line such as "Here is my homework for PS1"). There will be several attachments, consisting of Scala files and text files of various sorts, including runs of your program. For this assignment, attach the files FavMovie.scala , MyWeight.scala, ps1q2A.out, and ps1q2B.txt (or .doc or .html or …).
As we said in an announcement a few days ago,
the file Resources > Scripts contain some helpful shell
scripts for compiling and running Scala programs. The Scala
compiler usually generates
more class files than the Java compiler for a program of similar
size. To hide this clutter away, create a bin directory
below the directory your code appears in, then
use the shell scripts scc
and sca instead of scalac
and scala. (These scripts work in any Unix system,
including OS X and Linux.
Windows users should choose scc.bat
and sca.bat instead.)
Important: When a problem says that a certain output is to be produced, it is always phrased as precisely as possible, so that in producing your answer you know exactly what to print out. Make sure what is asked for is what your program produces, without any ruffles, flourishes, or improvements.
1 Download Scala to the computer you normally use, if you don't have it running already. (If a system administrator needs to do this, let me know.) Your first program should print out your favorite movie. Call this FavMovie.scala
2A Create an object MyWeight
defined in a file MyWeight.scala. The object should
have two constant public members
MAX_WEIGHT: The maximum desirable weight in
kilograms (a Doubledouble).
MIN_WEIGHT: The minimum desirable weight in
kilograms (a Double).
This must be less than MAX_WEIGHT.
Of course, these don't have to be your actual target weight range.
The object should have a
private var weight that is the current
value of your (or some hypothetical person's) weight.
The object should have three public methods with this behavior:
MyWeight.getWeight: Returns the current value of the
private varible weight, your weight in kilograms.
The type
of getWeight is () => Double
MyWeight.gain(w): Adds w
to the value of weight. The argument w may be
negative. If the value of weight ever falls
below MIN_WEIGHT or rises
above MAX_WEIGHT, gain should throw an
exception. (You don't have to create a new class of exception, just
evaluate throw new Exception(description
string).) The "header" of gain is
def gain(w: Double): Unit
To the file MyWeight.scala add another
object MyWeightApp that extends App.
It should have the object MyWeight fluctuate up and down,
gaining 5 kg and losing 4 kg every year. It should print out the lines:
Start: 65.0 kg Year 1: gain 5.0 lose 4.0 At year end: 66.0 kg Year 2: gain 5.0 lose 4.0 At year end: 67.0 kg Year 3: ...
until an exception is thrown. Your program should catch it and print:
Maximum weight exceeded; weight = ...
The program should iterate for at most 50 years; after that you can eat whatever you want. If this happens without exceeding the maximum weight, the program prints
Maximum time horizon exceeded; weight = ...
So no matter what it always prints the final value
of weight.
The following subroutine may be of use in computing what to print
out. If you want to use it, feel free; you can put the definition
at the beginning of MyWeightApp
// String that represents a Double to 1 decimal digit of accuracy – def doubleApprox(d: Double): String = "%5.1f".format(d).trim
Run the program MyWeightApp and capture the output in a file ps1q2A.out.
2B What does the program do
if MAX_WEIGHT = 75, MIN_WEIGHT =
40, weight = 77, and you
execute MyWeight.gain(-1)? What should it do? Rewrite
the specification so that it won't throw an exception in this case.
You don't have to rewrite the code. Just turn in a concise
specification of how the object MyWeight should
behave. (There is no one correct answer to this problem. You have
to think about what would make the object more useful.)
You are allowed to assume additional public or private
members for the object. You are not allowed to
change MyWeight into a class or
change MIN_WEIGHT or MAX_WEIGHT
into vars.
Put your revised specification in a Microsoft Word, text, or html file
with the same
general layout as problem 2A. Call it ps1q2B.html
or .text or .docx.