Programming Assignment 2
Due: Week of Jan. 23 before lab
-
(25 points) Problem 2.5 of the book.
Calculate Tips.
Write a program that reads the subtotal and the gratuity rate, then computes
the gratuity and total. For example, if the user enters
10
for a subtotal and15%
for gratuity rate, the program displays$1.5
as gratuity and$11.5
as total.- Name the file and program
ch2pr5.java
. - Example execution (
java ch2pr5
) user provides input:
Enter the subtotal and a gratuity rate: 10 15
The gratuity is $1.5 and total is $11.5
- Name the file and program
-
(25 points) Problem 2.7 of the book.
Find the number of years.
Write a program that prompts the user to enter the minutes (e.g., 1 billion)
and displays the number of years and days for the minutes. For simplicity,
assume a year has 365 days.
- Name the file and program
ch2pr7.java
. - Example execution (
java ch2pr7
) user provides input:
Enter the number of minutes: 1000000000
1000000000 minutes is approximately 1902 years and 214 days>
- Name the file and program
-
(25 points) Problem 2.15 of the book.
Distance.
Write a program that prompts the user to enter two points
(x1, y1)
and(x2, y2)
and displays their distance between them. The formula for computing the distance is( (x2 - x1)2 + (y2 - y1)2)0.5
.- Name the file and program
ch2pr15.java
. - Example execution (
java ch2pr15
) user provides input:
Enter x1 and y1: 1.5 -3.4
Enter x2 and y2: 4 5
The distance between the two points is 8.764131445842194
- Name the file and program
-
(25 points) Problem 2.17 of the book.
Wind-chill temperature.
How cold is it outside? The temperature alone is not enough to provide the
answer. Other factors including wind speed, relative humidity, and sunshine
play important roles in determining coldness outside. In 2001, the
National Weather Service (NWS) implemented the new wind-chill temperature to
measure the coldness using temperature and wind speed. The formula is
twc = 35.74 + 0.6215ta - 35.75v0.16 + 0.4275tav0.16
whereta
is the outside temperature in degrees Fahrenheit andv
is the speed measured in miles per hour.twc
is the wind-chill temperature. The formula cannot be used for wind speeds below 2 mph or temperatures below -58°F or above 41°F.
Write a program that prompts the user to enter a temperature between -58°F and 41°F and a wind speed greater than 2 and displays the wind-chill temperature.- Name the file and program
ch2pr17.java
. - Example execution (
java ch2pr17
) user provides input:
Enter the temperature in Fahrenheit between -58°F and 41°F: 5.3
Enter the wind speed (>=2) in miles per hour: 6
The wind chill index is -5.567068455881625
- Name the file and program
-
Bonus.
(10 points) Problem 4.5 of the book.
Polygon area.
A regular polygon is an n-sided polygon in which all sides are of the same
length and all angles have the same degree (i.e., the polygon is both
equilateral and equiangular). The formula for computing the area of a
regular polygon is
Area = (n*s2)/(4*tan(π/n))
Heres
is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area.- Name the file and program
ch4pr5.java
. - Example execution (
java ch4pr5
) user provides input:
Enter the number of sides: 5
Enter the side length: 6.5
The area of the polygon is 72.69017017488385
- 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).