Difference between revisions of "Car class lab assignment"
From WLCS
Line 1: | Line 1: | ||
== Car class == | == Car class == | ||
− | Create a Car class using the following specifications | + | Create a Car class using the following specifications |
'''BE SURE TO COMMENT YOUR CODE''' | '''BE SURE TO COMMENT YOUR CODE''' |
Latest revision as of 11:19, 22 October 2018
Car class
Create a Car class using the following specifications
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()
- Displays year + color + make + model + "in" + gear + "going" + speed + "mph"
- Example "2014 grey Subaru Forester in drive going 88 mph"
- 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);
}
}