Documentation, Reference Parameters, Command

Download Report

Transcript Documentation, Reference Parameters, Command

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.
 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
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.
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 JavaDocs:
 @param – parameters
 @return – return description
 Example: MethodJavaDoc.java
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.
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.
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.
Command Line Arguments
 Remember the main method header?
public static void main(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 args array.
 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!
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.
o Depending on the input, take in the appropriate input in order to perform the
function (coordinates, lengths of sides)
o Compute the area.
o 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