Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone help me with this java problem :( Write a program that solves the cl

ID: 3677598 • Letter: C

Question

can someone help me with this java problem :(

Write a program that solves the classic "random writer" problem. This problem deals with reading input files of text and examining the frequencies of characters. On the basis of those frequencies, you can generate randomized output that appears to match the writing style of the original document The longer the chains you link together, the more accurate the random text will sound, for example, level 4 random text (text with chains of 4 letters long) generated from Tom Sawyer might look like this: "en themselves, Mr. Welshman, but him awoke, the balmy shore. I'll give him that he couple overy because in the slated snuffindeed structure's kind was rath. She said that the wound the door a fever eyes that WITH him." Level 10 random text from the same source might look like this: "you understanding that they don't come around in the cave should get the word beauteous was over-fondled, and that together and decided that he might as we used to do-it's nobby fun. I'll learn you." Search the Internet for "Random Writer" to learn more about this problem, such as the specification posed by computer scientist Joseph Zachary.

Explanation / Answer

I have solved the classic problem in java. I have simulated the below code with 2 readers and writers in java. Working java code:

import java.util.Random;

class Database {   
private Random generator = new Random();
private int data = 0;
int nr = 0;
private synchronized void startRead() {
nr++;
}
private static synchronized void endRead() {
nr--;
if (nr==0) notify();
}
public static void read() {
startRead();
System.out.println("read: " + data);
endRead();
}
public static synchronized void write() {
int temp;
while (nr>0)
try { wait(); }
catch (InterruptedException ex) {return;}

temp = data;
data = 99999;
try {
Thread.sleep(generator.nextInt(500));   
} catch (java.lang.InterruptedException e) {}
data = temp+1;   
System.out.println("wrote: " + data);
notify();
}
}
  
class Random_Reader extends Thread {
int rounds;
Database RW;
private Random generator = new Random();

public Random_Reader(int rounds, Database RW) {
this.rounds = rounds;
this.RW = RW;
}
public static void run() {
for (int i = 0; i<rounds; i++) {
   try {
    Thread.sleep(generator.nextInt(500));
} catch (java.lang.InterruptedException e) {}
RW.read();
}
}
}

class Random_Writer extends Thread {
int rounds;
Database RW;
private Random generator = new Random();

public static Random_Writer(int rounds, Database RW) {
this.rounds = rounds;
this.RW = RW;
}
public static void run() {
for (int i = 0; i<rounds; i++) {
   try {
    Thread.sleep(generator.nextInt(500));
} catch (java.lang.InterruptedException e) {}
RW.write();
}
}
}

class ReadWrite { // main program where 2 readers and writers are created
static Database RW = new Database();
public static void main(String[] arg) {
int rounds = Integer.parseInt(arg[0],10);
new Random_Reader(rounds, RW).start();
new Random_Reader(rounds, RW).start();
new Random_Writer(rounds, RW).start();
new Random_Writer(rounds, RW).start();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote