Assignment 7
Working with Java
Submit before 11:30 PM Monday November 15
Overview
You will write several Java programs, which include some simple
marble jar experiments using the Java MarbleJar class and demo code in the mjDemo
zip folder.
Requirements
- Write a short Java program that uses the Scanner library to read
in 2 strings and a number. Your program should then print a short
story/song that includes the 2 strings and repeats some element in
the story/song the given number of times.
- Download and Java MarbleJar code and run it. Experiment with
different method calls.
- Write a class (static) function in Main called add_with_same that takes an
integer as a parameter called count and returns a marble jar object
with the number of blue marbles and the number of red marbles equal
to count (total of count*2 marbles). Here's the decaration header:
private static MarbleJar add_with_same(int count) {
This function will need to
create a new marble jar object and use a for-loop to repeatedly add
red and blue marbles with the number of times indicated by count.
Write code that tests your function.
Note that add_with_same does not have any I/O (no user input or
print). To run it, call it from the main method, passing it a
value and then assigning it to a variable declared as a
MarbleJar. Here's an example:
MarbleJar some_jar = add_with_same(10);
// run some code that draws the marbles to show it works
- Write code that queries the user for the number of blue and red
marbles. It should then call your add_with_same function to obtain
a marble jar with that number of blue and red marbles. It should
then draw two marbles and report if they are the same.
- Using a for-loop, write code that repeats the previous step
10,000 times and report the percentage of times that the two drawn
colors are the same. You will want to comment out any print
statements in the for loop!
- Conduct experiments where the number of each color is 5, 10 and
100. Are they percentages similar? Why or why not?
Deliverables
Submit two attachments:
- A txt file (e.g. README.txt) that includes the following:
- A summary of your
accomplishments (include collaboration, process
notes and findings from your experiment)
- Appropriate demonstration runs
- Source code.
The assignment is worth 10 points. In addition to meeting the
requirements, full credit requires a neatly formatted txt file with
well written content.