CS 149: Introduction to Programming
James Madison University, Spring 2019 Semester

Homework 10: Mutable Objects

Objectives

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:

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:

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:

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:

Before
    /**
     * 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.
     */
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 feature works similarly for end-of-line comments:

Before
        // 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.
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.

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: