Question

Directions: Follow the instructions below to explore sorting objects using boxes below. You will sort the...

Directions: Follow the instructions below to explore sorting objects using boxes below. You will sort the boxes by the largest volume.

  1. Copy and Paste the Box method below into BlueJ

public class Box

{

private double length, height, depth;

  

public Box( double length, double height, double depth )

{

this.length = length;

this.height = height;

this.depth = depth;

}

  

public double volume()

{

return length*height*depth;

}

// compare this Box to another Box

int compareTo( Box other )

{

  

//Insert code requested in #2 here

}

  

public String toString()

{

return( "length: " + length + ", height: " + height + ", depth: " + depth + ", volume: " + volume() );

}

  

}

  1. Compare the Box that is running the method with other. Return a negative integer, zero, or a positive integer, when the Box is less than, equal, or greater than other.
  2. Save what you have so far as Box.java
  3. Copy and paste the following code into BlueJ

public class BoxSort

{

// Sort an array of Box

public static void selectionSort( Box[] array )

{

// Find the object reference that should go in each cell of

// the array, from cell 0 to the end

for ( int j=0; j < array.length-1; j++ )

{

// Find min: the index of the reference that should go into cell j.

// Look through the unsorted references (those at j or higher)

int min = j;

for ( int k=j+1; k < array.length; k++ )

if ( array[k].compareTo( array[min] ) < 0 ) min = k;

// Swap the reference at j with the reference at min

Box temp = array[j];

array[j] = array[min];

array[min] = temp;

}

}

  

public static void main ( String[] args )

{

Box[] boxArray =

{ new Box( 1.0, 2.3, 2.7), new Box( 1.0, 4.9, 3.2), new Box( 3.0, 1.3, 2.7),

new Box( 3.0, 0.1, 4.67), new Box( 1.3, 1.3, 1.3), new Box( 4.0, 2.3, 1.7),

new Box( 2.2, 2.1, 1.67), new Box( 2.3, 7.3, 6.3), new Box( 2.0, 3.3, 5.3)

};

  

// print out the array

System.out.println("Before: ");

for ( Box box : boxArray )

System.out.println( box );

System.out.println( );

  

// sort the array

selectionSort( boxArray );

  

// print out the array

System.out.println("After: ");

for ( Box box : boxArray )

System.out.println( box );

System.out.println( );

   }

}

  1. Save what you created in the previous step as BoxSort.java in the same subdirectory as Box.java. Compile and run as usual.
  2. Modify BoxSort above to use the sort method.
  3. Why do these two files work so well together? Type your answer in the comment box when you turn in the assignment.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Box.java

public class Box

{

   private double length, height, depth;

   public Box(double length, double height, double depth)

   {

       this.length = length;

       this.height = height;

       this.depth = depth;

   }

   public double volume()

   {

       return length * height * depth;

   }

   // compare this Box to another Box

   int compareTo(Box other)

   {
       // Insert code requested in #2 here
       double vol=volume();
       double othVol=other.volume();
       if(vol<othVol)
           return -1;
       else if(vol>othVol)
           return 1;
       else
           return 0;

   }

   public String toString()

   {

       return ("length: " + length + ", height: " + height + ", depth: "
               + depth + ", volume: " + volume());

   }

}

===============================

// BoxSort.java

public class BoxSort

{

   // Sort an array of Box

   public static void selectionSort(Box[] array)

   {

       // Find the object reference that should go in each cell of

       // the array, from cell 0 to the end

       for (int j = 0; j < array.length - 1; j++)

       {

           // Find min: the index of the reference that should go into cell j.

           // Look through the unsorted references (those at j or higher)

           int min = j;

           for (int k = j + 1; k < array.length; k++)

               if (array[k].compareTo(array[min]) < 0)
                   min = k;

           // Swap the reference at j with the reference at min

           Box temp = array[j];

           array[j] = array[min];

           array[min] = temp;

       }

   }

   public static void main(String[] args)

   {

       Box[] boxArray =

       { new Box(1.0, 2.3, 2.7), new Box(1.0, 4.9, 3.2),
               new Box(3.0, 1.3, 2.7),

               new Box(3.0, 0.1, 4.67), new Box(1.3, 1.3, 1.3),
               new Box(4.0, 2.3, 1.7),

               new Box(2.2, 2.1, 1.67), new Box(2.3, 7.3, 6.3),
               new Box(2.0, 3.3, 5.3)

       };

       // print out the array

       System.out.println("Before: ");

       for (Box box : boxArray)

           System.out.println(box);

       System.out.println();

       // sort the array

       selectionSort(boxArray);

       // print out the array

       System.out.println("After: ");

       for (Box box : boxArray)

           System.out.println(box);

       System.out.println();

   }

}

====================================

output:

Before:
length: 1.0, height: 2.3, depth: 2.7, volume: 6.21
length: 1.0, height: 4.9, depth: 3.2, volume: 15.680000000000001
length: 3.0, height: 1.3, depth: 2.7, volume: 10.530000000000001
length: 3.0, height: 0.1, depth: 4.67, volume: 1.4010000000000002
length: 1.3, height: 1.3, depth: 1.3, volume: 2.1970000000000005
length: 4.0, height: 2.3, depth: 1.7, volume: 15.639999999999999
length: 2.2, height: 2.1, depth: 1.67, volume: 7.715400000000002
length: 2.3, height: 7.3, depth: 6.3, volume: 105.77699999999999
length: 2.0, height: 3.3, depth: 5.3, volume: 34.98

After:
length: 3.0, height: 0.1, depth: 4.67, volume: 1.4010000000000002
length: 1.3, height: 1.3, depth: 1.3, volume: 2.1970000000000005
length: 1.0, height: 2.3, depth: 2.7, volume: 6.21
length: 2.2, height: 2.1, depth: 1.67, volume: 7.715400000000002
length: 3.0, height: 1.3, depth: 2.7, volume: 10.530000000000001
length: 4.0, height: 2.3, depth: 1.7, volume: 15.639999999999999
length: 1.0, height: 4.9, depth: 3.2, volume: 15.680000000000001
length: 2.0, height: 3.3, depth: 5.3, volume: 34.98
length: 2.3, height: 7.3, depth: 6.3, volume: 105.77699999999999

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Directions: Follow the instructions below to explore sorting objects using boxes below. You will sort 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
  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

    Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational...

    please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational class ****JAVA**** Extend the Number class and implement the Comparable interface to introduce new functionality to your Rational class. The class definition should look like this: public class Rational extends Number implements Comparable<Rational> { // code goes here } In addition a new private method gcd should be defined to ensure that the number is always represented in the lowest terms. Here is the...

  • Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to...

    Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....

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