Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

  1. Apply the singleton design pattern to the Record class.

  2. Implement the body of the read() method as per the given specification.

  3. Implement the body of the write(String msg) method as per the given specification.

  4. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

Find y'. y= |x + X (x) (x) X 1 02x+ 2x 1 O 2x + 1/3 Ex 2x +

Answered: 1 week ago