© 2007 lawrenceville press slide 1 chapter 7 top-down development problem-solving approach ...
TRANSCRIPT
![Page 1: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/1.jpg)
© 2007 Lawrenceville PressSlide 1
Chapter 7Chapter 7
Top-Down DevelopmentTop-Down DevelopmentChapter 7Chapter 7
Top-Down DevelopmentTop-Down Development
Problem-solving approach
Breaking a task down into smaller subtasks
First level of subtasks translates into the main() method
Levels of tasks below main() are developed into a series of additional methods
![Page 2: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/2.jpg)
© 2007 Lawrenceville PressSlide 2
Chapter 7Chapter 7
Using MethodsUsing MethodsChapter 7Chapter 7
Using MethodsUsing Methods
Used to implement a specific task
Methods must be part of a class
A main() method defines the first level of subtasks with calls to methods that implement the subtasks
Using methods to define tasks is called procedural abstraction
![Page 3: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/3.jpg)
© 2007 Lawrenceville PressSlide 3
Chapter 7Chapter 7
A MethodA MethodChapter 7Chapter 7
A MethodA Method
public static void fahrenheitToCelsius() {double fTemp, cTemp;Scanner input = new Scanner(System.in);
System.out.println("Enter a Fahrenheit temperature: ");fTemp = input.nextDouble();input.close;
cTemp = (double)5/(double)9 * (fTemp - 32);System.out.println("The Celsius temperature is "+cTemp);
}
class
meth
od
class
meth
od
access
level
access
level
retu
rn ty
pe
retu
rn ty
pe
method name
method name
bodybody
![Page 4: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/4.jpg)
© 2007 Lawrenceville PressSlide 4
Chapter 7Chapter 7
Method ParametersMethod ParametersChapter 7Chapter 7
Method ParametersMethod Parameters
A method can have parameters for accepting values from a calling statement
Parameters are used inside the method to perform the method's task
The data passed to a method are called arguments
The drawBar() method declaration has one parameter named length:
public static void drawBar(int length)
![Page 5: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/5.jpg)
© 2007 Lawrenceville PressSlide 5
Chapter 7Chapter 7
Pass by ValuePass by ValueChapter 7Chapter 7
Pass by ValuePass by Value
In Java, arguments are passed by value
A primitive data type gives the method a copy of its value. The method does not have access to the original data.
An object gives the method a copy of its reference that points to methods for changing object data. A method can change the data stored in an object because the method has access to the object's methods.
![Page 6: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/6.jpg)
© 2007 Lawrenceville PressSlide 6
Chapter 7Chapter 7
Multiple ParametersMultiple ParametersChapter 7Chapter 7
Multiple ParametersMultiple Parameters
A method can have multiple parameters
Multiple parameters must be separated by commas
The order of the arguments passed must match the order of the parameters
The modified drawBar() method declaration has two parameters:public static void drawBar(int length, String mark)
![Page 7: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/7.jpg)
© 2007 Lawrenceville PressSlide 7
Chapter 7Chapter 7
Method OverloadingMethod OverloadingChapter 7Chapter 7
Method OverloadingMethod Overloading
More than one method of the same name can be included in a class
The compiler uses the types, order, and number of parameters to determine which method to execute
![Page 8: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/8.jpg)
© 2007 Lawrenceville PressSlide 8
Chapter 7Chapter 7
The return StatementThe return StatementChapter 7Chapter 7
The return StatementThe return Statement
A return statement is used to send a value back to the calling statement
A return statement can return only one value
A method that returns a value must include the return type in the method declaration. For example, the cubeOf() method returns a double:
public static double cubeOf(double x)
A method that returns a value is called from a statement that will make use of the returned value. For example:
cube = cubeOf(num);
![Page 9: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/9.jpg)
© 2007 Lawrenceville PressSlide 9
Chapter 7Chapter 7
Documenting MethodsDocumenting MethodsChapter 7Chapter 7
Documenting MethodsDocumenting Methods
Methods should be carefully commented so that a reader of the program understands what task the method is performing and what data, if any, will be returned by the method
Method documentation should appear just above a method
Documentation should include a brief description of the method, any preconditions, and the postcondition
![Page 10: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/10.jpg)
© 2007 Lawrenceville PressSlide 10
Chapter 7Chapter 7
Preconditions and Preconditions and PostconditionsPostconditions
Chapter 7Chapter 7
Preconditions and Preconditions and PostconditionsPostconditions The precondition states what must be true at
the beginning of a method for the method to work properly.
The postcondition states what must be true after the method has executed if the method has worked properly.
Preconditions and postconditions should not state facts that the compiler will verify. They should also not refer to variables or information outside the method.
The postcondition should not state how the method accomplished its task.
![Page 11: © 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development Problem-solving approach Breaking a task down into smaller subtasks First level](https://reader036.vdocuments.mx/reader036/viewer/2022082710/56649e025503460f94aed7b7/html5/thumbnails/11.jpg)
© 2007 Lawrenceville PressSlide 11
Chapter 7Chapter 7
The GradeConverter FlowchartThe GradeConverter FlowchartChapter 7Chapter 7
The GradeConverter FlowchartThe GradeConverter Flowchart