Lab 6.1: Fix My Program
We are asked to write a program to solve the following problem:
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 (the number of) completely filled jars you will have.
We therefore complete the problem-solving phase by using the Design Recipe:
MilkJarCalculator : number, number ; number
program is given the volume of milk in gallons (milkVolume) and the volume of each milk jar in gallons (jarVolume)
program calculates and outputs the exact number of jars that will be completely filled with milk (numberOfFullJars)
Examples:
-----------+-----------+--------------------
milkVolume | jarVolume | numberOfFullJars
-----------+-----------+--------------------
5.5 | 0.75 | 7
-----------+-----------+--------------------
5.5 | 1 | 5
-----------+-----------+--------------------
0 | 0.75 | ABORT (No Milk)
-----------+-----------+--------------------
-5.5 | 1 | ABORT (Owed Milk?)
-----------+-----------+--------------------
5.5 | 0 | ABORT (No Jars?)
-----------+-----------+--------------------
5.5 | -1 | ABORT (Huh????)
-----------+-----------+--------------------
Algorithm:
1. Get the milkVolume in gallons
2. If milkVolume is 0 or negative, ABORT program
3. Get the jarCapacity in gallons
4. If jarCapacity is 0 or negative, ABORT program
5. Calculate the numberOfFullJars as milkVolume/jarVolume
6. Output the numberOfFullJars
7. END program
We proceed to the implementation phase, and write the following program.
public class MilkJarCalculator
{
public static void main(String[] args)
{
double milkVolume; // gallons
double jarCapacity; // gallons
int numberOfFullJars; // number of completely filled jars of milk
// Get the milkVolume in gallons
milkVolume = 5.5;
// If milkVolume is 0 or negative, ABORT program (TO BE COMPLETED LATER)
// Get the jarCapacity in gallons
jarCapacity = 0.75;
// If jarCapacity is 0 or negative, ABORT program (TO BE COMPLETED LATER)
// Calculate the numberOfFullJars as milkVolume/jarCapacity
numberOfFullJars = milkVolume / jarCapacity;
// Output the numberOfFullJars
System.out.println(numberOfFullJars);
} // END program
} // end class MilkJarCalculator
However, when we compile the program, we get an error. What is wrong with it? Why? How can we fix it?
· Create a new NetBeans project named CPS150_Lab_6_1
·
· Copy and paste the highlighted code above to the project's main method
·
· Correct the code so that the whole number of completely filled milk jars is calculated and output.
·
Lab 6.2: Clean Up My Code
We are now asked to:
Write a program that converts yards to the number of feet, as well as the number of inches (e.g., there are 10.5 feet in 3.5 yards, and there are 126.0 inches in 3.5 yards).
Again, we complete the problem-solving phase using the Design Recipe:
ConvertYards : number ; number, number
program is given a distance measure in yards
program converts the measure to feet and to inches
Examples:
yards | feet | inches
------+------+--------
3.5 | 10.5 | 126.0
------+------+--------
0.0 | 0.0 | 0.0
------+------+--------
-3.5 | *** ABORT ***
------+------+--------
We incorporate our algorithm in a program:
public class ConvertYards
{