Landing : Athabascau University

Reflections on Assignment 3: Revision

Last updated January 30, 2024 - 6:45am by Joan Oladunjoye

 

Assignment 3 Reflection:

Project 1: White Space Counter:

Files linked with this project: WhiteSpace.java and text file entitled excerpt.txt.

This challenge question is mapped to the following learning objectives:

     articulate the principles of object-oriented problem solving and programming.

     outline the essential features and elements of the Java programming language.

     explain programming fundamentals, including statement and control flow and recursion.

     apply the concepts of class, method, constructor, instance, data abstraction, function abstraction, inheritance, overriding, overloading, and polymorphism.

     program with basic data structures using array, list, and linked structures.

     explain the object-oriented design process and the concept of software engineering.

     program using objects and data abstraction, class, and methods in function abstraction.

     analyze, write, debug, and test basic Java codes using the approaches introduced in the course.

     analyze problems and implement simple Java applications using an object-oriented software engineering approach.

Overview of Program.

 

This program illustrates Reading from a text file using File Reader Buffered Reader classes. In this program, a text file is opened and the number of whitespaces separated words in a specified file is counted. The name of the input file will be obtained via the command-line argument.  

 

In this program, a class was defined and within this class the main method was declared.  This main method serves as the entry point to the Java program.  The main method accepts an array of strings (‘args’) representing command-line arguments and specifies that it may throw an input/output exception (IOException). IOExceptions allows for robust handling of programs as it checks a program to see if there are any errors in user input output operations within a computer system.

Next the command line argument is validated using an IF function to check if only one command line argument (filename) is entered after the class name when running file on terminal.  The statement also ensures that file name is actually entered after class name has been entered to console, in the format java WhiteSpace excerpt.txt.  If not, the program is terminated and the user will need to run program again ensuring that correct parameters are entered as required by the program. Next a line of code was entered to help retrieve file name from command line argument after which the file reader and buffer reader classes were used to read the contents of the file that required reading.  While writing this program, I found out that it is good practice to use both classes together, of course this is always dependent on project at hand.  However, I learnt that the file reader reads characters from text files one character at a time while the buffered reader has the added functionality of being able to read larger amounts of data at a time. The buffered reader also has the added advantage of have the readline() method that allows for text to be read line by line within a program which makes processing of data easier when needed.  For this challenge question, I chose to use the fileReader class directly to read characters from the file and not to use the FileInputStream (creates a stream of bytes) and InputStreamReader (converts bytes to characters)classes that reads data in bytes because I didn’t think these two classes were necessary for this challenge question.

Next, I used the ‘while loop’ to read and count the number of words and white spaces in my given text.  I encountered some geometric challenges here which initially resulted in a logical error when my program was run. This logical error produced output in terms of white space count that was way above the number of whitespaces available within given text.  

 

Test Plan

A test plan outlines the testing approach, scope, resources, and schedule for testing. Test plan carried out on program entitled WhiteSpace.java

My test plan includes the following normal, borderline/boundary, and extreme test scenerios.

References:

  1. @author W3spoint. Command Line Arguments In   Java.https://www.w3schools.blog/command-line-arguments-in-java.Accessed 10th January 2024
  2. Baeldung. Difference Between FileReader and BufferedReader in Java.https://www.baeldung.com/java-filereader-vs-bufferedreader#:~:text=FileReader%20can%20be%20enough%20if,BufferedReader%20is%20the%20better%20option. Accessed 15th January 2024
  3. Eck, David J. "Introduction to Programming Using Java (Version 8.1)."July 2019. Accessed 9th January 2024.
  4. Geeksforgeeks.https://www.geeksforgeeks.org/different-ways-reading-text-file-java/. Accessed 12th January 2024
  5. HandShake 4U. Read a text file, Display the no of Alphabets, Paragraph, Words, Whitespace and Character. Https://youtu.be/6f33GWE3WWc. Accessed 12th January 2024.

Project 2: File Display with Pause:
 
Files linked with this project: DisplayData.java and text file entitled excerpt.txt.

  • This challenge question is mapped to the following learning objectives:
  •      articulate the principles of object-oriented problem solving and programming.
  •      outline the essential features and elements of the Java programming language.
  •      explain programming fundamentals, including statement and control flow and recursion.
  •      apply the concepts of class, method, constructor, instance, data abstraction, function abstraction, inheritance, overriding, overloading, and polymorphism.
  •      program with basic data structures using array, list, and linked structures.
  •      explain the object-oriented design process and the concept of software engineering.
  •      program using objects and data abstraction, class, and methods in function abstraction.
  •      analyze, write, debug, and test basic Java codes using the approaches introduced in the course.
  •      analyze problems and implement simple Java applications using an object-oriented software engineering approach.

Overview of Program.
 
This program illustrates Reading from a text file using File Reader & Buffered Reader
classes. This program, reads a text file specified by the user, (excerpt.txt) and then displays
its content line by line with a pause between each line, waiting for the user to
press the enter key to display the next line.
 
Although this program seems quite easy upon completion, it was a bit challenging for me to get it all to work the way it ought to. The program can be explained as outlined below:

It uses Scanner to get user input for the filename.
It then uses a combination of FileReader and BufferedReader to open and read the specified file.
It reads the file line by line in a loop and displays each line with a blank line in between.
After displaying each line, it waits for the user to press the enter key before proceeding to the next line.
It handles potential exceptions, such as FileNotFoundException and IOException, and prints corresponding error messages.
I learnt a bit more about exception rules while completing this project and specifically about the e.getMessage()used to retrieve error message associated with the exception e
 
Test Plan:
 
My test plan for this program covers the following normal, borderline/boundary, and extreme test scenarios.

References:

  1. Eck, David J. "Introduction to Programming Using Java (Version 8.1)."July 2019. Accessed 20th January 2024.
  2. Geeks4Geeks. Java Program to Read a Large Text File Line by Line. https://www.geeksforgeeks.org/java-program-to-read-a-large-text-file-line-by-line/. Accessed 23rd January 2024
  3. Have java find/read a file from user input? https://stackoverflow.com/questions/19626468/have-java-find-read-a-file-from-user-input. Accessed 23rd January 2024
  4. How To Read a File Line-By-Line in Java https://www.digitalocean.com/community/tutorials/java-read-file-line-by-line. Accessed 23rd January 2024
  5. Oracle. "Search Results for e.getMessage in Java Documentation." Oracle Documentation, https://docs.oracle.com/search/?q=e.getMessage&category=java&product=en%2Fjava. Accessed 25thJanuary 2024.
  6. PsychoTech Institute™. Java Program to take the name of file from user, read the contents and display it on the console. https://youtu.be/XGqPedVrss8. Accessed 20th January 2024.

Project 3: Text File Reader and Display:
 
Files linked with this project: TextFileReader.java, TextFileReaderDemo.java and text file entitled excerpt.txt.
 

  • This challenge question is mapped to the following learning objectives:
  •      articulate the principles of object-oriented problem solving and programming.
  •      outline the essential features and elements of the Java programming language.
  •      explain programming fundamentals, including statement and control flow and recursion.
  •      apply the concepts of class, method, constructor, instance, data abstraction, function abstraction, inheritance, overriding, overloading, and polymorphism.
  •      program with basic data structures using array, list, and linked structures.
  •      explain the object-oriented design process and the concept of software engineering.
  •      program using objects and data abstraction, class, and methods in function abstraction.
  •      analyze, write, debug, and test basic Java codes using the approaches introduced in the course.
  •      analyze problems and implement simple Java applications using an object-oriented software engineering approach.

 
Overview of Program.
 
This program defines a class called TextFileReader with the following features:

It has an instance variable, an array of 100 strings (txtArray).
The class has two constructors: a default constructor that initializes the array to empty strings, and a parameterized constructor that takes a file name as an argument and reads the file's contents into the array.
The parameterized constructor reads lines from the file and fills the array until it's full.
The class includes a contents() method, converting the array into a StringBuffer for display purposes.
display() method prints the array to standard output with line numbers, starting from 1. The line numbers and corresponding strings are formatted as "Line #: "
The TextFileReaderDemo class serves as a testing ground for the functionality of the TextFileReader class.
It instantiates TextFileReader with a specified text file, retrieves the contents, displays them, and outputs the contents with line numbers.
The file name (excerpt.txt) for testing is provided as a command-line argument.
 
 
Test Plan:
 
My test plan for this program covers the following normal, borderline/boundary, and extreme test scenarios.
 
References:
Geeks4Geeks. Java Program to Read a Large Text File Line by Line. https://www.geeksforgeeks.org/java-program-to-read-a-large-text-file-line-by-line/. Accessed 23rd January 2024
 
Geeks4Geeks. StringBuffer class in Java. https://www.geeksforgeeks.org/stringbuffer-class-in-java/?ref=lbp
 
How To Read a File Line-By-Line in Java. https://www.digitalocean.com/community/tutorials/java-read-file-line-by-line. Accessed 22nd January 2024
 
Ram N.Java. How to store String values in Java Array? | Java Array (String values) | Java Tutorial. https://youtu.be/rsH5b2ZODHY. Accessed 25th January 2024
 
Ihechikara Vincent Abba. Default Constructor in Java – Class Constructor Example
https://www.freecodecamp.org/news/default-constructor-in-java/. Accessed 26th January 2024
 
Technotip. Using Constructors & Member Function: Java. https://youtu.be/gUXq1psurdk Accessed 27th January 2024.
 
