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

Slides:



Advertisements
Similar presentations
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Advertisements

Procedural programming in Java
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
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.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Chapter Four Defining Your Own Classes continued.
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.
Introduction to Methods
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.
CS1101: Programming Methodology Aaron Tan.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Utilities (Part 2) Implementing static features 1.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Procedural programming in Java Methods, parameters and return values.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Classes and Objects in Java
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Methods Chapter Why Write Methods? Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Documentation Javadocs. Design/Documentation An essential ingredient of good Object Oriented programming is known as design by contract. This means that.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
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.
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.
Methods.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Chapter 5 Methods. 2 Contents 1. Introduction to Methods 2. Passing Arguments to a Method 3. More about Local Variables 4. Returning a Value from a Method.
Variable Scope & Lifetime
Lecture 3: Method Parameters
CS0007: Introduction to Computer Programming
Java Course Review.
by Tony Gaddis and Godfrey Muganda
Object Oriented Systems Lecture 03 Method
Methods.
Something about Java Introduction to Problem Solving and Programming 1.
Scope, Comments, Code Style, Keyboard Input
Chapter Topics Chapter 5 discusses the following main topics:
Starting Out with Java: From Control Structures through Objects
Starting Out with Java: From Control Structures through Objects
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
class PrintOnetoTen { public static void main(String args[]) {
Lecture 3: Method Parameters
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Lecture 5- Classes, Objects and Methods
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
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.
Presentation transcript:

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

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 – – 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. 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