Question
The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in
The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from a data file. This program consists of two parts. The first part is the Weight class and the second part is the Program Core.
The Weight Class is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods and two private methods:
- A public constructor that allows the pounds and ounces to be initialized to the values supplied as parameters.
- A public instance method named lessThan that accepts one weight as a parameter and returns whether the weight object on which it is invoked is less than the weight supplied as a parameter.
- A public instance method named addTo that accepts one weight as a parameter and adds the weight supplied as a parameter to the weight object on which it is invoked. It should normalize the result.
- A public instance method named divide that accepts an integer divisor as a parameter. It should divide the weight object on which the method is invoked by the supplied divisor and normalize the result.
- A public instance toString method that returns a string that looks as follows: x lbs y oz, where x is the number of pounds and y the number of ounces. The number of ounces should be displayed with four places to the right of the decimal.
- A private instance method toOunces that returns the total number of ounces in the weight object on which is was invoked.
- A private instance method normalize that normalizes the weight on which it was invoked by ensuring that the number of ounces is less than the number of ounces in a pound.
Both instance variables must be private. In addition, the class should contain a private named constant that defines the number of ounces in a pound, which is 16. The class must not contain any other public methods.
This is the first part.....
package cmis242;
public class CMIS242 {
public class Weight {
private int pounds; private double ounces; private final int noOfOuncesInPound = 16;
public Weight(int pounds, int ounces) { this.pounds = pounds; this.ounces = ounces; this.normalize();
} public boolean lessThan(Weight weight) {
if (this.pounds < weight.pounds) return true; else if (this.pounds > weight.pounds) return false; else {
if (this.ounces < weight.ounces) return true; else return false;
} }
public void addTo(Weight weight) {
this.ounces = this.ounces + weight.ounces;
this.normalize();
this.pounds = this.pounds + weight.pounds;
} public void divide(int div) {
double noOfOunces = this.toOunces();
double resultInOunces = noOfOunces / div;
int pounds = 0;
if (resultInOunces > noOfOuncesInPound) {
while (resultInOunces > noOfOuncesInPound) {
pounds += 1; resultInOunces = resultInOunces - noOfOuncesInPound;
}
} this.pounds = pounds; this.ounces = resultInOunces;
} @Override public String toString() {
return this.pounds + " lbs " + String.format(String.valueOf(this.ounces), ".4f") + " oz";
} private double toOunces() {
return this.ounces + this.pounds * noOfOuncesInPound;
} private void normalize() {
if (this.ounces > noOfOuncesInPound) {
while (this.ounces > noOfOuncesInPound) { this.pounds += 1; this.ounces -= noOfOuncesInPound;
}
}
} /
}
I need help with the second part.
The second part is the Program Core. It should consist of the following four class (static) methods.
- The main method that reads in the file of weights and stores them in an array of type Weight. It should then display the smallest, largest and average weight by calling the remaining three methods. The user should be able to select the data file from a default directory by using the JFileChooser class. Reading the data can follow the coding used in ReadEmails.java found in Week 2 |Code Examples. Attached to this assignment are two files containing weights: PRJ1WeightsLess and PRJ1WeightsMore. The second file contains more than 25 entries. The data file contains one weight per line divided into two parts: the pound as integer and the percentage of a pound, an ounce, as a float. If the number of weights in the file exceeds 25, an error message should display and the program should terminate.
- A private class method named findMinimum that accepts the array of weights as a parameter together with the number of valid weights it contains. It should return the smallest weight in that array.
- A private class method named findMaximum that accepts the array of weights as a parameter together with the number of valid weights it contains. It should return the largest weight in that array.
- A private class method named findAverage that accepts the array of weights as a parameter together with the number of valid weights it contains. It should return the average of all the weights in that array.
weightsLess.txt
233, 7500 23, 5000 56, 3125 79, 0625 45, 8125 76, 3750 76, 6250 15, 4375 56, 3750 345, 1230 34, 4375 654, 6875 8, 0025 5, 3125 987, 2500 56, 8125 24, 2500 92, 0000 35, 3125 32, 0625 35, 5900
weightsMore.txt
233, 7500 23, 5000 56, 3125 79, 0625 45, 8125 75, 4375 76, 3750 76, 6250 11, 1875 15, 4375 23, 7500 56, 3750 345, 1230 34, 4375 654, 6875 8, 0000 5, 3125 987, 2500 92, 0000 56, 8125 24, 2500 343, 5000 92, 0000 35, 3125 241, 1250 32, 0625 35, 5900
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started