Programming Assignment 5


Due: Week of Feb. 13 before lab


  1. (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 as hours:minutes:seconds. For example convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, and convertMillis(555550000) returns a string 154: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
  2. (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
  3. (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 prefix 0s. The size of the string is the width. For example, format(34, 4) returns 0034 and format(34, 5) returns 00034. If the number is larger than the width, the method returns the string representation for the number. For example, format(34, 1) returns 34. Write a test program that prompts the user to enter a number and its width and displays a string returned by invoking format(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
  4. 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


General Instructions, Turning in assignments, and Grading

General Instructions

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.

Points