Pseudocode And Algorithm Development
Description
of problem:
The U.S. post office has rules about mailing packages. A package cannot be mailed first class if the sum of its length and girth is greater than 100 inches, or if the package weighs more than 70 pounds. The girth is the perimeter around the height and width, where the length is defined as the longest of the three dimensions.
Write a program that
takes in the weight of the package and the three dimensions of the package
in any order. The program
should determine the longest dimension of the package, calculate the girth,
and compute the size of the box. The
program should then print out one of the following messages about this
package:
1. Package is too large and too heavy.
2. Package is too large.
3. Package is too heavy.
4. Package is acceptable.
Development
of pseudocode:
Stepwise refinement 1 - Overall sections of this problem:
Get data from user
Solve math
Print answer
Stepwise refinement 2 - More detailed pseudocode version:
Prompt user for three dimensions
Prompt user for weight
Determine longest of three dimensions
Calculate the girth using the other two dimensions
If package is too big and too heavy, print appropriate message
else if package is too big, print appropriate message
else if package is too heavy, print appropriate message
else print package is acceptable
Stepwise refinement 3 - Determining longest of three dimensions:
My strategy is to end up with dim1 holding the largest value
Compare dim2 and dim1, if dim2 is greater, swap dim1 and dim2, dim1 will be holding largest value
Compare dim3 and dim1, if dim3 is greater, swap dim1 and dim3, dim1 is still holding largest value
Dim1 has largest value, compute math for package
Source code answer
for mail problem:
import chn.util.*;
class CheckMail
{
private
int myWeight, myLength, myWidth, myHeight;
//
Here are the constructor methods...
public
CheckMail ()
{
myWeight = myLength = myWidth = myHeight = 1;
}
public
CheckMail(int weight, int length, int width, int
height)
{
myWeight = weight;
myLength = length;
myWidth = width;
myHeight = height;
}
public
void dataInput()
{
ConsoleIO keyboard = new ConsoleIO();
int temp = 0;
System.out.print("Enter the weight --> ");
myWeight = keyboard.readInt();
System.out.print("Enter 3 dimensions separated by spaces -->
");
myLength = keyboard.readInt();
myWidth = keyboard.readInt();
myHeight = keyboard.readInt();
if (myWidth > myLength)
{
// swapping values of myWidth and myLength, using third
variable temp
temp = myWidth; myWidth
= myLength; myLength =
temp;
}
if (myHeight > myLength)
{
// swapping values of myHeight and myLength, using third
variable temp
temp = myHeight; myHeight
= myLength; myLength =
temp;
}
System.out.println();
System.out.println();
}
// prints out answers
public
void printAnswer()
{
int total = myLength + (myWidth*2) + (myHeight*2);
boolean tooLarge = (total > 100);
boolean tooHeavy = (myWeight > 70);
System.out.println("Weight = " + myWeight + "
lbs");
System.out.println("Length = " + myLength);
System.out.print("Other two dimensions = ");
System.out.println(myWidth + "
" + myHeight);
System.out.println();
System.out.print(" Package
is - ");
if (tooLarge && tooHeavy)
System.out.println("too large and too heavy");
else if (tooLarge && !tooHeavy)
System.out.println("too large");
else if (!tooLarge && tooHeavy)
System.out.println("too heavy");
else if (!tooLarge && !tooHeavy)
System.out.println("acceptable");
System.out.println();
}
public
static void main(String[] args)
{
CheckMail aPackage = new CheckMail();
aPackage.dataInput();
aPackage.printAnswer();
}
}