IN JAVA, ALSO MAKE A METHOD USING SCANNER TO READ THE FILE, MAKE THE CODE SIMPLE
ID: 3880305 • Letter: I
Question
IN JAVA, ALSO MAKE A METHOD USING SCANNER TO READ THE FILE, MAKE THE CODE SIMPLE SO I CAN FOLLOW PLEASE, THANKS
Write a program FedEx.java. You must use a one-dimensional array of double content to store information provided in each line of the input file. The program must contain a method to perform each of the four tasks listed (in bullet points) below. Each of the methods must have exactly three parameters: three one-dimensional arrays to transfer the input data to the method.
The shipping center manages deliveries of each day in a single text file. Each text file contains exactly three lines. Each line contains n double numbers representing a dimension of n boxes. The first line represents the length, the second line represents the width, and the third line represents the height of each of the boxes. That is, each column represents the length, width, and height of a box.
The sample content of a file representing five packages to be delivered on a day:
2.00 10.00 8.00 8.00 2.00
4.50 8.45 1.20 3.00 3.50
8.00 2.50 4.00 1.00 1.5
The length, width, and height of the first box in the sample input file are 2.00, 4.50, and 8.00; the length, width, and height of the second box in the input file are 10.00, 8.45, and 2.50; so and so forth. The name of the input file is always info.txt.
Your program must output the following information on the terminal/console.
• Display the volume of each of the boxes.
• Display total volume of all the boxes
• Display the length, width, height, and volume of the largest box (that is the box with the largest volume).
• Display the length, width, height, and volume of the smallest box (that is the box with the smallest volume).
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//FedEx.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FedEx {
static double[] length = null, width = null, height = null;
/**
* method to load data from file and to fill the arrays
*/
static void readFromFile() {
File file = new File("packages.txt");
try {
Scanner scanner = new Scanner(file);
int n = 0;
/**
* Finding the number of packages. This can be done by analyzing the
* number of data items seperated by whitespace in a single line
*/
if (scanner.hasNext()) {
/**
* Getting the line
*/
String row = scanner.nextLine();
/**
* Dividing the line by white space, it will create an array of
* String, the number of elements in the resultant array will be
* equal to the total number of elements
*/
String splitted_row[] = row.split(" ");
/**
* assigning the length to a variable
*/
n = splitted_row.length;
}
/**
* initializing the arrays with the calculated capacity
*/
length = new double[n];
width = new double[n];
height = new double[n];
/**
* Re initializing the scanner and reading the data
*/
scanner = new Scanner(file);
/**
* Splitting the first line, and adding the elements to the length
* array
*/
String[] line1Elements = scanner.nextLine().split(" ");
for (int i = 0; i < n; i++) {
/**
* converting string to double and adding to the array
*/
length[i] = Double.parseDouble(line1Elements[i].trim());
}
/**
* Splitting the second line, and adding the elements to the width
* array
*/
String[] line2Elements = scanner.nextLine().split(" ");
for (int i = 0; i < n; i++) {
width[i] = Double.parseDouble(line2Elements[i].trim());
}
/**
* Splitting the third line, and adding the elements to the height
* array
*/
String[] line3Elements = scanner.nextLine().split(" ");
for (int i = 0; i < n; i++) {
height[i] = Double.parseDouble(line3Elements[i].trim());
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (Exception e) {
System.out.println("Invalid file format");
}
}
/**
* method to print the volume of each boxes
*
* @param lengths
* -array of lengths
* @param widths
* - array of widths
* @param heights
* -array of heights
*/
static void printVolume(double[] lengths, double[] widths, double[] heights) {
for (int i = 0; i < lengths.length; i++) {
double volume = lengths[i] * widths[i] * heights[i];
System.out.println("Volume of box " + (i + 1) + " is " + volume);
}
}
/**
* method to print the total volume of all boxes
*
* @param lengths
* -array of lengths
* @param widths
* - array of widths
* @param heights
* -array of heights
*/
static void printTotalVolume(double[] lengths, double[] widths,
double[] heights) {
double total = 0;
for (int i = 0; i < lengths.length; i++) {
double volume = lengths[i] * widths[i] * heights[i];
total += volume;
}
System.out.println("Total volume of all boxes is " + total);
}
/**
* method to print the details of largest box
*
* @param lengths
* -array of lengths
* @param widths
* - array of widths
* @param heights
* -array of heights
*/
static void findLargestBox(double[] lengths, double[] widths,
double[] heights) {
int indexOfLargest = 0;
double largestVolume = 0;
for (int i = 0; i < lengths.length; i++) {
double volume = lengths[i] * widths[i] * heights[i];
if (i == 0) {
/**
* Assuming First element as the largest, this will execute only
* for one time
*/
largestVolume = volume;
indexOfLargest = 0;
}
if (volume > largestVolume) {
/**
* if the current box has higher volume than the assumed one,]
* set it as the largest volume
*/
largestVolume = volume;
indexOfLargest = i;
}
}
System.out.println("The largest box is of length: "
+ lengths[indexOfLargest] + ", width: "
+ widths[indexOfLargest] + ", height: "
+ heights[indexOfLargest]+", volume: "+largestVolume);
}
/**
* method to print the details of smallest box
*
* @param lengths
* -array of lengths
* @param widths
* - array of widths
* @param heights
* -array of heights
*/
static void findSmallestBox(double[] lengths, double[] widths,
double[] heights) {
int indexOfSmallest = 0;
double smallestVolume = 0;
for (int i = 0; i < lengths.length; i++) {
double volume = lengths[i] * widths[i] * heights[i];
if (i == 0) {
/**
* Assuming First element as the smallest, this will execute only
* for one time
*/
smallestVolume = volume;
indexOfSmallest = 0;
}
if (volume < smallestVolume) {
/**
* if the current box has lower volume than the assumed one,]
* set it as the smallest volume
*/
smallestVolume = volume;
indexOfSmallest = i;
}
}
System.out.println("The smallest box is of length: "
+ lengths[indexOfSmallest] + ", width: "
+ widths[indexOfSmallest] + ", height: "
+ heights[indexOfSmallest]+", volume: "+smallestVolume);
}
public static void main(String[] args) {
/**
* loading data
*/
readFromFile();
if (length != null && width != null & height != null) {
/**
* Displaying the stats
*/
printVolume(length, width, height);
printTotalVolume(length, width, height);
findLargestBox(length, width, height);
findSmallestBox(length, width, height);
}
}
}
/*packages.txt*/
2.00 10.00 8.00 8.00 2.00
4.50 8.45 1.20 3.00 3.50
8.00 2.50 4.00 1.00 1.5
/*OUTPUT*/
Volume of box 1 is 72.0
Volume of box 2 is 211.25
Volume of box 3 is 38.4
Volume of box 4 is 24.0
Volume of box 5 is 10.5
Total volume of all boxes is 356.15
The largest box is of length: 10.0, width: 8.45, height: 2.5, volume: 211.25
The smallest box is of length: 2.0, width: 3.5, height: 1.5, volume: 10.5