Tutorialspoint. How to convert an array of Strings in a single String in java? https://www.tutorialspoint.com/how-convert-an-array-of-strings-in-a-single-string-in-java
Accessed 27th January 2024.
 
 
Project 4: Floating Point Array Calculator:
 
File/s linked with this project: FloatingPoint.java
 
This challenge question is mapped to the following learning objectives:
     articulate the principles of object-oriented problem solving and programming.
     outline the essential features and elements of the Java programming language.
     explain programming fundamentals, including statement and control flow and recursion.
     apply the concepts of class, method, constructor, instance, data abstraction, function abstraction, inheritance, overriding, overloading, and polymorphism.
     program with basic data structures using array, list, and linked structures.
     explain the object-oriented design process and the concept of software engineering.
     program using objects and data abstraction, class, and methods in function abstraction.
     analyze, write, debug, and test basic Java codes using the approaches introduced in the course.
     analyze problems and implement simple Java applications using an object-oriented software engineering approach.
 
Overview of Program.
This program does the following: 

It creates three floating-point arrays (arryNum1, arryNum2, and arryNum3), each with 25 elements.
It uses a for loop to fill the first array (arryNum1) with the loop counter values.
It uses another for loop to fill the second array (arryNum2) with the square of the loop counter values.
It uses a third for loop to fill the third array (arryNum3) with the sum of corresponding elements from the first two arrays.
It prints a blank line to separate the input and output.
It displays the content of all three arrays using a formatted output, showing the counter (or loop index) along with the elements from each array in a specific format.
The final output displays each element in the format: "counter: element1 + element2 = element3".
.
Test Plan
 
My test plan for this program covers the following normal, borderline/boundary, and extreme test scenarios.
References:

  1. Geeksforgeeks. Arrays in Java. https://www.geeksforgeeks.org/arrays-in-java/ Accessed 26th January 2024.
  2. W3Schools. Java How to Calculate the Sum of Elements. https://www.w3schools.com/java/java_howto_sum_of_array_elements.asp Accessed 26th January 2023.
  3. Iterating an Array using a For Loop - Initializing and Printing Arrays Java Tutorial – Appficial. https://youtu.be/FJmnBVZpEKc. Accessed 28th January 2023.
  4. homeandlearn.co.uk. Java Formatted Strings. https://www.homeandlearn.co.uk/java/java_formatted_strings.html. Accessed 28th January 2023.

Project 5: Bookshelf Sorter with ArrayList<> using Collectors.sort method and BookComparator:
 
File/s linked with this project: Book.java & BookShelf.java.
 
This challenge question is mapped to the following learning objectives:
     articulate the principles of object-oriented problem solving and programming.
     outline the essential features and elements of the Java programming language.
     explain programming fundamentals, including statement and control flow and recursion.
     apply the concepts of class, method, constructor, instance, data abstraction, function abstraction, inheritance, overriding, overloading, and polymorphism.
     program with basic data structures using array, list, and linked structures.
     explain the object-oriented design process and the concept of software engineering.
     program using objects and data abstraction, class, and methods in function abstraction.
     analyze, write, debug, and test basic Java codes using the approaches introduced in the course.
     analyze problems and implement simple Java applications using an object-oriented software engineering approach.


Project Overview:
 
This program consists of three classes, the Book class, the Book Comparator class and the Bookshelf class. The class BookComparator was created in the class Book and instantiated in the class Bookshelf. This program can be referred to as a book management system. The book class represents a book with private attributes such as title, ISBN, author, edition, publisher, and year of publication. It provides a constructor to initialize the attributes when creating a new book object and it includes getter and setter methods to access and modify the private attributes. The BookComparator Class implements the Comparator interface, which allows custom sorting of the Book objects. It Overrides the compare method to define a custom comparison logic. It first compares the titles of two books. If titles are different, it returns the result of comparing titles. If titles are the same, it then compares the years of publication. Finally, the BookShelf Class contains the main method, serving as the entry point for the program.  Here an ArrayList of Book objects are created representing a bookshelf with 12 books.
 
Test Plan:
 
My test plan for this program covers the following normal, borderline/boundary, and extreme test scenarios.
 
References:

  1. Alex Lee. ArrayList in Java Tutorial #36. Https://youtu.be/pTAda7qU4LY. Accessed 29th January 2024.
  2. Baeldung. Comparator and Comparable in Java. Https://www.baeldung.com/java-comparator-comparable. Accessed 29th January 2024.
  3. Michael Oswald.Java Comparator - What, Why, How Explained! Https://youtu.be/vV8mQVGbzpY. Accessed 29th January 2024.
  4. ShristiTechAcademy Java Programming - How to add userdefined objects to an ArrayList? https://www.youtube.com/watch?v=ba3U0X9J6Lc. Accessed 29th January 2024.