Landing : Athabascau University

assignment3

Last updated December 3, 2022 - 7:56am by Yuyang Liu

Reflections

Problem 1:

This question is about reading and processing files in Java. Particularly, opens a file and counts the whitespace-separated words in that file. I chose to obtain the name via prompt and user input. There are many different file reader classes available in Java, for example:

 

1. Using BufferedReader class

2. Using Scanner class

3. Using File Reader class

4. Reading the whole file in a List

5. Read a text file as String .

 

In this case, I have tried Scanner class as well as BufferedReader. And I found Scanner class more convenient. Scanner class provides a hasNext() function to check the file, and a nextLine() function to read a line, very similar to system.in which has been used in the previous assignments many times. I used a while loop to read every line of the file and split the words into a word array. I then count the number of words in the array to know how many words each line contains. Finally, add up all lines, I can have the total number of words. What needs to be carefully handled here is exceptions. Read operations need to be surrounded with try catch blocks to handle the potential file exception such as FileNotFound exception.

 

Problem 2:

This question is about reading and processing files in Java. Particularly, opens a (text) file and displays the contents of that file one line at a time. I obtain the name of the input file for this program via prompt and user input, rather than via command line arguments. 

Scanner class provides a hasNext() function to check the file, and a nextLine() function to read a line. I used a while loop to read every line of the file and print the line as instructed. However the line will only be printed when the user has entered in keyboard. I didn’t check what the user-entered in this case, I assume that user will type enter. So, I only wait randomly for a input to continue printing the next line and my solution works properly.

 

Problem 3:

Two classes are needed for this problem. The first one is TextFileReader that open the file and read the contents of the file into the array of Strings line by line until the array is full. I used a while loop and a scanner class to read every line of the file. I have a array of size 100. While reading every single line, I add the line into the array until it is full.

StringBuffer is also used in this problem which is a new topic. StringBuffer works just like a list and we can append things into it. So, I append all the contents in the array to the StringBuffer. For the display method, I loop over the array, check whether the content is null (some may be null because we only have about 30 lines in the excerpt.txt). And print the contents line by line.

 

Problem 4:

This problem has nothing to do with file processing, three arrays are created. So, I have three loops, each loop is used to fill the corresponding array with index counters (from 0 to 24) in total 25 floats. And print the three arrays as requested.

 

Problem 5

This problem is more complex than the previous ones. It relates to the previous problem which is about class Book. I modified the original book class by adding a constructor for initializing books. I also add a tostring method to make it easy to print the books. At the beginning, I didn’t have the toString method, I need to write a lot of code to deal with the printing.

In addition, comparator is used in this problem. This topic is confusing for beginners, especially, when trying to figure out how to distinguish ascending and descending order. Also, we can only have one method in the bookshelf class, so how to sort the books without writing other helper functions is critical. I choose to use the sort method provided by list interface. And a comparator is specified in the sort method.

 

Comments

These comments are moderated. Your comment will not be visible unless accepted by the content owner.

Only simple HTML formatting is allowed and any hyperlinks will be stripped away. If you need to include a URL then please simply type it so that users can copy and paste it if needed.

(Required)

(Required)