Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS0007: Introduction to Computer Programming Methods: Documentation, Reference Parameters, Modularization 2.

Similar presentations


Presentation on theme: "CS0007: Introduction to Computer Programming Methods: Documentation, Reference Parameters, Modularization 2."— Presentation transcript:

1 CS0007: Introduction to Computer Programming Methods: Documentation, Reference Parameters, Modularization 2

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 problem is 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: 1. A short description of what the method does 2. The names of the parameters and what they are used for 3. 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 – – return description Example: MethodJavaDoc.java

5 Value Parameters public static void method1() { int x = 23; method2(x); System.out.println(x); } public static void method2(int y) { 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 public static void method1() { DecimalFormat formatter = new DecimalFormat("#.0"); method2(formatter); System.out.println(formatter.format(3.5)); } public static void method2(DecimalFormat inFormatter) { 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 formatter changes 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 public static void method1() { String x = "Heim"; method2(x); System.out.println(x); } public static void method2(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.

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. oDepending on the input, take in the appropriate input in order to perform the function (coordinates, lengths of sides) oCompute the area. oDisplay 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


Download ppt "CS0007: Introduction to Computer Programming Methods: Documentation, Reference Parameters, Modularization 2."

Similar presentations


Ads by Google