Difference between revisions of "Car class lab assignment"
From WLCS
m (Reverted edits by Ysukivewug (talk) to last revision by Admin) |
|||
Line 1: | Line 1: | ||
− | + | == Car class == | |
Create a Car class using the following specifications (HINT: Use your Person class as a template): | Create a Car class using the following specifications (HINT: Use your Person class as a template): | ||
Line 24: | Line 24: | ||
** sets the current gear to "D" and changes the current speed to the newSpeed | ** sets the current gear to "D" and changes the current speed to the newSpeed | ||
− | + | == CarTestMain: == | |
Use the following code to test your Car class: | Use the following code to test your Car class: | ||
Revision as of 10:27, 15 October 2015
Car class
Create a Car class using the following specifications (HINT: Use your Person class as a template):
BE SURE TO COMMENT YOUR CODE
Attributes (private):
- color (default: "no color")
- year (default: 0)
- make (default: "no make")
- model (default: "no model")
- speed (default: 0)
- gear (default: "no gear")
Methods (public)
- default constructor (sets all attributes to their defaults)
- setters (mutators) for all attributes
- getters (accessors) for all attributes
- String toString()
- void park()
- sets the current speed to 0 and the current gear to "P"
- void drive(int newSpeed)
- sets the current gear to "D" and changes the current speed to the newSpeed
CarTestMain:
Use the following code to test your Car class:
public class CarTestMain
{
public static void main(String [] args)
{
//use the default constructor to create a new instance of Car
Car myCar = new Car();
//testing all getters (accessors)
System.out.println("myCar.getColor(): " + myCar.getColor());
System.out.println("myCar.getYear(): " + myCar.getYear());
System.out.println("myCar.getMake(): " + myCar.getMake());
System.out.println("myCar.getModel(): " + myCar.getModel());
System.out.println("myCar.getSpeed(): " + myCar.getSpeed());
System.out.println("myCar.getGear(): " + myCar.getGear());
//testing all setters (mutators)
myCar.setColor("green");
myCar.setYear(2001);
myCar.setMake("Isuzu");
myCar.setModel("Rodeo");
myCar.setSpeed(55);
myCar.setGear("D");
System.out.println("myCar.getColor(): " + myCar.getColor());
System.out.println("myCar.getYear(): " + myCar.getYear());
System.out.println("myCar.getMake(): " + myCar.getMake());
System.out.println("myCar.getModel(): " + myCar.getModel());
System.out.println("myCar.getSpeed(): " + myCar.getSpeed());
System.out.println("myCar.getGear(): " + myCar.getGear());
//test drive()
myCar.drive(88);
//test toString()
System.out.println(myCar);
//test park()
myCar.park();
System.out.println(myCar);
}
}