lec10 - 1.00 Lecture 10 Static Methods and Data Reading for...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1.00 Lecture 10 Static Methods and Data Reading for next time: Big Java: sections 13.1-13.7 Static Class Methods, Data • Static data fields: – Only one instance of data item for entire class • Not one per object – “Static” is a historic keyword from C and C++ – “Class data fields” is a better term • These are the alternative to “instance data fields” (which are a field in each object) • Static methods: – Do not operate on objects and do not use any specific object – Have access only to static data fields of class • Cannot access instance fields in objects • You can pass arguments to static methods, as with all methods – “Class methods” is a better term • These are the alternative to “instance methods” (that operate on an object) 1 When to Use Static Data • Variables of which there is only one for a class – For example, the next ID number available for all MIT students (assuming they are issued sequentially). In a Student class: Student private static nextID=1; private static int nextID=1; // 1 private int ID; // 1 public static int getID() { return static getID() return String name; private String name; // 1 value per class value per class value per instance value per instance nextID++;} nextID++;} value per instance value per instance … • Constants used by a class (final keyword) – Have one per class; don’t need one in each object public static final 16; public static final int MAX_TERMS_AS_STUDENT= 16; static final double ABSOLUTE_ZERO= 273.0; public static final double ABSOLUTE_ZERO= 273.0; – If ABSOLUTE_ZERO is in class Temperature, it is invoked by ABSOLUTE_ZERO – – Constants are all caps by tradition (C, C++) Static variables in C, C++ are different than in Java double tKelvin= Temperature.ABSOLUTE_ZERO tCelsius; double tKelvin= Temperature.ABSOLUTE_ZERO + tCelsius; When to Use Static Methods • For methods that use only their arguments and thus don’t need an object for member data public static double double p) public static double pow(double b, double p) library, takes to the power // Math library, takes b to the p power • For methods that only need static data fields public static getID() return nextID++;} public static int getID() { return nextID++;} static variable (see // nextID is a static variable (see prev page) • Main method in the class that starts the program – No objects exist yet for it to operate on! • All methods in C are like static Java methods, since C has no classes/objects; C++ has both Java-like and Clike methods 2 Exercise • We’ll experiment with whether rail locomotives have enough power to haul a train at a given velocity Force Resistance: static friction, rolling friction, air Decreases with velocity Increases with velocity Locomotive Locomotive force limited by horsepower, adhesion All cars alike (same weight) Exercise • Declare a class Train (Eclipse: File->New->Class) – Create one public constant: gravity g= 9.8 – You’ll finish this class later • Declare a class Engine (Eclipse: File->New->Class) – Variables • Mass • Power • Coefficient of friction mu (0.3), a public constant for all engines – Constructor, as usual. How many arguments does it have? – getMass() method – getForce() method with one argument, velocity • f1= power/velocity • f2= mass * g * mu • Return the minimum of f1, f2 (limit of engine horsepower) (limit of adhesion to rail) (use Math.min) • Save / compile 3 Exercise, p.2 • Write a static version of getForce() in class Engine – Supply all needed variables as arguments – Used by other classes that don’t want to create an Engine object – Method overloading: • We can have multiple methods with the same name as long as they take different arguments. • We cannot have two methods that differ only in return type • Overloading is general; it’s not related to static vs instance Exercise, p.3 • Write class Car (Eclipse: File->New->Class) – Two private variables: • A single mass for all cars • Car type (coach, snack, first-class) – Constructor. How many arguments does it have? – Set and get methods for the single car mass 4 Exercise, p. 4 • Finish class Train • Data members: – – – – – – Gravity g (already defined) Constant c1= 0.00015 (rolling resistance) Constant c2= 110.0 (air resistance) One engine (object) Number of cars (int) (Which data members are static?) • Constructor – What variables does it set? • Method getNetForce, with one argument: velocity – Compute weight= g*(engine mass + no of cars * car mass) – Compute net force= engine force - c1*weight*v - c2*v*v – Return net force Static data and methods // In main() method you’ll write next: // In a main() method you’ll write next: of Car creates its static data // 1st mention of Car creates its static data Car.setAvgMass(50000); Car c1= new Car(“coach”); c1= new Car(“coach”); c2= new Car(“first class”); Car c2= new Car(“first class”); 0 50000 coach c1= Car 0 50000 Car Valid method calls even method calls even no Car objects exist: if no Car objects exist: Car.get/setAvgMass() Car.get/setAvgMass() Car method calls: Valid method calls: // Static c1.get/setAvgMass() // Static Instance c1.get/setCarType() // Instance 0 50000 first class c2= Car Car Valid method calls: Valid method calls: // Static c2.get/setAvgMass() // Static // Instance c2.get/setCarType() // Instance 5 Exercise, p.5 • Download TrainTest and add one line to it: public class public class TrainTest { static void main(String args) public static void main(String args) { r34= new Engine(90000, 5500000); 90 tonnes, 5500 kW Engine r34= new Engine(90000, 5500000); // 90 tonnes, 5500 kW vel= 30.0; 30 m/s, 70mph double vel= 30.0; // 30 m/s, 70mph Instance method // Instance method force34= r34.getForce(vel); double force34= r34.getForce(vel); Static method // Static method double f34= Engine.getForce(vel, 90000, 5500000); f34= Engine.getForce(vel, 90000, 5500000); Don't need to create Cars. All we need is their mass // Don't need to create Cars. All we need is their mass But we must set their mass: it here // But we must set their mass: do it here Train // Train amtrak41= new Train(r34, 10); Train amtrak41= new Train(r34, 10); Instance method // Instance method force41= amtrak41.getNetForce(vel); double force41= amtrak41.getNetForce(vel); Static method (if you had time) // Static method (if you had time) f41= Train.getNetForce(vel, 10, r34); double f41= Train.getNetForce(vel, 10, r34); } } Solution: 2 engines, 2 trains class TrainTest3 Solution with two trains, two engines public class TrainTest3 { // Solution with two trains, two engines static void main(String args) public static void main(String args) { Engines // Engines r34= new Engine(90000, 5500000); 90 tonnes, 5500 kW Engine r34= new Engine(90000, 5500000); // 90 tonnes, 5500 kW w96= new Engine(120000, 4000000); Engine w96= new Engine(120000, 4000000); vel= 30.0; 30 m/s, 70mph double vel= 30.0; // 30 m/s, 70mph Instance methods // Instance methods force34= r34.getForce(vel); double force34= r34.getForce(vel); force96= w96.getForce(vel); double force96= w96.getForce(vel); Static methods // Static methods f34= Engine.getForce(vel, 90000, 5500000); double f34= Engine.getForce(vel, 90000, 5500000); f96= Engine.getForce(vel, 120000, 4000000); double f96= Engine.getForce(vel, 120000, 4000000); Can't and don't need to create Cars, but set their // Can't and don't need to create Cars, but set their avg wgt here Car.setAvgMass(50000); Trains // Trains Train amtrak41= new Train(r34, 10); amtrak41= new Train(r34, 10); amtrak171= new Train(w96, 10); Train amtrak171= new Train(w96, 10); Instance methods // Instance methods force41= amtrak41.getNetForce(vel); double force41= amtrak41.getNetForce(vel); force171= amtrak171.getNetForce(vel); double force171= amtrak171.getNetForce(vel); Static methods // Static methods f41= Train.getNetForce(vel, 10, r34); double f41= Train.getNetForce(vel, 10, r34); f171= Train.getNetForce(vel, 10, w96); double f171= Train.getNetForce(vel, 10, w96); } } 6 Variable Lifecycles • Instance (or object) variables – Created when their containing object is created – Initialized to default if not explicitly initialized • 0 for numbers, false for boolean, null for objects – Destroyed when Java garbage collector finds there are no remaining active references to object • Static (or class) variables – Created when class is first used in program – Initialized to default if not explicitly initialized • 0 for numbers, false for boolean, null to objects – Usually exist for rest of program (unless unloaded) • Local variables (or block variables) – Created in the statement where they’re defined – Not initialized by default. Contain unpredictable data – Destroyed when block is exited (at ending brace ) 7 ...
View Full Document

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

Ask a homework question - tutors are online