Big Java / Java Concepts Lab 4

Using Numbers


Suppose you have 5 1/2 gallons of milk and want to store them in milk jars that can hold up to 0.75 gallons each. You want to know ahead of time, how many completely filled jars you will have. The following program has been written for that purpose. What is wrong with it? Why? How can you fix it?

public class MilkJarCalculator
    public static void main(String args[])
       double milk = 5.5; // gallons
       double jarCapacity = 0.75; // gallons
       int completelyFilledJars = milk / jarCapacity;




You want to know how many feet is 3.5 yards, and how many inches is 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;
       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" make it hard to maintain and debug. Modify the program so that it uses constants to improve legibility and make it easier to maintain.


Run the program. What is the output? What change(s) would you make to the program to make the output more readable?

Arithmetic Operations and Mathematical Functions


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)


PMT: periodic payment

i: periodic interest or compound rate

n: number of payments

What is the present value of an annuity with that pays out $10,000 of retirement income in each of the next 20 years if the interest rate is 8%?

Write a program to calculate the present value of an annuity, for the values given in the problem. Remember that you can use Math.pow(x, y) to calculate xy.

What is your program?


What is the output of the following program? Why?

public class ArithmeticTester
    public static void main(String args[])
       int age1 = 18;
       int age2 = 35;
       int age3 = 50;
       int age4 = 44;

       double averageAge = (age1 + age2 + age3 + age4) / 4;


Fix the program in the previous problem so that it yields the correct result.


What is the output of the following program? Why?

public class DoubleTester
    public static void main(String args[])
       double probability = 8.70;
       int percentage = (int) (100 * probability);


Fix the program from the previous problem so that it displays the correct result. Remember you can use Math.round to convert a floating-point value to its closest integer.

String Programming


Using substring and concatenation, give a program containing a sequence of commands that will extract characters from inputString = "The quick brown fox jumps over the lazy dog" to make outputString = "Tempus fugit". Then print outputString.

Reading Input


The Scanner class lets you read keyboard input in a convenient manner. To construct a Scanner object, simply pass the object to the Scanner constructor:

Scanner in = new Scanner(;

Once you have a scanner, you use the nextInt or nextDouble methods to read the next integer or floating-point number. For example:

System.out.print("Enter quantity: ");

int quantity = in.nextInt();
System.out.print("Enter price: ");
double price = in.nextDouble();
Modify the program you created in problem 4 (the one that calculates the present value of an annuity) so that the user can provide the values for pmt, i, and n through the console.