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
ID: 3753858 • Letter: 2
Question
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 widtif, 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 3. How many cubic and non-cubic packages are there in the array? 4. Report the indices and dimensions of the cubie packages of the largest object.Explanation / Answer
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. Thanks
// Package.java
public class Package {
//attributes
private double width;
private double height;
private double length;
//default constructor
public Package() {
width=0;
height=0;
length=0;
}
//constructor with 3 double values
public Package(double width, double height, double length) {
this.width = width;
this.height = height;
this.length = length;
}
/**
* finds and returns the volume of the package
*/
public double getVolume(){
return width*length*height;
}
/**
* returns true if the package is cubic
*/
public boolean isCube(){
if(width==height && height==length){
return true;
}
return false;
}
//getters
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public double getLength() {
return length;
}
}
// Runner.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Runner {
/**
* method to load Packages from an input data file
*
* @param filename
* input file name
* @return an array of Package objects, without empty elements
* @throws FileNotFoundException
* if the file is not found
*/
public static Package[] loadPackages(String filename)
throws FileNotFoundException {
Package packages[] = new Package[100];
int count = 0;
Scanner file = new Scanner(new File(filename));
while (file.hasNext()) {
double width = file.nextDouble();
double height = file.nextDouble();
double length = file.nextDouble();
// creating a package, adding to array
Package p = new Package(width, height, length);
packages[count] = p;
count++;
}
// returning the resized array, after removing all empty values
return Arrays.copyOf(packages, count);
}
/**
* method to find and display the largest package and its dimensions
*
* @param packages
* array of Package objects
*/
public static void findLargestPackage(Package[] packages) {
int indexLargest = -1;
for (int i = 0; i < packages.length; i++) {
if (indexLargest == -1) {
indexLargest = i;
} else if (packages[indexLargest].getVolume() < packages[i]
.getVolume()) {
indexLargest = i;
}
}
Package largest = packages[indexLargest];
System.out.printf("Largest package is at index %d and has dimensions"
+ " width = %.2f, height = %.2f, length = %.2f"
+ " and volume = %.2f ", indexLargest, largest.getWidth(),
largest.getHeight(), largest.getLength(), largest.getVolume());
}
/**
* method to find and display the number of cubic and non cubic packages
*
* @param packages
* array of Package objects
*/
public static void cubicAndNonCubicPackages(Package[] packages) {
int countCubic = 0, countNonCubic = 0;
for (int i = 0; i < packages.length; i++) {
if (packages[i].isCube()) {
countCubic++;
} else {
countNonCubic++;
}
}
System.out.println("There are " + countCubic + " cubic and "
+ countNonCubic + " non cubic packages");
}
/**
* method to find and display the dimensions of cubic packages
*
* @param packages
* array of Package objects
*/
public static void printDimensionsOfCubicPackages(Package[] packages) {
System.out.println("Indices & dimensions of cubic packages:");
for (int i = 0; i < packages.length; i++) {
if (packages[i].isCube()) {
System.out.println("Index: " + i);
System.out.println("Width: " + packages[i].getWidth());
System.out.println("Height: " + packages[i].getHeight());
System.out.println("Length: " + packages[i].getLength());
System.out.println("Volume: " + packages[i].getVolume());
System.out.println();
}
}
}
public static void main(String[] args) {
//defining an array of Packages
Package packages[];
//getting input file name
System.out.print("Enter input file name: ");
Scanner keyboard = new Scanner(System.in);
String filename = keyboard.nextLine();
try {
//loading packages into the array
packages = loadPackages(filename);
//displaying largest package details
findLargestPackage(packages);
//displaying cubic and non cubic packages count
cubicAndNonCubicPackages(packages);
//displaying cubic packages dimensions
printDimensionsOfCubicPackages(packages);
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
}
//packages.txt
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
//OUTPUT
Enter input file name: packages.txt
Largest package is at index 0 and has dimensions width = 20.00, height = 10.00, length = 8.00 and volume = 1600.00
There are 1 cubic and 5 non cubic packages
Indices & dimensions of cubic packages:
Index: 4
Width: 3.5
Height: 3.5
Length: 3.5
Volume: 42.875
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.