Here are some announcements regarding the end of the semester, and the specifications for the last Enhanced Labs. Don t forget that you need to take the Common Final Examination on Saturday, May 5, from 1:00 3:00 p.m. in one of these rooms (depending on your professor): Section Location of Final Exam 1 (Shultz) Plaza 136 2, 3 (Kramer) Plaza 238 A number of Computer Science faculty are collaborating to produce this exam. The Core Competencies should cover the required material, but note that each problem on the exam may overlap one or more Core Competencies. You will be receiving a copy of my records for which Labs, Participation Activities, and Challenge Activities you have completed. Please check this record against your memory of what you have done and let me know of any errors. This record will also let you see where you stand, grade-wise, in the course, and determine how much last-minute (not really, we still have three whole weeks) work you need to do. Core Competencies WSMA and EAC have been cancelled. They represent important material, but we wanted to cut the number of CC s, and these seemed less essential (WSMA is a lot like the TAP CC s, and EAC is probably pointlessly easy). Also, for Core Competency DOO you will only be required to identify classes and some of their instance variables, not their instance methods. Remaining Enhanced Labs The last Enhanced Labs that you might chose to do are grouped into two categories. Each lab in each category is worth 4 lab points, but we are only adding 8 more lab points to the total. Thus, to get a perfect Lab score, you only have to do 8 points out of these remaining 24 points of Enhanced Labs. And, if you complete more of these, you can get extra credit. Category 1: Working with ArrayList and Creating New Classes Enhanced Lab 99.1 [4 lab points] Your goal on this Lab is to create an application named WordCounter that will ask the user for the name of a file and will then scan that file, counting the number of different words occurring, and produce a report in a data file whose name is the original file name with -counts appended, that lists all the words occurring in alphabetical order, with the number of times each word written next to it. You should assume that the input file has been produced by running the application RemoveNonWords (available in the Labs folder) to remove all bothersome symbols, leaving CS 1050 Spring 2018 Page 83
a file that just has a bunch of words (sequences of 0 or more lowercase letters) separated by spaces and end-of-lines. For example, if the user enters the name story where the file story looks like this: good dogs like to eat hot dogs all dogs run and play with dogs hot good time then a file named story-counts should be created that looks like this: all 1 and 1 dogs 4 eat 1 good 2 hot 2 like 1 play 1 run 1 time 1 to 1 with 1 Here are some hints on how to approach this lab: You will need to make a class WordWithCount (or whatever you want to call it) that has as its instance variables a word and the number of times it has been seen. You will need an ArrayList<WordWithCount> variable that stores the list, in alphabetical order, of all the words seen so far. You will need a loop to repeatedly get the next word in the input file. Then you will need to take that string and scan the list until you either find an item in the list that matches the string and increment that object s count, or you find an item in the list that has a word occurring after the string, in which case you need to insert a new item right there, or you hit the end of the list without either of the first two things happening, in which case you need to add a new item on the end of the list. Here is an example showing how the list should look after each word is processed (the WordWithCount objects are shown like <dog,1> and the ArrayList is showing as usual): CS 1050 Spring 2018 Page 84
Word processed Resulting List [ ] dog [ <dog,1> ] cat [ <cat,1>, <dog,1> ] zebra [ <cat,1>, <dog,1>, <zebra,1> ] cow [ <cat,1>, <cow,1>, <dog,1>, <zebra,1> ] dog [ <cat,1>, <cow,1>, <dog,2>, <zebra,1> ] Once the input file has been scanned into the list, you will need another loop to create the report file. Enhanced Lab 99.2 [4 lab points] In this Lab you will be using ArrayList to calculate GPA s for a collection of students. Read the entire specification before you design and code your program! Specification Write a Java program to read grades from a file, calculate the grade point averages (GPAs) and write input data and results to an output file (see below for the input data). Use an ArrayList to hold the data stored in instances of the Gpa class you will create. The class has instance variables name (string), a GPA (double), and the total number of credit hours that the student passed with grades of A, B, C or D (integer). Any other grade is not a passing grade and its credit hours should not be counted in the total credits. Each input line contains the name of a student followed by a # and then by unknown number of pairs in the form a blank, an integer representing number of credits followed by a blank and then a letter representing the grade. After all the pairs of credits/grades is a #. Do not assume that there is at least one pair on a line. For example, there could be a line with only John Doe # #. Do NOT store the pairs anywhere in the program; process them fully as you encounter them. To calculate the GPA, assume an A is worth 4 points, a B is 3, C is 2, D is 1 and F is 0. If the pair contains any other value for a grade, the pair should be ignored and move on to the next pair (or the end of the data line denoted by a #). If there are no pairs on a line, the GPA is 0. As soon as it s read, echo the input data to the output file. After all the data has been read, write to the output file three blank lines, then a formatted table with headings giving the student names, GPAs, and the number of hours passed. At the end of the table, print the CS 1050 Spring 2018 Page 85
number of students, the average GPA, and the total of the hours passed. If you encounter an error in the input like an invalid letter, output a message to that effect to the output file. The main program consists mostly of calls to methods. Do not use global variables except for any formatting constants you might declare. The file names for this lab are: GPACalculator.java Gpa.java LabGPA-input.txt LabGPA-output.txt The main program The class file containing GPA information The input file The output file Note that the class Gpa contains only the three instance variables described above and a constructor to initialize the instance variables. Use the split method to parse each input line. Help on parsing the input line is at the end of this document. The input data follows. Van Zell David # 4 A 4 B # Toole Nancy # 3 X 3 B 5 A 4 A # Tucker Henry # 5 A 3 F 2 U 4 F # Van Zell Sally # 4 C 6 B # Tucker Becky # 4 A 3 A 2 A 5 A 3 A # Stratton Charles # 2 S 2 D 5 B 4 D 1 C # Toole Charles # 4 U # Nulle, Nellie # # Thomas William # 4 B 5 A 2 D 3 B # Sample output is below. Use leftpad and padstring (or printf) as needed to line up the columns. Do not use tabs. The GPA should be formatted to two decimal places. CS 1050 Spring 2018 Page 86
Example: Name GPA Total Credits ------------------------- ------- ------------------ Van Zell David 3.50 8 etc. Totals Average Sum of all credits GPA Hints on Parsing an Input Line The first input line is Van Zell David # 4 A 4 B # Use the split method once with a # as the separator. This creates a 2-element array, say split1, with split1[0] equal to the name (that needs to be trimmed) and split1[1] which contains # 4 A 4 B #. Now, use the split method again on split1[1] with a separator of a blank. This creates a 6-element array, say split2, that has: Index Contents 0 # 1 4 2 A 3 4 4 B 5 # Process the split2 array accordingly. Remember split2 is a string array, so digits must be converted to integers to be able to process them as integers. CS 1050 Spring 2018 Page 87
Enhanced Lab 99.3 [4 lab points] Using an ArrayList, find the sum, average, minimum and maximum of the elements. Specification Write a Java program that does the following: 1. Perform these steps until the user enters a 0. a. Read an integer value from the console. The value can be positive or negative. b. Add the value to an ArrayList. Note that the 0 is not entered into the ArrayList and all this step does is gather the data. No processing as in step 2 is performed. 2. After the user enters a 0, process the ArrayList with one for loop as follows: a. Calculate the total of the values entered. b. Calculate the average of the values entered. Ensure you handle the case of no entries correctly. c. Find the minimum value. d. Find the maximum value. Output to the console, with appropriate messages, the number of values read, the total, average, minimum and maximum values. To get your program checked off, it must be able to handle at least these cases: a. A program run with at least five non-zero numbers entered. b. A program run with only a 0 entered, meaning there are no numbers in the list to process. Write the program as one main method or use a few methods, at your discretion. CS 1050 Spring 2018 Page 88
Category 2: Some Fun Projects that Don t Use ArrayList Enhanced Lab 99.4 [4 lab points] This one is for the hard-core mathematicians! You might have seen this algorithm in a calculus class, but if you have only had algebra, you can still do this lab. This Lab asks you to implement an application that will let the user enter a starting guess for the root of a function f(x), and will then use finite-difference Newton s method to efficiently find a root of f to the full accuracy allowed by the machine arithmetic. The idea for this lab is that if you have a number a that you think is a decent guess for the root of some function f(x), then you can find the equation of the line passing through the points (a, f(a)) and (a + h, f(a + h)), and then compute the value of x for which that line crosses the x axis and use it as a better approximation to a root of f. This can all be done inside a loop so that it quickly finds a very accurate approximation to a root of f. Because this isn t a math class, here are all the details that you can use to quite easily implement this application. Note that the user has to actually type in the formula for f(x) and compile and run the application, so it s not for users who don t know how to program in Java at least a little. The slope of the line through (a, f(a)) and (a + h, f(a + h)) is m = f(a + h) f(a), h so the equation of that line is or y f(a) x a = m, y = m(x a) + f(a). If we set y equal to 0 and solve for x, we obtain x = a f(a) m. Then we can use that value as the next value of a and repeat the process. For a typical function f, a good value for h is 10 7. We want to display the values of a and f(a) inside the loop, and exit the loop when f(a) is less than 10 12, say. Implement these ideas and test them on some function(s) that you know have roots. For example, use f(x) = x 2 3, use 2 as the first value of a and verify that the root, which is 3, is obtained to about 12 decimal places. CS 1050 Spring 2018 Page 89
Enhanced Lab 99.5 [4 lab points] The application you will create in this Lab will convert a given dollar amount to words. Specification Convert a number between $0.01 and $9999.99 inclusive to words as if you are writing a check. Examples: Data In words $3.14 Three and 14/100 dollars $54.54 Fifty-Four and 54/100 dollars $100.00 One Hundred and 00/100 dollars $1024.54 One Thousand Twenty-Four and 54/100 dollars $2718.28 Two Thousand Seven Hundred Eighteen and 28/100 dollars The program will repeatedly read numbers from the keyboard, displaying on-screen each one converted to words, stopping when an out-of-range number is entered. You should use at least the test cases given above, but note that your logic should cover every possible in-range number. Enhanced Lab 99.6 [4 lab points] Your job on this Lab is to implement the game Lights Out. Your program must display graphically a 5 by 5 grid of squares, each of which is yellow to represent a light that is on or black to represent a light that is off. When the game starts, each light is randomly (50-50 chance) turned on or off. When the user presses the mouse button on one of the squares, it changes state (off to on or on to off) and similarly flips the state of all 2, 3, or 4 of its horizontal and vertical neighbors. The player wins when they manage to turn off all of the lights (hence the name!). CS 1050 Spring 2018 Page 90