week-07 - COMP 202 Week 7 COMP 202 Introduction to...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 •E x a m p l e - 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); } }
Background image of page 2
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 •E x a m p l e - 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); } }
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
COMP 202 – Introduction to Computing 1 COMP 202 - Week 7 9 ParamPassTest.java public class ParamPassTest { public static void main (String args[]) { int param = 3; addOne(param); System.out.println("param in main is " + param); } public static void addOne(int p) { p = p + 1; System.out.println("param inside addOne is " + p);
Background image of page 9

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

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

Page1 / 56

week-07 - COMP 202 Week 7 COMP 202 Introduction to...

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

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