65-lecture16 - Computer Science 65 Intro to CS Classes and...

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

View Full Document Right Arrow Icon
Computer Science 65 Intro to CS March 27, 2008 Classes and Objects Constructors
Background image of page 1

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

View Full Document Right Arrow Icon
Assignment #6 notes
Background image of page 2
Background image of page 3

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

View Full Document Right Arrow Icon
Background image of page 4
Background image of page 5

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

View Full Document Right Arrow Icon
Announcements Read Sections 6.3, 6.4
Background image of page 6
Declaring and Creating an object (in a different class) Review To declare an object variable, simply place the variable name after the Class name Rectangle box; Objects must also be created By default, a method exists that has the same name as the Class Called the default constructor Use the keyword new and the default constructor to allocate the appropriate amount of memory in RAM for the object. box = new Rectangle(); //note declaration and creation can also be done in one // line of code Rectangle box = new Rectangle();
Background image of page 7

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

View Full Document Right Arrow Icon
Constructors A constructor is The _____________________ does not need to be specified in the class Has the same name as the class No parameters
Background image of page 8
Default Constructor Recall Rectangle class Create a new Rectangle by Rectangle box = new Rectangle() Default Constructor sets all numeric fields to ___________________ and all boolean fields to _________
Background image of page 9

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

View Full Document Right Arrow Icon
Converting the UML Diagram to Code public class Rectangle { private double width; private double length; public void setWidth(double w) { width = w; } public void setLength(double len) { length = len; } public double getWidth() { return width; } public double getLength() { return length; } public double getArea() { return length * width; } } Rectangle - width : double - length : double + setWidth(w : double) : void + setLength(len : double): void + getWidth() : double + getLength() : double + getArea() : double
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 36

65-lecture16 - Computer Science 65 Intro to CS Classes and...

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

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