1 / 11

CSC 1051 – Data Structures and Algorithms I

CSC 1051 – Data Structures and Algorithms I. Designing Methods & Classes. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/. Last time:. Lab 7: Creating methods Syntax for declaring a method –

vine
Download Presentation

CSC 1051 – Data Structures and Algorithms I

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. CSC 1051 – Data Structures and Algorithms I Designing Methods & Classes Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ CSC 1051 M.A. Papalaskari, Villanova University

  2. Last time: Lab 7: Creating methods • Syntax for declaring a method – • method header • method body • Method invocation • Parameters CSC 1051 M.A. Papalaskari, Villanova University

  3. Next • More about methods • Designing classes CSC 1051 M.A. Papalaskari, Villanova University

  4. //********************************************************************// FancySnowman.java Author: Awesome Learner//// Learning applets in Lab 5 and methods in Lab 7, wow.//********************************************************************importjavax.swing.JApplet;importjava.awt.*;publicclass Snowman extendsJApplet{ //-----------------------------------------------------------------// Draws a winter scene, including some snowmen.//-----------------------------------------------------------------publicvoid paint (Graphics page){ << paint method-body >> } //-----------------------------------------------------------------// Draws a snowman at MID, TOP.//----------------------------------------------------------------- publicvoidpaintSnowman(Graphics page, int MID, int TOP){ << paintSnowman method-body >> } } Lab 7 applet CSC 1051 M.A. Papalaskari, Villanova University

  5. Method Header • A method declaration begins with a method header char calc (int num1, int num2, String message) method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal parameter return type CSC 1051 M.A. Papalaskari, Villanova University

  6. Method Body • The method header is followed by the method body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } sumand result are local data They are created each time the method is called, and are destroyed when it finishes executing The return expression must be consistent with the return type CSC 1051 M.A. Papalaskari, Villanova University

  7. compute myMethod myMethod(); Method Control Flow • If the called method is in the same class, only the method name is needed CSC 1051 M.A. Papalaskari, Villanova University

  8. main doIt helpMe obj.doIt(); helpMe(); Method Control Flow • The called method is often part of another class or object CSC 1051 M.A. Papalaskari, Villanova University

  9. char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } Parameters • When a method is called, the actual parameters in the invocation are copied into the formal parameters in the method header ch = obj.calc (25, count, "Hello"); CSC 1051 M.A. Papalaskari, Villanova University

  10. Examples: • Write a method that has one int parameter num, and prints “Happy Birthday” num times • Write a method with two double paramenters a and b that computes and returns the sum of squares: a2 + b2of its two int parameters • Write a method that has one int parameter num, and returns the String “Happy Birthday” num times • Write a method that has one char parameter (assume it is a letter) and returns that char shifted forward 3 letters of the alphabet (with wrap-around). So ad, be, …, xa, yb, zc. CSC 1051 M.A. Papalaskari, Villanova University

  11. Homework • Chapter 4, Section 4.4 • EX4.5- EX4.9 Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 M.A. Papalaskari, Villanova University

More Related