This looks like a lot of methods to write, but each one is pretty short and simple. They'll probably each be ten to twenty lines long, and most of this is contracts, examples, and inventories.
For all the programming assignments, be sure to follow the design recipe. First, create a class; for now, all your methods will be in one class. Then, for each method,
javadoc
form) in Java comments.
JUnit
or Tester
form once we get to this).
JUnit
, you can just hit the "Run Test Cases" button.
If you wrote them using Tester
, you can right-click on the test
class and choose "testEverything()".
If you wrote your examples in comments, copy and paste each example, one
by one, into the CodePad, and see whether the answers match what you
said they would be.
Be sure to choose meaningful names for methods and parameters, and watch for opportunities to re-use methods you, I, or the textbook have already written.
Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
For now, I recommend putting all of these in one class (say,
Homework2
).
Develop a method named hoursToMinutes
which takes in a number of hours and returns the number of minutes in that
many hours.
Develop a method named
daysToHours
which takes in a number of days and returns the
number of hours in that many days.
Develop a method named
daysToMinutes
which takes in a number of days and returns the
number of minutes in that many days.
Hint: re-use previously-written methods. You should
be able to write this method with no numbers or arithmetic
operators in the method body
(although you'll need numbers in your examples, of course).
Develop a method named
squarePerim
which takes in the length of a side of a
square, and returns its perimeter.
Develop a method named
circleArea
which takes in the radius of a circle and
returns its area. The formula is π r2; use
Math.PI
as the value of π.
Develop a method named
avgThree
which takes in three numbers (not necessarily
integers) and returns their average (i.e. add them up and
divide by 3).
Develop a method named
dhmToMinutes
that takes in three numbers: how many days,
how many hours, and how many minutes, in that order, and returns the
total number of minutes.
Hint: You should be able to write this with no
numbers or arithmetic operators in the body of the method, by
re-using previously-written methods.
Develop a method named
celsiusToKelvin
that takes in a number of degrees Celsius
and returns the same temperature as written in Kelvin. (A degree
Kelvin is the same size as a degree Celsius, but 0 Kelvin is approximately
-273 Celsius. This gives you at least one example:
MyClass.celsiusToKelvin(-273) should be about 0.
Come up with at least two more examples of your own.)
Note that the method should handle fractional degrees, not only
integers.
Develop a method named
fahrenheitToCelsius
that takes in a number of degrees
Fahrenheit
and returns the same temperature as written in Celsius. (The formula
is C = (F-32)*5/9 .)
Develop a method named
fahrenheitToKelvin
that takes in a number of degrees
Fahrenheit and returns the same temperature in Kelvin.
Hint: You should be able to write this with no
numbers or arithmetic operators in the body of the method, by
re-using previously-written methods.
You've just learned that 0 Kelvin is not -273 C, but more accurately -273.15 C. How much of the previous three methods do you have to change in order to improve the accuracy?
Develop a method named
convert3digits
that takes in the "hundreds",
"tens", and "ones" digits of a number, in that
order, and returns the number. For example,
MyClass.convert3digits(5,2,8) should be 528
Develop a method named
convert3reversed
that takes in the "ones",
"tens", and "hundreds" digits of a number, in that
order, and returns the number. For example,
MyClass.convert3reversed(7,0,1) should be 107.
Hint: You should be able to do this with no
numbers or arithmetic operators in the body of the method, by
re-using previously-defined methods.
Develop a method named
quadraticFormula
that takes in the coefficients
a
, b
, and c
of a quadratic, and
computes the value of the largest real root. You may
assume that there is at least one real root.
The formula is (-b+sqrt(b2-4ac))/(2a).
Function name | Contract | Examples | Skeleton and inventory | Method definition |
---|---|---|---|---|
hoursToMinutes |
/5 | /5 | /5 | /10 |
daysToHours |
/5 | /5 | /5 | /10 |
daysToMinutes |
/5 | /5 | /5 | /10 |
squarePerim |
/5 | /5 | /5 | /10 |
circleArea |
/5 | /5 | /5 | /10 |
avgThree |
/5 | /5 | /5 | /10 |
dhmToMinutes |
/5 | /5 | /5 | /10 |
celsiusToKelvin |
/5 | /5 | /5 | /10 |
fahrenheitToCelsius |
/5 | /5 | /5 | /10 |
fahrenheitToKelvin |
/5 | /5 | /5 | /10 |
Modifications | /5 | /5 | /10 | |
convert3digits |
/5 | /5 | /5 | /10 |
convert3reversed |
/5 | /5 | /5 | /10 |
quadraticFormula |
/5 | /5 | /5 | /10 |
Following directions | /10 |
Writing contracts from word problems | /10 |
Choosing examples | /10 |
Names, white space, indentation, general readability | /10 |
Coding | /10 |
Code re-use and method composition | /10 |