174k views
1 vote
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 an input file. This program consists of two classes. The first class is the Weight class, which 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 three 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 variable 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 must not contain any other public methods.
The second class should be named Project1. 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 input file from the default directory by using the JFileChooser class. The input file should contain one weight per line. If the number of weights in the file exceeds 25, an error message should be displayed 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.
Be sure to follow good programming style, which means making all instance variables private, naming all constants and avoiding the duplication of code. Furthermore you must select enough different input files to completely test the program.

User Etrit
by
6.1k points

1 Answer

2 votes

Answer: Provided in the explanation section

Step-by-step explanation:

CODE USED :

//weight class

public class Weight

{

//defining private variables pound, ounces and number of ounces in pound which is 16

private int pounds;

private double ounces;

private int numberOfOuncesInPound=16;

//constructor which initializes the weight variable with the coming input

public Weight(int p, double o)

{

this.pounds=p;

this.ounces=o;

}

//checks if the incoming weight object is less than the current object weight

public boolean lessThan(Weight w)

{

//first comparison between pounds of the objects, if current object is less, then return true

if(this.pounds<w.pounds)

return true;

//else if it is less then return false

else if(this.pounds>w.pounds)

return false;

//else if both are equal then we compare the ounces

else

{

//if current object ounces is less than we return true else false

if(this.ounces<w.ounces)

return true;

else

return false;

}

}

//adds incoming weight to the current weight object and then normalize it

public void addTo(Weight w)

{

this.pounds+=w.pounds;

this.ounces+=w.ounces;

normalize();

}

//divide the current weight with factor x which is a parameter of the function

public void divide(int x)

{

this.ounces=toOunces();

this.ounces/=x;

this.pounds=0;

normalize();

}

//returns a string which contains the data in a Particular Format

public String toString()

{

return this.pounds+" lbs "+this.ounces+" oz";

}

//returns weight converted into ounces

private double toOunces()

{

return this.pounds*this.numberOfOuncesInPound+this.ounces;

}

//this function checks if number of ounces is greater than 16 then it normalizes it by converting into pounds

private void normalize()

{

while(this.ounces>this.numberOfOuncesInPound)

{

this.ounces-=this.numberOfOuncesInPound;

this.pounds++;

}

}

}

cheers i hope this helped !!

User Balaji Koduri
by
5.9k points