Markov Chain Algorithm in Java
Markov Chain Algorithm in Java
Prefix Class
class Prefix {
public Vector pref; // NPREF adjacent words from input
static final int MULTIPLIER = 31; // for hashCode()
}
Chain Class
class Chain {
static final int NPREF = 2; // size of prefix
static final String NONWORD = "\n";
// "word" that can't appear
Hashtable statetab = new Hashtable();
// key = Prefix, value = suffix Vector
Prefix prefix = new Prefix(NPREF, NONWORD);
// initial prefix
Random rand = new Random();
Markov Class
class Markov {
static final int MAXGEN = 10000; // maximum words generated
public static void main(String[] args) throws IOException
{
Chain chain = new Chain();
int nwords = MAXGEN;
chain.build(System.in);
chain.generate(nwords);
}
}
2. Reading/Writing in Java
Reading and Writing
Examples
(i) The following code illustrates the use of classes Input Stream Reader, Buffered Reader
and the method readLine()
public static void main(String[] args)throws IOException
{
…
//We construct an input stream is
//It allows us to read input text line by line
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader is = new BufferedReader(converter);
(ii) This is a modification of Markov chain algorithm. The program takes input from the
file alice30.txt and it sends the output to the file output.txt.
class MarkovModifiedInOut{
static final int MAXGEN = 10000; // maximum words generated
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new
FileReader("alice30.txt"));
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
chain.build(in);
in.close();
chain.generate(nwords, out);
out.close();
}
}
Debugging – an overview
To reduce debugging time stick to the following principles:
(i) Good design
(ii) Good style
(iii) Boundary conditions tests
(iv) Assertions
(v) Sanity checks
(vi) Defensive programming
(vii) Well-designed interfaces
(viii) Limited global data
(ix) Checking tools
Loop Syntax:
while condition B do
P
end while
With the help of the following two examples we will discuss how one may use loop
invariants to design tests of loops.
Example 1:
Product(int x, int y)
//pre-condition: x,y ≥ 0
//post-condition: j=x*y
int i,j
i:=0
j:=0
while i ≠ x do
j:=j+y
i:=i+1
end while
return j
end function Product
Loop invariant:
Q: j=i*y
Loop invariant:
Q: gcd(i,j)=gcd(a,b)
Problem 2: Prepare C++ code for the following test of Euclid’s algorithm:
• Design an integer-valued function with two integer arguments implementing
the loop of Example 2.
• Design a Boolean valued function Q, which checks whether the invariant
holds. In the implementation of Q perform direct calculation of the greatest
common divisor. Find the largest positive integer (no larger than the smaller
of the arguments), which divides both input arguments.
• Prepare C++ code for testing your implementation of the loop. Your code
should verify whether the invariant holds after the initialization of variables
and after every execution of the update. It should prompt the user for two
input integers and report the result of the test.