Programming Assignment 4
Due: Week of Feb. 6 before lab
-
(25 points) Problem 5.1 of the book.
Count positive and negative numbers and compute the average of numbers.
Write a program that reads an unspecified number of integers, determines how
many positive and negative values have been read, and computes the total and
average of the input values (not counting zeros). Your program ends with the
input
0
. Display the average as a floating-point number.- Name the file and program
ch5pr1.java
. - Example execution (
java ch5pr1
) user provides input:
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25 - Example execution (
java ch5pr1
) user provides input:
Enter an integer, the input ends if it is 0: 0
No numbers are entered except 0
- Name the file and program
-
(25 points) Problem 5.25 of the book.
Compute π.
You can approximate
π
using the following series:
π = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + … + (-1)i+1/(2i - 1))
Write a program that displaysπ
value fori = 10000, 20000, …, 100000
. UseSystem.out.printf
for proper formatting.- Name the file and program
ch5pr25.java
. - Example execution (
java ch5pr25
):
10000 3.141492654
20000 3.141542654
30000 3.141559320
40000 3.141567654
50000 3.141572654
60000 3.141575987
70000 3.141578368
80000 3.141580154
90000 3.141581542
100000 3.141582654
- Name the file and program
-
(25 points) Problem 5.31 of the book.
Financial application: compute CD value. Suppose you put $10,000 into a CD
with an annual percentage yield of 5.75%. After one month, the CD is worth
10000 + 10000 * 5.75 / 1200 = 10047.92
After two months, the CD is worth
10047.91 + 10047.91 * 5.75 / 1200 = 10096.06
After three months, the CD is worth
10096.06 + 10096.06 * 5.75 / 1200 = 10144.44
and so on.
Write a program that prompts the user to enter an amount (e.g.,10000
), the annual percentage yield (e.g.,5.75
), and the number of months (e.g.,18
) and displays a table as shown below. UseSystem.out.printf
to format the money amounts properly.- Name the file and program
ch5pr31.java
. - Example execution (
java ch5pr31
) user provides input:
Enter the initial deposit amount: 10000
Enter annual percentage yield: 5.75
Enter maturity period (number of months): 18
Month CD Value
1 10047.92
2 10096.06
3 10144.44
4 10193.05
5 10241.89
6 10290.97
7 10340.28
8 10389.82
9 10439.61
10 10489.63
11 10539.89
12 10590.40
13 10641.14
14 10692.13
15 10743.37
16 10794.84
17 10846.57
18 10898.54
- Name the file and program
-
(25 points) Problem 5.43 of the book.
Math: combinations. Write a program that displays all possible combinations
for picking two numbers from integers
1
to7
. Also display the total number of all combinations.- Name the file and program
ch5pr43.java
. - Example execution (
java ch5pr43
):
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
The total number of all combinations is 21
- Name the file and program
-
Bonus.
(10 points) Problem 5.17 of the book.
Display pyramid. Write a program that prompts the user to enter an integer
from
1
to15
and displays a pyramid like below.- Name the file and program
ch5pr17.java
. - Example execution (
java ch5pr17
) user provides input:
Enter the number of lines: 7
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
- 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).