CS1103 Learning Journal Unit 6 - Files and Networking; Advanced GUI Programming

CS1103 Learning Journal Unit 6 - Files and Networking; Advanced GUI Programming

I learned about file manipulation, network communication, and advanced GUI programming this week. First, file operations include reading and writing Files. Specifically, if we want to read a file, we can use the FileReader class to create an input stream. But since the FileReader class has only some primitive methods, we can use Scanner or BufferedReader class to wrap the FileReader object; if we want to write a file, we can use the FileWriter class. Similarly, we can wrap the FileWriter object with the PrintWriter class to call its prosperous methods. In addition, one point to note is that it is a good practice to call the close() method to close the file after reading and writing it. Second, network communication mainly involves two aspects. One is to use the URL and URLConnection classes to read network resources. Concretely, we need to create a URL object first and then call the openConnection() method to get a URLConnection object, through which we can obtain the InputStream. The other is to utilize a socket to communicate between server and client. To be more specific, we need to adopt the Socket class to create a Socket object on the client-side and then use the ServerSocket class to create a ServerSocket object as the listener on the server-side. We can establish a connection between the client and the server in this case. Additionally, it is worth mentioning that before calling the close() method to end the connection, it is necessary to call the flush() method to ensure the successful sending of the data. Third, advanced GUI programming includes lots of the classes and terms: BufferedImages, Pixels, Resources, Cursors and Icons, image formats, Graphics2D Class, FontMetrics Class, Transparency, Antialiasing (UoPeople, nd), which are well used and consolidated in programming assignments.

Through the discussion assignments, I have a deeper understanding of Sockets and ServerSockets. Specifically, Socket and ServerSocket are two classes under the java.net package (UoPeople, n.d.). For the Socket class, it contains a constructor Socket(host, port), which can be used to create a Socket object to connect to a given host and port (Horstmann, 2019). For the ServerSocket class, it is used primarily on the server-side. We can use the constructor ServerSocket(port) of the ServerSocket class to create an object and monitor a port. Then we can call the accept() method to get a server socket object and establish a connection between the server and client. After transferring the data, we need to call the close() method to terminate the connection. However, before closing the connection, the flush() method needs to be called to ensure that the data was sent successfully (Eck).

In the programming assignment and exercise, I reinforce my knowledge of file manipulation. Specifically, in the programming lab, the image data needs to be saved to a text file. Hence, I used the constructor of the FileWriter class to create an output stream, wrapped it with the BufferedWriter class, and then output the content to the corresponding text. In the exercise, I practised using the recursive method to list a dictionary and all its subdirectories. During the process, I mastered some standard methods of the File class, including isDirectory(), exists(), and list(). Through this training, I have a deeper understanding of the File class in java.

In addition to completing the above assignments, I also took the graded quiz. There are two questions worth mentioning. Specifically, they ask about the order of elements in LinkedHashSet and TreeSet. The answer is that the elements in LinkedHashSet are in insertion order, and the elements in TreeSet are in sorted order. In the discussion forum unit 5, I clearly explain why: “LinkedHashSet is implemented based on a linked hash table, which remembers the order we inserted items. So when we iterate over the elements in the LinkedHashSet with an iterator, the output order is the insertion order. However, TreeSet is a class that implements collections based on red-black trees. We can insert elements into the collection in any order. But when we iterate over the collection, the values are automatically presented in sorted order” (He, 2022).

Word count: 680

References

1.Horstmann, C. S. 2019. Core Java Volume I–Fundamentals (11th ed.). Pearson.

2.Eck, D. J. (2019). Introduction to Programming Using Java.
http://math.hws.edu/javanotes

3.University of the People. (n.d.). Learning Guide Unit 6.
https://my.uopeople.edu/mod/book/view.php?id=279562

4.University of the People. (n.d.). Discussion Forum Unit 5.
https://my.uopeople.edu/mod/forum/discuss.php?d=729616

​


This is the ending, thanks for reading.