Lompat ke konten Lompat ke sidebar Lompat ke footer

CS506 Assignment 1 CS 506 Assignment 1 2022


CS506 Assignment 1 CS 506 Assignment 1 2022



The add person method first takes input for name,  address, and phone number and then constructs a PersonInfo object by using the recently taken input values. Then the newly constructed object is added to the ArrayList - persons. 

The search person & delete person methods are using the same methodology i.e. first they search the required record by name and then print his/her detail or delete the record permanently from the ArrayList. 

Both the methods are taking string argument, by using this they can perform their search or delete operation. We used for loop for iterating the whole ArrayList.By using the size method of ArrayList, we can control our loop as ArrayList indexes range starts from 0 to one less than size. 

Notice that,  inside the loop, we retrieve each PersonInfo object by using the down casting operation.  After that, we compare each  PersonInfo object’s name by the one passed to these methods using an equal method since  Strings are always being compared using an equal method. 

Inside if block of search person, the print method is called using PersonInfo object that will display person information on GUI.  On the other hand,  inside if a block of delete person method,  remove method of ArrayList class is called that is used to delete record from persons i.e. ArrayList.

Example Code: CException.java 

The following program reads a line (hello world) from a file and prints it on the console. The File reading code is probably new for you. We’ll explain it in the coming handouts (Streams). For now, assuming that the code written inside the main read one line from a file and prints that to the console.

Compile & Execute 

If you try to compile this program, the program will not compile successfully and displays the message of the unreported exception. This happens when there is code that can generate a checked exception but you have not handled that exception. Remember checked exceptions are detected by the compiler. As we early discussed, without handling the Checked exception, our program won’t compile.

Modify CException.java 

As we have discussed earlier,  it is mandatory to handle checked exceptions. In order to compile the code above,  we modify the above program so that the file reading code is placed inside a  try block. The expected exception  (IOException)  that can be raised is caught in the catch block.

Compile & Execute 

After making changes to your program, it would compile successfully. On executing this program, hello world would be displayed on the console Note: Before executing, make sure that a text file named input.txt must be placed in the same directory where the program is saved. Also, write hello world in that file before saving it.

Stream classification based on Functionality 

Based on functionality streams can be categorized as Node Stream and Filter Stream. Node Streams  are  those  which  connect  directly  with  the  data  source/sick   and  provide  basic functionality to read/write data from that source/sink 

FileReader fr = new FileReader(“input.txt”); 

You can see that FileReader is taking a data/source “input.txt” as its argument and hence it is a node stream. 

FilterStreams sit on top of a node stream or chain with other filter streams and provide some additional functionality e.g. compression, security, etc. FilterStreams take another stream as their input.  

BufferedReader bt = new BufferedReader(fr);  

BufferedReader makes the IO efficient (enhances the functionality) by buffering the input before delivering. And as you can see that BufferedReader is sitting on top of a node stream which is FileReader.


Download Here

Posting Komentar untuk "CS506 Assignment 1 CS 506 Assignment 1 2022"