Chapter 7 Top-Down Development

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
The Web Warrior Guide to Web Design Technologies
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6 Sub Procedures
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Chapter 5 : Methods. Why Write Methods?  Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
A variable is a name for a value stored in memory.
Chapter 4 Assignment Statement
Chapter 5 Functions for All Subtasks 1
Chapter 5 Functions for All Subtasks 1
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 10: Void Functions
Chapter 3: Using Methods, Classes, and Objects
by Tony Gaddis and Godfrey Muganda
Chapter 3 Assignment Statement
Microsoft Office Illustrated
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Methods.
Method Mark and Lyubo.
Can perform actions and provide communication
One class is an extension of another.
Chapter 6 The while Statement
Chapter Topics Chapter 5 discusses the following main topics:
Chapter 6 Sub Procedures
Chapter 3 Introduction to Classes, Objects Methods and Strings
Can perform actions and provide communication
Classes, Objects, and Methods
Starting Out with Java: From Control Structures through Objects
Chapter 4 void Functions
Chapter 3 Classes and Objects
6 Chapter Functions.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Can perform actions and provide communication
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
CS360 Client/Server Programming Using Java
Using Objects (continued)
Object Oriented Programming in java
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Chapter 7 Top-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 Refer to page 157 in the text. © 2007 Lawrenceville Press

Used to implement a specific task Methods must be part of a class 7/5/2018 5:53 PM Chapter 7 Using 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 Refer to pages 157 and 158 in the text. © 2007 Lawrenceville Press

method name class method access level return type body 7/5/2018 5:53 PM Chapter 7 A Method method name access level class method return type body 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); } Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains five (5) animations. Refer to pages 159 and 160 in the text. A method declaration includes an access level. <press space bar> The keyword public is called an access modifier. A public method can be called by any other method. The keyword static declares the method a class method. <press space bar> A class method can be called from the class itself. The return type of a method indicates what type of value the method will send back to the calling statement. <press space bar> The keyword void indicates that nothing is returned. Method names should indicate an action. <press space bar> Verbs make good method names. A method names should begin with a lowercase letter and then an uppercase letter should begin each word within the name. The body of the method starts with the first opening brace and ends with the closing brace. <press space bar> © 2007 Lawrenceville Press

Chapter 7 Method Parameters 7/5/2018 5:53 PM Chapter 7 Method 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) Refer to page 160 in the text. © 2007 Lawrenceville Press

In Java, arguments are passed by value 7/5/2018 5:53 PM Chapter 7 Pass 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. Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains three (3) animations. Refer to page 161 in the text. The object instantiation, String text, associates the object name text with a memory location <press space bar> The first assignment statement points to the location in memory that stores the mixed-case string <press space bar> The second assignment statement, moves the pointer to the location in memory that stores the lowercase string <press space bar> © 2007 Lawrenceville Press

Chapter 7 Multiple Parameters 7/5/2018 5:53 PM Chapter 7 Multiple 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) Refer to page 161 in the text. © 2007 Lawrenceville Press

Chapter 7 Method Overloading 7/5/2018 5:53 PM Chapter 7 Method 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 Refer to page 162 in the text. The String class is used for representing text. The first character of a string is at index position 0. The last character is at position length()-1. © 2007 Lawrenceville Press

Chapter 7 The return Statement 7/5/2018 5:53 PM Chapter 7 The 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); Refer to page 163 in the text. © 2007 Lawrenceville Press

Chapter 7 Documenting Methods 7/5/2018 5:53 PM Chapter 7 Documenting 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 Refer to page 164 in the text. © 2007 Lawrenceville Press

Chapter 7 Preconditions and Postconditions 7/5/2018 5:53 PM Chapter 7 Preconditions and Postconditions 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. Refer to page 165 in the text. © 2007 Lawrenceville Press

Chapter 7 The GradeConverter Flowchart 7/5/2018 5:53 PM Chapter 7 The GradeConverter Flowchart Refer to page 166 in the text. The GradeConverter application begins by 1. prompting the user for a number 2. determining the letter grade that corresponds to the entered numeric grade and displaying the result 3. repeating steps 1 and 2 until the user chooses to quit © 2007 Lawrenceville Press