| CSCI A201/A597Lecture Notes 22 Spring 2000 |
Mikey's Garlic Mashed Potatoes
Mikey's GMP
(In which the cook needs all the help that he can get!)
Note to the customer:
If you want GMP better bring your own ingredients!Chef:
Also, when you pass them to the cook pass them in the right order, and one by one.
Check ingredients carefully, one by one.A few more considerations.
If something doesn't match print error message and stop.The necessary ingredients should be:
- one baking potato (call it
p1)- another baking potato (call it
p2)- some butter (call it
b)- some whipping cream (call it
w)- garlic (call it
g)- cream cheese (call it
cc)- chives (call it
ch)- salt (call it
s)- pepper (call it
p)Start cooking:
- wash
p1.- peel
p1.- quarter
p1and callqp1the result.- wash
p2.- peel
p2.- quarter
p2and callqp2the result.- get recipient (
rec) and some water (wat).- put
wat,s,qp1andqp2inrec.- identify free burner
bu.- put
reconbuand turnbuon.while(qp1not fork tender andqp2not fork tender) {
wait 1 minute patiently
}- drain water out of
recand callbpwhat's left.- identify masher
m1and take it.- mash
bpwithm1, callbpmthe result.- take saucepan
sp.- divide
ccinto two halves:cc1andcc2.- add
cc1andbtosp.- look at watch, record time
now.- compute
thenthe time after one minute- start simmering
while( current time less thanthen) {
stir
wait 2 seconds
}- add
bmpto saucepansp- locate spoon
spoonand stir with it insp- add
cc1to saucepansp- stir with
spooninsp- add
gtospand stir withspoon- turn burner
burneroff- place contents of
spinto plateplateand call itgmp- place
chon top ofgmpand call itgmp:gmp += ch;gmp = gmp + s(add salt)- return
gmp
class Mikey {
public static GarlicMashedPotatoes recipeOne( Potato p1,
Potato p2,
Butter b,
WhippingCream w,
Garlic g,
CreamCheese c,
Chives ch,
Salt s,
Pepper p) {
// see the steps detailed above
...
}
Testing the chef. We set up the following situation:
class TestingTheChef {
public static void main(String[] args) {
// first get all the ingredients the chef needs
Potato myPotato1 = new Potato();
Potato myPotato2 = new Potato();
Butter myButter = new Butter(0.5);
...
Pepper myPepper = new Pepper();
GarlicMashedPotatoes result = Mikey.recipeOne(myPotato1, myPotato2, myButter, ..., myPepper);
}
}
Now compile and run the classes. Bring your own plate to store the result.
Note: if you call the chef with an incorrect number of arguments (ingredients) then the chef won't be able to use this recipe and will report a compilation error and stop. If you call the chef with the expected number of ingredients but pass them to the chef in the wrong order the chef will also complain and stop. This chef is following the instructions exactly as written.
We take the problems one by one and think about them in the same way:
1. First we need to identify the inputs of this procedure. Then we need to describe the type of result (or output, if any). Then we will need to describe how (the method by which) we obtain the result.
Inputs:
Random number generator (call it gen)
low representing the lower endpoint of the interval
high representing the higher endpoint of the interval
low and high inclusive
First compute the number of possible outcomes in the interval:Code this in Java and place it in your classThen askint num = (high - low + 1);gento give you a random integer:Make this value positive (if necessary) and take the remainder when dividing byint val = gen.nextInt();num:val = Math.abs(val) % num;valnow will be in between0and(num - 1).Translate the value into the desired range and return it.
return (val + low);
Library. Then in your
other class, which has the main method, invoke the method a few times
(did you give it a name already?) to test it.
2. This problem is very much related to the previous one.
So the description here will be a bit more general.
Inputs:
gen)
low representing the lower endpoint of the interval
low representing the higher endpoint of the interval
low and high
float or double, whichever suits you best. Method:
It depends howNow code this in Java, give it a name, place it ingenis providing the random numbers but most likely they will be random numbers between0and1, let's call itval. This value can be looked at as a percentage.The length of the target interval is
so if we want to translate(high - low)valinto a location on the segment fromlowtohighwe will have to multiply the length of the interval by the percentage:This is a number that represents a fraction of the length of the target interval betweenval * (high - low)lowandhigh. If we addlowto this we will obtain the desired random number that corresponds tovaland falls in betweenlowandhighand we could return it:As we said it depends on the source you're using. The book says there are two such sources that you could use:return val * (high - low) + low;Math.randomor aRandomobject which could returnnextFloat(). In both cases the method described here would work.
Library and call it
a few times from the main of the other class to test it just to make sure
everything is OK.
3. This problem was covered in the last set of lecture notes.
Inputs:
DrawingWindow d
Pt p
Pt q
Pt r
This procedure does not have any output per se, in the sense that it does not return a value. So it should be declared asMethod:void. However we expect it to change the state ofd, in drawing a triangle with vertices inp,q, andron it.
Create three lines (one that connectsWhen you're done, give it a name, code it in Java, place it inpandq, another one that connectsqandrand another one that connectsrtop) and draw each one on the drawing windowd.
Library
and call it a few times from main to test it.
4. This problem was exemplified and developed in class on Tuesday.
Inputs:
base1, base2 and top
DrawingWindow d where the action should happen
No values are returned but we expect the drawing window to exhibit the "rubber band" phenomenon while this procedure is running.Method:
Set the painting mode toinvertMode()ind. Then create two lines,left(frombase1totop) andright(frombase2totop) and draw them. Then wait formousePress. For as long as the mouse is pressed do this:
- Erase
leftandright(by drawing them again)- Get the new mouse position, and store it in
top- Re-create
leftandrightfor (newtop)- Ask
dto draw the newleftandrightfor you.
5. This problem is solved completely in lab notes 10.
Inputs:
Pt p
Pt q
p and q
Compute the distances between the points on the horizontal and on the vertical. Square each on of them, add them up and return the square root of the result. That is the distance between the two points by Pythagora's theorem.
6. I will only provide the formula here and we can discuss this in class or office hours.
Inputs:
Pt p
Pt q
Pt that is the midpoint of the segment from p to q
Create a newAgain, we need to give it a name, code it in Java, place it in thePtwhose coordinates are the averages of the coordinates ofpandqand return it.One can achieve that in one line:
return new Pt((p.x() + q.x())/2, (p.y() + q.y())/2);
Library
of methods and invoke it from main a few times for testing purposes. As lab
notes 11 will show, we need to be fairly thorough when testing.
7. See lab notes 11 for this problem.
Inputs:
Pt that is the center of mass of the three given points
Use the fact that the center of mass of any triangle is at the intersection of its medians and that medians cross each other at 2/3 of their length from the vertex and 1/3 from the base. The median is the segment that connects a vertex with the midpoint of the opposing side, so the method that we defined at 6 will come in handy here.
8. Read 4.4.3 in the text to see what a predicate means.
Inputs:
Circle c1
Circle c2
boolean value telling whether the circles overlap or not
Two circles overlap if the distance between their centers is smaller than the sum of the two radii. The question is how do we get the center and the radius out of aName this, code it in Java, place it inCircle. The key is in appendixEand chapter 2. So this method could also be described in one line (although a long one):So we use the method developed at problem 5.return ( Library.distance(c1.center(), c2.center()) <= c1.radius() + c2.radius());
Library and use it
a few times from main to test its functionality.
9. Interesting problem with more than one solution.
Inputs:
Rect r1
Rect r2
boolean value reflects the truth value of the following
assertion: the two rectangles overlap each other (true or false).
Same as in the case of circles we need to work with the distance between the two possibly overlapping entities. The two rectangles overlap when the distance between their two centers on the horizontal is less than half of their combined widths and the distance between their two centers on the vertical is less than half of their combined heights.The other method is to realize that
r1 does not overlap
r2 if r1 is entirely to the north of r2 or
entirely to the east of r2 or entirely to the south of r2
or entirely to the west of r2. (We have thus described the outside
of r2). So now we need to return the negation of this, whatever truth value
it may have.
Choose a method, code it in, place it in Librat, test it.
10. This is described on page 56 of the book.
Inputs:
String text
Pt p
Create aName it, code it, place it inTextobject out of theStringand place it atp(the book uses the mouse position for that). Then draw aRoundRectaround it (the book draws a regularRectaround it instead). These steps are like in the book, where they are also explained.
Library.
11. This problem works with the mouse.
There's more than one way to think about this problem so if the rectangle is provided we might as well assume that the window is also provided so we place it in the list of inputs.
Inputs:
DrawingWindow d
Rect r
Outputs:
void, message printed
Given the window ask it to monitor the mouse movement, and since that's the only thing that you were doing you might as well pause and wait for mouse press. When that happens the window will return its location as aPt. Store it (call itpress) and now wait for the mouse to be released. When that happens its location is returned as aPtso you need to store that value too (call itrelease). Now print an "INSIDE" message if the provided rectangle contains bothpressandrelease. Otherwise print "OUTSIDE", meaning that at least one of the two points, if not both, are outside the provided rectangle.
12. We rely on what we have learned on page 102.
Inputs:
double seconds
void, method pauses for that many seconds
We measure time in small units: milliseconds. Each is 1/1000 of a second. So givensecondstransform them into milliseconds and pause for that many milliseconds. (If you know how to pause1000milliseconds you know how to pause for any number of milliseconds. Just make sure that when you transform the seconds (adouble) into milliseconds you end up with a whole number of milliseconds).
So these are the recipes. The main method should be using
them to cook a banquet.