Transcript COS260Day15

COS 260 DAY 16
Tony Gauvin
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Agenda
• Questions?
• Assignment 3 not corrected
• Still waiting for student submissions
• Assignment 4 posted
• All from chap 7
• Due Nov 10 @ 9:30 AM
• Finish Chap 7 Arrays in Java
• Programming Lab
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Ch 1 -2
Schedule for next two weeks
• Oct 30
• More on Arrays
• Nov 3
• Begin Inheritance, Polymorphism and Interfaces
• Nov 6
• No Class, I am at a Board meeting in Lexington, KY
• Nov 10
• Finish Inheritance, Polymorphism and Interfaces
• Assignment 4 due
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
New Assignment Grading Criteria
• Criteria 1 30 % Does the program compile with no errors or
warnings?
• Criteria 2 50% Does the program run and produce the intended
outputs for the intended inputs?
• Criteria 3 5% Is the Code properly Commented?
• Criteria 4 5% Does the Code adhere to proper style and conventions
for Java?
• Criteria 5 10% Is the code an original creative work of the student?
Using leveraged code without citation  0
Students submitting duplicate code as another student  0
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Arrays
Chapter 7
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Objectives
• Order (sort) the elements of an array
• Search an array for a particular item
• Define, use multidimensional array
• Text fields, text areas in applets
• Drawing arbitrary polygons in applets
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Sorting, Searching Arrays: Outline
• Selection Sort
• Other Sorting Algorithms
• Searching an Array
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Selection Sort
• Consider arranging all elements of an array so they are ascending
order
• Algorithm is to step through the array
• Place smallest element in index 0
• Swap elements as needed to accomplish this
• Called an interchange sorting algorithm
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Selection Sort
• Figure 7.5a
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Selection Sort
• Figure 7.5b
...
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Selection Sort
• Algorithm for selection sort of an array
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Selection Sort
• View implementation of selection sort, listing 7.10
class ArraySorter
• Chapter 7 Code\ArraySorter.java
• View demo program, listing 7.11
class SelectionSortDemo
• Chapter 7 Code\SelectionSortDemo.java
Sample
screen
output
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Other Sorting Algorithms
• Selection sort is simplest
• But it is very inefficient for large arrays
• East to change from ascending to decending
• Chapter 7 Code\ArraySorter2.java
• Chapter 7 Code\SelectionSortDemo2.java
• Java Class Library provides for efficient sorting
• Has a class called Arrays
• http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
• Class has multiple versions of a sort method
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Bubble sort (assignment 4 program 3)
http://www.csit.parkland.edu/~mbrandyberry/CS1Java/Lessons/Lesson28/BubbleSort.htm
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Searching an Array
• Method used in OneWayNoRepeatsList is sequential search
• Looks in order from first to last
• Good for unsorted arrays
• Search ends when
• Item is found … or …
• End of list is reached
• If list is sorted, use more efficient searches
• Sort first
• Search second
• Fastest is binary search
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Multidimensional Arrays: Outline
• Multidimensional-Array Basics
• Multidimensional-Array Parameters and Returned Values
• Java's Representation of Multidimensional
• Ragged Arrays
• Programming Example: Employee Time Records
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Multidimensional-Array Basics
• Consider Figure 7.6, a table of values
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Multidimensional-Array Basics
• Figure 7.7 Row and column indices for an array named table
table[3][2] has
a value of 1262
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Multidimensional-Array Basics
• We can access elements of the table with a nested for loop
• Example:
• View sample program, listing 7.12
class InterestTable
• Chapter 7 Code\InterestTable.java
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Multidimensional-Array Basics
Sample
screen
output
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Multidimensional-Array Parameters and
Returned Values
• Methods can have
• Parameters that are multidimensional-arrays
• Return values that are multidimensional-arrays
• View sample code, listing 7.13
class InterestTable2
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Java's Representation of Multidimensional
Arrays
• Multidimensional array represented as several onedimensional arrays
• Given
int [][] table = new int [10][6];
• Array table is actually 1 dimensional of type int[]
• It is an array of arrays
• Important when sequencing through
multidimensional array
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Ragged Arrays
• Not necessary for all rows to be of the same length
• Example:
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Programming Example
• Employee Time Records
• Two-dimensional array stores hours worked
• For each employee
• For each of 5 days of work week
• Array is private instance variable of class
• View sample program, listing 7.14
class TimeBook
• Chapter 7 Code\TimeBook.java
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Programming Example
Sample
screen
output
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Programming Example
• Figure 7.8 Arrays for the class TimeBook
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Graphics Supplement: Outline
• Text Areas and Text Fields
• Programming Example: A Question-and-Answer Applet
• The Classes JTextArea and JTextField
• Drawing Polygons
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Text Areas, Text Fields
• Text area is object of class JTextArea
• Displayed as a place for user to enter multiple lines of text
• View sample code, listing 7.15
class Oracle
• Chapter 7 Code\Oracle.java
Sample
screen
output
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
JTextArea and JTextField
• Class JTextArea
• Used for multiple lines of user input
• Class JtextField
• Used for single line of user input
• Both classes include methods
• setText
• getText
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Drawing Polygons
• Class Graphics has method drawRect
• But only 4 sides and only 90 degree corners
• Method drawPolygon can draw polygon of any number of sides
• Three arguments
int for x values of coordinates
• Array of int for y values of coordinates
• Array of
• Number of points (vertices)
• A polyline like a polygon, not closed
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Drawing Polygons
• Figure 7.9
A polygon and
a polyline
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Drawing Polygons
• View sample applet, listing 7.16
class House
House is a filled
polygon
Door is a polyline
Window is an
ordinary polygon
Sample
screen
output
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Summary
• An array is a collection of variables all of the same type
• Arrays are objects, created with operator new
• Elements numbered starting with 0, ending with 1 less than length
• Indexed variable can be used as a parameter – treated like variable of
base type
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Summary
• Entire array can be passed as parameter to a method
• Method return value can be an array
• Partially filled array usually stores values in initial segment, use an
int to track how many are used
• Privacy leak caused by returning array corresponding to private
instance variable
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Summary
• Selection sort orders an array into ascending or descending order
• Multidimensional arrays are implemented as an array of arrays
• Treat two-dimensional array as a table with rows and columns
• Text fields, text areas in an applet provide areas for text input/output
• Possible to draw polygons, polylines in an applet
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved