Choose the most correct answer to all the following questions.
Review Weeks 1 to 3
Terminology Client Application: An application that uses a class. Interface: An interface utilizes public methods. It is not an entire class, it is a list of methods used by a class. Encapsulation: Hides a program’s implementation details from the client application using private classes method and fields. If an object contains only private methods and object then it has no interface, very rarely would anything be designed like this but it is possible. Byte Offset: A single records length. Must be 0 or greater, and must be of a long data type for precision. Record Structure: The unchanging details that are generic to a set of records so that they are all the same which is integral when utilizing random access. Binary File: Cannot be read in a text editor, requires specific file reading application. Character File: Can be read in a simple text editor. Each char is encoded with a character set. The character set used by java is Unicode. Unicode is a 16 bit character set. The implication here is that one character is two bytes of storage in memory. Storing ‘1’ uses 2 bytes, storing ‘’35876” uses 10 bytes. Super Class: java.lang.Object – java.io.RandomAccessFile
Code - The StringBuffer is a useful container for a field in a record. o For example: o StringBuffer sb = new StringBuffer(“john”); o sb.setLength(15); o (Note: The four chars are in the container, aswell as 11 nulls) o StringBuffer sb = new StringBuffer(“aaaaaaaaaaaaaaaaaaaaa”); o Sb.setLength(15); o (Notes: There are more chars than there is space in the StringBuffer so the first 15 chars are stored and the rest are lost) - A StringBuffer object can be directly written to a file o For example: o File.writeChars(“sb.ToString()); o (Note: uses writeChar method of RandomAccessFileClass to write a StringBuffer (being changed to a String) to a file. - The RandomAccessFile class has an interface which supports dataInput and dataOutput. o It’s constructor is RandomAccessFile(File file, String mode) o (Note: mode “r “, “rw”, “rws”, or “rwd”) o Useful dataInput interface: o RandomAccessFileClassName.writeInt(integer) o RandomAccessFileClassName.writeDouble(double) o RandomAccessFileClassName.writeChars(string) - The try block is used to handle exceptions. A try block is minimally at least a try statement with catch statements, a finally statement extends the try blocks capabilities. o Try{//Statements of concern} o Catch(exceptionClass paramVar){//Statements to deal with exception} o Finally{Statements that will always run, error or none} o (Note: The only way to stop a finally block from running is to add an explicit System.exit(0); because it will tell the system to stop running this code right now.)
Information - Java imposes no restrictions on file structure. File structure is entirely created by the programmer. - Character fields of a record must be fixed length for precise accessibility. o To Make a Field a Fixed Length o Decide how many chars you will need o (Note: Memory requires 2 bytes per char) o Create a container for the field. The best way to do this is with the String Buffer. o StringBuffer sb = new StringBuffer(15); o (Note: This containers length is still 0, it’s capacity is 15) o sb.setLength(15); o (Note: By default nulls have been written into the file, now this container has 15 nulls in it) - An int takes up 4 bytes in memory, a double requires 8 bytes, and a char uses 2 bytes. - The RandomAccessFile class is not like any other file reader or file writer classes. It’s superclass is object. It has a file input stream and a file output stream. This class is unique in that it uses a filePointer to point to a location in a file. Similarly, when we open a file the filePointer automatically seeks to 0, and would read fro 0 until we told it to stop. Aswell, if we wanted to read the third record, we would move the filePointer to the zone that contains the bytes we would like to edit. It is critical to place the filePointer where we want to get our data from or the data extracted will be useless. The space to write data must be initialised or there will be leftover bytes that will ruin our file reading capabilities. - The first line of a constructor must call to the superclass.
Java imposes no limitation on file structure (sequential or random).
True
False
The statement block of a top checking conditional loop executes 1 or more times.
True
False
A double data type value requires 4 bytes of storage space in memory.
True
False
Character files utilize storage space more efficiently then binary files.
True
False
The RandomAccessFile class contains a method that can detect when the end of a file is reached.
True
False
What method of the RandomAccessFile class is used to write an integer value to a file?
What method of the RandomAccessFile class is used to write a double value to a file?
.writeInt();
.writeDouble();
.writeChars();
.writeString();
What method of the RandomAccessFile class is used to write multiple characters to a file?
.writeInt();
.writeDouble();
.writeChars();
.writeString();
The two arguments that must be supplied to the constructor of the RandomAccessFile class are:
String move and String filename("r" or "rw")
String filename and String move("r" or "rw")
String filename and String mode("r" or "rw")
String mode("r", "w", or "rw") and String filename
The two values that may be used for the second argument of the RandomAccessFile constructor are
readable "rw" or re-writable "r"
readable "r" or re-writable "rw"
r, w or x
readable "r" or readable-writable"rw"
What is the length of sb after the following code execute? StringBuffer sb = new StringBuffer(15);
It won't execute.
15
-1
0
Consider the following: your random access file uses fixed length records. The records are numbered sequentially, starting with record 1. Which formula will best allow you to access any record in the file?
(recSize) x recNum
(recNum - 1) x recSize
(recNum + 1) x recSize
(recSize - 1) x RecNum
How many bytes are required to store a single character in memory?
1
2
4
8
Which character set is used by the Java language?
ASCII
Unicode
ANSI
Times New Roman
When an object uses Encapsulation, it's ____________ details are ____________ from the client application.
interface, private
implementation, private
interface, hidden
implementation, hidden
Encapsulation requires the use of __________ fields and/or methods.