8/9: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.

Slides:



Advertisements
Similar presentations
Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
Advertisements

Java threads Threaded class extends thread Synchronized class is a monitor Runnable interface gives thread methods All code is (C) Copyright 1999 by Deitel.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
 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.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
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
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
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.
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.
Java I--Copyright © 2000 Tom Hunter. Chapter 6 Methods: Part II.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
GUI programming Graphical user interface-based programming.
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
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
 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.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
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.
Examples for Project 2 From Deital and Deital, Java: How to Program, Third Edition Reusable and flexible UI Data Records with Sequential and Random Access.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
 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 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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1Introduction 6.2Program Modules in Java 6.3 Math Class Methods 6.4Methods.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
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.
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.
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]
Introduction to Programming
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.
Presentation transcript:

8/9: 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 enter, result; JTextArea output; int a[]; String display = "";

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

BinarySearch.java pt.3 result = new JTextField( 22 ); result.setEditable( false ); c.add( result ); output = new JTextArea( 6, 60 ); output.setFont(new Font("Courier",Font.PLAIN,12 ) ); c.add( output ); //create array and fill it with even numbers 0-28 a = new int[ 15 ]; for ( int i = 0; i < a.length; i++ ) a[ i ] = 2 * i; }

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 ( a, 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 array[], 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 ( 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 low, int mid, int high ) { DecimalFormat twoDigits = new DecimalFormat ( "00" ); for ( int i = 0; i < a.length; i++ ) { if ( i high ) display += " "; else if ( i == mid ) //mark middle element in output display += twoDigits.format ( a [ i ] ) + "* "; else display += twoDigits.format ( a [ i ] ) + " "; } 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. 297 DoubleArray.java Tomorrow: Final Exam –Correct the code –Create the code –What’s the output –Full period (1 ½ hrs.)