Download presentation
Presentation is loading. Please wait.
Published byMaude Wilcox Modified over 9 years ago
1
COSC 1P02 Introduction to Computer Science 8.1 Cosc 1P02 “For myself, I am an optimist--it does not seem to be much use being anything else.” Winston Churchill
2
COSC 1P02 Introduction to Computer Science 8.2 Class Average Marks for test in a course Average is sum divided by number of marks Running total sum values as see them at each point sum is sum of values so far add next value initial value (no values so far) is 0 Summation algorithm Reading data ASCIIPrompter Writing results ASCIDisplayer Example
4
ASCIIPrompter Methods
5
ASCIIDisplayer Methods
6
COSC 1P02 Introduction to Computer Science 8.6 Reading Data from a File Tedious and error prone to enter data as program running Instead create a file of data (text editor/word processor) Reading data from a file ASCIIDataFile fields (tab delimited) Example data includes student number not really a number but text String report header, body (detail lines), summary separate methods for each part
7
ASCIIDataFile Methods
8
COSC 1P02 Introduction to Computer Science 8.8 End of File Tedious and error prone for user to have to count number of pieces of data A file contains a finite amount of data each read consumes one value eventually a read must happen when there is no more data called End Of File (EOF) method isEOF() returns true when last read failed because of EOF Indefinite loop need to loop an unknown number of times loop until something happens Example expect if student number then will be mark count number of students
10
COSC 1P02 Introduction to Computer Science 8.10 while Statement An indefinite loop Syntax Execution evaluates condition if true, executes body if false, quits (continues after body) Read to end of file loop condition always true so executes body if statement if condition is true, executes then part then part is one statement so doesn’t need {} break statement quits whatever loop it is executed within
13
COSC 1P02 Introduction to Computer Science 8.13 Fill a Box Problem packing to move Condition Termination infinite loop Generating random values Math.random() Output Summation
14
COSC 1P02 Introduction to Computer Science 8.14 While...
15
COSC 1P02 Introduction to Computer Science 8.15 Filling a Box
16
COSC 1P02 Introduction to Computer Science 8.16 Fill private void fill ( ) { intsize;// capacity of box intamt;// amount in box intitem;// item item to pack in.setLabel("Enter box size"); size = in.readInt(); writeSize(size); amt = 0; while ( amt < size ) { item = (int) (10 * Math.random() + 1); amt = amt + item; writeItem(item,amt); }; writeResult(amt); };// fill Condition checks if the amount put into the box exceeds the capacity. While this condition is true the “while loop” keeps repeating. We put an item into the box of varying size. Calculated using random(). Keep a running total of what is in the box.
17
COSC 1P02 Introduction to Computer Science 8.17 High and Low Mark Algorithm is value higher (lower) the highest (lowest) so far Initial state? Double.MAX_VALUE Independent if s independent decisions Finding maximum value pattern
18
COSC 1P02 Introduction to Computer Science 8.18
19
COSC 1P02 Introduction to Computer Science 8.19 Finding the Maximum (minimum) Value Programming Pattern
20
COSC 1P02 Introduction to Computer Science 8.20 private void display ( ) { intnumStd;// number of students doubletotMark;// total of marks doubleaveMark;// average mark doublehighMark;// highest mark doublelowMark;// lowest mark intaStdNum;// one student's student nmber doubleaMark;// one student's mark numStd = 0; totMark = 0; highMark = - Double.MAX_VALUE; lowMark = Double.MAX_VALUE; while ( true ) { aStdNum = in.readInt(); if (in.isEOF() ) break; aMark = in.readDouble(); numStd = numStd + 1; totMark = totMark + aMark; if ( aMark > highMark ) { highMark = aMark; }; if ( aMark < lowMark ) { lowMark = aMark; }; aveMark = totMark / numStd; writeDetail(numStd,aveMark,highMark,lowMark); };// display High Mark.. Initialization of the hi and lo mark. If the new mark is higher then the previous high mark then replace with new mark. The same is done for the low mark, inequality is changed
21
COSC 1P02 Introduction to Computer Science 8.21 Reports Most data processing involves production of a report (typically printed) Report header detail lines summary (footer) Report format tabular in nature each row represents one entity (e.g. student) similar pieces of data (e.g. mark) in a column header has a title and a heading for each column summary may be columnar or free-form
23
COSC 1P02 Introduction to Computer Science 8.23 ReportPrinter Support for printed reports in columnar format title for report centered at top of page data written to field field has a name, a label and a width name used to specify field when writing label forms part of heading width is width of column report designed by setting title and adding fields fields added L-R across report handles moving to next line and pagination Methods setting title adding fields writing into fields Example PDFCreator
24
ReportPrinter Methods
25
COSC 1P02 Introduction to Computer Science 8.25 The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.