Homework 10: Mutable Objects

Objectives

  • Write methods that manipulate the state of objects.
  • Avoid extra garbage collection in string processing.
  • Read information from Rectangle class.

Rectangle UML

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).

example car of 6 by 6 in 10x10 space

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.

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.

Polygon

Submissions for all problems for HW10 should be made to https://autolab.cs.jmu.edu 

 

 

Back to Top