1 / 16

COMP 14: decomposition, overloading, relationships

COMP 14: decomposition, overloading, relationships. June 7, 2000 Nick Vallidis. Announcements. Midterm exam is on Friday Review tomorrow. Homework. read 4.1-4.6 assignment P4 is due next Tuesday! (doing it is a good way to study for the exam…)

nerita
Download Presentation

COMP 14: decomposition, overloading, relationships

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 14: decomposition, overloading, relationships June 7, 2000 Nick Vallidis

  2. Announcements • Midterm exam is on Friday • Review tomorrow

  3. Homework • read 4.1-4.6 • assignment P4 is due next Tuesday! (doing it is a good way to study for the exam…) • e-mail me any questions you'd like me to answer in the review tomorrow (do this before tomorrow morning)

  4. Review • What is encapsulation? • What is an abstraction? • What are the visibility modifiers and what do they do? • How is a method declared?

  5. Today • Constructors • Object relationships • Method overloading • Method Decomposition

  6. Constructors • A constructor is a special method that is used to set up a newly created object • The programmer does not have to define a constructor for a class • There is a "default constructor" automatically created for you that does nothing.

  7. Constructors • When writing a constructor, remember that: • it has the same name as the class • it does not return a value • it has no return type, not even void • it often sets the initial values of instance variables

  8. Object relationships • An aggregate object is an object that contains references to other objects • An Account object is an aggregate object because it contains a reference to a String object (p. 189) • An aggregate object represents a has-a relationship • A bank account has a name

  9. Object relationships • Sometimes an object has to interact with other objects of the same type • For example, we might concatenate two String objects together as follows s3 = s1.concat(s2); • One object (s1) is executing the method and another (s2) is passed as a parameter

  10. Method overloading • Method overloading is the process of using the same method name for multiple methods • The signature of each overloaded method must be unique • The signature includes the number, type, and order of the parameters

  11. Method overloading • The compiler must be able to determine which version of the method is being invoked by analyzing the parameters • The return type of the method is not part of the signature

  12. Version 1 Version 2 float tryMe (int x, float y) { return x*y; } float tryMe (int x) { return x + .375; } Invocation result = tryMe (25, 4.32) Method overloading

  13. Overloaded methods • The println method is overloaded: println (String s) println (int i) println (double d) • The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total);

  14. Overloading constructors • Constructors can be overloaded • An overloaded constructor provides multiple ways to set up a new object

  15. Method decomposition • A method should be relatively small, so that it can be readily understood as a single entity • A potentially large method should be decomposed into several smaller methods as needed for clarity • Therefore, a service method of an object may call one or more support methods to accomplish its goal

  16. Guideline for decomp. • A general rule is to make your methods small enough so that they can fit on one screen • Definitely ok to be shorter • can be longer if decomposing it introduces unnecessary complexity

More Related