Docsity

Creating Objects from Scratch: Points, Constructors, and Instance Variables in Java, Study notes of Computer Science

Toggle sidebar
bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Creating Objects From Scratch
Constructor Methods,
Instance Variables,
and Class Variables
Friday, November 16, 2007
Objects From Scratch 19-2
A Simple Example: Points
Suppose we want to describe Point objects with two instance
variables named x and y.
Execution
Land
ObjectLand
p1
Point
x
y
3
4
Point p1 = new Point(3,4);
System.out.println(
โ€œp1 = โ€ + p1.toString());
Point p2 = new Point(p1.y, 2*p1.x);
Console
System.out.println(โ€œp2 = โ€ + p2);
p1.x = p2.x + 5;
p2.y++;
System.out.println(โ€œp1 = โ€ + p1);
x
y
4
6
Point
p2
p1 = (3,4)
p2 = (4,6)
p1 = (9,4)
Point (unlike Location)
components can
change over time.
p2 = (4,7)
System.out.println(โ€œp2 = โ€ + p2);
9
7
pf3
pf4
pf5

Partial preview of the text

Download Creating Objects from Scratch: Points, Constructors, and Instance Variables in Java and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science Wellesley College

Creating Objects From Scratch

Constructor Methods,

Instance Variables,

and Class Variables

Friday, November 16, 2007 Objects From Scratch 19- 2

A Simple Example: Points

Suppose we want to describe Point objects with two instance variables named x and y. Execution Land ObjectLand p Point x y 3 4 Point p1 = new Point(3,4); System.out.println( โ€œp1 = โ€ + p1.toString()); Point p2 = new Point(p1.y, 2*p1.x); Console System.out.println(โ€œp2 = โ€ + p2); p1.x = p2.x + 5; p2.y++; System.out.println(โ€œp1 = โ€ + p1); x y 4 6 Point p p1 = (3,4) p2 = (4,6) p1 = (9,4) Point (unlike Location) components can change over time. p2 = (4,7) System.out.println(โ€œp2 = โ€ + p2); 9 7

Objects From Scratch 19- 3 Declaring the Point Class public class Point { // by default, extends the Object class // Instance Variables public int x; // x coordinate public int y; // y coordinate // Constructor Method public Point (int initx, int inity) { this.x = initx; // set x coordinate to initx this.y = inity; // set y coordinate to inity // the โ€œthis.โ€ is optional above and elsewhere } // Instance Method public String toString() { return โ€œ(โ€ + this.x + โ€œ,โ€ + this.y + โ€œ)โ€); } } Objects From Scratch 19- 4 Constructor Methods in a JEM p ExecutionLand ObjectLand Point p1 = new Point(3,4); current frame

PT Point x y 0 0 PT this PT Steps in the invocation of a constructor method:

  1. Invoking a constructor method creates an execution frame with parameter variables. new Point (3,4) initx 3 inity (^4) this.x = initx; this.y = inity;
  2. A new instance of the class is created in ObjectLand with the instance variables specified in the class initialized to the default values of their type.
  3. A reference to the new instance is stored in a variable named this in the constructor method execution frame.
  4. The body of the constructor method is executed.
  5. The value in this is returned as the result of the constructor method. 3 (^34) 4

Objects From Scratch 19- 7 Some Buggle Instance Methods // Some instance methods in the Buggle class public Location getPosition () { } public Location setPosition (Location loc) { } // getHeading(), setHeading(), getColor(), setColor() // brushUp(), and brushDown() are similar. Objects From Scratch 19- 8 More Buggle Instance Methods // More instance methods in the Buggle class // Some methods from the Direction contract are helpful for these! public Location left () { } public Location forward () { // Donโ€™t worry about walls } public Location forward (int n) { // Donโ€™t worry about walls }

Objects From Scratch 19- 9 Extending Buggles with New State Letโ€™s define a CountingBuggle class with an instance variable named count used in the following instance method: public int distanceFromWall() { this.count = 0; while (!this.isFacingWall()) { // Assume we can handle walls this.count++; this.forward(); } this.backward(this.count); return this.count; } For example: CountingBuggle counter = new CountingBuggle(); counter.brushUp(); System.out.println(counter.distanceFromWall()); Objects From Scratch 19- 10 Defining the CountingBuggle Class public class CountingBuggle extends Buggle { // New instance variable (in addition to those of Buggles) // Constructor method public CountingBuggle() { // Implicitly executes the body of the Buggle() constructor // and then executes the body below: } // Instance method public int distanceFromWall() {โ€ฆ as defined on previous slide โ€ฆ} }

Objects From Scratch 19- 13 Summary of Class Declarations There are five kinds of declarations in a Java class: o three kinds of method declarations:

  • Instance methods
  • Class methods
  • Constructor methods o two kinds of method declarations:
  • Instance variables
  • Class variables