Question
Any help on this would be very much appreciated! I have been struggling with it for a while... Question: Write a program that solves the
Any help on this would be very much appreciated! I have been struggling with it for a while...
Question:
Write a program that solves the class "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 themself, Mr. Welshman, but him awoke, the balmy shore. I'll give him that he couple overy because in the slated snufflindeed structure's kind was rath. She said that the would the door a fever eyes that WITH him." Level 1- 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 nobody 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.
Code so far:
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 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 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(); } }
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