week-07-handout

week-07-handout - 1 COMP 202 –Introduction to Computing 1...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 COMP 202 –Introduction to Computing 1 COMP 202 - Week 7 1 COMP 202 – Week 7 Using Objects Topics : • How to use objects in java code • Object reference as parameters and return values • Scope and garbage collection • Overloading methods COMP 202 –Introduction to Computing 1 COMP 202 - Week 7 2 Using Objects • A method in one class can use an object of another class • Example- import java.util.Random; public class RandomNumbers { public static void main (String args) { Random generator = new Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println ("A random integer: " + num1); num1 = Math.abs (generator.nextInt()) % 10; System.out.println ("0 to 9: " + num1); } } COMP 202 –Introduction to Computing 1 COMP 202 - Week 7 3 Using Objects • Another class can have objects of a class as a member variables • Example- public class Point { private int x,y; Point(int px, int py){ x = px; y = py; } public moveRight(int steps){ x += steps; } public moveLeft(int steps){ x -= steps; } } public class Line { private Point start, end; Line(int x1, int y1, int x2, int y2){ start = new Point(x1, y1); end = new Point(x2, y2); } public moveRight(int steps){ start.moveRight(steps); end.moveRight(steps); } } 2 COMP 202 –Introduction to Computing 1 COMP 202 - Week 7 4 Object names are References • Recall that an object reference holds the memory address of an object • Rather than dealing with arbitrary addresses, we often depict a reference graphically as a “pointer” to an object Box b1 = new Box(10,4); b1 COMP 202 –Introduction to Computing 1 COMP 202 - Week 7 5 Assignment Revisited • The act of assignment takes a copy of a value and stores it in a variable • For primitive types: num2 = num1; Before num1 5 num2 12 After num1 5 num2 5 COMP 202 –Introduction to Computing 1 COMP 202 - Week 7 6 Reference Assignment • For object references, assignment copies the memory location: Box b1 = new Box(10, 5); Box b2 = new Box(16, 8); b2 = b1; b1.expand(); Before b1 b2 After b1 b2 3 COMP 202 –Introduction to Computing 1 COMP 202 - Week 7 7 Aliases • Two or more references that refer to the same object are called aliases of each other • One object (and its data) can be accessed using different variables • Aliases can be useful, but should be managed carefully • Changing the object’s state (its variables) through one reference changes it for all of its aliases COMP 202 –Introduction to Computing 1 COMP 202 - Week 7 8 Passing parameters to Methods • Parameters in a Java method are passed by value • This means that a copy of the actual parameter (the value passed in) is stored into the formal parameter (in the method header) – i.e. not the real/original value • Passing parameters is essentially an assignment • When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other COMP 202 –Introduction to Computing 1 COMP 202 - Week 7 9 ParamPassTest.javaParamPassTest....
View Full Document

This note was uploaded on 06/28/2008 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 19

week-07-handout - 1 COMP 202 –Introduction to Computing 1...

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

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