Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape.
Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Multiple-Subscripted Arrays Multiple subscripts –a[ i ][ j ] –Tables with rows and columns –Specify.
Searching Arrays Linear search Binary search small arrays
Chapter 9 Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using Arrays 7.5 References and Reference Parameters.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
COS 260 DAY 16 Tony Gauvin.
1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
8/9: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
Arrays Multi-dimensional initialize & display Sample programs Sorting Searching Part II.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
Chapter 12 Recursion, Complexity, and Searching and Sorting
4/15: Searching Arrays Look at BubbleSort.java Searching arrays: the linear search Searching arrays: the binary search.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Objectives - 11  We will work with processing Arrays.  Objectives:  Describe the concept of an array and its benefits.  Define the terms index, traverse,
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
CS1101: Programming Methodology
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
11/30: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays:
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
8/2: Recursion About Scoping.java Recursion Program of the Day.
8/8: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays: the.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Objectives You should be able to describe: One-Dimensional Arrays
Data Structures Array - Code.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array.
ㅎㅎ Fourth step for Learning C++ Programming Two functions
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using Arrays 7.5 References and Reference.
Constructor Laboratory /12/4.
Data Structures Array - Code.
Chapter 7 Part 2 Edited by JJ Shepherd
C Arrays (2) (Chapter 6) ECET 264.
Data Structures (CS212D) Week # 2: Arrays.
Arrays.
Presentation transcript:

Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions or procedures

Java API Application Programming Interface java.sun.com/j2se/1.4.1/docs/api/index.html Reduces programming development time Avoids introducing programming errors Examples: –Math class methods –String class methods –Integer class methods

Example: Programmer defined method Uses prepackaged Math class method Import java.awt.Container; Import javax.swing; Public class PowerTest extends JApplet{ public void init () { String s1 = JOptionPane.showInputDialog(“Enter the base value”); String s2 = JOptionPane.showInputDialog(“Enter the exponent value”); double base = Double.parseDouble (s1); double exponent = Double.parseDouble (s2); double power = computePower (base, exponent); JTextArea outputArea = new JTextArea(); outputArea.setText (base + “ raised to the power of : “ + exponent + “ = “ + power); Container container = getContentPane (); container.add (outputArea); } public double computePower (double x, double y) { return Math.pow(x,y); }//end computePower }//end PowerTest

Recursion! P Calls itself directly or indirectly Has a base case which ends recursion Recursive calls result in simpler problems which get closer to the base case Each recursive call is added to the stack Memory can become exhausted

Example of simple recursion: Fibonacci sequence p ,1,1,2,3,5,8,13,21,… public long fibonacci (long n) { // base case if (n == 0 || n == 1) return n; //recursive call else return fibonacci (n–1) + fibonacci (n–2); } //end fibonacci

Arrays! Data structure Object A list of elements of the same type Elements referenced: subscript or index Index starts with zero Length field For loops

Bubble Sort p. 298 public void bubbleSort (int array[ ]) { for (int pass = 1; pass < array.length; pass++) { for (int element = 0; element < array.length -1; element++) { if (array [element] > array [element +1] ) swap (array, element, element + 1); }//end inner for loop for comparisons }//end outer for loop to control passes }//end bubbleSort

Binary Search p public int binarySearch (int array[ ], int key) { int low = 0; int high = array.length – 1; int middle; while (low <= high) { middle = (low + high) / 2; buildOuput (array, low, middle, high); if (key == array [middle] ) return middle; else if (key < array [middle] ) high = middle – 1; else low = middle + 1; }//end while return -1; }//end binarySearch

Multidimensional Array Example p – building output string for a two- dimensional array public void buildString ( ) { output += “ “; for (int counter = 0; counter < exams; counter++ ) output += “[“ + counter + “] “; for (int row = 0; row < students; row++) { output += “\ngrades [“ + row + “] “; for (int column = 0; column < exams; column++) output += grades [row] [column] + “ “; }//end outer for }//end buildString

Summary Read over the chapters and these slides Ask questions! Practice! Next step: –Get in your teams –Get your assignment –See what you can do

“Review” Programming Assignment Write an applet that prints a table that tells us the fate of our $10,000 investment under various interest rate scenarios (from 5% to 10% in increments of 0.50%) if we keep the money for 5, 10, 15, 20, and 30 years. Note, that if we keep the money for 30 years, that investment grows to almost $200,000 at 10% interest! Your solution should involve applets, methods, arrays, repetition, and selection. All of our review topics! Get started now in lab by working together to design your test code and writing your psued-ocode (CALTAL). Work with another person to brainstorm about a plan. Turn in what you come up with at the end of today’s lab. Now, on your own, finish! Due one week from today.