Object-Oriented Programming Details: References, Overloaded Methods, and Garbage Collector

Slide Note
Embed
Share

Today in class, we covered returning half of Exam 3, focusing on object-oriented programming intricacies such as passing references as arguments, overloaded methods, and the role of the garbage collector. Modifications to lab requirements were discussed to ensure comprehensive use of implemented methods.


Uploaded on Sep 29, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. SE1011 Week 7, Class 3 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. Modification to requirements for lab Your game must make use of all of the methods implemented in the Die (or alternative) class. Your game must make use of appropriate methods so that no single method is too complicated. Don't make any static methods except main. You must document how to play your game. SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 2

  3. Lab 7 Upload Your main class must be Lab7.java Your Die class must be Die.java All other classes can have any name you want you shouldn't name them Class1.java, Class2.java, etc. These are completely optional. SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 3

  4. Passing references as arguments Complex r3 = new Complex(3); Complex i = new Complex(0,1); Complex prod = i.multBy(r3); sout("result: "+prod); // Desired output: "result: 0.0 + 3.0i" SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 4

  5. Passing references as arguments Complex r3 = new Complex(3); Complex i = new Complex(0,1); Complex prod = i.multBy(r3); sout("result: "+prod.toString()); // Desired output: "result: 0.0 + 3.0i" SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 5

  6. Passing references as arguments Complex r3 = new Complex(3); Complex i = new Complex(0,1); Complex prod; prod = i.multBy(r3); sout("result: "+prod.toString()); // Desired output: "result: 0.0 + 3.0i" SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 6

  7. the multBy method main( ) 85638 real real 3.0 r3 Complex(double real, double imag) this 85642 double 85638 imag Complex ref 0.0 Complex ref other double i Complex 85638 85642 85642 Complex ref Complex ref real 0.0 double imag prod 1.0 double Complex Complex ref 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; } 7

  8. the multBy method main( ) 85638 real real 3.0 r3 Complex(double real, this double imag) 85642 double 85638 imag Complex ref 0.0 Complex ref other double i Complex 85638 85642 85642 Complex ref Complex ref real 0.0 result double imag 85646 prod 1.0 Complex ref double Complex Complex ref public Complex multBy(Complex other) { Complex result = new Complex(); // HERE result.real = this.real * other.real - this.imag * other.imag; result.imag = this.real * other.imag + this.imag * other.real; return result; } 85646 real 0.0 double imag 0.0 double Complex 8

  9. the multBy method main( ) 85638 real real 3.0 r3 Complex(double real, this double imag) 85642 double 85638 imag Complex ref 0.0 Complex ref other double i Complex 85638 85642 85642 Complex ref Complex ref real 0.0 result double imag 85646 prod 1.0 Complex ref double Complex Complex ref public Complex multBy(Complex other) { Complex result = new Complex(); result.real = this.real * other.real - this.imag * other.imag; result.imag = this.real * other.imag + this.imag * other.real; // HERE return result; } 85646 real 0.0 double imag 3.0 double Complex 9

  10. the multBy method main( ) 85638 real real 3.0 r3 double 85638 imag 0.0 Complex ref double i Complex 85642 85642 Complex ref real 0.0 double imag prod 1.0 double 85646 Complex Complex ref 85646 public static void main(String[] args) { Complex r3 = new Complex(3); Complex i = new Complex(0,1); Complex prod = i.multBy(r3); // HERE System.out.println("result: "+prod); real 0.0 double imag 3.0 double Complex } 10

  11. Exercise: What is the state of memory at "HERE 2"? public void mystery(Incubator other) { long total; // HERE 1 total = this.numBacteria + other.numBacteria; this.numBacteria = total/2; other.numBacteria = total/2; // HERE 2 } SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 11

  12. Exercise: What is the state of memory at "HERE 2"? main( ) 85638 numBacteria real 1_000_000_000 inc1 Complex(double real, double imag) this 85638 long 85638 Incubator ref Incubator ref other inc2 Incubator 85642 85642 85642 Incubator ref Incubator ref numBacteria 2_000_000_000 total long long Incubator public void mystery(Incubator other) { long total; // HERE 1 total = this.numBacteria + other.numBacteria; this.numBacteria = total/2; other.numBacteria = total/2; // HERE 2 } 12

  13. How to allow this? (In addition to the other) Complex i = new Complex(0,1); Complex prod = i.multBy(3.0); sout("result: "+prod.toString()); // Desired output: "result: 0.0 + 3.0i" SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 13

  14. SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 14

  15. Acknowledgement This course is based on the text Introduction to Programming with Java by Dean & Dean, 2nd Edition SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder 15

Related