This can lead to unexpected results x y 3 5 110100 p1

This preview shows page 1 - 2 out of 2 pages.

This can lead to unexpected results. x y 3 5 110100 p1 110100 "Jed" 100110 name 100110 4 1 2 3 4 5 6 7 8 9 Point p1 = new Point(3, 5); p1.x = 4; methodB(p1); System.out.println(p1.x + " " + p1.y); } private void methodB(Point p2) { p2.y = p2.x * 2; System.out.println(p2.x + " " + p2.y); } Passing object parameters Object variables hold a reference or a pointer to an object of the type of the variable: (1) start() (2) methodB() x y 110100 p1 110100 p2 110100 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import java.awt.*; public class L31Ex01 { public void start() { Point p1 = new Point(50, 20); printPoint(p1, 1); methodC(p1); printPoint(p1, 2); } private void methodC(Point pt) { int temp; if (pt.x > pt.y) { temp = pt.x; pt.x = pt.y; pt.y = temp; } } private void printPoint(Point p, int num) { System.out.println(num + ". (" + p.x + "," + p.y + ")"); } } Ex01 //What is the output? 6 1 2 3 4 5 6 7 8 9 10 11 12 13 Ex02 – Handling arrays of objects Point[] pts = { new Point(20, 15), new Point(20, 34), new Point(10, 34) }; pts[1] = new Point(pts[0]); pts[2] = pts[0]; for(int i=1; i < pts.length; i++) { pts[i].translate(10, 20); pts[i-1] = pts[i]; } System.out.println("1. " + pts[0].x + "," +pts[0].y); System.out.println("2. " + pts[1].x + "," +pts[1].y); System.out.println("3. " + pts[2].x + "," +pts[2].y); System.out.println("4. " + pts[0].equals(pts[2])); System.out.println("5. " + (pts[0] == pts[1]));
Image of page 1

Subscribe to view the full document.

CompSci 101 Lecture 31 2 7 1 2 3 4 5 6 7 8 9 10 11 12 13 Ex03 – Handling arrays of objects Rectangle[] rts = { new Rectangle(20, 15, 30, 10), new Rectangle(10, 35, 20, 5), new Rectangle(25, 5, 10, 10) }; Point p = new Point(20, 20); rts[1] = rts[0];
Image of page 2
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Subroutine, new Rectangle, printPoint

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern