Todays topics Java Arrays Upcoming Functions Reading Great

  • Slides: 18
Download presentation
Today’s topics Java Arrays Upcoming Functions Reading Great Ideas, Chapter 3 Comp. Sci 001

Today’s topics Java Arrays Upcoming Functions Reading Great Ideas, Chapter 3 Comp. Sci 001 9. 1

Arrays v v Aggregate data type Deal with items of same type q q

Arrays v v Aggregate data type Deal with items of same type q q v Analogies q q v v v Mailboxes in post office CD racks with slots Simplifies naming q v Lists of words Numbers 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 Comp. Sci 001 9. 2

Using arrays v Use subscript or index to access an element x[5] = 20;

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

Creating Arrays v Declaration double weights[]; v Definition weights = new double[50]; v Combine

Creating Arrays v Declaration double weights[]; v Definition weights = new double[50]; v Combine declaration and definition double weights[] = new double[50]; int num[] = new int[6]; ? ? ? num[1] = 21; num[5] = 13; ? Comp. Sci 001 21 ? ? ? 13 9. 4

Arrays & Loops num[0] = 0; int k = 2; while(k < num. length)

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

Array Examples v Sum up elements in a list (4 int k = 0,

Array Examples v Sum up elements in a list (4 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]; k = k + 1; } v v v ways to do same thing) int k while { sum k = } int k while { k = sum } = 9, sum = 0; (k >= 0) = sum + data[k]; k - 1; = 10, sum = 0; (k > 0) k - 1; = sum + data[k]; Count occurrences of something Search for something Information retrieval Comp. Sci 001 9. 6

Hotel Program public class Hotel extends java. applet. Applet implements Action. Listener { Text.

Hotel Program public class Hotel extends java. applet. Applet implements Action. Listener { Text. Field m. Instruct, m. Hotel. Census; Int. Field g. Room. No, g. No. Guests; Button b. Register; int k=0, tot. Guests = 0, no. Occupied = 0, room. No, no. Guests; int room[]; public void init() { room = new int[500]; k = 0; while (k < 500) { room[k] = 0; k = k + 1; } Comp. Sci 001 9. 7

Hotel Program. 2 m. Instruct = new Text. Field(60); m. Instruct. set. Text( "Enter

Hotel Program. 2 m. Instruct = new Text. Field(60); m. Instruct. set. Text( "Enter room number, number of guests, then press Register" ); g. Room. No = new Int. Field(6); g. No. Guests = new Int. Field(6); b. Register = new Button("Register"); m. Hotel. Census = new Text. Field(60); b. Register. add. Action. Listener(this); add(m. Instruct); add(g. Room. No); add(g. No. Guests); add(b. Register); add(m. Hotel. Census); } Comp. Sci 001 9. 8

Hotel Program. 3 public void action. Performed(Action. Event event) { Object cause = event.

Hotel Program. 3 public void action. Performed(Action. Event event) { Object cause = event. get. Source(); if (cause == b. Register) { room. No = g. Room. No. get. Int(); no. Guests = g. No. Guests. get. Int(); if (room[room. No] != 0) m. Hotel. Census. set. Text("That room is occupied!"); else { room[room. No] = no. Guests; tot. Guests = tot. Guests + no. Guests; no. Occupied = no. Occupied + 1; m. Hotel. Census. set. Text("There are " + tot. Guests + " occupying " + no. Occupied + " rooms. "); } } Comp. Sci 001 9. 9

Simple Statistics v What should a simple statistics program include? q q Get data

Simple Statistics v What should a simple statistics program include? q q Get data into array o One item at a time o Practical program would use files Allow display of data o Display one item at a time o (Could have used Text. Area to display all at once) Actual computations o Maximum, Minimum, Mean, N Control o Appropriate buttons Comp. Sci 001 9. 10

Simple Statistics v How do we compute the Mean (Average)? q q q v

Simple Statistics v How do we compute the Mean (Average)? q q q v How do we find extrema? q q v Sum Count Compute Largest Smallest Will package as functions (methods) Comp. Sci 001 9. 11

Stats Program public class Array. Stats extends java. applet. Applet implements Action. Listener {

Stats Program public class Array. Stats extends java. applet. Applet implements Action. Listener { Text. Field m. Instruct, m. Answer; Int. Field i. Count; double list[]; Button b. Store, b. Show, b. Extremes, b. Mean, b. Clear; int count, next. Free, next. Use; 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; } Comp. Sci 001 9. 12

Stats Program. 2 double max(double[] list, int size) { int k = 1; double

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; } Comp. Sci 001 9. 13

Stats Program. 3 double min(double[] list, int size) { int k = 1; double

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; } Comp. Sci 001 9. 14

Stats Program. 4 public void init() { list = new double[100]; m. Instruct =

Stats Program. 4 public void init() { list = new double[100]; m. Instruct = new Text. Field(70); m. Answer = new Text. Field(70); m. Instruct. set. Text("Enter Value, then press Store button"); i. Count = new Int. Field(10); b. Store = new Button("Store"); b. Show = new Button("Show"); b. Extremes = new Button("Extremes"); b. Mean = new Button("Mean"); b. Clear = new Button("Clear"); next. Free = 0; next. Use = 0; b. Store. add. Action. Listener(this); b. Show. add. Action. Listener(this); b. Extremes. add. Action. Listener(this); b. Mean. add. Action. Listener(this); b. Clear. add. Action. Listener(this); add(m. Instruct); add(i. Count); add(b. Store); add(b. Show); add(b. Extremes); add(b. Mean); add(b. Clear); add(m. Answer); } Comp. Sci 001 9. 15

Stats Program. 5 public void action. Performed(Action. Event event) { int value, total; Object

Stats Program. 5 public void action. Performed(Action. Event event) { int value, total; Object cause = event. get. Source(); if (cause == b. Store) { value = i. Count. get. Int(); list[next. Free] = value; next. Free = next. Free + 1; i. Count. set. Int(); // clear Int. Field } if (cause == b. Show) { m. Answer. set. Text("The value in element "+next. Use+" is "+ list[next. Use]); next. Use = (next. Use + 1)% next. Free; } Comp. Sci 001 9. 16

Stats Program. 6 if (cause == b. Extremes){ m. Answer. set. Text("The largest data

Stats Program. 6 if (cause == b. Extremes){ m. Answer. set. Text("The largest data item is " + max(list, next. Free) + " and the smallest data item is " + min(list, next. Free)); } if (cause == b. Mean) { m. Answer. set. Text("The average is " + mean(list, next. Free)); } if (cause == b. Clear) { next. Use = 0; next. Free = 0; m. Answer. set. Text("The old data has been cleared out"); } } } Comp. Sci 001 9. 17

New Stuff in Stats Program v Java Programming: Functions (Methods) q q q v

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