4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.

Slides:



Advertisements
Similar presentations
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1Introduction 25.2Primitive.
JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 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.
Final Review. Review A Binary Search divides a sorted data structure into two equal parts and each time throws away the part that cannot contain the item.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small 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.
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.
Chapter 8 ARRAYS Continued
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
 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.
Recursion A recursive computation solves a problem by using the solution of the same problem with simpler values The same computation occurs repeatedly.
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.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods 6.5 Method Definitions 6.6 Argument Promotion.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
4/15: Searching Arrays Look at BubbleSort.java Searching arrays: the linear search Searching arrays: the binary search.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
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 
Chapter 6 - Methods Outline 6.1Introduction 6.3 Math Class Methods 6.4Methods 6.5Method Definitions 6.6Java API Packages 6.7Random Number Generation 6.8Example:
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
Layout Managers Arranges and lays out the GUI components on a container.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
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.
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
 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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction 26.2Implementing a Time Abstract Data.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
11/30: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
1 Chapter 13 – More GUI Components Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic.
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.
Fibonacci Sequence Fibonacci sequence is a sequence of numbers defined by f 1 = 1 f 2 = 1 f n = f n-1 + f n-2 First ten terms – 1, 1, 2, 3, 5, 8, 13, 21,
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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods 6.5 Method Definitions 6.6 Argument.
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.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
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.
Data Structures Array - Code.
Graphical User Interface
Presentation transcript:

4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day

BinarySearch.java pt.1 //fig. 7.13: BinarySearch.java: binary search of an array import java.awt.*; import java.awt.event.*; import java.text.*; import javax.swing.*; public class BinarySearch extends JApplet implements ActionListener { JLabel enterLabel, resultLabel; JTextField enterField, resultField; JTextArea output; int array[]; String display = "";

BinarySearch.java pt.2 public void init () { Container container = getContentPane(); container.setLayout ( new FlowLayout() ); enterLabel = new JLabel ( "Enter key" ); container.add( enterLabel ); enterField = new JTextField( 5 ); enterField.addActionListener( this ); container.add( enterField ); resultLabel = new JLabel ( "Result" ); container.add( resultLabel );

BinarySearch.java pt.3 resultField = new JTextField( 22 ); resultField.setEditable( false ); container.add( resultField ); output = new JTextArea( 6, 60 ); output.setFont ( new Font( “Monospaced", Font.PLAIN, 12 ) ); container.add( output ); array = new int[ 15 ]; for ( int count = 0; count < array.length; count++ ) array[ count ] = 2 * count; }

BinarySearch.java pt.4 public void actionPerformed ( ActionEvent e ) { String searchKey = e.getActionCommand(); //initialize display string for new search display = "Portions of array searched\n"; //perform the binary search int element = binarySearch ( array, Integer.parseInt ( searchKey ) ); output.setText( display ); if ( element != -1 ) result.setText( "Found it in slot " + element ); else result.setText( "Value not found" ); }

BinarySearch.java pt.5 //binary search method public int binarySearch ( int array2[], int key ) { int low = 0; //low subscript int high = array.length - 1;//high subscript int middle; //middle subscript while ( low <= high ) { middle = ( low + high ) / 2; //The next line displays the part of the array still //available for searching through for the key. buildOutput ( array2, low, middle, high );

BinarySearch.java pt.6 if ( key == array[middle] ) //a match return middle; else if ( key < array[middle] ) high = middle - 1; //search lower array else low = middle + 1; } return -1; //searchKey not found }

BinarySearch.java pt.7 //build 1 row of output w/ part of the array being processed. void buildOutput ( int array3[], int low, int mid, int high ) { DecimalFormat twoDigits = new DecimalFormat ( "00" ); for ( int counter = 0; counter < a.length; counter ++ ) { if (counter high ) display += " "; else if (counter == mid ) //mark middle element in output display += twoDigits.format ( array3 [counter ] ) + "* "; else display += twoDigits.format ( array3 [counter ] ) + " "; } display += "\n"; }

Multiple-Subscripted Arrays Used to represent a table of values, not just a row. By convention, the 1st number = row number, and the 2nd number = column number. EX: int sample[] [] = new int [ 4 ] [ 2 ]; might have values like this: col 0col 1 row 031 row 120 row 268 row 375

Multiple-Subscripted Arrays Another way to remember: “first the row, then the position” int sample [] [] = { {3,1},{2,0},{6,8},{7,5} }; col 0col 1 row 031 row 120 row 268 row 375

Multiple-Subscripted Arrays EX: int sample[] [] = new int [ 4 ] [ 2 ]; sample [3][0] = 7 sample [1][1] = 0 sample [0][1] = ? sample [2][0] = ? col 0col 1 row 031 row 120 row 268 row 375

Multiple-Subscripted Arrays But how do you use them? Think of them as arrays of arrays (lists of lists). for ( int i = 0; i < id.length; i++ ) { for ( int j = 0; j < id[i].length ; j++ ){ g.drawString(“ “ + (id[ i ][ j ]),x,y); } see StringArray.java

Program of the Day pg. 347 DoubleArray.java