/** * The purpose of this file is to illustrate some additional concepts * about objects and classes. This is not a correct commenting style * and should not be duplicated in any real programs you write. * * Concepts * 1) What is a "calling object" ? * 2) How do you pass in a parameter of the class type? * 3) How do you return a value of the class type? */ class WeightTester { public static void main ( String args[] ) { // Here we are ONLY declaring the variables w1 and w2. // No constructor will be called yet because no object // has been created yet. Weight w1; Weight w2; // Notice here we are creating two new objects of the // Weight class. The constructor will be called for // each object. w1 = new Weight(5,3); w2 = new Weight(2,10); // Now we are invoking the print method for each object. // In the first call, w1 is the variable of type Weight // that is making a call to the method "print" which is // part of the Weight class. Thus w1 is the "calling // object" -- meaning it is the object calling the // method print. Ditto for w2 in the second statement. w1.print(); w2.print(); // Note about the above statements. Here we call a method // print which prints the weights to the screen. It would // be a far better design to use a toString method instead. // The only reason I do it this way here is so that I don't // give away the code for the homework assignment in #10. /* * Here again is an example of invoking a method which is * part of the class. w1 is the calling object which is * calling the "add" method. We have two new twists. First * note that w2 is included as well. This is a PARAMETER * which is also sent to the add method. Look at the code * for the add method to see how we access both w1 and w2 * inside the method. The other twist, a return type of * Weight is given as well. We must create a new variable * of type Weight inside the method (3 different objects). * We will fill in the appropriate pounds and ounces and * return it from the add method so that we can assign * it to variable w3 in this program. */ Weight w3 = w1.add(w2); w3.print(); } }