Title: COSC 1P03
1Cosc 1P03
Example is not the main thing in influencing
others, it is the only thing. Albert
Schweitzer
2Arrays
- 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)
3RainFall
- 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 double12
- totRain 0
- for ( int i0 iltrainfall.length i )
- rainfalli in.readDouble()
- totRain totRain rainfalli
-
- aveRain totRain / rainfall.length
- writeHeader(year,aveRain)
- for ( int i0 iltrainfall.length i )
- if ( rainfalli gt aveRain )
- writeDetail(i,year,rainfalli)
4Processing 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
5Data File
COSC 1P02 Term Test 111111 Ima First 100 222222 Ma
ry Marvelous 90 333333 Pretty Good 80 444444 Im
Ok 70 555555 Joe Average 60
6Output
7Memory Model
8 - Reading data
- traversal
- Student constructor
- termination
- no more data
- no more space
- error
- Computing standard deviation
- array as parameter
- second traversal
- Traversal pattern
9Traversal Pattern
10Std. 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 StudentMAX_STD
- numStd 0
- sum 0
- while ( true )
- aStudent new Student(stData)
11Calculating 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 i0 iltnumStd i )
- aMark studentsi.getMark()
- sum sum pow(aMark-ave,2)
-
- return sqrt(sum/numStd)
-
- // computeStd
12Multidimensional Arrays
- e.g. tables
- 2 or more dimensions
- enrollment data by university and department
- declaration
- type ident or type ident
- creation
- new typeexpr
- subscripting
- identexpr
- length
- regular vs irregular arrays
- variable-sized
- fill upper left corner
- one counter for each dimension
This form is more consistent with other type
declarations. Hence type ident.
13Processing 2-Dimensional Arrays
- random access
- look-up table
- sequential access
- row-major order
- lexicographic order
- pattern
- column-major order
- pattern
- regular arrays
14E.g. Tabulating Enrolments
- data
- right-sized array
- totals
- readStats
- row-major processing
- sumRows
- row processing
- sumCols
- column processing
- sumAll
- row-major processing
- writeStats
- row-major report generation
15Arrays vs Sounds
16(No Transcript)
17(No Transcript)
18(No Transcript)
19(No Transcript)
20(No Transcript)
21(No Transcript)
22Reversing a char array
O L I V E R
R L I V E O
R E I V L O
R E V I L O
23Palindrome Revisited
private void checkPalindromes ( ) int
button String str // string to be checked as
palindrome String reversed // reversed version
of str while ( true ) button
form.accept() if ( button1 ) break str
form.readString("in") reversed
reverse(str) if ( str.equalsIgnoreCase(reversed
) ) form.writeString("out","This is a
palindrome") else form.writeString("ou
t","This is not a palindrome") //
checkPalindromes
24Palindrome Revisited.
private String reverse ( String str ) char
theString // string as array of
characters char c int i theString
str.toCharArray() for ( i0
ilttheString.length/2 i ) c
theStringi theStringi theStringtheString
.length-1-i theStringtheString.length-1-i
c return new String(theString) //
reverse
25(No Transcript)
26Universities
Departments
27(No Transcript)
28(No Transcript)
29Enrollments
public class UStats private final String
UNIVS " Adams"," Beacon"," Madison","
Lexington" private final String DEPTS
"Math.","Business","Comp. Sci.","Biology","French
" private ASCIIDataFile dataFile // file
for input private ASCIIReportFile report //
file for report private ASCIIDisplayer msg //
displayer for messages public UStats ( )
dataFile new ASCIIDataFile() report new
ASCIIReportFile() msg new ASCIIDisplayer()
display() dataFile.close() report.close()
msg.close() // constructor private
void display ( ) int enrol //
enrolment stats int uTotals // University
totals int dTotals // Department
totals int total // grand total msg.writ
eLabel("Processing.....") enrol new
intDEPTS.lengthUNIVS.length readStats(enrol
) uTotals sumRows(enrol) dTotals
sumCols(enrol) total sumAll(enrol) writeSt
ats(enrol,uTotals,dTotals,total) msg.writeLabel
(".....complete") msg.writeEOL() //
display
30Enrollments.
private void readStats ( int stats )
int i, j for ( i0 iltstats.length
i ) for ( j0 jltstatsi.length j )
statsij dataFile.readInt()
// readStats / This method sums the
enrollments by row./ private int sumRows (
int stats ) int sums int i,
j sums new intstats.length for ( i0
iltstats.length i ) sumsi 0 for (
j0 jltstatsi.length j ) sumsi
sumsi statsij return
sums // sumRows
31Enrollments..
//This method sums the enrollments by
column. private int sumCols ( int stats
) int sums int i, j sums new
intstats0.length for ( j0
jltstats0.length j ) sumsj
0 for ( i0 iltstats.length i )
sumsj sumsj statsij
return sums // sumCols //This method
computes the grand sum of the enrollment
statistics. private int sumAll ( int
stats ) int sum int i, j sum
0 for ( i0 iltstats.length i ) for
( j0 jltstatsi.length j ) sum sum
statsij return sum //
sumAll
32Enrollments...
//This method computes the grand sum of the
enrollment statistics. private int sumAll (
int stats ) int sum int i, j sum
0 for ( i0 iltstats.length i )
for ( j0 jltstatsi.length j )
sum sum statsij return
sum // sumAll Alternate Form of
sumAll private int sumAll ( int stats )
int sum int i, j sum 0 for ( int
u stats ) for ( int d u ) sum
sum d return sum // sumAll
Can be read as For each array slice u in stats,
and For each element d in u.
Important to realise that each time the loop
starts u and d represent the next value.
There are no indices, thus they do not need to be
updated. No i or j
33Enrollments.
/ This method initializes the enrolment
report. /
private void initReport ( )
report.setTitle("Enrolment Report",getDateInstance
().format(new Date()))
report.addField("dept","",10) for ( int
i0 iltUNIVS.length i )
report.addField("u"i,UNIVSi,getIntegerInstance(
),10) report.addField("total",
" Total",getIntegerInstance(),10)
// initReport
34Enrollments..
/This method displays the enrollment stats in a
tabular format with labels and totals for each
row and column and a grand total. / private
void writeStats( int stats, int rSums, int
cSums, int sum ) int i, j for ( i0
iltstats.length i ) report.writeString(DEP
TSi) for ( j0 jltstatsi.length j )
report.writeInt(statsij) repor
t.writeInt(rSumsi) report.writeString("T
otal") for ( j0 jltcSums.length j )
report.writeInt(cSumsj) report.writ
eInt(sum,10) // writeStats
35What 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
36Array Declarations
37Good Luck on your Exam!!