Question
Record class is a java class for which we want to ensure that only one instance of the class exists. If we apply the singleton
Record class is a java class for which we want to ensure that only one instance of the class exists. If we apply the singleton design pattern to this class, we will be able to ensure that. Therefore, modify the Record class as follows:
-
Apply the singleton design pattern to the Record class.
-
Implement the body of the read() method as per the given specification.
-
Implement the body of the write(String msg) method as per the given specification.
-
Complete the main method as per the comments in it.
When the main method is run for the first time, a text file called record.txt should get created contain- ing two lines as follows:
Hello-1
Hello-2
The console output should show as follows:
Currently the file record.txt contains the following lines:
Hello-1 Hello-2
When the main method is run again, the file record.txt should have two more lines appended. There- fore, the file then should contain the following lines as follows:
Hello-1
Hello-2
Hello-1
Hello-2
The console output then should show as follows:
Currently the file record.txt contains the following lines:
Hello-1 Hello-2 Hello-1
Hello-2
Thus, every run should append the two lines to the record.txt file. And, consequently the console out- put changes.
Partial implementation of the Record class is given below:
public class Record { // Name of the associated file private String filename;
public Record(String n) {
filename = n; }
// Effects: Reads and prints the contents of the associated // file to the standard output. public void read() {
try {// Write the code here
} catch (IOException e) { System.out.println("An error occurred.");
e.printStackTrace(); }
}
// Effects: Appends the specified message, msg, to the
// associated file. public void write(String msg) {
try {// Write the code here
} catch (IOException e) { System.out.println("An error occurred.");
e.printStackTrace(); }
}
public static void main(String[] args) {
// Fill the blank below that obtains the sole instance
// of the Record class. // (You should not invoke the Record constructor here.)
______________________________________ ;
// Do not modify the code below
r.write("Hello-1 ");
r.write("Hello-2 ");
System.out.println("Currently the file record.txt " + "contains the following lines:");
r.read(); }
}
Hint: Following are the webpages containing documentation of some java classes that can be used for writing to or reading from text files: https://docs.oracle.com/javase/8/docs/api/java/io/FileWriter.html
https://docs.oracle.com/javase/8/docs/api/java/io/FileReader.html
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started