James Madison University, Spring 2019 Semester
Homework 10: Mutable Objects
Objectives
- Write methods that manipulate the state of objects.
- Avoid extra garbage collection in string processing.
Exercise 10.1 Parking Lot
There's a major event happening at the fairgrounds, and cars are lining up from everywhere. Unfortunately, we don't have a neatly designed, paved parking lot yet -- just a couple of grassy fields that have been roped off. That's where you come into the story. We need a program to help vehicles find a parking space when they arrive.
Define a class named ParkingLot with the following method: public static int search(Rectangle[] lot, Rectangle car)
. This method searches the lot (array) for the first parking space large enough to fit the car. When a space is found, the car's location is "centered" within it. For example, if the space is 10x10 at location (0, 0) and the car is 6x6, then the car's location should be updated to (2, 2).
The dimensions of each Rectangle are measured in feet. That means a typical parking space size of nine feet by eighteen feet would be represented with Rectangle(9, 18). There must be at least two feet of space around all sides of the car when parked. Otherwise the passengers will have a hard time getting out without damaging adjacent vehicles.
The search method should calculate and return the amount of square feet remaining in the parking space after parking the car. So a 10x10 space with a 6x6 car in it would have 64 square feet left over. If no parking space is found with enough room to surround the car, the method should return -1.
For this exercise, you may assume that width will be less than or equal to height (i.e., portrait orientation). Don't worry about having to rotate the rectangles -- there is no parallel parking at the fairgrounds.
Exercise 10.2 Tree Planter
Arbor Day is just around the corner, and it's time to plant some trees! You've been hired by Parks and Recreation to analyze the current layout of trees and make recommendations for where the next trees should be planted. Define a class named TreePlanter with the following methods:
-
public static double distance(Point here, Point[] trees)
Computes the distance from here to all the other trees, and returns a sum total. If one of the other trees is already planted here, this method returns -1 to indicate a conflict.
-
public static Point optimize(Rectangle yard, Point[] trees)
Determines the next place to plant a tree, given the yard boundaries and the locations of other trees. This method returns the "first" location that is farthest away from the other trees. Locations should be considered in (x, y) order. If multiple locations tie for being optimal, return the one with the lowest x value. (And if they also happen to have the same x value, return the one with the lowest y value.)
Your solution must use Point objects in loops and decisions. To encourage this approach, Autolab will reject submissions containing words like int, long, Integer, or Long. You must also NOT use the == operator.
Exercise 10.3 Soccer Game
Wouldn't it be amazing to develop a real video game like EA SPORTS FIFA? You have a long way to go on your programming journey, but at least you're on the right track! Let's try making a simpler game in the meantime. Define a class named SoccerGame with the following methods:
-
public static boolean inBounds(Polygon ball)
Determines whether the ball is in bounds. The boundaries are from (0, 0) to (12000, 8000) which represents a 120x80 meter field. If the ball is touching the boundary line, it is still considered in play. The entire ball must be past the line to be considered out of bounds.
-
public static boolean pass(Polygon ball, Rectangle player, int dx, int dy)
Passes the ball to another player. The ball keeps moving in the x and y direction by the specified amounts (at a time) until it either reaches the player or goes out of bounds. This method updates the location of the ball, but the player doesn't move. It returns true of the pass is received, otherwise false (if it goes out of bounds).
Note the following when testing your methods: a typical soccer ball is about 22 cm in diameter, and a typical player can "occupy" about 4 square meters (without having to move a lot). You can generate the points of a Polygon using an online tool like Math Open Reference.
Exercise 10.4 It's Classified
According to Wikipedia (which means that it must be true), classified information is material that a government body deems to be sensitive and must be protected. For this assignment, you've been granted Top Secret security clearance. Your task is to redact (or "sanitize") classified information in a controversial document that is about to be made public.
Define a class named Classified with the following method: public static String redact(String text, String[] words)
. This method searches a document and replaces all classified words with the string "[CLASSIFIED]". Many of these documents will be very long, so you will need to use a StringBuilder for efficiency.
To help you avoid writing inefficient code, Autolab will reject submissions that use string methods such as replaceAll, replaceFirst, split, and substring. You must also NOT use a Scanner, a StringTokenizer, or any other class that automatically parses text. Here is a simple algorithm for solving this problem:
- put the text into a StringBuilder
- begin searching at the first character
- while the index is inside the document:
- find the start of the next word
- find the end of the next word
- if the word is a classified word:
- replace it with "[CLASSIFIED]"
- advance index to the end of the word
- return the StringBuilder contents
Be careful not to replace words that are substrings of other words. (If "CS" is a classified word, don't replace "DOCS" with "DO[CLASSIFIED]".) Just make sure that the character after the word is not alphabetic (e.g., punctuation or whitespace). The Character class has methods you may find useful; see the ones beginning with the word "is".
Exercise 10.5 Comment Wrap
Eclipse IDE has a nifty feature for automatically formatting your source code whenever you press Ctrl+Shift+F. Among other things, it will reformat comments that are longer than 80 characters wide (or whatever limit you set in the preferences). For example:
After/** * This is a fairly long comment. I don't know who would ever write something like this, but I'm going to do it anyway as an example. Note that the web browser is automatically wrapping the text so you don't have to scroll horizontally. But all these words are on the same line. */
/** * This is a fairly long comment. I don't know who would ever write * something like this, but I'm going to do it anyway as an example. Note * that the web browser is automatically wrapping the text so you don't have * to scroll horizontally. But all these words are on the same line. */
This feature works similarly for end-of-line comments:
After// This is a fairly long comment. I don't know who would ever write something like this, but I'm going to do it anyway as an example. Note that the web browser is automatically wrapping the text so you don't have to scroll horizontally. But all these words are on the same line.
// This is a fairly long comment. I don't know who would ever write // something like this, but I'm going to do it anyway as an example. // Note that the web browser is automatically wrapping the text so you // don't have to scroll horizontally. But all these words are on the // same line.
For this exercise, you will implement the same feature. Define a class named CommentWrap with the following method: public static String format(String text)
. Use a StringBuilder to insert newlines at appropriate indexes, followed by the right number of spaces, and * or // to be consistent with the start of the comment.
As with the previous exercise, Autolab will reject submissions that use split, substring, Scanner, StringTokenizer, and other libraries that parse text for you automatically. Here is a recommended algorithm:
- put the text into a StringBuilder
- remember indentation and comment type
- scan the StringBuilder from start to end:
- find the end of the current line
- if the line is more than 80 chars:
- search backwards from 80 for whitespace
- insert newline, spaces, & comment chars
- advance index to beginning of next line
- return the StringBuilder contents