3 problems, worth 10, 20, and 20 points, respectively (plus ex. cr. of up to 20%)
In this problem, you will create a simple ASCII animation that represents a tired student's trek from the dining hall to either the east or west side of campus. You may want to refer to the example and notes we went over in class to help with this.
Output: Your program should represent the student by an ASCII character (a visible, printable one!) of your choice. It should also somehow indicate the endpoints of the student's path. The example below uses vertical bars '|' to do this. You should ask the user for the width of the path (the distance from the center to the path's edge).
When run, the program should continually output lines of text displaying the position of the student on the path. Each line should show a random "step" of the student: unmoving or left or right, until she is adjacent to one or the other ends of the path, at which point the program stops. At the end, the program should print out the total number of steps taken during its run.
Example Run: Here is an example run. The student in this case randomly stays still, moves left or moves right. The S character represents the student, while the vertical bar | characters represent the end of the path:
How wide would you like your path? 24
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
< many lines skipped... >
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
| S |
|S |
The student took 996 steps.
Method to write For this problem, you should write a method
public static void printLine(int w, int p)
that prints a single line of the student's wander. The
input variable p represents the position of the
student. The variable w represents the
distance from the center of the path to either edge.
The end of the path should be one space past w,
and w (or -w) is where the wandering student
should stop.
Hint on drawing One way to draw a single line of output is with a for loop, in which i runs from -w to w. Outside this for loop would be the characters that mark either end of the path. Inside the for loop you might want to print one character per loop -- an if statement is a good method for deciding which character to print.
Extra Credit For extra credit (of up to 10%), improve this simple ASCII animation according to your own tastes. For example, you may want to have more than one person wandering; you may have the wanderers "bounce off" each other or annihilate each other -- creativity is welcome!
In this problem you will write a program to compute pi (3.14159...). This problem will revisit the idea of Monte Carlo methods used in the Monte Hall program of HW4, this time using a geometric metaphor.
main method:
Your main method should offer the user a choice of two options, for
example, as follows:
There are two more options available for extra credit -- see the
bottom of this assignment for details on them.
Welcome to the pi estimator. You may
(0) Decide how many darts to throw
(1) Throw darts to estimate pi to a certain accuracy
(9) Quit
Which would you like to try?
public static boolean throwDart()
This method takes no inputs. It "throws a dart" at
a 2x2 square by computing a random point (x,y)
with -1 <= x < 1
and -1 <= y < 1. This dartHit
method should return
Enter an integer number of darts you would like to throw 100000
Total throws: 100000
Total hits: 78371
Estimate of PI: 3.13484
< the original menu prints out again >
Thus, for option 0 your program should prompt
for an integer number of darts to use. (You may assume this will be
a positive integer.) Then, you should "throw" that many darts at
a square dartboard spanning from -1 to 1 in both x and y.
In essence, this means generating
a random point (x,y) with -1 <= x < 1
and -1 <= y < 1. You should use the H.randDouble(low,high)
function to generate both x and y.
For each dart "thrown," determine whether it landed within the circle of radius 1 centered at the point (0, 0). Your program should count the number of times it hits this circle.
With a dart thrown randomly into the enclosing square,
the chances of hitting the circle is pi/4.
As a result, you can
estimate pi by
After throwing the desired number of darts, the program should
print the total number thrown, the total number of "hits,"
and the resulting estimate of pi.
4.0 * ( number of hits ) / ( number of darts )
main), the following is (approximately)
what your program should do (feel free to personalize, if you wish):
Enter a positive tolerance (< 1.0) 0.0000001
Total throws: 34119
Total hits: 26797
The resulting estimate of pi is 3.1415926609806855
The "actual" value of pi is 3.141592653589793
< the main menu would appear again here >
That is, for option 1 your program should prompt for
a tolerance within which you want to estimate pi.
This will be a double, and you can assume it will be
strictly positive and less than 1.
Your program should then "throw darts" in the same way
it does for choice 0, determining whether or not each dart hits
the circle. Each time, however,
the program should compute a new
estimate of pi
using the formula mentioned above. It should continue
until the estimated value of pi
is within the user-specified tolerance
of the actual value of pi.
You should use
Java's built-in value Math.PI as the "actual" value of pi.
After the program has estimated pi within the desired tolerance, it
should print out this estimate, the "true" value of pi (Math.PI),
as well as the total number of
darts needed and the total number of darts that hit the inner circle.
This part is really a
test of how much work Monte Carlo estimation requires to
achieve a particular level of accuracy.
Be sure to submit your CS5App.java file under homework 6,
problem 2. Extra credit is available for adding menu options
to estimate ln(x). See the end of the assignment for details.
The problem The Flesch index is a measure of the readability of a particular text. It is often used by textbook publishers and editors to ensure that material is written at a level appropriate for the intended audience. If you're interested in several different numeric measures of readability, there is quite a complete explanation here.
The Flesch index is computed as follows:
nwords).
nsyls).
nsents).
206.835 - 84.6 * (numSyls / numWords) - 1.015 * (numWords / numSents)
which should be rounded down (cast) to an integer. Note, too, that
the divisions in the above formula assume that the variables are doubles.
If they are ints in your code, you will have to cast them to
doubles before dividing!
Here are some resulting readability scores for a few example publications or styles of writing (cited from Cay Horstmann's book, p. 266). It is, in fact, possible to have a negative readability score:
This problem asks you to compute the Flesch readability score for
text that is read in from a file.
In order to do this, you should start from the skeleton
code provided:
public static void main(String[] args)
{
H.inputFromFile("../testfile.txt"); // this is the file in Hw6Pr3 being read in
// you may copy ANY text to this file...
while (true) // loop indefinitely
{
String w1 = H.nw(); // get one "word" from the file
H.pl("w1 is " + w1); // you will want to remove this eventually
if (w1.equals("END")) // if the word END is encountered, we quit the loop
break;
String w2 = dePunct(w1); // this line removes punctuation from w1
H.pl("w2 is " + w2); // you will want to remove this eventually
}
}
The above code demonstrates how to get word-by-word input from a file (and you can put anything you like in that file) and it also demonstrates a method that is provided for removing punctuation (dePunct). You should try this program out with different text in the file testfile.txt to see how it works. Notice that the loop that reads in words runs one time per word (not one time per line of text). A word is separated from other words by whitespace.
Definitions and assumptions
public static int countSyllables(String w)
This method should run through the characters of the input argument word.
Reiterating the rules above,
each sequence of one or more vowels in the word should count as a syllable, with one
exception: if the lone vowel e or E is at the end of the word, it should
not count as a syllable. If any other vowel or more than 1 vowel stand at the end
of the word, it should count as a syllable. Y and y
will always count as vowels for this problem.
Your method should return the number of syllables in the input word
with one exception: it is possible by the above counting system that a word will end
up with zero syllables, and your code should make sure that the
countSyllables method returns at least one syllable for every word.
public static boolean isVowel(char c)
that returned true if c was one of the possible vowels ('a', 'e',
'i', 'o',
'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y'). Yes, we will count y as a vowel
for this problem. This method returns false if c is anything else
-- in that case, you can assume c is a consonant.
Thus, in order to check if a char c is a consonant, use
if ( isVowel(c) == false )
or
if ( !isVowel(c) )
Here are two sample runs. You should text your program against these inputs
to be sure that everything is working correctly.
Remember -- this text is already in the file testfile.txt which
is in the Hw6Pr3 folder. You can open this from JCreator. In
fact you can paste any text, e.g., your last Hum paper into this
file to try it out... be sure you end with END!
Here's the example:
Please type in a number of words or sentences, and I will
assess their readability...
Fourscore and seven years ago our fathers brought forth on this continent a new nation
conceived in Liberty and dedicated to the proposition that all men are created equal. END
Total words: 29
Total sentences: 1
Total syllables: 48
Flesch readability score: 37
--------------------------------------------------
Please type in a number of words or sentences, and I will
assess their readability...
The cow is of the bovine ilk. One end is moo the other milk! END
Total words: 14
Total sentences: 2
Total syllables: 16
Flesch readability score: 103
Extra Credit and Submission See below for a extra credit possibilities -- basically by finding the readability score of one (or more) of your Hum/Soc papers and suggesting (but not coding) possible improvements to the above word- syllable- and sentence-counting strategies. (The reason we ask you not to code your suggested improvements is that it may change how your program runs on our test cases!)
Be sure to submit your CS5App.java file under
homework 6, problem 3.
Part 2
Part two of the extra credit offers an opportunity to
extend your pi-estimating problem. You may add options #2
and #3 to your menu, as follows:
Each of these options should work just like the corresponding
option for estimating pi, except that the user should be
asked for a value of x for which she would like
to estimate ln(x), the natural logarithm of x.
You may assume that we will only test x≥1.
(2) Estimate ln(x) using a chosen number of darts
(3) Estimate ln(x) to a chosen precision
The same dart-throwing approach should be used, though this one is up to you to design. Keep in mind that ln(x) is by definition the integral from 1 to x of the function f(x) = 1.0/x.
Part 3 Part three of the extra credit is to run the Flesch readability program on one (or more) of your Hum/Soc papers and report the readability score in a comment at the top of the file. You should also point out somewhere in your paper that the syllable, sentence, or word-counting used by the program was wrong for your paper and, for full credit, suggest how to fix the code so that it would count correctly. Do not change your code from the way it is described in the problem, however, because then it may report different results on our test cases!