Lab 2: Simple Data Definitions and Methods
Goals: The goals of this lab are to design some simple data definitions, examples and methods in Java.
Submission: Submit your solutions to Problems 3, 4, 5 and 6 to handins by Tuesday Jan 17th at 11:59pm. This will be graded for effort. You can earn full credit even if your solutions are not perfectly correct. However, you should follow the design recipe for designing methods that was covered in Lecture 3. In particular, you should include templates, purpose statments and tests when designing methods.
2.1 Data Definitions for Unions of Data
Problem 1
A deli menu includes soups, salads, and sandwiches. Every item has a name and a price (in cents - so we have whole numbers only).
For each soup and salad we note whether it is vegetarian or not.
Salads also specify the name of any dressing being used.
For a sandwich we note the kind of bread, and two fillings (e.g peanut butter and jelly; or ham and cheese). Assume that every sandwich will have two fillings, and ignore extras (mayo, mustard, tomatoes, lettuce, etc.)
Define classes to represent each of these kinds of menu items. Think carefully about what type each field of each class should be. Do you need to define any interfaces? Construct at least two examples each of soups, salads, and sandwiches.
Self-Referential Data
Problem 2
The HtDP book includes the data definition for Ancestor Trees:
;; An Ancestor Tree (AT) is one of ;; -- 'unknown ;; -- (make-tree Person AT AT) ;; A Person is defined as above
Convert this data definition into Java classes and interfaces. What options do you have for how to translate this into Java? Make examples of ancestor trees that in at least one branch cover at least three generations.
2.2 Have a Ball
In BSL if you wanted to define a Ball, it might look something like this:
;; A Ball is a (make-ball Posn Number Color) (define-struct ball (center radius color)) (define b (make-ball (make-posn 0 0) 5 "blue"))
In Java, that same definition looks like this:
import java.awt.Color; class Ball { CartPt center; int radius; Color color; Ball(CartPt center, int radius, Color color) { this.center = center; this.radius = radius; this.color = color; } } class CartPt { int x; int y; CartPt(int x, int y) { this.x = x; this.y = y; } } class ExamplesBall { Ball b = new Ball(new CartPt(0, 0), 5, Color.BLUE); }
In Java, center, radius, and color are all called fields. They are attributes of a ball.
Problem 3 Add templates to the Ball and CartPt classes.
Now, in BSL, if we wanted the area of a ball, we would have a function that looked like this:
;; Ball -> Number ;; Returns the area of a ball (define (area b) (* pi (sqr (ball-radius b))))
But this approach can quickly get messy. And wouldn’t it make more sense for the ball to know its own area? That’s where methods come in. In Java, functions that live inside objects are called methods. These methods can use the data that’s inside the objects, as in the below example.
import tester.Tester; import java.awt.Color; class Ball { CartPt center; int radius; Color color; Ball(CartPt center, int radius, Color color) { this.center = center; this.radius = radius; this.color = color; } // Returns the area of this ball double area() { return Math.PI * Math.pow(this.radius, 2); } } class CartPt { int x; int y; CartPt(int x, int y) { this.x = x; this.y = y; } } class ExamplesBall { Ball b = new Ball(new CartPt(0, 0), 5, Color.BLUE); boolean testBall(Tester t) { return t.checkInexact(b.area(), 78.5, 0.001); } }
Note the use of this.radius. What this is doing is referencing the number we gave the ball when we created it. In this example, ball b has a radius of 5, so the area of b should be somewhere around 78.5. If you need the radius of another ball, you can access it with otherBall.radius. This works for the other fields as well.
Also note the use of checkInexact. The area of the given ball is roughly 78.5, but not exactly. In fact, all comparisons with floating-point numbers cannot be done completely precisely, so whenever we need to test such values, we must use checkInexact instead of the more familiar checkExpect. The last parameter, 0.001 specifies the tolerance we’re willing to accept: here, we’re saying the two values must be equal to within 0.1% (or a factor of 0.001).
Problem 4
Design a circumference method for Ball.
Problem 5
Design a distanceTo method in the Ball class. The method should take in a Ball and calculate the distance between the center of this Ball and the given one’s. Remember to delegate to the CartPt class!
Problem 6
Design an overlaps method which returns a boolean indicating whether this ball overlaps with another, given Ball.
Challenge Problem
Do all of the above again, this time with a Rectangle class! You’ll need x and y positions, as well as width, height, and color.
Then, design a diagonal method for Rectangle that calculates the length of the diagonal inside the Rectangle.