Lab 8 - Lab Assignment 4
Lab 8 - Lab Assignment 4
Lab 8
(Lab Assignment 4)
File Management
Learning outcome:
Construct and Demonstrate Input/Output Class in Solving Programming Problems (P4, A3)
** Copy or other forms of cheating is forbidden. The standard penalty for the first offence is to
award 0 to all parties concerned.
QUESTION 1
Based on Lab Assignment 1 (Question 1), the info of patient health is stored in a text file known
as patientData.txt. Contents of the file as below (instance data field) are separated by a delimiter “:”.
Write a test program that will implement the HealthRisk class. Your program should have exception
handling to test whether the input file and already created or not. If the input file is not found, throw
the exception, show a message “Input file not found!!” and exit from the program. Write the output
file as similar to the output that you have produced in this question or any creativity output.
patientData.txt
Ramli Ahmad:M:80:
Ina Ghani:F:143:
John Elwis:M: 70:
Mary Elton: F: 90:
Ridzuan Ismail: M:100:
Herri Johnny:M:79:
QUESTION 2
Diberi satu gambar rajah UML bagi kelas yang dikenali sebagai Document. Kelas ini
mempunyai medan data infile berjenis java.io.File. Ia juga mempunyai pembina dan kaedah.
Kaedah countCharacters() mengira bilangan aksara dalam infile tidak termasuk aksara
whitespaces . Kaedah countWords() membilang perkataan dalam infile. Kaedah countLines()
membilang baris dalam infile. Perkataan dipisahkan oleh aksara whitespaces.
Document
-infile: java.io.File
+Document()
+Document(infile: java.io.File)
+countCharacters(): int
+countWords(): int
+countLines(): int
Anda dikehendaki:
a) Implemen kelas Document.
b) Implemen program Test yang akan mewujudkan objek File bagi fail input bernama
upm.txt yang terdapat di direktori C:\MyDocument\. Program ini akan mewujudkan
objek Document untuk objek File yang telah dibina. Program ini akan mencetak
bilangan aksara, bilangan perkataan, dan bilangan baris yang terdapat dalam fail
upm.txt ke dalam satu fail output dipanggil output.txt.
Program anda perlulah mempunyai pengendalian pengecualian untuk menentukan
kewujudan fail input.
Given is a UML diagram for a class known as Document. The class contains a data field
infile of type java.io.File. It also contains constructors and methods. Method countCharacters()
calculates the number of characters excluding whitespace characters in infile. Method
countWords() counts the words in infile. Method countLines() counts the lines in infile. Words
are separated by whitespace characters.
Document
-infile: java.io.File
+Document()
+Document(infile: java.io.File)
+countCharacters(): int
+countWords(): int
+countLines(): int