1 / 23

Announcements

Announcements. Newsgroup: cornell.class.cs100 Assignment W1 due tomorrow at the beginning of class Assignment P1 can be picked up at the end of class. Today’s Topics. Review Classes Instance of a Class (an object) Fields Declaration of class variables The new operator Class methods.

nicolea
Télécharger la présentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Announcements • Newsgroup: cornell.class.cs100 • Assignment W1 due tomorrow at the beginning of class • Assignment P1 can be picked up at the end of class Lecture 3

  2. Today’s Topics • Review • Classes • Instance of a Class (an object) • Fields • Declaration of class variables • The new operator • Class methods Lecture 3

  3. Review • What’s the difference between a variable and a constant? • When might you want to use an ‘if’ statement? • What is the purpose of ‘block statements’? Lecture 3

  4. Review Example 1 final int THRESHOLD = 65; System.out.println(“Enter temperature: ”); int temperature = Integer.parseInt(stdin.readLine()); if (temperature < THRESHOLD) { System.out.println(“It’s cold.\n”); } Lecture 3

  5. Review Example 2 final boolean FOURLEGS = true; final boolean BARKS = true; final boolean MEOWS = false; if (FOURLEGS == false) System.out.println(“not four-legged”); else if (BARKS) System.out.println(“4 legs, probably a dog”); else if (MEOWS) System.out.println(“4 legs, probably a cat); else System.out.println(“4 legs, no idea what.”); Lecture 3

  6. Rewrite of Previous example: final boolean FOURLEGS = true; final boolean BARKS = true; final boolean MEOWS = false; if (FOURLEGS == false) System.out.println(“not four-legged”); if ( FOURLEGS && BARKS && (MEOWS == false)) System.out.println(“4 legs, probably a dog”); if (FOURLEGS && MEOWS && (BARKS == false)) System.out.println(“4 legs, probably a cat); if (FOURLEGS && (MEOWS == false) && (BARKS == false)) System.out.println(“4 legs, no idea what.”); Lecture 3

  7. Review: Formatting Output • What does the following produce? • “\tCourse\tStudents\n\tCS100\tMany” Course Students CS100 Many Lecture 3

  8. Why Classes? • Recall: a variable is a named box into which one can place values • Suppose we want boxes (variables) that contain more than one value? • For example:Coordinate c x -532 x -532 y 25 Lecture 3

  9. x 25 y 25 More examples • Coordinate c1 • Coordinate c3 • Coordinate is a class. It’s like a type, except that you (the programmer) define it. • c1 and c3 are instances of the class; we call them objects • c1.x, c1.y, c3.x, c3.y are fields of objects c1 and c3 respectively. x 3 y 4 Lecture 3

  10. Declaring a class in Java publicclass Coordinate { publicint x; publicint y; } • keywords public and class • name of the class -- naming convention: capitalize all words in class names, e.g. StringBuffer • body of the class (within { and }) • field declarations (normal type and class declarations) • methods and constructors (later in the course) Lecture 3

  11. Type declarations -- Class declarations • int x;// x • Coordinate w;// w • null is a Java constant -- it denotes the absence of an object • Just as x hasn’t been assigned (so defaults to 0), w hasn’t been assigned, it has only been declared 0 null Lecture 3

  12. Take a breath • So, objects (like c, as declared in Coordinate c) have a type -- the type is the object’s class (here: Coordinate) • Classes have methods and fields • fields are data variables associated with a class and its objects • methods contain the executable code of a class Lecture 3

  13. Creating a class instance • To create a new instance of a class, use the operator new • Coordinate w;// w • w = new Coordinate();// w null x ? y ? Lecture 3

  14. Referencing fields x 3 y 5 // w 6 // x x = w.x; x 3 y 5 // w // x 3 Lecture 3

  15. x 7 y 5 x 3 y 5 Assigning to fields // w w.x = 7; // w Lecture 3

  16. x 7 y 1 x 7 y 1 x 3 y 5 x 3 y 5 Instance1 = Instance2 ?? // w • Assigning one instance to another creates aliases -- 2 names refer to the same object. // h h = w; // w // h Lecture 3

  17. x 7 y 1 x 3 y 5 x 3 y 5 What if you want to copy? // w h.x = w.x; h.y = w.y; // h // w x 3 y 5 // h Lecture 3

  18. Methods of Classes publicclass Coordinate { publicint x; publicint y; // Set field x to p*p public void setX(int p) { x= p*p; } // Set field y to q public void setY(int q) { y= q;} // Return the sum of the squares of the fields public int sumSquares() { return x*x + y*y; } } Classes can have methods that operate on fields of the class Lecture 3

  19. Return types of methods // Return the sum of the squares of the fields public int sumSquares() { return x*x + y*y; } • sumSquares should return an integer to wherever it is called from Lecture 3

  20. Returns • return <expression> • terminates execution of the method in which it appears • returns value of <expression> to the place of the call to the method • NB: <expression> must match the return type in the method header • If the type is not void, Java insists that a return statement exist • Can also have plain ‘return’ with no expression if void Lecture 3

  21. Method calls • Suppose we want to call some of the methods of class Coordinate • Use object name followed by a dot ‘.’ followed by method name: • c.setX(3); // x field of c becomes 9 • a = c.sumSquares(); // a becomes . . . 81? Lecture 3

  22. Example • c.setX(3) • c.setY(2) • // Store 85 in ss = c.sumSquares(); // c x 9 y 2 Lecture 3

  23. Another example d = new Coordinate(); d.setX(c.sumSquares()); d.setY(c.x) // d x 7225 y 9 Lecture 3

More Related