Download presentation
Presentation is loading. Please wait.
Published byMorgan Rogers Modified over 9 years ago
2
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions or procedures
3
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
4
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
5
Recursion! P. 250 - 259 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
6
Example of simple recursion: Fibonacci sequence p. 253-258 0,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
7
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
8
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
9
Binary Search p. 302-306 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
10
Multidimensional Array Example p. 312-313 – 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
11
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
12
“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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.