lecture11 - Administrative Issues Method Overloading Method...

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

View Full Document Right Arrow Icon
Method Overloading, Method Decomposition, and Object Relationships Lecture 11 Administrative Issues • Ongoing – Program 2 due tonight at 11pm – HW 2 due tomorrow (June 5) • Coming Soon… – MIDTERM EXAM: FRIDAY JUNE 6 – Review tomorrow… Yesterday… • Covered methods in detail – Return statements – Formal and Actual Parameters – Constructors • Write the Java code to define the Umbrella class from a UML Class Diagram UML Object Diagrams davesUmbrella: Umbrella • We have the following constructor header public Umbrella(String newColor) { } • Given the following UML Object Diagrams, the code to instantiate the objects uses both the new operator and the constructor annesUmbrella: Umbrella color = “Blue” open = false; color = “Pink” open = false; davesUmbrella = new Umbrella(“Blue”); annesUmbrella = new Umbrella(“Pink”); Method Overloading • Usually, method names are unique • However… Method overloading allows multiple methods to share the same name – Overloaded methods must have different parameter lists We’ve Already Used Overloaded Methods println() is heavily overloaded • You have used it without knowing! • Just a few variations println(String s); println( int i); println( double d); println( char c); println( boolean b); println(Object o);
Background image of page 1

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

View Full DocumentRight Arrow Icon
Overloading println() System.out.println(“I am ”); System.out.println(age); System.out.println(“years old.”); • Two different methods are being called – First and third, println(String s); – Second, println( int i); Overloading Example // Even weight version public float average( int num1, int num2) { return ((num1+num2)/2.0); } // Arbitry weight version public float average( int num1, float weight1, int num2, float weight2) { float part1 = num1 * weight1; float part2 = num2 * weight2; return (part1+part2); } Overloading Example int num1=90, num2=100; float weight1=0.4, weight2=0.6; System.out.println(myObj.average(num1, num2)); System.out.println(myObj.average(num1, weight1, num2, weight2)); // Even weight version public float average( int num1, int num2) { } // Arbitry weight version public float average( int num1, float weight1, int num2, float weight2) { } What Public Methods To Provide? • Object Services
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

lecture11 - Administrative Issues Method Overloading Method...

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

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