1 / 9

Methods: Documentation, Reference Parameters, Modularization 2

Methods: Documentation, Reference Parameters, Modularization 2. CS0007: Introduction to Computer Programming. Review. Methods are… a collection of statements that perform a specific task. Why do we have methods? to break problems in smaller, more manageable, subproblems .

decker
Download Presentation

Methods: Documentation, Reference Parameters, Modularization 2

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. Methods: Documentation, Reference Parameters, Modularization 2 CS0007: Introduction to Computer Programming

  2. Review • Methods are… • a collection of statements that perform a specific task. • Why do we have methods? • to break problems in smaller, more manageable, subproblems. • Instead of writing one long method that contains all of the statements necessary to solve a problem, writing several small methods that solve each specific part of the problemis called… • Divide and Conquer • If you use Divide and Conquer to design your programs you are using… • Modular Design

  3. Review • A method is only executed when… • called. • An argument is… • data passed to a method during a method call. • The local variables in methods that take values from arguments are called… • parameters • void methods are… • methods that do not return a value. • Value returning methods return a value to where? • where it was called.

  4. Documenting a Method • When documenting a method, you should include three things: • A short description of what the method does • The names of the parameters and what they are used for • A description of what the method returns • Example: MethodDoc.java • You will be expected to include this kind of comments for the methods you write • You can use special tags in your documentation comments to produce better documentation in JavaDocs: • @param– parameters • @return – return description • Example: MethodJavaDoc.java

  5. Value Parameters publicstaticvoidmethod1() { intx = 23; method2(x); System.out.println(x); } publicstaticvoidmethod2(inty) { y = 11; System.out.println(y); } • What will this display to the screen if method1 is called? • Answer: 11then 23 • The value of x does not change despite the fact that it is passed as an argument to a parameter whose value does change. • This is because all arguments that are primitive types are passed by value (value parameter) • When a variable is passed by value, only a copy of the variables value is passed to the corresponding parameter.

  6. Reference Parameters publicstaticvoidmethod1() { DecimalFormatformatter = newDecimalFormat("#.0"); method2(formatter); System.out.println(formatter.format(3.5)); } publicstaticvoidmethod2(DecimalFormatinFormatter) { inFormatter.setMaximumFractionDigits(2); System.out.println(inFormatter.format(3.5)); } • What will this display to the screen if method1 is called? • Answer: 3.50 then 3.50 • The value of formatterchanges because it is used as an argument whose corresponding parameter’s (inFormatter) value is changed. • This is because all arguments that are reference variables are passed by reference (reference parameters). • When an argument is passed by reference, the reference to the object that the argument is pointing to is sent to the corresponding parameter.

  7. Reference Parameters publicstaticvoid method1() { String x = "Heim"; method2(x); System.out.println(x); } publicstaticvoidmethod2(String y) { y = "Eric"; System.out.println(y); } • What will this display to the screen if method1 is called? • Answer: Eric then Heim • Wait…isn’t x a reference variable, so shouldn’t it change when y does? • Answer: No, because Strings are immutable in Java. • If an object is immutable, it means it cannot be changed.

  8. Command Line Arguments • Remember the main method header? publicstaticvoidmain(String[] args) • Notice it has a string array parameter args…what is that? • Answer: args accepts command-line arguments. • Command line arguments are arguments that can be passed to a program when running it from the command line. • Java Program Here I am • “Here”, “I”, and “Am”become the three elements in the argsarray. • Example: CommandLineArgs.java • Notice, main can take a variable number of arguments. • How? • Answer: It accepts an array of any length! • If you allow a parameter to be an array, you can allow the method to take a variable number of arguments!

  9. Modular Design • Again, the goal of modular design is to break a problem down into smaller subproblems. • Then, when you go to program a solution to the problem, you can create a method that solve each subproblem • For instance, say we wanted to create a program that would do the following: • Take in a user’s desired function: • Find the area of a triangle given the lengths of the sides. • Find the area of a rectangle given the length and width. • Find the area of a regular pentagon given the length of a side. • Depending on the input, take in the appropriate input in order to perform the function (coordinates, lengths of sides) • Compute the area. • Display the area. • We can write a program in one long method, but that could be overwhelming and hard to read… • OR we could break it down into modules for us to program one at a time. • GeometryCalculator.java

More Related