# L27 - Computer Science 101 SS C Lecture 27 Contents 2 Point...

This preview shows pages 1–3. Sign up to view the full content.

CompSci101 Lecture 28 1 Computer Science 101 SS C Lecture 27 Contents Point class Rectangle class An application which uses Rectangle objects. Coursebook: §15.7 2 Point objects Point objects are used for storing information about a position in space. A Point object stores two int values: an x and y value. Some example Point instances are shown on the right: 3 The awt package The Point class is deFned in the java.awt package. The statement: must be at the top of any class which uses Point objects. import java.awt.*; 4 Point class - constructor To create a Point object we commonly use one of the following constructors: public Point(int x, int y) public Point(Point p) Construct and initialises a point at the speciFed (x,y) location in the coordinate space. Construct and initialises a point with the same location as the speciFed Point, p. 1 2 3 4 5 6 7 8 9 10 11 5 Ex01 - Draw the 4 Point objects public class L27 { public void start() { int xPos = 20; int yPos = 15; } } Point pt1 = new Point(xPos, yPos); Point pt2 = new Point(10, 30); Point pt3 = pt2; Point pt4 = new Point(pt3); import java.awt.*; 1 2 3 4 5 6 7 8 9 10 11 6 Points - public instance variables The two instance variables of the Point class are public, i.e. we can access these two instance variables directly: import java.awt.*; public class L27 { public void start() { Point point1 = new Point(20, 15); Point point2 = new Point(10, 35); } } //output point2 .x = point1 .y ; point1 .x = point2 .y ; int xSum = point1 .x + point2 .x ; System.out.println(xSum);

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

View Full Document
CompSci101 Lecture 28 2 7 Points - instance methods public void move(int x, int y) Moves this point to the specifed location in the (x,y) coordinate plane. public void translate(int dx, int dy) Translates this point, at location (x,y), by dx along the x axis and dy along the y axis so that it now represents the point (x+dx, y+dy). public String toString() Returns a string representation oF this point and its location in the (x,y) coordinate space. public boolean equals(Point pt) Determines whether or not two points are equal. 1 2 3 4 5 6 7 8 9 10 11 12 8 Points - move() import java.awt.*; public class L27 { public void start() { Point point1 = new Point(20, 15); Point point2 = new Point(10, 35); } } //output point1.move(10, 8); point2.move(point2.y, point2.x); String outS1 = point1.x + "," + point1.y; String outS2 = point2.x + "," + point2.y; System.out.println(outS1 + " " + outS2); 1 2 3 4 5 6 7 8 9 10 11 12 9 Points - translate() import java.awt.*; public class L27 { public void start() { Point point1 = new Point(20, 15); Point point2 = new Point(10, 35); } } //output point1.translate(10, 8);
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

### Page1 / 5

L27 - Computer Science 101 SS C Lecture 27 Contents 2 Point...

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

View Full Document
Ask a homework question - tutors are online