51 You want to know how many feet are in 35 yards and how many inches are in 35

51 you want to know how many feet are in 35 yards and

This preview shows page 5 - 9 out of 10 pages.

5.1) You want to know how many feet are in 3.5 yards, and how many inches are in 3.5 yards. You write the following program for that purpose: public class DistanceConverter { public static void main(String[] args) { double yards = 3.5; double feet = yards * 3;
Image of page 5
double inches = feet * 12; System.out.println(yards + "yards are" + feet + "feet"); System.out.println(yards + "yards are" + inches + "inches"); } } The problem with the program above is that using "magic numbers" makes it hard to maintain and debug. Modify the program so that it uses constants to improve legibility and make it easier to maintain. public class DistanceConverter { public static void main(String[] args ) { final double yards = 3.5; final double feet = yards * 3; final double inches = feet * 12; System. out .println( yards + " yards are " + feet + " feet." ); System. out .println( yards + " yards are " + inches + " inches." ); } } 5.2) Run the DistanceConverter program from Lab 4.5.1. What is the output? What change(s) would you make to the program to make the output more readable? 6) Adding (incrementing) or subtracting (decrementing) the value one from an integer variable is a common, everyday operation. To increment an int variable x, we could code x = x + 1; As an alternative, we could use the special operators ++ and -- to increment and decrement a variable. Use the first method to increment x in the program below. Print the value of x after incrementing. Use the ++ operator to increment y in the program below. Print the value of y after incrementing.
Image of page 6
public class IncrementDemo { public static void main(String[] args ) { int x = 10; int y = -3; x = x + 1; System. out .println( x ); y ++; System. out .println( y ); } } The outputs are 11 and -2, which was as expected. 7.1) An annuity (sometimes called a reverse mortgage) is an account that yields a fixed payment every year until it is depleted. The present value of the annuity is the amount that you would need to invest at a given interest rate so that the payments can be made. The present value of an annuity (PVann) at the time of the first deposit can be calculated using the following formula: PVann = PMT • ({[(1 + i)n - 1 - 1] / i } / (1 + i)n - 1 + 1) where: PMT: periodic payment i: periodic interest or compound rate n: number of payments What is the present value of an annuity that will pay out $10,000 in each of the next 20 years if the interest rate is 8 percent? Write a program to calculate the present value of an annuity for these values. Remember that you can use Math.pow(x, y) to calculate xy.
Image of page 7
What is your program?
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 10 pages?

  • Fall '12
  • DuiWei
  • Remainder, System.out.println

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture