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 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 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 three 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 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.

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 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.

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.

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

//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++;
       }
   }
}

1 //weight class 2 public class Weight // defining private variables pound, ounces and number of ounces in pound which is 16

لا ع rr ا ا ا ا ا ادامه ای که به ما د ا د ا ما لا ا د به او بیا د ا با ما ه داده اه اه ا ع 1 ? public void addTo (Weight w) 0

//Project1 class

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JPanel;

public class Project1
{
   //findMinimum function takes weight array input and size of array and returns weight which is minimum in array
   private static Weight findMinimum(Weight[] w,int sizeOfArray)
   {
       //initializing minimumWeight to first element of the array
       Weight minimumWeight = w[0];
       //traverse through all the array and then compares using "lessThan" function
       //if "minimumWeight" is greater than array "i-th" element, then we change the minimumWeight to "i-th" element
       for(int i=1;i<sizeOfArray;i++)
       {
           if(!(minimumWeight.lessThan(w[i])))
           {
               minimumWeight=w[i];
           }
       }
       //returns the minimumWeight
       return minimumWeight;
   }
   //findMaximum function takes weight array input and size of array and returns weight which is maximum in array
   private static Weight findMaximum(Weight[] w,int sizeOfArray)
   {
       //initializing maximumWeight to first element of the array
       Weight maximumWeight = w[0];
       //traverse through all the array and then compares using "lessThan" function
       //if "maximumWeight" is less than array "i-th" element, then we change the maximumWeight to "i-th" element
       for(int i=1;i<sizeOfArray;i++)
       {
           if(maximumWeight.lessThan(w[i]))
           {
               maximumWeight=w[i];
           }
       }
       //returns the maximumElement
       return maximumWeight;
   }
   //findAverage function takes weight array input and size of array and returns weight which is average
   public static Weight findAverage(Weight[] w, int sizeOfArray)
   {
       //initializing a variable "averageWeight" to 0 pound 0 ounces
       Weight averageWeight = new Weight(0,0);
       //adding all the array elements to "averageWeight" variable and then normalizing it
       for(int i=0;i<sizeOfArray;i++)
       {
           averageWeight.addTo(w[i]);
       }
       //dividing the "averageWeight" variable by sizeOfArray to get the average
       averageWeight.divide(sizeOfArray);
       //returns the averageWeight
       return averageWeight;
   }
   //main function
   public static void main(String[] args) throws IOException
   {
       //defining an array of weight with size of 25
       Weight w[]= new Weight[25];
       //variable to count number of weights in the file, initializing it to 0
       int countNoOfWeights = 0;
       //creating a jFilechoose object to get the file
       JFileChooser fileChooser = new JFileChooser();
       //setting the default directory, change according to your need
       fileChooser.setCurrentDirectory(new File(new File("E:\\Project\\").getCanonicalPath()));
       //then we show dialog box in which user select a file
       int result = fileChooser.showOpenDialog(new JPanel());
       //defining a file variable to store the file which user choose
       File selectedFile;
       //if user chooses a file
       if(result == JFileChooser.APPROVE_OPTION)
       {
           //then we get that file in "selectedFile" variable
           selectedFile = fileChooser.getSelectedFile();
           //defining a scanner variable to take input from file
           //giving parameter to the scanner variable is the file that user chooses
           Scanner scan = new Scanner(selectedFile.getAbsoluteFile());
           //defining variables to take input from file
           int pound;
           double ounce;
           //runs till end of file
           while(scan.hasNext() )
           {
               //takes pound input which is int
               pound=scan.nextInt();
               //takes ounce input which is double
               ounce=scan.nextDouble();
               //assigns the incoming variable to the weight array index
               w[countNoOfWeights] = new Weight(pound,ounce);
               //increasing the countNoOfObjects which acts as index also
               countNoOfWeights++;
               //if no of objects is more tham 25 then we exit the program as instructed using "Return;"
               if(countNoOfWeights>=25)
               {
                   System.out.print("Number of weights in the file exceeds 25");
                   return;
               }
           }
           //printing minimum, maximum, average of the array
           System.out.println("Minimum Weight in the array is " + findMinimum(w,countNoOfWeights).toString());
           System.out.println("Maximum Weight in the array is " + findMaximum(w,countNoOfWeights).toString());
           System.out.println("Average Weight in the array is " + findAverage(w,countNoOfWeights).toString());
       }
   }
}


1 import java.io.File; 2 import java.io.IOException; 3 import java.util.Scanner; 5 import javax.swing.JFileChooser; 6 import

286 private static Weight findMaximum (Weight[] w, int sizeofArray) //initializing maximumweight to first element of the arra

con i // dividing the averageWeight variable by sizeofArray to get the average averageWeight divide (sizeofArray); //return

- 0 090) ON 0 0 0 //then we get that file in selectedFile variable selectedFile = fileChooser.getSelectedFile(); //defining

INPUT: In file "data.txt"

12 12.23
12 4.5
11 11
10 2
2 5.2
14 3
57 4.2


12 12.23 12 4.5 11 11 10 2 25.2 143 57 4.2

OUTPUT:

-File Choosing:

- . 0 0 0 0 0 0 00 00 00 00 00 00 00 00 00 0 0 0 0 0 0 0 0 0 0 0 OOOOOOOOO 0 0 0 0 //defining a scanner variable to take inpu
-RESULT:

Minimum Weight in the array is 2 lbs 5.2 oz
Maximum Weight in the array is 57 lbs 4.2 oz
Average Weight in the array is 17 lbs 3.732857142857142 oz

Minimum Weight in the array is 2 lbs 5.2 oz Maximum Weight in the array is 57 lbs 4.2 oz Average Weight in the array is 17 lb

Ask any questions if you have in comment section below.

Please rate the answer.

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 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...

  • 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...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • 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...

  • Use BlueJ to write a program that reads a sequence of data for several car objects...

    Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info for any number of cars. (You should not assume that it will always be seven lines in the input file). Use notepad to create input file "inData.txt". File should be stored in the same folder where all files from BlueJ for this program...

  • The second programming project involves writing a program that accepts an arithmetic xpression of unsigned integers...

    The second programming project involves writing a program that accepts an arithmetic xpression of unsigned integers in postfix notation and builds the arithmetic expression tree that epresents that expression. From that tree, the corresponding fully parenthesized infix expression should be displayed and a file should be generated that contains the three address format instructions. This topic is discussed in the week 4 reading in module 2, section II-B. The main class should create the GUI shown below Three Adddress Generator...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two...

    Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two are of type int and are called xLoc and yLoc and the third is of type int called ID. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Update the constructor to set the three new instance variables and add appropriate get and set methods for the four new variables. All set methods...

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