-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoxSorter.java
51 lines (48 loc) · 1.39 KB
/
BoxSorter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Created by Noah Shaw
*/
import java.util.Scanner;
public class BoxSorter {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Greet the user
System.out.println("Welcome to the box sorter!"
+ "\nEnter the information about boxes and I will sort them!");
//Construct the array
Box[] boxes = new Box[5];
//Prompt the user to input the values for each box
for(int i = 0; i < boxes.length; i++)
{
System.out.println("Enter the label, length, width, and height (all in feet) of box " + (i+1) + ".");
Scanner keyboard = new Scanner(System.in);
String label = keyboard.nextLine();
double length = keyboard.nextDouble();
double width = keyboard.nextDouble();
double height = keyboard.nextDouble();
boxes[i] = new Box(label, length, width, height);
}
//Sort the boxes using bubble sort
System.out.println("Sorting boxes.");
boolean hasSwapped = true;
while(hasSwapped)
{
hasSwapped = false;
for(int i = 0; i < boxes.length - 1; i++)
{
if(boxes[i].getVolume() > boxes[i+1].getVolume())
{
//Swap
Box temp = boxes[i];
boxes[i] = boxes[i+1];
boxes[i+1] = temp;
hasSwapped = true;
}
}
}
//Print the boxes in order from least to greatest volume
for(int i = 0; i < boxes.length; i++)
{
System.out.println(boxes[i].toString());
}
}
}