/* * Lab 2 * * The purpose of today's lab is to use self-referential data structures * and to explore the use of methods for classes. * * Before continuing, please make sure that the language level is set to * "ProfessorJ - Beginner" */ /**********************************************************************/ /* * As a review of the material we learned last week, implement the * GroceryItem class. The class should have a name, weight(in ounces), price, and * quantity. Remember to follow design recipe and write down your purpose * statements. */ /**********************************************************************/ /**********************************************************************/ /* That was quick, right? Now create a couple of examples of your * GroceryItems - for instance, * - Spaghetti Sauce, 64 oz, $3, 2 * - Cookies, 16 oz, $5, 4 */ /**********************************************************************/ /**********************************************************************/ /* * Now, we want to be able to ask questions about these GroceryItems. For * instance, consider the problem of the total price of an item - it's always * nice to know exactly how much you're spending on each thing. * * How would we calculate this value? Simply enough, we would retrieve the * price of the item, and then multiply by the quantity we planned to buy. * However, repeating that calculation every time we wanted to get at the * total price of a GroceryItem would be tedious and error-prone. We want to * tie this behavior up in one place so that we don't need to code it again. * Thus, methods. Methods are essentially Scheme functions in a different * wrapper, and we can use them in the same fashion. * * For our totalPrice method, we first write a purpose statement: * * // totalPrice: Called on a GroceryItem, returns an integer representing the total * // cost in dollars of purchasing it. * * And then the actual code * int totalPrice() { * return this.price * this.quantity; * } * * Some things to notice about this method - we have to specify * what the type of the return value is, which we defined in our purpose * statement. When we call the method on an object, it provides the object * that the method was called on as an implicit argument named 'this', which * we use to access our price and quantity information. * * Add the method above to your GroceryItem class and try it out on your examples. */ /* * Now, it's time to write some methods on your own. Try writing a totalWeight * method that when called on a GroceryItem returns the total weight of that item in * Oz. * */ /* * Functions without arguments are good, but functions with arguments are even * better, as they allow us to compare two things. Say that I'm a student on a limited * budget, and want to compare two GroceryItems and ask which is cheaper. To do that, * I'll need to get my hands on another object to compare - an argument. * * // isCheaper: Given another GroceryItem, returns true if that item is cheaper. * boolean isCheaper(GroceryItem that) { * return this.price > that.price; *} * * Since we passed the other GroceryItem as an argument named 'that', we can access * the values it contains. Add isCheaper to your GroceryItem class, and write a couple * of examples of it in action. * For instance, * GroceryItem fruit = new GroceryItem("Apple", 4, 1, 2); * GroceryItem soda = new GroceryItem("Coke", 8, 2, 2); * fruit.isCheaper(soda) -> false */ /* * Write some other methods that compare two GroceryItems. Consider asking * which weighs more, whether an item weighs less than a given weight, * which item has the smallest price/quantity ratio, or other possible comparisons. * Implement these methods following your design recipe and add them to your GroceryItem * class. */ /* * Now, let's think about the following problem: We want to plan a trip to * the grocery store to buy a large number of items. To do that, we'll need * to keep a list of all the groceries that we buy. * Thus, we will create a GroceryList class to define our data. Since our * list will have lots of purchases, we will create a self-referential union to * store our data. * * A GroceryList is one of * - A NoGroceries * - A SomeGroceries of * a GroceryItem * x a GroceryList * * Implement this class in the space below. */ /**********************************************************************/ /**********************************************************************/ /**********************************************************************/ /* * Lists are interesting and very useful, of themselves, but it would be even * better if we could ask questions of the list and get back useful information. * Thankfully, we can do just that by invoking methods on them. As we've seen in class * a method is basically a Scheme function. ... just wait until next week's lab :) * */ /* * Let us try another example: * * You want to make a computer program that would play chess. To start with, * you need to represent the playing pieces and their position on the 8 x 8 * chess board. To start with, we will represent only the rook and the queen. * You should record whether the piece is black or white. * The rook can move along the vertical and horizontal * lines. The queen can do the same and also move along the diagonals. * * To help you with the data definition, we suggest the following: * - start with the class that will represent the position of the piece on the board * - next define a union of classes that represent the different pieces * - you will need only two variants for now * * Implement these classes in the space below. Make sure you include * examples of your data. */ /**********************************************************************/ /**********************************************************************/ /* * We now need to figure out whether a rook can take the queen. * Start with figuring out whether the rook can move to some given position. * Do the same for the queen. * * Think of what needs to be done. Follow the design recipe. * Do not forget the template! * If you feel things are getting too complicated - see if you can * use a helper function - even if it is in another class. * * It is much harder to figure out whether a queen can take a rook, * but you already have the solution for the first half of the problem. * * If you find it too hard, try the same question for a pawn. * Pawn threatens and can take only a piece in a position diagonaly * ahead (one step forward and one step to the left or right). * * Eventually, you would like to have a method that works for all * pieces, but that will wait till next week. */ //Class for containing examples: class Examples { Examples() {} // Don't mess with this line! // Examples from above: // GroceryItem g = new GroceryItem("Spaghetti", 64, 3, 2); /* * Run the following code in your interactions window: * * Examples e = new Examples(); * * and then test out your examples like so: * * e.p * */ }