Ch. 6 Review Assignment (Cars)
due: should do before the test
You are to create a class called Car that will allow you to display and manipulate car objects. The Car class will contain fields as shown below in the beginnings of the class:
class Car
{
int x; // x coordinate of the top left of the car's rectangle
int y; // y coordinate of the top left of the car's rectangle
color colour; // the car's rectangle's colour
int dir; // 1 means the car is travelling horizontally to the right
// -1 means to the left, 2 means vertically down
// -2 means vertically up
boolean crashed; // determines whether the car is crashed or not
- Write a constructor method with no parameters that will create a red car at location (0, 0). The car will not be crashed. It will be moving to the right.
- Write a constructor method that has 2 parameters representing the x and y location of the car. The constructor should replace any negative parameters with zero. The rest of the fields will be set as in the previous constructor.
- Write a constructor method that has 3 parameters representing the x and y location of the car and its colour. The constructor should replace any negative coordinates with zero. The rest of the fields will be set as in the previous constructor.
- Write a constructor method that has 5 parameters representing all fields. The order of the parameters should be the same order the fields were given in the class definition. The constructor should replace any negative coordinates with zero. If the direction is anything other than 1, -1, 2 or -2 replace it with zero.
- Write a toString method for the Car class. For the car at coordinates (40, 65), moving up and not crashed, it should return the String (without the quotes)
"(40, 65) dir=-2 crashed:false"
- Write an instance method display, that draws the car. A car will be a filled rectangle 100 pixels wide by 50 pixels high with two filled black circles for wheels. The wheels should half overlap the rectangle. The rectangle should be the colour given by the colour field with a location given by the appropriate fields. A crashed car will have an X on top of it. Here is an example with two car objects created by:
Car c1 = new Car();
c1.display();
Car c2 = new Car(200, 200, #FFFF00, 2, true);
c2.display();
- Write an instance method move, that will adjust the car's coordinates (if it is not crashed) 1 pixel in the direction indicated by the dir field. If the car touches the boundary of the output window it should change the dir field so the car will start moving in the opposite direction. After the coordinates have been adjusted, call the display() method. If the car is crashed, do not change the coordinates.
- Write an instance method collidingWith, that has one Car parameter. The method should check to see if the explicit Car parameter is intersecting at all with the implicit parameter. If the two cars are touching then both cars should have their crashed fields set to true. Then call both car's display() method.
- Write an instance method equals, which has one Car parameter. It will return true if the implicit parameter and the explicit parameter's colours are the same and their crash states are the same. Otherwise it will return false.
You need to put a comment at the beginning of each method as well as the beginning of the class. See the website for an example of what should be in the opening comment.
To submit your work:
Create a tab to test your Car class. Make a setup() and draw() methods to make sure all the Car methods work properly. Make another tab for the Car class. I will replace your setup() and draw() methods with my own to mark your Car class.
Marking Scheme
Assignment #6 (Car):
Total Marks (if it was to be marked):
____
23
Comments & Indentation (3) _____
Correct toString() (2) _____
Correct display() (3) _____
Correct move() (3) _____
Correct collidingWith() (3) _____
Correct equals() (2) _____
Correct constructors (4) _____
Process Work (3) _____