Programming Assignment 5
Due: Week of Feb. 13 before lab
-
(25 points) Problem 6.25 of the book.
Convert milliseconds to hours, minutes, and seconds. Write a method that
converts milliseconds to hours, minutes, and seconds using the following
header:
public static String convertMillis(long millis)
The method returns a string ashours:minutes:seconds
. For exampleconvertMillis(5500)
returns a string0:0:5
,convertMillis(100000)
returns a string0:1:40
, andconvertMillis(555550000)
returns a string154:19:10
. Your main method should take an amount of milliseconds from a user and display the number in the new format.- Name the file and program
ch6pr25.java
. - Example execution (
java ch6pr25
) user provides input:
Enter a time in milliseconds: 5500
Time in hours:minutes:seconds: 0:0:5 - Example execution (
java ch6pr25
) user provides input:
Enter a time in milliseconds: 100000
Time in hours:minutes:seconds: 0:1:40 - Example execution (
java ch6pr25
) user provides input:
Enter a time in milliseconds: 555550000
Time in hours:minutes:seconds: 154:19:10
- Name the file and program
-
(50 points) Problem 6.27 of the book.
Emirp. An emirp (prime spelled backwards) is a nonpalindromic prime number
whose reversal is also a prime. For example, 17 is a prime and 71 is a
prime, so 17 and 71 are emirps. Write a program that displays the first 100
emirps. Display 10 numbers per line, separated by exactly one space.
Hint: use three methods to help your implementation: int reverse(int),
boolean isPrime(int), boolean isEmirp(int).
- Name the file and program
ch6pr27.java
. - Example execution (
java ch6pr27
):
13 17 31 37 71 73 79 97 107 113
149 157 167 179 199 311 337 347 359 389
701 709 733 739 743 751 761 769 907 937
941 953 967 971 983 991 1009 1021 1031 1033
1061 1069 1091 1097 1103 1109 1151 1153 1181 1193
1201 1213 1217 1223 1229 1231 1237 1249 1259 1279
1283 1301 1321 1381 1399 1409 1429 1439 1453 1471
1487 1499 1511 1523 1559 1583 1597 1601 1619 1657
1669 1723 1733 1741 1753 1789 1811 1831 1847 1867
1879 1901 1913 1933 1949 1979 3011 3019 3023 3049
- Name the file and program
-
(25 points) Problem 6.37 of the book.
Format an integer. Write a method with the following header to format the
integer with the specified width:
public static String format(int number, int width)
The method returns a string for the number with one of more prefix0
s. The size of the string is the width. For example,format(34, 4)
returns0034
andformat(34, 5)
returns00034
. If the number is larger than the width, the method returns the string representation for the number. For example,format(34, 1)
returns34
. Write a test program that prompts the user to enter a number and its width and displays a string returned by invokingformat(number, width)
.- Name the file and program
ch6pr37.java
. - Example execution (
java ch6pr37
) user provides input:
Enter an integer: 34
Enter the desired width: 4
Formatted number: 0034 - Example execution (
java ch6pr37
) user provides input:
Enter an integer: 34
Enter the desired width: 5
Formatted number: 00034 - Example execution (
java ch6pr37
) user provides input:
Enter an integer: 34
Enter the desired width: 1
Formatted number: 34
- Name the file and program
-
Bonus.
(50 points) Problem 6.33 of the book.
Current date and time. Invoking
System.currentTimeMillis()
returns the elapsed time in milliseconds since midnight of January 1, 1970. Write a program that displays the date and time. You obviously must account for leap years. Months should be displayed as three letter abbreviations. The time should not account for time zones, i.e., the time displayed will be in UTC+0 or GMT. Hint: use functions liberally.- Name the file and program
ch6pr33.java
. - Example execution (
java ch6pr33
):
Current date and time is Jan 29, 2017 16:41:5
- 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).