ch03-3Obj - UsingObjects Ch.3.3 (C),2007.Theymaynotbe...

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

View Full Document Right Arrow Icon
1 Building Java Programs Using Objects Ch. 3.3 These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2007.  They may not be  rehosted, sold, or modified without expressed permission from the authors.  All rights reserved.
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
2 Point objects suggested reading: 3.3
Background image of page 2
3 Point objects Java has a class of objects named  Point . To use  Point , you must write:   import java.awt.*; Constructing a  Point  object, general syntax: Point   <name> = new Point( <x> , <y> ); Point   <name> = new Point(); // the origin, (0, 0) Examples: Point p1 = new Point(5, -2); Point p2 = new Point(); Point  objects are useful for several reasons: They store two values, an (x, y) pair, in a single variable. They have useful methods we can call in our programs.
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
4 Point object methods Data stored in each  Point  object: Useful methods of each  Point  object: Point  objects can also be printed using  println  statements: Point p = new Point(5, -2); System.out.println(p); // java.awt.Point[x=5,y=-2] Method name Description distance( p ) how far away the point is from point  p setLocation( x , y ) sets the point's x and y to the given values translate( dx , dy ) adjusts the point's x and y by the given amounts Field name Description x the point's x-coordinate y the point's y-coordinate
Background image of page 4
5 Using Point objects An example program that uses  Point  objects: import java.awt.*; public class PointMain { public static void main(String[] args) { // construct two Point objects Point p1 = new Point(7, 2); Point p2 = new Point(4, 3); // print each point and their distance apart System.out.println("p1 is " + p1); System.out.println("p2: (" + p2.x + ", " + p2.y + ")"); System.out.println("distance = " + p1.distance(p2)); // translate the point to a new location p2.translate(1, 7); System.out.println("p2: (" + p2.x + ", " + p2.y + ")"); System.out.println("distance = " + p1.distance(p2)); } }
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
6 Point objects question Write a program that computes a right triangle's perimeter. The perimeter is the sum of the triangle's side lengths 
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 21

ch03-3Obj - UsingObjects Ch.3.3 (C),2007.Theymaynotbe...

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

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