DOC PREVIEW
CORNELL CS 211 - Lecture Notes

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

lecture 10 1Lecture 10 Review of classeshc293. I tried replying to your email, but you were over yourquota and it bounced.Aaron Macy. I tried replying to your email, but the reply addresswas [email protected], which does not exist. How did you dothat? I could not find you in the Cornell electronic directory. Seeme before/after class.The only inalienable right responsibility pain-in-the-butt ofAmericans is to receive email. anonymousGod answers knee-mail. anonymouslecture 10 2Assignment A3 handed out todayDue next Friday.Class NoSuchElementException is defined in package java.util.Do not put a throws clause in your method headers unless theprogram won’t compile without it. You cannot put one in a methodthat is required by an implements-clause.To make an apptmnt for a one-on-one:With Gries: Call Cindy Pakkala at 255-8240With a TA: See them at recitation or contact them --see website fordetails.With a consultant. Go to Upson 304 or Purcell during office hours.If you can’t take it then, sign up on the sign-up sheet.lecture 10 3ReviewSee the Java bootcamp slides.Look at the beginning of Weiss.Read ProgramLive, chaps. 1, 3, 4.Listen to lectures on ProgramLive.lecture 10 4ClassesAssume you know the basics of a class definition. Think of aclass as a file-drawer, and the class definition gives the format ofall the manilla folders (objects) in the file-drawer. So, a classdefinition is a template.All non-static components (variables, methods) go in each folder.All static components go directly in the file-drawer, along withall the folders. There is only one copy of each of the staticcomponents.lecture 10 5Classpublic class C {private int x; private double y;public void m1(int) { …}public int m2() { … }}a0Cxy5truem1(int) m2()Object drawn like amanilla folderTab contains nameof object (address inmemory)public components can be referencedanywhere; private ones only in theclass itself.Generally, but not always, fields areprivate and methods are public.a0vv contains a pointer to, reference to,or the name of the object.lecture 10 6Summary of classes• Class defines content of file drawer and format of objects:• File drawer contains static components and created objects,drawn as manilla folders. The name of an object —itslocation in memory— is drawn on the tab of the folder.• new-expression, used to create objects. Know the 3 steps inevaluating it.• Constructor: called in new-expression to initialize fields.• Use of private and public.• Getter and setter methods.• static vs. non-static variables (instance variables or fields).• static vs. non-static methods (instance methods).• Method toString.• Two uses of keyword this.lecture 10 7use of thispublic class C{ int x; int y; // Constructor: … public C (int x, int y;) { this.x= x;this.y= y} // Constructor: … public C(int y) { this(0, y); y= y+1; }}when used in this fashion, thisrefers to the object in whichmethod C occurs. So, this.xrefers to component x of theobject.when used in this fashion, you have aconstructor call on a constructor inthis class. Must be first statement inconstructor.lecture 10 8SubclassesCirclea0ShapegetArea()compareTo(Object)getPerimeter()radius2.0Circle(double)getRadius()getArea()getPerimeter()toString()ObjecttoString()equals(Object)ShapegetArea()compareTo(Object)getPerimeter()Squareside4.0Square(double)getSide()getArea()getPerimeter()toString()a1ObjecttoString()equals(Object)Object: thesuperest class ofthem all. Extendsall classes thatdon’t explicitlyextend another.Has moremethods. Wedon’t alwaysdraw it.overriding methodinherited methodlecture 10 9Subclassespublic abstract class Shape implements Comparable { /** = … */ public int compareTo(Object ob) { … } // = the area of the shape public abstract double getArea(); // = the circumference of the shape public abstract double getPerim();}public class Square extends Shape { private double side; /** Constructor: …/ public Square(double s) { side= s; } public double getSide() { return side; } public double getArea() { return side * side; } public double getPerimeter() { return 4 * side; } public String toString() { return "Sq: " + side; }}lecture 10 10is-a relationpublic abstract class Shape implements Comparable { /** = … */ public int compareTo(Object ob) { … } …}public class Square extends Shape { private double side; /** Constructor: …/ public Square(double s) { side= s; } …}When designing a set of classes, use the is-a relation to help youdecide about subclasses.If a square is a shape, then make Square a Subclass of Shape.The subclass has more properties than the superclass.lecture 10 11Two uses of superpublic class S { /** = … */ private int s; // Constructor: … public C(int s) {this.s= s; } public int m(int p) { … }}public class T extends S { private double t; /** Constructor: …/ public SS(int s, double t) { super(s); this.t= t; } public int m(int p) { … int x= super.m(p); }} When used in this fashion,super refers to the superclasspartition of the object inwhich method m occurs. Here,super.m(p) calls this method.When used in this fashion,you have a constructor callon a constructor in thesuperclass. Must be firststatement in constructor.lecture 10 12Apparent type: type with which a variable is declared. Itdetermines what can be referenced. Syntactic property.public class CompDemo { public static void main(String[] pars) { Shape[] sh= new Shape[8]; sh[0]= new Circle(0); sh[4]= new Square(0); … }}Apparent type of s[0]: Shape.Only these are legal: sh[0].toString(), sh[0].equals(…),sh[0].getArea(), sh[0].getPerimeter(), sh[0].compareTo(…).a0ShapegetArea()compareTo(Object)getPerimeter()ObjecttoString()equals(Object)lecture 10 13Real type: type of object that is in thevariable. It determines is actually referenced.Semantic property.public class CompDemo { public static void main(String[] pars){ Shape[] sh= new Shape[8]; sh[0]= new Circle(0); sh[4]= new Square(0); … }}Circlea0ShapegetArea()compareTo(Object)getPerimeter()radius2.0Circle(double)getRadius()getArea()getPerimeter()toString()ObjecttoString()equals(Object)sh[0].getArea() calls thissh[0]a0lecture 10


View Full Document

CORNELL CS 211 - Lecture Notes

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?