Programming Assignment 2


Due: Wed. Sep. 13 before lab


  1. (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 and 15% 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
  2. (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
  3. (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
  4. (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

    where ta is the outside temperature in degrees Fahrenheit and v 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
  5. 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))

    Here s 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


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