Difference between revisions of "Car class lab assignment"
From WLCS
(→CarTestMain:) |
|||
| Line 1: | Line 1: | ||
| + | ---- | ||
| + | <div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;"> | ||
| + | ---- | ||
| + | =[http://enececufo.co.cc Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page]= | ||
| + | ---- | ||
| + | =[http://enececufo.co.cc CLICK HERE]= | ||
| + | ---- | ||
| + | </div> | ||
=== Car class === | === 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 27: | Line 35: | ||
Use the following code to test your Car class: | Use the following code to test your Car class: | ||
| − | + | <source lang="java"> | |
public class CarTestMain | public class CarTestMain | ||
{ | { | ||
| Line 69: | Line 77: | ||
} | } | ||
} | } | ||
| − | + | </source> | |
Revision as of 04:04, 24 November 2010
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:
<source lang="java"> 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); }
} </source>