Highlights of yesterday by gghuang: 1. blueprint (non-static, instance members are in there) 2. we used BlueJ to explore objects that we defined (Clock) Let's open BlueJ. Google it, download the .jar archive, open with Java. Specify: folder on Desktop to install, location of Java. Click Done when install finishes, open bluej folder, click on batch file. Create a new project, define a class Car. Every Car is created in the factory with a certain effiency (mileage), expressed in miles per gallon. Example: efficiency of 29 miles/gallon means with 2.5 gallons the car has the autonomy of 73.5 miles. You can drive a car, and you can add fuel to the car's tank. Write Java code that models a Car as described. 3.5 min (4:14pm) So the solution, that took us a long time to develop, eventually looked like this: class Car { double efficiency; double fuel; int vin; static int lastVIN; Car(double e) { this.efficiency = e; Car.lastVIN += 1; this.vin = Car.lastVIN; } void addFuel(double amount) { this.fuel += amount; } void drive(double distance) { this.fuel -= distance / this.efficiency; } } Java is full of entities designed in this fashion that provide specialized functionality that we need. The entities are grouped in packages (like stores in real life, e.g. Lowe's) and we need to be aware of what's available. In lab we discussed three questions posed by Gang: Every Car is created in the factory with a certain effiency (mileage), expressed in miles per gallon. Example: efficiency of 29 miles/gallon means with 2.5 gallons the car has the autonomy of 73.5 miles. You can drive a car, and you can add fuel to the car's tank. Write Java code that models a Car as described. 3.5 min (4:14pm) dgerman@indiana.edu Questions from Gang: 1. class Car { double efficiency; double fuel; int vin; static int lastVIN; Car() { this(12); } Car(double e) { this.efficiency = e; Car.lastVIN += 1; this.vin = Car.lastVIN; } void addFuel(double amount) { this.fuel += amount; } void drive(double distance) { this.fuel -= distance / this.efficiency; } } class One{ Car a; One() { this.a = new Car(); } public static void main(String[] args){ } } Car a = new Car(); // default efficiency no fuel Car b = new Car(34); // efficiency of 34 no fuel Car c = new Car("Larry", 12); // owner, fuel; default efficiency Car d = new Car(null, 3.4); class Car { double efficiency; double fuel; int vin; static int lastVIN; String owner; Car(String name, double initialFuel) { this(12); this.owner = name; this.fuel = initialFuel; } Car() { this(null, 0); } Car(double e) { this.efficiency = e; Car.lastVIN += 1; this.vin = Car.lastVIN; } void addFuel(double amount) { this.fuel += amount; } void drive(double distance) { this.fuel -= distance / this.efficiency; } } 2. In the second case, class One{ Car a = new Car(); public static void main(String[] args){ One.fun(); Car a = new Car(); System.out.println("from main: " + a.report()); One.fun(); One b = new One(); System.out.println("also from main: " + b.a.report()); } static void fun() { Car a = new Car(); System.out.println("fun reports: " + a.report()); } } class Car { double efficiency; double fuel; int vin; static int lastVIN; String owner; String report() { return "I am car " + this.vin; } Car(String name, double initialFuel) { this(12); this.owner = name; this.fuel = initialFuel; } Car() { this(null, 0); } Car(double e) { this.efficiency = e; Car.lastVIN += 1; this.vin = Car.lastVIN; } void addFuel(double amount) { this.fuel += amount; } void drive(double distance) { this.fuel -= distance / this.efficiency; } } In the third case class Car { double efficiency; double fuel; int vin; static int lastVIN; String owner; String report() { return "I am car " + this.vin; } Car(String name, double initialFuel) { this(12); this.owner = name; this.fuel = initialFuel; } Car() { this(null, 0); } Car(double e) { this.efficiency = e; Car.lastVIN += 1; this.vin = Car.lastVIN; } void addFuel(double amount) { this.fuel += amount; } void drive(double distance) { this.fuel -= distance / this.efficiency; } } class One{ static Car a = new Car(); // 1 gets created first public static void main(String[] args){ One.fun(); // second car gets created Car a = new Car(); // third car System.out.println("from main: " + a.report()); One.fun(); // fourth car One b = new One(); System.out.println("also from main: " + b.a.report()); // 1 One c = new One(); System.out.println("also from main: " + c.a.report()); // 1 } static void fun() { Car a = new Car(); System.out.println("fun reports: " + a.report()); } } Later we discussed with Gang this example: /* */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class One extends Applet { public void init() { Reporter a = new Reporter(); this.addMouseMotionListener(a); } public void paint(Graphics g) { } } class Reporter implements MouseMotionListener { public void mouseMoved(MouseEvent a) { System.out.println("Mouse moved " + a.getX() + ", " + a.getY()); } public void mouseDragged(MouseEvent a) { System.out.println("Mouse dragged at " + a.getX() + ", " + a.getY()); } } Also this example: interface Multiplier { int multiply(int n, int m); } class One implements Multiplier { int multiply(int n, int m) { return n * m; } } class Two implements Multiplier { int multiply(int n, int m) { int sum = 0; for (int i = 0; i < m; i++) { sum += n; } return sum; } } --