CS 160, Spring 2015
Programming Quiz Q5
Practice Programming Quiz
Must be completed during recitation on May 4-6.
You have 50 minutes to complete the quiz.
- You CANNOT use any outside resources
- This includes the internet, past recitations or programs, textbooks, notes,
friends, enemies, classmates, cell phones, tablets, laptops, etc.
- Any and all questions should be directed to your TA and your TA's helper.
- This is a practice quiz copied from the final programming quiz last
semester.
Instructions:
Setup
- Create a project called Q5 in Eclipse.
- Create a class called Q5 in the project Q5 with a main method.
- Download QuizInterface.java to the source (workspace/Q5/src) directory.
- Download input.txt to the project (workspace/Q5) directory.
- Make your Q5 class implement this interface.
- Use Eclipse to add empty methods for all the methods required in the interface.
-
Create the following public static class variables.
- An integer called numberLines to store the line count.
- An integer called numberChars to store the character count.
- An integer called numberLetters to store the letter count.
- An integer called numberDigits to store the digits count.
- An array of strings called fileContents to store the file contents.
- An array of doubles called testArray to store some numbers.
- Initialize all integer counters to zero when they are declared.
- Allocate and initialize testArray with the values: 92.2, 45.9, 16.4, 31.2, 67.8, 95.0, 71.1.
- Do not allocate or initialize the fileContents array.
Instructions for each method:
- readFile
- This method has a single parameter, which is the name of the file.
- The purpose of this method is to read the contents of the file into an array.
- You must use a try/catch block and a Scanner object.
- The first line of the file has an integer that specifies the number of lines.
- Read this value into the variable numberLines.
- Call nextLine again to discard the rest of the line!
- Allocate the fileContents array to have numberLines elements.
- Write a for loop that reads the specified number of lines into fileContents.
- The catch exception code should do nothing.
- computeStatistics
- This method takes an array of Strings as a parameter and has no return value.
- The purpose of this method is to count all the characters in an array of strings.
- Count the number of characters in the array and assign to numberChars.
- Count the number of letters in the array and assign to numberLetters.
- Count the number of digits in the array and assign to numberDigits.
- Do not use fileContents in this method, instead count characters in the array passed in.
- printStatistics
- writeFile
- findLargest
- This method takes an array of doubles as a parameter and has a single return value of type double.
- The purpose of this method is to find the largest element in the array.
- Iterate the array and find the largest value, then return it.
- You do not have to handle an empty array.
TESTING: Test your methods by creating a Q5 object and using it to call each method, for example:
Q5 q = new Q5();
q.readFile(args[0]);
q.computeStatistics(fileContents);
q.printStatistics();
q.writeFile(args[1], fileContents);
System.out.println("Array = " + Arrays.toString(testArray));
System.out.println("Largest value = " + q.findLargest(testArray));
You must submit your Q5.java program to the Checkin tab to get graded for Programming Quiz 5.
Automated grading is enabled and you can submit multiple times.
© 2015 CS160 Colorado State University. All Rights Reserved.