Question

The first programming project involves writing a program that computes the minimum, the maximum and the...

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:

  1. A public constructor that allows the pounds and ounces to be initialized to the values supplied as parameters.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. A private instance method toOunces that returns the total number of ounces in the weight object on which is was invoked.
  7. 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.

  1. 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.
  2. 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.
  3. 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.
  4. 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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: if you are putting Weight class within CMIS242, then you have to add static keyword (which I did) to the class declaration, otherwise you cannot instantiate a Weight object from within main method of CMIS242. Otherwise you will have to put Weight class as a separate file named Weight.java

// CMIS242.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

import javax.swing.JFileChooser;

import javax.swing.JOptionPane;

public class CMIS242 {

    // if you are putting Weight class within CMIS242, then you have to add

    // static keyword to the class declaration, otherwise you cannot instantiate

    // a Weight object from within main method of CMIS242

    public static class Weight {

        private int pounds;

        private double ounces;

        private final int noOfOuncesInPound = 16;

        public Weight(int pounds, double 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 = (double) 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("%.4f",this.ounces) + " oz";

        }

        private double toOunces() {

             return this.ounces + this.pounds * noOfOuncesInPound;

        }

        private void normalize() {

             while (this.ounces > noOfOuncesInPound) {

                 this.pounds += 1;

                 this.ounces -= noOfOuncesInPound;

             }

        }

    }// end of Weight class

    // main method

    public static void main(String[] args) throws FileNotFoundException {

        // creating an array of 25 weights

        Weight weights[] = new Weight[25];

        int count = 0; // current count

        // creating and displaying a JFileChooser dialog

        JFileChooser fileChooser = new JFileChooser();

        int result = fileChooser.showOpenDialog(null);

        // checking if user selected a file correctly

        if (result == JFileChooser.APPROVE_OPTION) {

             // getting and opening selected file

             File file = fileChooser.getSelectedFile();

             Scanner scanner = new Scanner(file);

             // looping through the file

             while (scanner.hasNext()) {

                 // reading a line, splitting by comma

                 String line = scanner.nextLine();

                 String fields[] = line.split(",");

                 // ensuring that there are 2 fields in splitted line

                 if (fields.length == 2) {

                     // taking first value as pound

                     int pounds = Integer.parseInt(fields[0]);

                     // second as ounces

                     double ounces = Double.parseDouble(fields[1]);

                     // creating a Weight object using pounds an ounces

                     Weight weight = new Weight(pounds, ounces);

                     // if array is full, displaying error and quitting

                     if (count == weights.length) {

                         JOptionPane.showMessageDialog(null,

                                  "File contains too many weights!");

                         System.exit(0);

                     }

                     // otherwise adding to count index and incrementing count

                     weights[count] = weight;

                     count++;

                 }

             }

             // closing file

             scanner.close();

             // finding minmimum, maximum and average weights

             Weight min = findMinimum(weights, count);

             Weight max = findMaximum(weights, count);

             Weight avg = findAverage(weights, count);

             // displaying results in a JOptionPane dialog window

            JOptionPane.showMessageDialog(null, "Minimum weight: " + min

                     + "\nMaximum weight: " + max + "\nAverage weight: " + avg);

        }

    }

    // method to find the minimum weight from an array of weights

    public static Weight findMinimum(Weight weights[], int count) {

        if (count == 0) {

             // empty array

             return null;

        }

        // setting first weight as minimum weight

        Weight weight = weights[0];

        // looping through remaining weights

        for (int i = 1; i < count; i++) {

             // if current weight is less than what stored in weight variable,

             // setting current weight as new minimum

             if (weights[i].lessThan(weight)) {

                 weight = weights[i];

             }

        }

        // returning minimum

        return weight;

    }

    // method to find the maximum weight from an array of weights

    public static Weight findMaximum(Weight weights[], int count) {

        if (count == 0) {

             return null;

        }

        Weight weight = weights[0];

        for (int i = 1; i < count; i++) {

             // if current maximum weight is less than current weight, setting

             // current weight as new maximum

             if (weight.lessThan(weights[i])) {

                 weight = weights[i];

             }

        }

        return weight;

    }

    // method to find the average weight from an array of weights

    public static Weight findAverage(Weight weights[], int count) {

        if (count == 0) {

             return null;

        }

        // creating an empty weight

        Weight weight = new Weight(0, 0);

        // looping and adding each weight to weight

        for (int i = 0; i < count; i++) {

             weight.addTo(weights[i]);

        }

        // dividing by count to get the average

        weight.divide(count);

        return weight;

    }

}

/*OUTPUT*/


Add a comment
Know the answer?
Add Answer to:
The first programming project involves writing a program that computes the minimum, the maximum and the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    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: 1. A public constructor that allows...

  • A private class method named findMaximum that accepts the array of weights as a parameter together...

    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. each objects contain one integer and one double Weight(int, double) private static Weight findMaximum(Weight[] objects) { }

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Write a program to pack boxes with blobs. Write two classes and a driver program. A...

    Write a program to pack boxes with blobs. Write two classes and a driver program. A Blob class implements the following interface. (Defines the following methods.) public interface BlobInterface { /** getWeight accessor method. @return the weight of the blob as a double */ public double getWeight(); /** toString method @return a String showing the weight of the Blob */ public String toString(); } A Blob must be between 1 and 4 pounds, with fractional weights allowed. A default constructor...

  • Please Do It With Java. Make it copy paste friendly so i can run and test...

    Please Do It With Java. Make it copy paste friendly so i can run and test it. Thnak You. Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • This is a java program that runs on the Eclipse. Java Programming 2-1: Java Class Design...

    This is a java program that runs on the Eclipse. Java Programming 2-1: Java Class Design Interfaces Practice Activities Lesson objectives: Model business problems using Java classes Make classes immutable User Interfaces Vocabulary: Identify the vocabulary word for each definition below. A specialized method that creates an instance of a class. A keyword that qualifies a variable as a constant and prevents a method from being overridden in a subclass. A class that it can't be overridden by a subclass,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT