Today’s topics Java Arrays Upcoming Functions Reading

Slides:



Advertisements
Similar presentations
About Functions SUM, AVERAGE, MIN, MAX, COUNT, ROUND
Advertisements

CS0007: Introduction to Computer Programming Array Algorithms.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CompSci Today’s topics Java The Database Program Upcoming Recursion Reading Great Ideas, Chapter 4.
CPS Today’s topics Java Information Retrieval Upcoming Database Programs Reading Great Ideas, Chapter 4.
SNU IDB Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.
CompSci Today’s topics Java Applications Simulation Upcoming Software Engineering (Chapter 7) Reading Great Ideas, Chapters 6.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
CompSci Today’s topics Java Numbers Iteration Upcoming More Java Reading Great Ideas, Chapter 3.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
CompSci Today’s topics Java Writing Functions/Methods Upcoming Information Retrieval Reading Great Ideas, Chapter 4.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Today’s topics Java Input More Syntax Upcoming Decision Trees More formal treatment of grammers Reading Great Ideas, Chapter 2.
Today’s topics Java Looping Upcoming Arrays in Java Reading Great Ideas, Chapter 3.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CompSci Today’s topics Java Recursion Upcoming Graphics Reading Great Ideas, Chapter 4 (begin Chapter 5 for graphics)
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter VII: Arrays.
Sections 10.1 – 10.4 Introduction to Arrays
Today’s topics Java Information Retrieval Upcoming Database Programs
Arrays Low level collections.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Microsoft Visual Basic 2005: Reloaded Second Edition
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 7: Array.
Today’s topics Java Applications Upcoming Reading Graphics
C++ Arrays.
Siti Nurbaya Ismail Senior Lecturer
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Chapter 8: Collections: Arrays
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
CNG 140 C Programming (Lecture set 8)
Review for Final Exam.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays .
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions with arrays.
Starting Out with Programming Logic & Design
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Review for Final Exam.
Take out a piece of paper and PEN.
Take out a piece of paper and PEN.
Arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Introduction to Computer Programming IT-104
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Introduction to Computer Science
Presentation transcript:

Today’s topics Java Arrays Upcoming Functions Reading Great Ideas, Chapter 3

Arrays Aggregate data type Deal with items of same type Lists of words Numbers Analogies Mailboxes in post office CD racks with slots Simplifies naming What if you needed to come up with unique name for each data item? Allows use of loops Required for many mathematical and statistical problems Multiple elements or cells

Using arrays Use subscript or index to access an element x[5] = 20; foo.setText(“Result is " + x[5]); First element is element 0, not 1!!! Often used in loops int k = 0, sum = 0; while ( k < 10 ) { sum = sum + measurements[k]; k = k + 1; } Note that subscript is a variable, k

Creating Arrays int num[] = new int[6]; num[1] = 21; num[5] = 13; Declaration double weights[]; Definition weights = new double[50]; Combine declaration and definition double weights[] = new double[50]; int num[] = new int[6]; num[1] = 21; num[5] = 13; ? ? 21 13

Arrays & Loops Subscript range errors! ! ! num[0] = 0; int k = 2; while(k < num.length) { num[k] = k * k; k = k + 1; } 21 4 9 16 25 Subscript range errors! ! ! Java checks (many languages do not) Costs & tradeoffs

Array Examples Sum up elements in a list (4 ways to do same thing) Count occurrences of something Search for something Information retrieval int k = 0, sum = 0; while (k < 10) { sum = sum + data[k]; k = k + 1; } int k = 1, sum = 0; while (k <= 10) { sum = sum + data[k – 1]; int k = 9, sum = 0; while (k >= 0) { sum = sum + data[k]; k = k - 1; } int k = 10, sum = 0; while (k > 0) { k = k - 1; sum = sum + data[k];

Hotel Program public class Hotel extends java.applet.Applet implements ActionListener { TextField mInstruct, mHotelCensus; IntField gRoomNo, gNoGuests; Button bRegister; int k=0, totGuests = 0, noOccupied = 0, roomNo, noGuests; int room[]; public void init() { room = new int[500]; k = 0; while (k < 500) { room[k] = 0; k = k + 1; }

Hotel Program.2 mInstruct = new TextField(60); mInstruct.setText( "Enter room number, number of guests, then press Register" ); gRoomNo = new IntField(6); gNoGuests = new IntField(6); bRegister = new Button("Register"); mHotelCensus = new TextField(60); bRegister.addActionListener(this); add(mInstruct); add(gRoomNo); add(gNoGuests); add(bRegister); add(mHotelCensus); }

Hotel Program.3 public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == bRegister) { roomNo = gRoomNo.getInt(); noGuests = gNoGuests.getInt(); if (room[roomNo] != 0) mHotelCensus.setText("That room is occupied!"); else { room[roomNo] = noGuests; totGuests = totGuests + noGuests; noOccupied = noOccupied + 1; mHotelCensus.setText("There are " + totGuests + " occupying " + noOccupied + " rooms."); }

Simple Statistics What should a simple statistics program include? Get data into array One item at a time Practical program would use files Allow display of data Display one item at a time (Could have used TextArea to display all at once) Actual computations Maximum, Minimum, Mean, N Control Appropriate buttons

Simple Statistics How do we compute the Mean (Average)? Sum Count How do we find extrema? Largest Smallest Will package as functions (methods)

Stats Program public class ArrayStats extends java.applet.Applet implements ActionListener { TextField mInstruct, mAnswer; IntField iCount; double list[]; Button bStore, bShow, bExtremes, bMean, bClear; int count, nextFree, nextUse; double mean(double[] list, int size) { int k = 0; double sum = 0.0; while (k < size) { sum = sum + list[k]; k = k + 1; } return sum/size;

Stats Program.2 double max(double[] list, int size) { int k = 1; double largest = list[0]; while (k < size) { if (list[k] > largest) { largest = list[k]; } k = k + 1; return largest;

Stats Program.3 double min(double[] list, int size) { int k = 1; double smallest = list[0]; while (k < size) { if (list[k] < smallest) { smallest = list[k]; } k = k + 1; return smallest;

Stats Program.4 public void init() { list = new double[100]; mInstruct = new TextField(70); mAnswer = new TextField(70); mInstruct.setText("Enter Value, then press Store button"); iCount = new IntField(10); bStore = new Button("Store"); bShow = new Button("Show"); bExtremes = new Button("Extremes"); bMean = new Button("Mean"); bClear = new Button("Clear"); nextFree = 0; nextUse = 0; bStore.addActionListener(this); bShow.addActionListener(this); bExtremes.addActionListener(this); bMean.addActionListener(this); bClear.addActionListener(this); add(mInstruct); add(iCount); add(bStore); add(bShow); add(bExtremes); add(bMean); add(bClear); add(mAnswer); }

Stats Program.5 public void actionPerformed(ActionEvent event) { int value, total; Object cause = event.getSource(); if (cause == bStore) { value = iCount.getInt(); list[nextFree] = value; nextFree = nextFree + 1; iCount.setInt(); // clear IntField } if (cause == bShow) { mAnswer.setText("The value in element "+nextUse+" is "+ list[nextUse]); nextUse = (nextUse + 1)% nextFree;

Stats Program.6 if (cause == bExtremes){ mAnswer.setText("The largest data item is " + max(list, nextFree) + " and the smallest data item is " + min(list, nextFree)); } if (cause == bMean) { mAnswer.setText("The average is " + mean(list, nextFree)); if (cause == bClear) { nextUse = 0; nextFree = 0; mAnswer.setText("The old data has been cleared out");

New Stuff in Stats Program Java Programming: Functions (Methods) Parameters/Arguments Return statement Return type on method header (not just void) Control Entering and Displaying data Algorithms Mean Min Max