lecture19-Objects2

lecture19-Objects2 - Lecture 19: Objects Lecture 19:...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
cture 19: Objects Lecture 19: Objects CS 170, Section 000 November 2009 5 November 2009 11/5/2009 CS170, Section 000, Fall 2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Plan ¾ Logistics ± Objects: data fields, methods, constructors (review) ¾ Using objects to build applications ¾ Passing objects to/from methods tatic vs instance methods ¾ Static vs. instance methods ¾ Access and visibility ¾ HW6 (due Wed, November 11) ¾ If time: preview of CS190: Web technologies CS170, Section 000, Fall 2009 2
Background image of page 2
Logistics Homework 6: Due Wed Nov 11 th Poker simulation Looks like we will have 9 homeworks total, not 10 (still will drop lowest). Midterm 2: Tuesday, Nov 17 th Final: Dec 10 th 11/5/2009 CS170, Section 000, Fall 2009 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Programming with Objects and lasses Classes Define a class , or use existing classes in Java API Creating objects using a class and using the objects
Background image of page 4
Classes A Java class uses variables to define data fields A class uses methods to define behaviors which accesses or modifies the data fields A class provides a special type of methods, known as constructors, which are used to construct and initialize bjects from the class objects from the class 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Circle example Circle Properties: radius Behavior: compute area Circle class ata fields: radius Data fields: radius Constructors ethods: getArea() Methods: getArea()
Background image of page 6
UML design Circle radius: double Class name Data fields UML Class Diagram Circle() Circle(newRadius: double) getArea(): double Constructors and Methods circle1: Circle radius: 10 circle2: Circle radius: 25 circle3: Circle radius: 125 UML notation for objects dus: 0 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Classes class Circle { /** The radius of this circle */ ouble radius 1 0 double radius = 1.0; /** Construct a circle object */ Circle() { Data field } /** Construct a circle object */ ircle(double newRadius) { Constructors Circle(double newRadius) { radius = newRadius; } Where is the main method? /** Return the area of this circle */ double getArea() { return radius * radius * 3.14159; } Method 8 } }
Background image of page 8
Constructors Circle() { No-args constructor } Circle(double newRadius) { radius = newRadius; Constructor that takes one } onstructors are a special kind of methods that are double parameter Constructors are a special kind of methods that are invoked to construct objects 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Creating and Using Objects Declaring object reference variables Creating objects ccessing objects Accessing objects
Background image of page 10
Declaring Object Reference Variables To declare a reference variable, use the syntax: ClassName objectRefVar; Example: Circle myCircle; 11
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Creating Objects Using Constructors voking a class constructor using the new operator new ClassName(); ` Invoking a class constructor using the new operator to create an object ` To reference an object, assign the object to a reference riable Example: variable. Circle myCircle; myCircle = new Circle(5.0); 12
Background image of page 12
Declaring/Creating Objects in a Single Step ClassName objectRefVar = new ClassName(); Example: Create an object Assign object reference Circle myCircle = new Circle(); 13
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Accessing Objects Referencing the object’s data: objectRefVar.data e.g., myCircle.radius Invoking the object’s method: objectRefVar.methodName(arguments) g yCircle getArea() e.g., myCircle.getArea() 14
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 64

lecture19-Objects2 - Lecture 19: Objects Lecture 19:...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online