Programming Assignment 8
Due: Week of Mar. 20 before lab
-
(25 points) Problem 7.3 of the book.
Count occurrence of numbers. Write a program that reads the integers between
1 and 100 and counts the occurrences of each. Assume the input ends with
0
. Note that if a number occurs more than one time, the plural word "times" is used in the output. You must use an array in this problem, no other container can be used, e.g.,ArrayList
.- Name the file and program
ch7pr3.java
. - Example execution (
java ch7pr3
) user provides input:
Enter the integers between 1 and 100: 2 5 6 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 1 time
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
- Name the file and program
-
(25 points) Problem 7.25 of the book.
Algebra: solve quadratic equation. Write a method for solving a quadratic
equation using the following header:
public static int solveQuadratic(double[] eqn, double[] roots);
The coefficients of a quadratic equationax2 + bx + c = 0
are passed to the arrayeqn
and the real roots are stored in roots. The method returns the number of real roots. Write a program that prompts the user to enter values fora
,b
, andc
and displays the number of real roots and all real roots.- Name the file and program
ch7pr25.java
. - Example execution (
java ch7pr25
) user provides input:
Enter values for a, b, and c of ax^2 + bx + c = 0: 5.4 6.2 1.5
5.400000x^2 + 6.200000x + 1.500000 = 0 has 2 real roots.
Real root 0: x = -0.346515
Real root 1: x = -0.801633 - Example execution (
java ch7pr25
) user provides input:
Enter values for a, b, and c of ax^2 + bx + c = 0: 5 2 1
5.000000x^2 + 2.000000x + 1.000000 = 0 has 0 real roots. - Example execution (
java ch7pr25
) user provides input:
Enter values for a, b, and c of ax^2 + bx + c = 0: -3 -24 -48
-3.000000x^2 + -24.000000x + -48.000000 = 0 has 1 real roots.
Real root 0: x = -4.000000
- Name the file and program
-
(25 points) Problem 7.27 of the book.
Identical arrays. The arrays
list1
andlist2
are identical if they have the same contents. Write a method that returnstrue
iflist1
andlist2
are identical, using the following header:
public static boolean equals(int[] list1, int[] list2);
Write a test program that prompts the user to enter two lists of integers and displays whether the two are identical. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list. You are allowed to useArray.sort
, but at least consider trying to think of how to solve it without that function.- Name the file and program
ch7pr27.java
. - Example execution (
java ch7pr27
) user provides input:
Enter list1: 5 2 5 6 6 1
Enter list2: 5 5 2 6 1 6
Two lists are identical - Example execution (
java ch7pr27
) user provides input:
Enter list1: 5 5 5 6 6 1
Enter list2: 5 2 5 6 1 6
Two lists are not identical
- Name the file and program
-
(25 points) Problem 7.31 of the book.
Merge two sorted lists. Write the following method that merges two sorted
lists into a new sorted list.
public static int[] merge(int[] list1, int[] list2);
Implement the method in a way that takes at mostlist1.length + list2.length
comparisons. Write a test program that prompts the user to enter two sorted lists and displays the merged list. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list. You cannot useArrays.sort()
.- Name the file and program
ch7pr31.java
. - Example execution (
java ch7pr31
) user provides input:
Enter list1: 5 1 5 16 61 111
Enter list2: 4 2 4 5 6
The merged list is 1 2 4 5 5 6 16 61 111
- Name the file and program
-
Bonus.
(20 points) Problem 7.35 of the book.
Game: hangman. Write a hangman game that randomly generates a word and
prompts the user to guess one letter at a time, as shown in the sample run.
Each letter in the word is displayed as an asterisk. When the user makes a
correct guess, the actual letter is then displayed. When the user finishes a
word, display the number of misses and ask the user whether to continue to
play with another word. Declare an array to store words as follows:
// Add any words you wish in this array
String[] words = {"write", "that", "program"};- Name the file and program
ch7pr35.java
. - Example execution (
java ch7pr35
) user provides input:
(Guess) Enter a letter in word ******* > p
(Guess) Enter a letter in word p****** > r
(Guess) Enter a letter in word pr**r** > p
p is already in the word
(Guess) Enter a letter in word pr**r** > o
(Guess) Enter a letter in word pro*r** > g
(Guess) Enter a letter in word progr** > n
n is not in the word
(Guess) Enter a letter in word progr** > m
(Guess) Enter a letter in word progr*m > a
The word is program. You missed 1 time
Do you want to guess another word? Enter y or n> n
- Name the file and program
General Instructions, Turning in assignments, and Grading
General Instructions
- Name each file and program as listed in the instructions.
- The top of each program should have a comment block where the first line
is your name and following lines are a description of your program.
//Name: Jory Denny
//This program debates Batman vs Superman! - Use proper coding style (described more in Grading below)
- Follow turn-in instructions precisely.
- Failure to complete any of these steps will result in a significant loss of points.
Turn in Instructions
Each assignment will be turned in to both Blackboard (soft copy) and in class (hard copy). Assignments are due BEFORE, let me repeat, before class starts. This does not mean five minutes after class starts.
- Soft copy (Online submission)
- Create a compressed
.zip
file of all Java programs needed to compile your program and all input files (if needed) to run your program.- If you do not know how to create a compressed
.zip
file, there is this cool new website you can use to search for instructions by entering"How to create .zip Windows 10"
or"How to create .zip MAC OSX"
for example.
- If you do not know how to create a compressed
- Submit
.zip
file on Blackboard by the stated due date and time.
- Create a compressed
- Hard copy (In-class submission)
- The first page of your hard copy must be a signed coverpage.
- Next put the programs in order as described in the description.
- If you do not know how to print a java file, there is this cool
new website you can use to
search for instructions by entering
"How to open and print .java file Windows 10"
or"How to open and print .java file MAC OSX"
for example. In combination with this, you may have to consult University of Richmond webpages to learn how to use campus printers. I recommend printing directly from sublime text editor on University computers (has printing feature enabled) and will print with syntax-highlighting (colors).
- If you do not know how to print a java file, there is this cool
new website you can use to
search for instructions by entering
- Staple all pages together.
- Turn in packet before class begins.
- I reserve the right to assign a 0 to any assignment failing to comply with these instructions. Even for something as small as a missing staple.
Points
- Each assignment is graded out of 100 points (not including bonus).
- Criteria and point distribution
- If the code is not named precisely or does not compile, -75% on a problem.
- If the code does not generate the correct output, -50% on a problem.
- Following instructions and algorithm used to solve, 25% on a problem. Following instructions is extremely important in computer science, train yourself to think like a computer. There are many ways to solve a problem, some may be better or worse than others.
- Stylistic elements of written code,
25% of a problem. Style includes (but is not
limited to):
- Descriptive comments on intent and purpose of code
- Descriptive and consistant naming conventions
- Indenting properly (after an opening brace, tab right by 1 indent; after a closing brace, tab left by 1 indent)
- Consistent spacing
- Consistent bracket placement (same line or on new line)
- Avoiding code duplication
- If there are any discrepencies in grades please see the instructor during his office hours or by appointment (do not discuss with the lab assistants or graders).