1 / 12

Methods

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.

Download Presentation

Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Java APIApplication 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

  3. Example:Programmer defined methodUses 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

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

  5. Example of simple recursion:Fibonacci sequencep. 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

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

  7. Bubble Sortp. 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

  8. Binary Searchp. 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

  9. Multidimensional Array Examplep. 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

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

  11. “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.

More Related