CS115 lecture notes, Spring 1999 Week 9, Friday Schedule exam! Internalization --------------- Remember this table from notes18 mechanism abstraction --------- ----------- method implementation method documentation (body) what it does how it works what parameters/return value method invocation method invocation mechanism (high-level view) follow the stack leap of faith iteration tables abstract loop constructs traversal loop variable "goes from a to b" indices At the time I mentioned that you could start to understand language features by learning the mechanism, but that in order to progress, you need to start thinking more abstractly. Here's what I mean by thinking abstractly: 1) when you see a loop, think in terms of traversals, updates, etc., rather than thinking about each iteration Example: use of the word "ith" Hint: make the innards of a loop into a method, to separate the traversal from the internal operation Or: get the loop working before you do anything in the middle 2) when you see a method invocation, think in terms of functions, rather than the 5-step model 3) when you see a recursive method, try to make the leap of faith think of the recursive call like just another method invocation Creating your own object types ------------------------------ 1) you've already done it. Every time you write a new class definition, you create a new object type with the same name On day 1, you created a new object type named Hello. You could have created a variable with type Hello. It's usually not useful to do that because a Hello object would not have had any instance variables. 2) How do you specify the instance variables? They go right after the first line of the class definition, before any of the methods. public class Time { public int hour, minute; public double second; // method definitions go here } Notice the reappearance of our favorite word! Every time you create a Time object, it will have a copy of the instance variables. 3) Once you define a new class, you can create new variables with that type: public static void main (String[] args) { Time fred; fred = new Time (); fred.hour = 17; fred.second = 3.14159; } 4) Usually you want to put all the methods associated with an object into the class definition for that object. Second example: A complex number consists of a real part and an imaginary part, both doubles: public class Complex { double real, imag; public static void main (String[] args) { Complex c = new Complex (); c.real = 1.0; c.imag = 2.0; } } Constructors ------------ When you create a new object, you usually want to initialize the instance variables. Constructors are a special kind of method that does that. For the Time class, a constructor might look like: public Time () { this.hour = 0; this.minute = 0; this.second = 0.0; } Notice the weird syntax. 1) no static 2) no return type 3) no return statement 4) "this" is a Java keyword that indicates "the current object" Write a constructor for Complex in this space... How do constructors get invoked? -------------------------------- When you invoke the new command, Java allocates space for your object and then invokes your constructor. Your constructor initializes the instance variables. Inside the constructor, the name of the new object is "this" Your constructor does not return a reference to "this". When your constructor returns, flow of execution resumes from the point of the new command. Summary ------- Every object is an instance of some object type. Every object type is defined by a class definition. The class definition specifies the names and types of the instance variables. You can provide one or more constructor methods, whose job is to initialize the instance variables.