120 likes | 136 Views
Learn how to accumulate counts of items in an array, including character manipulation and reading from a text file.
E N D
FURTHER PROGRAMMINGLecture 6 • Accumulating counts of items • Specifically for FP2 • Character manipulation • Reading one char at a time from a text file MSc IT Further Programming Lecture 6
Accumulating Totals in an Array : Why? • Find out how many IT students have enrolled for each strand • Simulate throwing a die 100 times (using Random numbers) and count how many times each number (1..6) is thrown • Given original marks, convert this to a grade and count the number of students with each grade • Given sales figures, add them up for each quarter (or month, or saleman….) MSc IT Further Programming Lecture 6
Accumulating Totals in an Array : Class • Frequency class • Instance variables • an array of labels, one for each item • a corresponding parallel array, for counting or totalling, with one element for each item • The Constructor • Instantiates the array of countscounts = new int [firstArray.length]; • ALSO, each element of the array must be initialised to zero, so that later on, we can just add one to any particular element. Always good style – not essential in java because default integer value is 0. MSc IT Further Programming Lecture 6
Index of DBS is 2 Increase count where the index = 2 Accumulating Totals in an Array : Adding an item • Frequency class • Method to add an item • Get the index for the element of the counting array it belongs to. This code will vary depending on the type of item counted. • Increment that element of the array by one MSc IT Further Programming Lecture 6
SEARCHING AN ARRAY • Assume that the array is NOT sorted • only available search algorithm is linear search • To find the first (or only) match • start at the beginning, look at each item in turn • either required item is found • or end of list is reached without success • end the search when the required item is found • it is inefficient to continue searching • If there are several matching items • you might e.g. want to count them, return the last … • this will require variation from the basic algorithm MSc IT Further Programming Lecture 6
BASIC SEARCH ALGORITHM Set 'item found' false Set 'current item' at beginning of list While (item not found) and (more items) if current item equals search item set found to true take note of current item else move on to next item End of while loop If (item is found) return appropriate value Else return some indicator of no value MSc IT Further Programming Lecture 6
SEARCHING FOR DBS • Found = false, number of items = 4, • current index = 0, value at index 0 = CHI • This is not = DBS, so increase current index to 1 • Current index = 1, value at index 1 = BBI • This is not = DBS, so increase current index to 2 • Current index = 2, value at index 2 = DBS • This is = DBS, so set found = true, index position to the current index = 2 • Now loop stops because found = true MSc IT Further Programming Lecture 6
OTHER WAYS OF SEARCHING • You could search through ALL the items with a for loop or a simpler while loop • But if there is more than one occurrence, you will find the last one • It is poor style to carry on searching when you have found what you are looking for • You could use ‘break’ to stop the loop MSc IT Further Programming Lecture 6
Accumulating Totals in an Array : using the frequency class • In the main method • Declare and instantiate an object of the frequency class • For each item to be taken into account • Obtain it (from user / text file ….) • Pass it to the frequency class to be processed • i.e. counts incremented • Present some sort of report • Print details to a console or text file • Display details in a GUI MSc IT Further Programming Lecture 6
FREQUENCIES WHEN YOU CAN CALCULATE THE INDEX • For sequential items, you might not need 2 parallel arrays. Sometimes the index in the frequency array can be calculated: • If you know the month number (e.g. 4), then you know that the 4th element of the array (i.e. with index = 4 – 1 = 3) is the one to be incremented.In this case it is not necessary to search an array to find the index position. • You can do this with the alphabet MSc IT Further Programming Lecture 6
Comparing CHARACTERS • Characters are represented in the computer by a numeric code • ‘A’ is represented by the integer 65, ‘B’ by 66 etc • To get the next character, add on 1, cast back to a char char nextChar = (char)(‘A’+1); • To find the index position in an alphabetic array int index = ch – ‘A’; • To find if a character is alphabetic if (ch >= ‘A’ && ch <= ‘Z’) MSc IT Further Programming Lecture 6
Reading a text file character by character • If you want all the characters in a file, and lines do not matter, you do not need to use the BufferedReader • The read method returns -1 for end-of-file (not null!) FileReader fIn = new FileReader(filename); boolean endOfFile = false; while (!endOfFile) { int ch = fIn.read(); if (ch == -1) { endOfFile = true; } else //process char { //do something with the character } } MSc IT Further Programming Lecture 6