/******************************************************* * CSC A06 Tutorial III * * Prepared By Eric Beiers * * * * Class Name: Car.java (version 2) * * * * Purpose: This class represents a car, * * and holds variables to represent * * its various charactoristics. * * * *******************************************************/ public class Car { // static variables are common to all occurances of an object private static int numberOfCars = 0; // default values for all the variables of the class private int numWheels = 4; private int doors = 2; private String trunkColour = "Red"; private String hoodColour = "Red"; private String roofColour = "Red"; private String interiorColour = "Red"; private String sideColour = "Red"; private String model = "Sport Wagon"; private boolean CDPlayer = false; private boolean isTurnedOn = false; // Constructor for Car Class public Car() { numberOfCars++; } // Constructor for Car Class, modifies default variables in car class public Car(int numWheels, int doors, String colour, String model, boolean CDPlayer, boolean isTurnedOn) { numberOfCars++; this.numWheels = numWheels; this.doors = doors; this.trunkColour = colour; this.hoodColour = colour; this.roofColour = colour; this.interiorColour = colour; this.sideColour = colour; this.model = model; this.CDPlayer = CDPlayer; this.isTurnedOn = isTurnedOn; } // switches car on and off public void UseKey() { isTurnedOn = !isTurnedOn; } // prints info about this car public void PrintDescription() { System.out.println("This car has:"); System.out.println(" Wheels: " + this.numWheels); System.out.println(" Number of Doors: " + this.doors); System.out.println(" Colours: " + this.trunkColour + " trunk, " + this.hoodColour + " hood, " + this.roofColour + " roof and a " + this.interiorColour + " interior."); System.out.println(" Model: " + this.model); if (this.CDPlayer) { System.out.println(" It has a CD Player"); } else { System.out.println(" It does not have a CD Player."); } if (this.isTurnedOn) { System.out.println(" .. and finally, it is turned ON."); } else { System.out.println(" .. and finally, it is turned OFF."); } System.out.println(""); } // whatColour method that returns the colour of the item sent to it public String whatColour(String item) { if (item.toLowerCase() == "trunk") { return this.trunkColour; } else if (item.toLowerCase() == "hood") { return this.hoodColour; } else if (item.toLowerCase() == "roof") { return this.roofColour; } else if (item.toLowerCase() == "interior") { return this.interiorColour; } else { return this.sideColour; } } // whatColour method that returns the colour of the sides public String whatColour() { return this.sideColour; } // changeColour method that changes the colour of a part of the car public void changeColour(String item, String colour) { if (item.toLowerCase() == "trunk") { this.trunkColour = colour; } else if (item.toLowerCase() == "hood") { this.hoodColour = colour; } else if (item.toLowerCase() == "roof") { this.roofColour = colour; } else if (item.toLowerCase() == "interior") { this.interiorColour = colour; } else { this.sideColour = colour; } } // changeModel method changes the model variable public void changeModel(String model) { if (model.length() > 0) { this.model = model; } } }