//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //A Car "Driver" Program //Description: Tests methods for a car that features color, acceleration, and // a few stubbed features. //CS 284 //Programmed by Jonathan Voris //for 2/1/06 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class car { private static int speed = 0; private static int acceleration = 10; private static String color = "black"; /*Car Color Methods*/ public static String getColor() { return color; } public static void repaint(String newColor) { //check that the new color is valid before applying the change if (isValidColor(newColor)) { color = newColor; } } //isValidColor takes a string representation of a color and returns //true if it is a valid one; otherwise it displays an error and returns false public static boolean isValidColor(String color) { if ((color.equals("red")) || (color.equals("black")) || (color.equals("blue")) || (color.equals("green"))) { return true; } else { System.out.println(color + " is not a color!"); return false; } } /*Car Speed Methods*/ public static int getSpeed() { return speed; } //setSpeed is private because it should not be used to directly //modify the speed of the car - accelerate and decelerate should //be used instead private static void setSpeed(int newSpeed) { //check that the new speed is valid before applying the change if (isValidSpeed(newSpeed)) { speed = newSpeed; } } private static int getAcceleration() { return acceleration; } public static void accelerate() { setSpeed(getSpeed() + getAcceleration()); } public static void decelerate() { setSpeed(getSpeed() - getAcceleration()); } //isValidSpeed takes a int representation of a speed and returns //true if it is a valid one; otherwise it displays an error and returns false public static boolean isValidSpeed(int speed) { if (speed > 65) { //speed is "invalid" because it exceeds the speed limit System.out.println("Slow down, you're over the speed limit!"); return false; } else if (speed < 0) { //speed is "invalid" because it is negative System.out.println("You've already stopped moving!"); return false; } else { //we must be between 0 and 65 - return true return true; } } /*Special Car Modification Methods*/ //addSpoiler places a spoiler on the car - this is an as-yet unimplemented //stub function public static void addSpoiler() { System.out.println("You don't have enough money for a spoiler."); } //addSpoiler puts rims on the car - this is an as-yet unimplemented //stub function public static void buyRims() { System.out.println("You don't have enough money for rims."); } public static void main(String[] args) { //test the car object in its base state System.out.println("Our new car is parked outside!"); System.out.println("Car color is " + getColor() + "."); System.out.println("Car is travelling at " + getSpeed() + " MPH."); System.out.println(); //test car color System.out.println("Let's get the car repainted..."); //invalid color test repaint("fifteen"); System.out.println("New car color is " + getColor() + "."); //valid color test repaint("red"); System.out.println("New car color is " + getColor() + "."); System.out.println(); //test car speed System.out.println("Nice. Let's take our new car for a spin."); for (int x=0; x<7; x++) { //test acceleration and upper speed boundary accelerate(); System.out.println("Car is travelling at " + getSpeed() + " MPH."); } for (int x=0; x<8; x++) { //test deceleration and upper speed boundary decelerate(); System.out.println("Car is travelling at " + getSpeed() + " MPH."); } System.out.println(); //test special car modifications System.out.println("Oh snap Xzibit just showed up! Let's pimp our ride."); addSpoiler(); buyRims(); System.out.println("Too bad, maybe later."); } }