Download presentation
Presentation is loading. Please wait.
Published byEugene Lyons Modified over 9 years ago
1
SE1011 Week 8, Class 1 Today Return Half Exam 3 (I have it with me) Object Oriented Programming Details References as arguments Overloaded methods Garbage collector again Muddiest Point SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 1
2
Muddiest Point how to specify which objects are used as "this" and "other" reference passing So was the last example an example of why we want the instance variables to be private? So that you can't edit the instances from another class without calling a method?public/private how do objects actually work? object implementation A professor assigned a task worth 1 point. The task was to mix dirt and water. Now that's a muddy point. So was this a lesson on how not to use objects?unused objects what is the significance of the keyword "new" when calling a new instance of a class? What happens if we don't use it?new keyword new diagram type: do we need to learn it? What exactly does each line/shape mean? sequence diagram SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 2
3
Muddiest Point The diagrams started making sense at the end of the hour. The lifelines especially were a little confusing at firstasequence diagram Using 'this' in constructorsthis how to retrieve the values stored in objects and use them as argumentsaccessor method why wold we call things in this way?reference passing? Why you would want to forget or delete the old class?garbage collection the diagram for the complex driver and user input was not clear to mesequence diagram Is there ever a time we want to write an unused Object?unused objects The new types of diagram seems a bit confusingsequence diagram SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 3
4
Object Construction new Complex(8,6); // why do this? Complex len5; len5 = new Complex(3,4); SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 4
5
the multBy method 5 public Complex multBy(Complex other) { // HERE Complex result = new Complex(); result.real = this.real * other.real - this.imag * other.imag; result.imag = this.real * other.imag + this.imag * other.imag; return result; } main(…) 85638 Complex real double imag double real 3.0 0.0 Complex ref this 85642 Complex(double real, double imag) Complex ref r3 85638 Complex ref i 85642 Complex real double imag double 0.0 1.0 Complex ref other 85638 Complex ref prod public static void main(String []) { … // HERE prod = i.multBy(r3); … }
6
Swapping Methods BUGGY swapping code: int x = 4; int y = 5; x = y; y = x; System.out.println("x: "+x); System.out.println("y: "+y); What does this code print when it is run? Why does it print this? (Can you fix it?) 6
7
Swapping bacteria in two incubators public void swap(Incubator other) { long temp = this.numBacteria; this.numBacteria = other.numBacteria; other.numBacteria = temp; } SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 7
8
How to allow both of these this? Complex i = new Complex(0,1); Complex prod1 = i.multBy(3.0); Complex angle45 = new Complex(1,1); Complex prod2 = i.multBy(angle45); sout("result: "+prod1.toString()); // Desired output: "result: 0.0 + 3.0i" sout("result: "+prod1.toString()); // Desired output: "result: -1.0 + 1.0i" 8
9
SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 9
10
Acknowledgement This course is based on the text Introduction to Programming with Java by Dean & Dean, 2 nd Edition SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 10
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.