Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

18
Text Processing and More about Wrapper Classes

Transcript of Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

Page 1: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

Text Processing and More about Wrapper Classes

Page 2: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

Contents

I. The Test Score Problem

II. Exercise

Page 3: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

A. The Test Score Problem

• Professor Harrison keeps her students’ test scores in a Microsoft Excel spreadsheet. Figure below shows a set of five test scores for five students.

• In addition to manipulating the scores in Excel, Dr. Harrison wants to have a Java application that accesses them. Excel, like many commercial applications, has the ability to export data to a text file.

Page 4: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

• When the data in a spreadsheet is exported, each row is written to a line, and the values in the cells are separated by commas. For example, when the data shown in the Figure is exported, it will be written to a text file in the following format:

87,79,91,82,9472,79,81,74,8894,92,81,89,9677,56,67,81,7979,82,85,81,90

• This is called the comma separated value file format. When you save a spreadsheet in this format, Excel saves it to a file with the .csv extension. Dr. Harrison decides to export her spreadsheet to a .csv file, and wants to have a Java program that reads the file and calculates the score average for each student. Help her to do this task.

Page 5: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

Solution

1. Design

2. Test Case

3. Developing Test Class

Page 6: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

1. Design

• Finding the main task

???

+ int[] getAverages(String csvFileName)

Page 7: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

• Finding the class name

TestScoreProvider

+ double[] getAverages(String csvFileName)

Page 8: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

2. Test Case

• Input: TestScores.csv

• Output: { 86.6, 78.8, 90.4, 72.0, 83.4 }

Page 9: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

3. Developing Test Class

Develop class TestScoreProvider

Page 10: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

• Ask TestScoreManager to provide test scores for calculating the averages

Page 11: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

Develop TestScoreManager

TestScoreProvider

+ double[] getAverages(String csvFileName)

TestScoreManager

+ double[][] getTestScores(csvFileName)

Page 12: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

Develop getScores()

Page 13: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.
Page 14: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.
Page 15: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.
Page 16: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

B. Exercise

• The file SalesData.txt contains the dollar amount of sales that a retail store made each day for a number of weeks

1245.67,1490.07,1679.87,2371.46,1783.92,1461.99,2059.77

2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36

2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36

• Each line in the file contains seven numbers, which are the dales numbers for one week. The numbers are separated by a comma.

Page 17: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

• Develop a program that opens the file and processes its contents. The program should be able to calculate:– The total sales for each week– The average daily sales for each week– The total sales for all of the weeks– The average weekly sales– The week number that had the highest amount of

sales– The week number that had the lowest amount of

sales

Page 18: Text Processing and More about Wrapper Classes. Contents I.The Test Score Problem II.Exercise.

References

1. Starting Out with Java – From Control Structures through Data Structures, Chapter 10. Tony Gaddis and Godfrey Muganda, 2007