0% found this document useful (0 votes)
25 views5 pages

Lab 20

The document outlines Exercise #20 for CSE-271: Object-Oriented Programming, focusing on file handling concepts including text vs. binary files, and relative vs. absolute paths. It includes objectives, setup instructions for Eclipse, and exercises that require students to demonstrate their understanding of file paths and binary file operations. Students are required to submit a PDF of the document and their modified Java source file via the Canvas CODE plug-in by the end of their lab session.

Uploaded by

zhenanli0513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Lab 20

The document outlines Exercise #20 for CSE-271: Object-Oriented Programming, focusing on file handling concepts including text vs. binary files, and relative vs. absolute paths. It includes objectives, setup instructions for Eclipse, and exercises that require students to demonstrate their understanding of file paths and binary file operations. Students are required to submit a PDF of the document and their modified Java source file via the Canvas CODE plug-in by the end of their lab session.

Uploaded by

zhenanli0513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DUE DATE: By the end of your lab session

CSE-271: Object-Oriented Programming


Exercise #20
Max Points: 20

Name: Zhenan Li

For your own convenient reference – You should first save/rename this
document using the naming convention MUid_Exercise20.docx (example:
raodm_Exercise20.docx) prior to proceeding with this exercise.

Objectives: The objectives of this exercise are to:


1. Review concepts of text vs. binary files
2. Review skills associated with relative and absolute path
3. Experiment with working with binary files

Fill in answers to all of the questions. For some of the questions you can simply copy-paste
appropriate text from Eclipse output into this document. You may discuss the questions or
seek help from your neighbor, TA, and/or your instructor.

Part #0: One time setup of Eclipse (IDE) – Only if needed

We already configured Eclipse’s source formatter and Checkstyle plug-in as


part of Lab #1. If your Eclipse is not configured (because you are using a
different computer) then use the instructions from Lab #1 to configure Eclipse.

Part #1: Relative and absolute paths Without a good


Estimate time: < 15 minutes
“ understanding of paths,
you will eventually be lost.
Background: Files are stored in a hierarchical manner - Yours truly
using directories and sub-directories. The sequence of directories that need to be traversed in
order to access a file is called the path. Path (to a file or directory) can be classified into the
following 2 categories:
 Absolute path: Paths that start with a / (forward slash or just slash, i.e., the division sign) or
C:\ (Windows) are absolute paths. Example: /home/raodm, ~/, or C:\Users\raodm.
 Relative path: Paths that do not start with a / are relative paths. Relative paths indicate
directory and file structures with respect to pwd (present working directory). Examples:
../cse271 or ../ or ../../courses/cse174/exercises or just test.txt,
etc.

Exercise: Briefly (2-to-3 sentences each) respond to the following questions regarding generic
concepts of file paths.
1. When we develop programs using files, we typically prefer to use relative paths. Why?
Because relative paths are preferred, using absolute paths may be incompatible because

Page 1 of 5
DUE DATE: By the end of your lab session

the user will place the java file in a different location on the computer.

2. Complete the following table to illustrate some of the general relative paths using a
suitable sequence of ./ or ../:
Description Relative path
Present working directory (pwd) ./
The parent directory ../
The grandparent directory ../../
The great-grandparent directory ../../../
A subdirectory called sub under pwd ./sub

3. Now, let’s practice a few path-related questions (similar to


exam questions in this course) using the directory hierarchy cse278
shown in the adjacent figure. Note that the absolute paths
are not known (so you will need to use only relative paths). ex1
hw1
a. Assume the present working directory in hw1. ex2
Complete the following Java statement to open a file cse381
named hello.txt that exists in ex2 directory.

FileInputStream fis = new ../ex2/hello.txt “);


