Student: OK that was a good example, but I need more practice. Give me another one.
Prof: I’m glad you’re game to try more! Here’s another one: write a program for representing pizzas (like that you might order from a restaurant), with operations that help compare and understand pizzas of different sizes for ordering for big groups – what is the price per square inch, is a 16″ pizza more pizza than two 10″ pizzas, and so on. What’s the first step?
Student: Define the data with a
record and write examples?Prof: Right! This problem talks about pizzas, which have a size and cost, so that’s a likely starting point. What might we call the record, and what might its fields and their types be?
Student: I guess that’s something like
Pizza and then numbers for size and cost?Prof: Great. Number on its own isn’t a type in Java, so we should pick one for ourselves – what do you think
double or int for these fields?Student: Well you were pretty clear earlier that we should use
int for money, and I’ll guess right away that I should use cents. And I guess pizzas don’t have to be an exact number of inches, so that could be a double.Prof: Great, so you can write the
record and examples now!Student: I guess I can…
Prof: Great, now tell me in English what those represent?
Student: A 16 inch pizza that costs 10 dollars, and a 10 inch pizza that costs 5 dollars.
Prof: Ah OK! And is that 16 inches across?
Student: Yeah, isn’t that usually what it means?
Prof: I think so. We could look it up! That often happens in modeling a problem: we need to do a small amount of research to make sure the way the world works agrees with how we set up our data. So 16 is the diameter of the pizza. That will matter for thinking about area.
Student: Sure, I guess.
Prof: OK so you have in mind that 16 was in inches, and the diameter. Can you think of field names that would make that clear?
Student: Well
costCents, like before. And maybe diameterInches? That seems long.Prof: Yeah you could also use a comment. So you could call the field
inches or diameter, and write a comment above the record saying the other part. That’s all a matter of style; there’s no 100% right answer, it’s just about communicating clearly.Student: OK, I’ll try this:
Prof: Great. Some coders might also put a longer comment and keep the names as just
size and cost, and put a long explanation. There are lots of ways to approach it. What you wrote is great. Let’s tackle a method next. Imagine we want a method betterDeal that takes another Pizza and returns true if the current pizza is cheaper per square inch, and false if the pizza given as an argument is cheaper.Student: Huh?
Prof: So imagine you are looking to feed a lot of people. You might want the pizzas that are the best price per amount of pizza. So a pizza is a “better deal” than another if it has, say, a better price per square inch.
Student: Hmm OK. I think I get it. So it’s like
Prof: Makes sense to me!
Student: Hmm, now I need to actually fill this in. So we need to compare the costs. But also the square inches of each. We kind of need the area of a pizza? And I guess we need the area of both of them.
Prof: Good observation. So, assuming circular pizzas (and given that we said diameter, I think that’s a solid assumption), you’ll need the formula for the area of a circle.
Student: How do I write π in Java?
Prof: There’s
Math.PI built-in! What do you think its type is?Student: Probably a
double, since it could never be exact. OK, cool:Prof: Wow, that was a lot of code to type at once. Nice work.
Student: Thanks! I really like the style of adding intermediate names to my code.
Prof: Indeed. This is a really rich program with a lot of detail! We might have more we can learn from it…
Practice
The method body and the calls are given. Fill in the type and name of each remaining field and of the parameter, one per box, and example data where the milk costs less in total than the eggs.
record ShoppingCartItem(String name, , ) {
boolean totalCheaper( ) {
int thisTotal = quantity * priceCents;
int otherTotal = other.quantity * other.priceCents;
return thisTotal < otherTotal;
}
}
ShoppingCartItem milk = new ShoppingCartItem();
ShoppingCartItem eggs = new ShoppingCartItem();
boolean milkCheaper = milk.totalCheaper(eggs);
boolean eggsCheaper = eggs.totalCheaper(milk);
boolean totalCheaper( ) {
int thisTotal = quantity * priceCents;
int otherTotal = other.quantity * other.priceCents;
return thisTotal < otherTotal;
}
}
ShoppingCartItem milk = new ShoppingCartItem();
ShoppingCartItem eggs = new ShoppingCartItem();
boolean milkCheaper = milk.totalCheaper(eggs);
boolean eggsCheaper = eggs.totalCheaper(milk);