Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: The goal of this assignment is to practice array of objects. Backgrou

ID: 3888460 • Letter: O

Question

Objective: The goal of this assignment is to practice array of objects.

Background: El Paso Packaging and Supply Co. has contracted you to create a software to summarize their inventory.

The client saves dimensions of packages in the inventory in a text file. While it sounds inefficient, the client is an angel from programmer’s point of view.JThis is because it is possible to write a program that will directly read from the file and create a summary of the inventory. A sample file is shown below.

20 10 8

4.5 8.45 12.2

8.0 2.5 4.0

1.0 15.0 18.0

3.5 3.5 3.5

6.0 5.0 10.0

That is, each line contains the width, height, and length of a package. The dimensions are separated by spaces.

Assignment: You need to write a program using object oriented programming idea for packages. That is, each package must be considered an object. To achieve this, you must write a class named Package. Make sure to create a Java file for the Package class. Some other required properties of the Package class are as follows.

1. All status variables of the Package class must be private.

2. Write no more than two constructors.

3. Must have a public method named getVolume() that will return the volume of the package.

4. Must have a public method named isCube() that will return true if the package is cubic, false otherwise.

5. The Package class must NOT contain any main method.

Feel free to write any additional method in the Package class, as you see fit.

The program file (the Java file that contains the main method) must be written in a file named Runner.java. The Runner class must not have any status variable. Runner must have the following functionalities. Each functionality must be implemented in a separate method in Runner.

1. Read the input text file provided by the client and create an array of Package objects. The sequence of the lines should be used in the sequence of objects in the array.

2. Find the largest package in the array. Report the index, dimensions, and volume of the largest object.

3. How many cubic and non-cubic packages are there in the array?

4. Report the indices and dimensions of the cubic packages.

5. Report average volume of cubic packages only.

Deliverables: You are expected to submit two Java files (Package.java and Runner.java) via Blackboard. You must demo your programs within one week after the due date. Your demo will be based on your last submission in the Blackboard. Your TA will instruct you with further details.

Explanation / Answer

Following are the contents of Package.java :


public class Package {
   private double width;
   private double height;
   private double length;
  
   public Package(){
       width = 0;
       height = 0;
       length = 0;
   }
   public Package(double w, double h, double l){
       width = w;
       height = h;
       length = l;
   }
   public double getVolume(){
       return width*height*length;
   }
   public boolean isCube(){
       if(width == height && height == length)
           return true;
       return false;
   }
   public String toString(){
       /*********************************************************
       * This function returns a textual description of package
       * We don't return directly the dimensions as that would
       * render the use of declaring variables private useless.
       *********************************************************/
      
       String packageDesc = "Width of Package :"+Double.toString(width)+
                           " Height of Package :"+Double.toString(height)+
                           " Length of Package :"+Double.toString(length);
       return packageDesc;
   }
}

Following are the contents of Runner.java :

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Runner {
   public static void main(String[] args){
      
       String filename = "packages.txt";                   // The name of the file containing details of package
       BufferedReader reader = null;                       // Main buffered Reader to store file contents
       BufferedReader tempReader = null;                   // Temporary buffer used to count lines in file
       FileReader fileReader1 = null;
       FileReader fileReader2 = null;

       try{
           /*************************************************************
           * Required variables:
           * maxVolume       : Stores maximum volume of any package
           * maxPackageIndex   : Stores index of largest package
           * numCubic           : number of cubic packages
           * numNonCubic       : number of non-Cubic packages
           * averageCubicVol    : Average volume of cubic packages
           * cubicVolume       : total Volume of cubic packages
           * index           : keep track of number of packages
           * largestIndex       : index of largest package
           *************************************************************/
          
           int maxPackageIndex = 0;
           int numCubic = 0;
           int numNonCubic = 0;
           int index = 0;
           int largestIndex = 0;
           double maxPackageVolume = 0;
           double averageCubicVol = 0;
           double cubicVolume = 0;
          
           Package largestPackage = new Package(0,0,0);                   // Package to store largest package object
           fileReader1 = new FileReader(filename);                           // Read file
           tempReader = new BufferedReader(fileReader1);// Create temporary string buffer of contents of file
           String line;
           int numLines = 0;
          
           /*************************************************************
           * This next while loop iterates over lines in file contents
           * and counts the number of lines. This will be the number of
           * packages that client has.
           *************************************************************/
          
           line = tempReader.readLine();
           while(line != null){
               numLines++;
               line = tempReader.readLine();
           }
          
           Package[] packages = new Package[numLines];   // An array of Package objects
           fileReader2 = new FileReader(filename);
           reader = new BufferedReader(fileReader2);   // Create string buffer of contents of file
           line = reader.readLine();                   // Read 1 line from file contents in reader
           while(line != null){                       // Loop will continue till last line of file is reached
              
               // To store the width height and length of each new package read from file
               double w,h,l;                  
              
               // Next we assign values w,h,l based on what is read from file.
               w = Double.parseDouble(line.split(" ")[0]);   // This assigns the first number in line to w
               h = Double.parseDouble(line.split(" ")[1]);   // This assigns the second number in line to h
               l = Double.parseDouble(line.split(" ")[2]);   // This assigns the third number in line to l
              
               Package tempPackage = new Package(w,h,l);   // Create a temporary package as read from file
               packages[index] = tempPackage;               // Add the new package in array
              
               // To check if the new package is larger than the previous largest package
               if(tempPackage.getVolume() > maxPackageVolume){
                   maxPackageVolume = tempPackage.getVolume();
                   largestPackage = tempPackage;
                   largestIndex = index;
               }
              
               //Check if the package is Cubic or non-Cubic
               if(tempPackage.isCube()){
                   // Increase number of cubic packages if cubic
                   numCubic++;                          
                   //Add volume of cubic package to total volume of cubic packages
                   cubicVolume += tempPackage.getVolume();  
                   // Print index and dimensions of cubic package
                   System.out.println("Cubic package at index : "+index+
                                       " with specifications: "+tempPackage.toString());
               }
               else{
                   // Increase number of non-cubic packages if non-cubic
                   numNonCubic++;  
               }
               index++;
               line = reader.readLine();
           }
           if(numCubic > 0){
               averageCubicVol = cubicVolume / numCubic;
           }
           System.out.println("Largest package is at index "+largestIndex+
                               " Package description of largest package are : "+
                               largestPackage.toString());
           System.out.println("Volume of largest package is "+largestPackage.getVolume()+" units" );
           System.out.println("Total number of non-Cubic packages are "+numNonCubic);
           System.out.println("Total number of Cubic packages are "+numCubic);
           System.out.println("Average volume of Cubic packages is "+averageCubicVol+" units");
       }
       catch(IOException e){
           System.out.println("File does not exist!");
       }
   }
}

I have included explainations in form of comments wherever necessary in the program code.

Few clarifications:

I create 2 buffered reader and 2 file openers as I read the file twice, once to get the number of lines and second to get actual data from each lines.

The toString() method of Package class describes the dimensions of package object from which it is called. You can create methods like getWidth() that directly return the width of package but then it defeats the purpose of data abstraction in Object oriented programming.

You need to change the variable 'filename' of type string to whatever will be the actual name of file and place it in same directory as you java files (I have named it 'packages.txt'). Also you can include the complete path of the file in filename incase the program does not identify file automatically.