Download presentation
Presentation is loading. Please wait.
Published byArleen Johns Modified over 9 years ago
1
F27SA1 Software Development 1 7. Java Programming 6 Greg Michaelson
2
Example: questionnaire 1 suppose a questionnaire has answers coded: – 0 == disagree strongly; 1 == disagree; 2 == neutral; 3 == agree; 4 == agree strongly suppose quest.txt has a sequence of coded questionnaire responses $ java TestFreq1 quest.txt 6 0s 10 1s 11 2s 10 3s 6 4s one variable per response count
3
Example: questionnaire 1 class Freq { int f0,f1,f2,f3,f4; Freq(){} void initFreq() { f0 = 0; f1 = 0; f2 = 0; f3 = 0; f4 = 0; } void showFreq() { System.out.println(f0+" 0s"); System.out.println(f1+" 1s"); System.out.println(f2+" 2s"); System.out.println(f3+" 3s"); System.out.println(f4+" 4s"); }
4
Example: questionnaire 1 void incFreq(int i) { if(i==0)f0 = f0+1; else if(i==1)f1 = f1+1; else if(i==2)f2 = f2+1; else if(i==3)f3 = f3+1; else if(i==4)f4 = f4+1; else System.out.println("bad input: "+i); }
5
Example: questionnaire 1 void doFreq(Scanner s) { initFreq(); while(s.hasNext()) incFreq(s.nextInt()); showFreq(); }
6
Example: questionnaire 1 class TestFreq1 { public static void main(String [] argv) throws FileNotFoundException { Freq f = new Freq(); File fin = new File(argv[0]); Scanner s = new Scanner(fin); f.doFreq(s); }
7
Example: questionnaire 1 very poor programming style duplication of: – variables – initialisation – test – increment – print hard to change to different number of questions generalise as operations on array of frequency counts
8
Arrays array is a linear sequence of elements of same type array can be any type N elements, numbered from 0 to N-1 use array for multiple data – that needs to be accessed more than once
9
Array declaration type [] identifier ; declares an array variable identifier does not allocate any memory to elements can be associated with an array of type of any size int [] squares; squares can be associated with any array of int
10
Array construction type [] identifier = new type [ expression ]; identifier = new type [ expression ]; evaluate expression to an integer associates identifier with space for integer elements of type array elements have no initial values
11
Array construction String [] names = new String[20]; names associated with an array of 20 String squares = new int[37]; squares associated with an array of 37 int
12
Array initialisation { expression 1, expression 2,... expression N } array literal creates array object with N values from expressions all expressions must have same type use in declaration or assignment – to set array variable to object with specific values
13
Array initialisation String [] fruit = {“apple”,”banana”,”cloudberry”,”date”}; fruit is associated with an array of String of length 4 where: fruit[0] == “apple” fruit[1] == “banana” fruit[2] == “cloudberry” fruit[3] == “date”
14
Array creation summary creates variableallocates spaceinitialises values definition√XX new X√X literalX√√ definition + new √√X definition + literal√√√
15
Array index assignment & expressions refer to elements as:... identifier [ expression ]... expression is the array index expression must return an integer from 0 to one less than the size of the array if the index is = size of the array then array bound error
16
Array assignment identifier [ expression 1 ]= expression 2 ; evaluates expression 2 to a value of the same type as identifier evaluates expression 1 to an integer sets element integer of identifier to value
17
Array assignment set square elements to square of index int i=0; while(i<37) { squares[i] = i*i; i = i+1; } index i from 0 to 36 numbers[0] == 0*0 numbers[1] == 1*1 numbers[2] == 2*2...
18
Array expression identifier [ expression ] evaluates expression to an integer returns the value of element integer of identifier
19
Array expression set sumSq to sum of elements of square int sumSq = 0; i = 0; while(i<37) { sumSq = sumSq+squares[i]; i = i+1; } sumSq == 0+ squares[0]+ squares[1]+... + squares[36]
20
Array length array.length returns the number of elements in array names.length 20 s quares.length 37 better to use length than an explicit integer value can use program with array of same type but different length
21
Example: questionnaire 2 f0, f1, f2, f3, f4 f[0], f[1], f[2], f[3], f[4] to do something for questionnaire answer i – do something to f[i]
22
Example: questionnaire 2 class Freq { int [] f; Freq(int n) { f = new int[n]; } void initFreq() { int i = 0; while(i<f.length) { f[i] = 0; i = i+1; }
23
Example: questionnaire 2 void showFreq() { int i = 0; while(i<f.length) { System.out.println(f [i]+" "+i+"s"); i = i + 1; } } void incFreq(int i) { if(i>=0 && i<=4) f [i] = f[i]+1; else System.out.println("bad input: "+i); }
24
Example: questionnaire 2 void doFreq(Scanner s) {... } } class TestFreq2 { public static void main(String [] argv) throws FileNotFoundException { Freq f = new Freq(); File fin = new File(argv[0]); Scanner s = new Scanner(fin); f.doFreq(s); }
25
iteration: for often using: int identifier = 0; while( identifier < expression ) { statements ; identifier = identifier +1; } to do things with: – identifier == 0, 1, 2... expression -1
26
iteration: for generalise to: type identifier = expression 1 ; while( expression 2 ) { statements ; identifier = expression 3 ; } expression 1 == initial value for identifier expression 2 == check if identifier has reached final value expression 3 == new value for identifier towards making expression 2 to be true
27
iteration: for for loop captures this structure: for( type identifier = expression 1 ; == initialise expression 2 ; == check identifier = expression 3 ) == change statements ;
28
Example: questionnaire 3 void initFreq() { for(int i = 0;i<f.length;i = i+1) f[i] = 0; } void showFreq() { for(int i = 0;i<f.length;i = i+1) System.out.println(f[i]+" "+i+"s"); }
29
Example: horizontal bar graph $ java TestHBar quest.txt 6 0s 10 1s 11 2s 10 3s 6 4s 0: ****** 1: ********** 2: *********** 3: ********** 4: ******
30
Example: horizontal bar graph class Freq { int [] f;... int [] getFreq() { return f; } }
31
Example: horizontal bar graph class HBar { String point; HBar(String point) { this.point = point; } void showBar(int n) { for(int i=0;i<n;i=i+1) System.out.print(point); System.out.println(); }
32
Example: horizontal bar graph void showGraph(int [] g) { for(int j=0;j<g.length;j=j+1) { System.out.print(j+": "); showBar(g[j]); }
33
Example: horizontal bar graph class TestHBar { public static void main(String [] argv) throws FileNotFoundException { Freq f = new Freq(); File fin = new File(argv[0]); Scanner s = new Scanner(fin); f.doFreq(s); HBar h = new Hbar(“*”); h.showGraph(f); }
34
Simplifying statement sequences can use sequences of definitions to structure actions: Freq f = new Freq(); File fin = new File(argv[0]); Scanner s = new Scanner(fin); f.doFreq(s); creates otherwise unnecessary variables – e.g. fin s
35
Simplifying statement sequences can nest constructor/ method calls without introducing variables: Freq f = new Freq(); File fin = new File(argv[0]); Scanner s = new Scanner(fin); f.doFreq(s); Freq f = new Freq(); Scanner s = new Scanner(new File(argv[0])); f.doFreq(s); Freq f = new Freq(); f.doFreq(new Scanner(new File(argv[0])));
36
Scope/extent of for variable the loop control variable defined at the start of the for – only exists during the for if you want to know the value of the variable after the for – must define it before the for e.g. to show value of identifier at end of loop type identifier ; for( identifier = expr 1 ; expr 2 ; id = expr 3 ) statements ; System.out.println( identifier );
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.