Landing : Athabascau University

Assignment 3 reflections

  • Public
By Osamah Sadeq August 30, 2022 - 9:24pm

Problem 1 reflection 

In this problem, we were supposed to Write a program that opens a file and counts the whitespace-separated words in that file. Once we run the code. We should get a message to the output saying, “Please Enter The Name Of Your File. Once we enter the name of the file which is excerpt.txt, it will tell us how many words there are in that file. I started the program by importing the file, where java’s representation of a file path name. Then, the name of the class, which is WordsCounter, as well as the main method. I learned to use Scanner exc = new Scanner (System.in); because it helps us to take input from the user. Then, I declared the 2 strings I have in my code, as well as declaring an integer. After that I asked the user to print to the output. I used Counter = .nextLine(); to scan the file name. After that, I used a while loop to scan the lines of the file. I also used trim(); to eliminates leading and trailing spaces. Finally, I used .split to convert lines into a string array. Overall, this problem took a bit of my time dealing with the excerpt.txt file. My code did not recognize the file, until I solved the issue later, I realized that I saved the txt file with one letter missing. 

 

 

Problem 2 reflection  

In this problem, we were supposed to Write a program that opens a (text) file and displays the contents of that file one line at a time. Once we run the code, the first line of the .txt file will pop up, once we hit "Enter", the second line will pop up. Each time we hit enter, a line of the .txt will pop up. I started importing the file path name. Then followed by the class name which is, ReadFiles, as well as a main method. Followed by the Scanner (System.in); as it will help us to take input from the user. I also used Read.nextLine(); where we will be getting a file name from the user. FileInputStream(“”); this statement will open the file for us. I also used scanner (file) to scan the given file. After that I initialized the string. While loop was involved in my code to run line by line until the last line of the file. Read.nextLine(); this was for the user to press “Enter”. catch (IOException e) this is to catch the errors, if any. The problem was ok because it is almost related to problem 1. 

 

 

Problem 3 reflection 

In this problem, we are supposed to write a program in which you create a class called TextFileReader. The TextFileReader class must contain an instance variable consisting of an array of Strings 100 elements long. As well as another class TextFileReaderDemo with the main method. Once we run the program, we are supposed to see a line reading the excerpt.exe. Underneath that line, we were supposed to see a number with each line. The first line of the txt is with number 1, second line with number 2, third line with number 4, until we reach to the last line of the text, which is 30 lines or words. I started the code by creating the first class which is TextFileReader. Then, an instance variable, as well as a default constructor. Also, a constructor that takes a string argument. “Read" = new BufferedReader (new FileReader (“”)); this statement is the read object for the file. num[counter]=”” ; counter++; This is to add the line to the array. I also used the .read() to be able to read the next line. While StringBuffer contents() was to convert an array to a string buffer. The statement after that, which is “new StringBuffer();” is to declare an empty “stingbuffer. The last thing was to print the data to the output. Next, was creating a class called TextFileReaderDemo” That has the main method. This problem was a bit challenging for me, I left it until the end, after I finished problem 4 and 5.  

 

Problem 4 reflection 

We were supposed to write a program that creates three floating point arrays. Fill the first two arrays with 25 floating point numbers using loop structures. Once we run the code, we are supposed to see 25 numbers starting from 0. The third line would be as follows 2; 2.0 + 2.0 = 6.0. We are multiplying the first 2 numbers by each other to get the second number. 2; this is the first number, 2.0 is the second number, multiplying these numbers will give us 4.0. Now we add the answer of multiplication to the second number, and it will give us 6. I started the problem with the name of the class as well as the main. method. []Array1=new float this statement is to store the required numbers which is 25. i=0 ;ii++ In this will be filling the first array with a loop statement from 0 to 25. This problem was ok.  

 

Problem 5 reflection 

In this problem, we were supposed to define a class called Book. The Book class should store attributes such as the title, ISBN, author, edition, publisher, and year of publication. These attributes must be private. We are supposed to make a program that displays the different types of book info. The first line that should be displayed on the screen is unsorted book information. The second line should display sorted books by year of publication in ascending order. The last line should display the book info sorted by title and year of publication. I started the problem with the name of the class as well as the main method. As well as using array-list to store the books. Bookself.add is where I stored the first set of books to the array list using the .add method. System.out.println("\nListing the Unsorted Books:"); where this to list the books are not sorted.  yearPubComparator is to sort the books by the year of publication. sort(bookself,Book.titleComparator); is to sort the books by the title. Book is the second class where it has the getters and setters.