Rts2 new rectanglerts0 forint i0 i rtslength i rtsix

This preview shows page 2 out of 2 pages.

rts[2] = new Rectangle(rts[0]); for(int i=0; i < rts.length; i++) { rts[i].x = rts[i].x + rts[i].height; } System.out.println("1. " + rts[0].x + " " + rts[0].y); System.out.println("2. " + rts[1].x + " " + rts[1].y); System.out.println("3. " + rts[2].x + " " + rts[2].y); System.out.println("4. " + rts[0].equals(rts[2])); System.out.println("5. " + (rts[0] == rts[1])); System.out.println("6. " + (rts[0].contains(p)) ); 8 1 2 3 4 5 6 7 8 9 10 11 12 Ex04 – Arrays are objects public class L31Ex04 { public void start() { int[] nums = {7, 6, 5, 4, 3 }; methodD(nums); System.out.println(nums[2] + ", " + nums[1] ); } private void methodD(int[] a) { for(int i = 0; i < a.length - 1; i++) { a[i+1] = a[i] + 1; } } } //What is the output? 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Ex05 – Arrays are objects import java.awt.*; public class L31Ex05 { public void start() { int[] nums = {7, 6, 5, 4, 3 }; nums = methodE(nums); System.out.println( nums[2] + ", " + nums[3] ); } private int[] methodE(int[] a) { int[] b = new int[a.length]; for(int i = 0; i < a.length; i++) { b[i] = a[i]; } a[2] = b[0]; a[3] = 2; return b; } } //What is the output? 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Ex06 – Arrays are objects import java.awt.*; public class L31Ex06 { public void start() { Rectangle[] rts = { new Rectangle(20, 15, 30, 10), new Rectangle(10, 35, 20, 5), new Rectangle(25, 5, 10, 10) }; methodF(rts); System.out.println(rts[0].x + " " +rts[1].x); } private void methodF(Rectangle[] a) { Rectangle[] b = new Rectangle[a.length]; for(int i = 0; i < a.length; i++) { b[i] = a[i]; b[i].x = b[i].x + 2; } } } //What is the output? 11 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Ex07 – Arrays are objects import java.awt.*; public class L31Ex07 { public void start() { Rectangle[] rts = { new Rectangle(20, 15, 30, 10), new Rectangle(10, 35, 20, 5), new Rectangle(25, 5, 10, 10) }; methodG(rts); System.out.println(rts[0].x + " " +rts[1].x); } private void methodG(Rectangle[] a) { Rectangle[] b = new Rectangle[a.length]; for(int i = 0; i < a.length; i++) { b[i] = new Rectangle(a[i]); b[i].x = b[i].x + 2; } } } //What is the output? 12 What you need to know Passing primitives as parameters Passing objects as parameters Handling arrays of objects Passing arrays as parameters
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