CSC152 2004F, Class 8: Review of Java and Weirdo Terminal Problems Admin: * Please use the following subject for email: CSC152 Homework for DATE: YOURNAME CSC152 Homework for Class 8: Eryn O'Neil CSC152 Homework for 2004.09.08: Michael Billups * Some of you have been having trouble with some details of Java and would like a more tutorial guide. You can try reading http://www.cs.grinnell.edu/~rebelsky/EIJ/ (Experiments in Java), although there are some differences between the two ways of looking at Javas. Overview: * Who needs some review? (Lots of People) Functional model: Build and call functions. To write a function, you use (define NAME (lambda (PARAMS) BODY)) To call a function, you use (NAME PARAMS) But You almost never call a function in isolation We still haven't specified what the body looks like Object-oriented model: Build objects and tell them to do things. To write instructions for building an object, you ... To build an object given those instructions, you ... To write instructions that a built object can use, you ... To tell an object to do those things, you ... But You almost never tell an object to do things in isolation There will be lots and lots of details to fill in /Building and Naming Objects/ new CLASSNAME(PARAMETERS) new says "I want a new object" CLASSNAME says "this is the kind of object" PARAMETERS are for the instructions to use * Observation: We like to name things (more in Java) Scheme: (let ((mylist (make-list 10 "hello"))) ...) TYPE NAME = new CLASSNAME(PARAMETERS) * Side note: Almost any time you create a new name in Java, you gotta give its type /Tell Objects to Do Things/ NAME.METHOD(PARAMETERS) pen.println(michael.getAge()); pen.println("The average age of my tutees is: " + (michael.getAge() + eryn.getAge() + eric.getAge())/3); * Question: What is the default behavior for pen.println(NAME) * Sees if NAME has a toString method. * A method(procedure) named "toString" that returns a string and takes no parameters must be in the class associated with NAME. * If so, calls it and prints the string. * If not, prints some fun stuff. /Describing Objects/ * You can think of each object being represented internally as a box with * a named slot for each piece of datum related to the object * a link to the class the object belongs to * You can think of the class as being a container for the instructions we describe below as well as a specification for which fields belong in the box. public class NAMEOFCLASS { TYPE NAMEOFSLOT1; TYPE NAMEOFSLOT2; TYPE NAMEOFSLOT3; OTHERSTUFF (See below) } /Constructors: Instructions for Building an Object/ * Whenever a constructor is called, Java builds a new box of the appropriate kind. * It is our responsibility to give the instructions for filling in the slots of that box. (And doing anyhing else that might be useful for the object.) public NAMEOFCLASS(PARAMETERS) { BODY } * All the parameters need types public TwoDimVector(double x, double y) { this.xcoord = x; this.ycoord = y; } // TwoDimVector(double,double) * For the sake of clarity, whenever you refer to a field, preface it by the name of the enclosing object. * If it's "the one executing this code", use "this" * Three parts to a class: * Fields: Here are the slots that get filled in * Constructors: Here's how you fill in the slots/fields when a new one is built * Methods: Here's what it can do and how it does that /Methods: Writing Instructions Objects Can Use/ Scheme: (define NAME (lambda (PARAMS) BODY)) Java public TYPE NAME(PARAMS) { BODY; } /Parts of the "BODY"/ 1. Return statements (You asked me to compute something, here it is) return EXPRESSION; 2. Name values TYPE NAME = EXPRESSION; 3. Tell objects to do things OBJECT.METHOD(); 4. Create objects /Future parts of bodies/ a. Conditionals b. Loops