/** * Vehicle class updated with static variables and methods * * @author (your name) * @version (a version number or a date) */ public class Vehicle { // instance variables private int mileage; String color; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* a static field - new field here similar to our description of Math.pow()*/ /* Here "total" exists in memory just once and is associated with the class*/ /* We will access this field INDEPENDENTLY from any objects. see main() */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ static int total; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* a static method - similar concept as with the static data field total, */ /* only one copy of this mehod exists, independently from any objects. */ /* That means we will access this method using the class name and not an */ /* object. see main() */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ static void wrecked() { total--; // we can still access total within the class. } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* another static method - similar to wrecked. */ /* only one copy of this mehod exists, independently from any objects. */ /* That means we will access this method using the class name and not an */ /* object. see main() */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ static void reset() { total = 0; // we can still access total within the class. } /** * Constructor for objects of class Vehicle */ public Vehicle() { // initialise instance variables mileage = 10; color = new String("red"); owners = 1; } public Vehicle(int odometer) { // initialise instance variables mileage = odometer; color = new String("black"); owners = 1; } public Vehicle(int odometer, String color) { // initialise instance variables mileage = odometer; this.color = color; System.out.println("This vehicle is " + color + " and has " + mileage + " miles."); owners = 1; } /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ public int drive(boolean longSpin) { String genericColor = new String("blue"); int shortRide = 10, longRide = 3000; if (color.equals(genericColor)) { // if (color.compareTo(genericColor) == 0) { mileage += shortRide; } else if (longSpin == true && (color.compareTo("red") == 0)) { mileage += longRide; } else { mileage++; } System.out.println("In drive(), this vehicle is " + color + " and has " + mileage + " miles."); return 0; } public static void main(String[] argv) { Vehical.reset(); // here, no Vehical objects even exist boolean longRide = true, notALongRide = false; Vehicle ford = new Vehicle(); Vehicle.total++; // update total using the class name, no object needed Vehicle chevy = new Vehicle(99999); Vehicle.total++; Vehicle mazda = new Vehicle(5000, "blue"); Vehical.total++; System.out.println("Total inventory of vehicles is " + Vehicle.total); mazda.drive(longRide); mazda.drive(longRide); chevy.drive(longRide); chevy.drive(notALongRide); ford.drive(longRide); ford.drive(notALongRide); Vehicle.wrecked(); System.out.println("Total inventory of vehicles is " + Vehicle.total); System.out.println("In Friday's lab, coaches recommended using static. Is that good?"); } }