CSCI A201/A597

Lecture Notes 22

Spring 2000


Methods and homework 8.

Mikey's Garlic Mashed Potatoes

Wash potatoes well, peel and quarter. Cook potatoes in boiling salted water until fork tender. Drain potatoes and return to the pot. Using a ricer or a masher, mash the potatoes slightly and set aside. In a saucepan, heat 1 tablespoon butter and half the cream and simmer and stir for 1 minute. Add contents of saucepan to the potatoes and stir well. On a low flame, stir in the rest of the cream and all of the roasted garlic. Finish with chives, salt and pepper. Serves 4 Garlic Lovers.


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!
Also, when you pass them to the cook pass them in the right order, and one by one.
Chef:
Check ingredients carefully, one by one.
If something doesn't match print error message and stop.

The necessary ingredients should be:

  1. one baking potato (call it p1)
  2. another baking potato (call it p2)
  3. some butter (call it b)
  4. some whipping cream (call it w)
  5. garlic (call it g)
  6. cream cheese (call it cc)
  7. chives (call it ch)
  8. salt (call it s)
  9. pepper (call it p)

Start cooking:

  1. wash p1.
  2. peel p1.
  3. quarter p1 and call qp1 the result.
  4. wash p2.
  5. peel p2.
  6. quarter p2 and call qp2 the result.
  7. get recipient (rec) and some water (wat).
  8. put wat, s, qp1 and qp2 in rec.
  9. identify free burner bu.
  10. put rec on bu and turn bu on.
  11. while (qp1 not fork tender and qp2 not fork tender) {
           wait 1 minute patiently
    }
  12. drain water out of rec and call bp what's left.
  13. identify masher m1 and take it.
  14. mash bp with m1, call bpm the result.
  15. take saucepan sp.
  16. divide cc into two halves: cc1 and cc2.
  17. add cc1 and b to sp.
  18. look at watch, record time now.
  19. compute then the time after one minute
  20. start simmering
  21. while ( current time less than then ) {
           stir
           wait 2 seconds
    }
  22. add bmp to saucepan sp
  23. locate spoon spoon and stir with it in sp
  24. add cc1 to saucepan sp
  25. stir with spoon in sp
  26. add g to sp and stir with spoon
  27. turn burner burner off
  28. place contents of sp into plate plate and call it gmp
  29. place ch on top of gmp and call it gmp:
    gmp += ch;
  30. gmp = gmp + s (add salt)
  31. return gmp
A few more considerations.

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:

  1. a Random number generator (call it gen)
  2. an integer number low representing the lower endpoint of the interval
  3. an integer number high representing the higher endpoint of the interval
Outputs:
  1. a value that is random and in between low and high inclusive
Method:
First compute the number of possible outcomes in the interval:
int num = (high - low + 1); 
Then ask gen to give you a random integer:
int val = gen.nextInt();
Make this value positive (if necessary) and take the remainder when dividing by num:
val = Math.abs(val) % num;
val now will be in between 0 and (num - 1).

Translate the value into the desired range and return it.

return (val + low);
Code this in Java and place it in your class 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:

  1. a source of random numbers (call it gen)
  2. an floating-point number low representing the lower endpoint of the interval
  3. an floating-point number low representing the higher endpoint of the interval
Outputs:
  1. a floating point value that is random and in between low and high
Note: by floating point we mean float or double, whichever suits you best.

Method:

It depends how gen is providing the random numbers but most likely they will be random numbers between 0 and 1, let's call it val. This value can be looked at as a percentage.

The length of the target interval is

(high - low)
so if we want to translate val into a location on the segment from low to high we will have to multiply the length of the interval by the percentage:
val * (high - low) 
This is a number that represents a fraction of the length of the target interval between low and high. If we add low to this we will obtain the desired random number that corresponds to val and falls in between low and high and we could return it:
return val * (high - low) + low; 
As we said it depends on the source you're using. The book says there are two such sources that you could use: Math.random or a Random object which could return nextFloat(). In both cases the method described here would work.
Now code this in Java, give it a name, place it in 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:

  1. DrawingWindow d
  2. Pt p
  3. Pt q
  4. Pt r
Outputs:
This procedure does not have any output per se, in the sense that it does not return a value. So it should be declared as void. However we expect it to change the state of d, in drawing a triangle with vertices in p, q, and r on it.
Method:
Create three lines (one that connects p and q, another one that connects q and r and another one that connects r to p) and draw each one on the drawing window d.
When you're done, give it a name, code it in Java, place it in Library and call it a few times from main to test it.

4. This problem was exemplified and developed in class on Tuesday.

Inputs:

  1. three points base1, base2 and top
  2. a DrawingWindow d where the action should happen
Outputs:
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 to invertMode() in d. Then create two lines, left (from base1 to top) and right (from base2 to top) and draw them. Then wait for mousePress. For as long as the mouse is pressed do this:

5. This problem is solved completely in lab notes 10.

Inputs:

  1. Pt p
  2. Pt q
Outputs: Method:
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:

  1. Pt p
  2. Pt q
Outputs: Method:
Create a new Pt whose coordinates are the averages of the coordinates of p and q and return it.

One can achieve that in one line:

return new Pt((p.x() + q.x())/2, (p.y() + q.y())/2); 
Again, we need to give it a name, code it in Java, place it in the 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:

Outputs: Method:
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:

  1. Circle c1
  2. Circle c2
Outputs: Method:
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 a Circle. The key is in appendix E and chapter 2. So this method could also be described in one line (although a long one):
return ( Library.distance(c1.center(), c2.center()) 
                          <= 
                          c1.radius() + c2.radius()); 
So we use the method developed at problem 5.

Name this, code it in Java, place it in Library and use it a few times from main to test its functionality.

9. Interesting problem with more than one solution.

Inputs:

  1. Rect r1
  2. Rect r2
Outputs: Method:
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:

  1. String text
  2. Pt p
Outputs: Method:
Create a Text object out of the String and place it at p (the book uses the mouse position for that). Then draw a RoundRect around it (the book draws a regular Rect around it instead). These steps are like in the book, where they are also explained.
Name it, code it, place it in 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:

  1. DrawingWindow d
  2. Rect r
There's also more than one way to think about how the method reports the results, so assume that no value is returned per se, but a message is being printed that says if the press and the release occurred both inside the rectangle or not.

Outputs:

  1. void, message printed
Method:
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 a Pt. Store it (call it press) and now wait for the mouse to be released. When that happens its location is returned as a Pt so you need to store that value too (call it release). Now print an "INSIDE" message if the provided rectangle contains both press and release. 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:

  1. double seconds
Outputs:
  1. void, method pauses for that many seconds
Method:
We measure time in small units: milliseconds. Each is 1/1000 of a second. So given seconds transform them into milliseconds and pause for that many milliseconds. (If you know how to pause 1000 milliseconds you know how to pause for any number of milliseconds. Just make sure that when you transform the seconds (a double) 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.


Last updated on Mar 30, 2000 by Adrian German