CSC152 2004F, Class 5: Objects and Classes (1) Admin: * Office hours: M-F 10-11 (maybe 10:15 on TuTh). * Weekend homework: To be determined. * Viewing Java source online. If there's a file with a .txt suffix and the same name as the .java file you want, it should work. * Who wants to demonstrate their homework? * Guiding example: Vectors in Two Space Overview: * Classes as templates * Methods * Fields * Constructors * The roles of classes * More design issues (if time) /Unix Stuff/ * "pwd" shows where you care * "cd" * "cd DIRECTORYorFOLDER" moves you to a particular directory * "share ." says that others can see which files you have in this directory * "share *" says that others can actually see the contents of those files /Sam happily critiques Eric's amazing song-writer/ * Mad libs comments * For madlibs, group similar things together (ask for all the verbs, then all the nouns, etc.); it's also nice to ask for things out of order. * For verbs, usually transitive/intransitive and form (first person, singular, pluperfect, etc.) is nice to ask, too. * For songs, ask for numbers of syllables. * Java comments * Always name Java classes with an initial capital letter. * When you're writing the same code again and again ... use a loop or a separate method * Please email me your files after class. /Classes as Templates/ * You've learned that classes can serve as repositories for the main method, which "directs" the program. * Classes serve another, more important purpose: They provide templates for objects. * Ancient Greek philosophers * Plato (not Playdoh) * The Cave: We can only see the shadows on the wall * Each thing we interact with is but a replica of the "Platonic ideal" * Socrates * Hippocrates * Aristotle * Classes provide the "ideals" for objects * Every thing that is an X has these capabilities (methods) * Every thing that is an X has these attributes (fields) * You can cast a new shadow of X by providing the following information (constructors) /Study through examples/ * Problem: Design a class for "Vector in Two Space" * What methods should the class provide? * add them! * take the dot product (x0*x1 + y0*y1) * take the cross product (???) * Find out the x coordinate * Find out the y coordinate * Find out the radius * Find out the theta * Design issue: The secocd thing you should think about when designing a class is what methods it might provide. * Structure of methods PROTECTION RETURN-TYPE NAME(PARAMETERS) { CODE TO COMPUTE THE RETURNED VALUE } // NAME(PARAMETERS) * Two "kinds" of methods * Mutators: Change the object (e.g., scale, rotate) * Observers: Get info about the object (e.g., getX) * Some mutators might be rewritten as observers (scale could change the existing vector or return a new vector) * These are all ADT issues! /Fields/ * The data used to represent the object * Early design question for the DS part of an object: What data naturally represents this object? * Structure of a field is much like the structure of a variable PROTECTION TYPE NAME optionally: = SOME_VALUE * We will not use protection levels right now. (The default is "not protected from other members of the same package, but protected from everything else.) * Question: How do we want to represent two space vectors? * At least two possible implementations: * x coordinate and y coordinate * radius and theta * both! * either! (whichever is most convenient at the moment) * complex numbers /Detour/ * What's the difference between "pen.println(foo)" and "return foo"? * println prints it on the screen * the return command sends the value back to the calling function * pen.println(vec.getX() + vec.getY()); * What's the difference between "(display x)" and "x"? * Display prints it on the screen. * x returns it to the calling function. /Homework Assignment/ * Now that you have more time to think about the problem, * What methods might two dimensional vectors provide? * What parameters should those methods take? * What kind of values should those methods return? * MICHAEL: IF YOU'RE READING THIS, DO THE WORK TOO!