COS 260 DAY 16 Tony Gauvin.

Slides:



Advertisements
Similar presentations
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Advertisements

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 6 Chapter 6.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Searching and Sorting Arrays
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 8 Arrays and Strings
Chapter 61 Arrays Chapter 6. 2 Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
 2007 Pearson Education, Inc. All rights reserved C Arrays.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays Chapter 7.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 7 Part 1 Edited by JJ Shepherd
Java How to Program, Late Objects Version, 10/e
Chapter 7 Part 2 Edited by JJ Shepherd
COS 260 DAY 20 Tony Gauvin.
Presentation transcript:

COS 260 DAY 16 Tony Gauvin

Agenda Questions? Assignment 3 not corrected Assignment 4 posted Still waiting for student submissions Assignment 4 posted All from chap 7 Due Nov 10 @ 9:30 AM Finish Chap 7 Arrays in Java All Campus Meeting 12:30 PM In Fax Auditorium Ebola Scare MMMC Staff & UMFK Administrators Programming Lab Clues and hints

Schedule for next two weeks Today 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

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

Arrays Chapter 7

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

Sorting, Searching Arrays: Outline Selection Sort Other Sorting Algorithms Searching an Array

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

Selection Sort Figure 7.5a

Selection Sort Figure 7.5b . . .

Selection Sort Algorithm for selection sort of an array

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

Other Sorting Algorithms Selection sort is simplest But it is very inefficient for large arrays East to change from ascending to descending 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

Bubble sort (assignment 4 program 3) http://www.csit.parkland.edu/~mbrandyberry/CS1Java/Lessons/Lesson28/BubbleSort.htm

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

Multidimensional Arrays: Outline Multidimensional-Array Basics Multidimensional-Array Parameters and Returned Values Java's Representation of Multidimensional Ragged Arrays Programming Example: Employee Time Records

Multidimensional-Array Basics Consider Figure 7.6, a table of values

Multidimensional-Array Basics Figure 7.7 Row and column indices for an array named table table[3][2] has a value of 1262

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

Multidimensional-Array Basics Sample screen output

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's Representation of Multidimensional Arrays Multidimensional array represented as several one- dimensional 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

Ragged Arrays Not necessary for all rows to be of the same length Example:

Programming Example Chapter 7 Code\TimeBook.java 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

Programming Example Sample screen output

Programming Example Figure 7.8 Arrays for the class TimeBook

Graphics Supplement: Outline Text Areas and Text Fields Programming Example: A Question-and-Answer Applet The Classes JTextArea and JTextField Drawing Polygons

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

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

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 Array of int for x values of coordinates Array of int for y values of coordinates Number of points (vertices) A polyline like a polygon, not closed

Drawing Polygons Figure 7.9 A polygon and a polyline

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

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

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

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