Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1P02 Introduction to Computer Science 1.1 Cosc 1P03 Week 12 Lecture slides “Example is not the main thing in influencing others, it is the only thing.”

Similar presentations


Presentation on theme: "COSC 1P02 Introduction to Computer Science 1.1 Cosc 1P03 Week 12 Lecture slides “Example is not the main thing in influencing others, it is the only thing.”"— Presentation transcript:

1 COSC 1P02 Introduction to Computer Science 1.1 Cosc 1P03 Week 12 Lecture slides “Example is not the main thing in influencing others, it is the only thing.” Albert Schweitzer

2 COSC 1P02 Introduction to Computer Science 1.2 Arrays  collections of values (objects)  elements  use  declare  create  process  memory model  length attribute  Returns the size of the array.  RainFall Example  Print out only those months with above average rainfall.  Palindrome Example  Strings as array of char  toCharArray & String(char[])

3 COSC 1P02 Introduction to Computer Science 1.3 RainFall private void display ( ) { String year; // year double[] rainfall; // rainfall for each month double totRain; // total rainfall for the year double aveRain; // average monthly rainfall year = in.readString(); rainfall = new double[12]; totRain = 0; for ( int i=0 ; i<rainfall.length ; i++ ) { rainfall[i] = in.readDouble(); totRain = totRain + rainfall[i]; }; aveRain = totRain / rainfall.length; writeHeader(year,aveRain); for ( int i=0 ; i<rainfall.length ; i++ ) { if ( rainfall[i] > aveRain ) { writeDetail(i,year,rainfall[i]); }; in.close(); out.close(); }; // display Declares the array variable rainfall Creates the array object of size 12 Length attribute returns the length of the rainfall array Store in rainfall at index location i. Scan through the rainfall array printing only those values greater then the average rainfall

4 COSC 1P02 Introduction to Computer Science 1.4 Processing “Variable-sized” Arrays  Example: standard deviation of student marks  data file  Array of Student objects  Array size not known  choose arbitrary size (constant)  keep count  Array organization  not all elements used  Std Dev formula:

5 COSC 1P02 Introduction to Computer Science 1.5 Data File COSC 1P02 Term Test 111111Ima First100 222222Mary Marvelous90 333333Pretty Good80 444444Im Ok70 555555Joe Average60

6 COSC 1P02 Introduction to Computer Science 1.6 Output

7 COSC 1P02 Introduction to Computer Science 1.7 Memory Model

8 COSC 1P02 Introduction to Computer Science 1.8  Reading data  traversal  Student constructor  termination  no more data  no more space  error  Computing standard deviation  array as parameter  second traversal  Traversal pattern

9 COSC 1P02 Introduction to Computer Science 1.9 Traversal Pattern

10 COSC 1P02 Introduction to Computer Science 1.10 Std. Dev private void genReport ( ) { String course; // course name String work; // name of piece of work Student[] students; // the class of students Student aStudent; // one student int numStd; // number of students double sum; // sum of marks double ave; // average mark double std; // standard deviation course = stData.readString(); work = stData.readString(); setUpReport(course,work); students = new Student[MAX_STD]; numStd = 0; sum = 0; while ( true ) { aStudent = new Student(stData); if ( stData.isEOF() | numStd >= MAX_STD ) break; students[numStd] = aStudent; numStd = numStd + 1; sum = sum + aStudent.getMark(); writeDetail(aStudent); }; ave = sum / numStd; std = computeStd(students,numStd,ave); writeSummary(ave,std); report.close(); stData.close(); }; // genReport Declare a Students array Create the array, Size MAX_STD Counter used to keep track of the number of students read. Put student into array indexed by numStd. Increment numStd so next array location can be used. Exit condition is EOF of data file or the array is out of room

11 COSC 1P02 Introduction to Computer Science 1.11 Calculating Std. Dev private double computeStd ( Student[] students, int numStd, double ave ) { double sum; // sum of squares of deviations double aMark; // a student mark sum = 0; for ( int i=0 ; i<numStd ; i++ ) { aMark = students[i].getMark(); sum = sum + pow(aMark-ave,2); }; return sqrt(sum/numStd); }; // computeStd Formal parameter students will point to the Students array. Pass the number of Student objects in the array Loop is run from 0 to numStd, the number of Student objects in the array

12 COSC 1P02 Introduction to Computer Science 1.12 Arrays vs Sounds

13

14

15

16

17

18

19 COSC 1P02 Introduction to Computer Science 1.19 Reversing a char array O L I V E R R E V I L O R L I V E O R E I V L O

20 COSC 1P02 Introduction to Computer Science 1.20 Palindrome Revisited private void checkPalindromes ( ) { int button; Stringstr;// string to be checked as palindrome Stringreversed;// reversed version of str while ( true ) { button = form.accept(); if ( button==1 ) break; str = form.readString("in"); reversed = reverse(str); if ( str.equalsIgnoreCase(reversed) ) { form.writeString("out","This is a palindrome"); } else { form.writeString("out","This is not a palindrome"); }; };// checkPalindromes Read in the candidate from “InString” Call method reverse which reverses the input. Check to see if the original and the reversed are the same, if so then YES! Appropriate message is written to the Result field

21 COSC 1P02 Introduction to Computer Science 1.21 Palindrome Revisited. private String reverse ( String str ) { char []theString;// string as array of characters charc; inti; theString = str.toCharArray(); for ( i=0 ; i<theString.length/2 ; i++ ) { c = theString[i]; theString[i] = theString[theString.length-1-i]; theString[theString.length-1-i] = c; }; return new String(theString); };// reverse Define a character array Method converts the string to a right sized character array Loop runs 1/2 the length of the char array Swap the first and last char Last is defined by length -1. As i is increased the upper index is decremented proportionately. Constructor converts the character array back to a string. Which is returned by the method.

22 COSC 1P02 Introduction to Computer Science 1.22 Enrollments public class UStats { private final String []UNIVS = {" Adams"," Beacon"," Madison"," Lexington"}; private final String []DEPTS = {"Math.","Business","Comp. Sci.","Biology","French"}; private ASCIIDataFiledataFile;// file for input private ASCIIDisplayerout;// file for report public UStats ( ) { dataFile = new ASCIIDataFile(); msg = new ASCIIDisplayer(); out = msg; display(); dataFile.close(); out.close(); };// constructor private void display ( ) { int [][] enrol;// enrolment stats int [] uTotals;// University totals int [] dTotals;// Department totals int total;// grand total enrol = new int[DEPTS.length][UNIVS.length]; readStats(enrol); uTotals = sumRows(enrol); dTotals = sumCols(enrol); total = sumAll(enrol); writeStats(enrol,uTotals,dTotals,total); };// display Arrays of strings used for the row and column headings Array declarations for enrolment table, row & column stats Rows are represented by the departments, Columns by the universities. Length of each dimension is based on the heading lengths.

23 COSC 1P02 Introduction to Computer Science 1.23 What is a Palindrome?  A string which reads the same forward and reverse.  E.g.  This is not a palindrome Happy like a fox  These are all palindromes No devil lived on I did did I Plum was I ere I saw mulp Sleep on no peels Zeus sees Suez

24 COSC 1P02 Introduction to Computer Science 1.24 Array Declarations

25 COSC 1P02 Introduction to Computer Science 1.25 Good Luck on your Exam!!


Download ppt "COSC 1P02 Introduction to Computer Science 1.1 Cosc 1P03 Week 12 Lecture slides “Example is not the main thing in influencing others, it is the only thing.”"

Similar presentations


Ads by Google