FileInputStream(“

b. Assume the present working directory in hw1. Assume you have to call the
following copy method:
class FileUtils {
/**
* Copies contents of a given source file to given destination file.
* @param srcFile The source file to be copied.
* @param destFile The duplicate file to be created.
*/
public static void copy(String srcPath, String destPath);
}

Illustrate a call (i.e., 1-line of code) to the above copy method to copy a file
named main.dat from the cse278 directory to a file named
main_copy.dat in the cse381 directory.

FileUtils.copy(“../cse278/main.dat”, “../../cse31/main_copy.dat”);

Part #2: Text and binary file concepts


Estimated time: < 20 minutes

Background: Files are used to store different types of data on a storage medium. The contents of
a file are classified into two main categories, namely text files and binary files. Binary files store

Page 2 of 5
DUE DATE: By the end of your lab session

bytes (a byte is 8-bits) of data while text files operate with characters. In Java a byte can have the
value -128–+127. In Java, a character occupies 2-to-4 bytes, depending on the encoding of
characters. Text files, typically contain bytes with the value in the range 32–127, also called as 7-
bit ASCII characters.

Exercise: In this part of the exercise, we will be reviewing some of the basic concepts of text
and binary files.

1. What is a byte? What is the difference between a byte and a character?


A byte is the basic data type of a computer. A byte occupies eight bits, and a character
consists of one or more bytes.

2. State two advantages of binary files (when compared to text files)


1. quicker to read (compared to plain text files) better for larger datasets.
2. Takes up less disk space

3. State two disadvantages of binary files


1. not readable by humans
2. They may not be portable across different operating systems

4. When would a binary file also be an ASCII-text file?


When the bytes in the binary file are in the range of +32 and +137

5. Consider the adjacent Dog class class Dog {


a. Copy-paste the Dog class into the space below. Next, String name = "Fido";
String breed = "Labradoodle";
suitably modify the Java class such that Dog objects }
can be written and read using
ObjectOutputStream and ObjectInputStream.

class Dog implements Serializable {


private static final long serialVersionUID = 123L;
private String name = "Fido";
private String breed = "Labradoodle";
}

b. Write a method public void outputDog(Dog pet, String zooName)


that writes pet into file with name specified by zooName using a suitable object
output stream. The method must catch all exceptions (hint: try-catch Exception).
void outputDog(Dog pet, String zooName) {
try {
ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream(zooName));
oos.writeObject(pet);
} catch (Exception e) {
e.printStackTrace();
}
}

Page 3 of 5
DUE DATE: By the end of your lab session

c. Write a method public Dog inputDog(String zooName) that reads a Dog


object from file with name specified by zooName using a suitable object input
stream. The method must catch all exceptions.

Dog inputDog(String zooName) {


Dog pet = null;
// complete method here.
try {
ObjectInputStream ois = new ObjectInputStream(new
FileInputStream(zooName));
pet = (Dog) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return pet;
}

Part #3: Text & object binary files


Estimated time: < 30 minutes

Background: Recollect that, binary files are not human-readable and require a special program
to create and manipulate binary files. Hence, binary files are typically created from text data that
is either entered by the user or read from a file. This would be the case immaterial of whether a
GUI is used. A similar approach is used for binary files associated with databases or even
spreadsheets.

Exercise: Complete this part of the exercise in the following manner:


1. Create a new Java project in Eclipse
2. Download the supplied starter code employee.txt, Employee.java and
EmployeeTester.java to your new Eclipse project.
3. Do not modify EmployeeTester.java
4. Follow the Javadoc comments and instructions in Employee.java to suitably
implement the methods.
5. Once you have correctly implemented the methods, run the main method in
EmployeeTester for testing.
Expected output:
Data loaded from text file:
1, John Smith , 35535
5, Mary Jones , 75355
3, Chen Wang , 63535
7, Manish Patel , 47535
2, Li Chyou , 55000
6, Akari Nokomoto , 42750
4, Ren Koyo , 59750
8, Hakim Mandela , 67500

Writing data to binary file...


Done.

Data loaded from binary file:


1, John Smith , 35535
5, Mary Jones , 75355

Page 4 of 5
DUE DATE: By the end of your lab session

7, Manish Patel , 47535


2, Li Chyou , 55000
4, Ren Koyo , 59750
8, Hakim Mandela , 67500

Part #4: Submit to Canvas via CODE plug-in


Estimated time: < 5 minutes

Exercise: You will be submitting the following files via the Canvas CODE plug-in:
1. This MS-Word document saved as a PDF file – Only submit PDF file.
2. The Java source file Employee.java that you modified in this exercise.

Ensure you actually complete the submission on Canvas by verifying your submission (after you
submit)

Page 5 of 5

You might also like