Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My methods for bubble sort and selection sort are wrong but how do i fix them to give me the right outputs for this lab?

My methods for bubble sort and selection sort are wrong but how do i fix them to give me the right outputs for this lab?

Assignment: In this assignment, you will be implementing versions of two sorting algorithms. You will use them to sort an array of objects representing cubes. Your first step is to implement a class called Cube that has three double variables to store the length, width, and height of the cube. Then, write a method that calculates the volume of the Cube as a member of the class. All other requirements are listed below.

Next, you should write a method in a class named Runner1 that will generate a random list of Cubes for testing. The method should take in a number representing the size of the list, and return an array of that many Cubes. Each Cube should have a randomly generated length, width, and height between 0 and 100.

Implement another method in Runner1 to print out your array of cubes. You should print a line for each cube listing the length, width, and height and then the volume.

As another preliminary step, implement a method CopyArray in Runner1 that creates a new array of cubes with identical values of a given one.

Now, write two methods inside Runner1, one named BubbleSort and the other named SelectionSort. Use parameters as you think appropriate. BubbleSort must implement the bubble sort algorithm we discussed in the class and SelectionSort must implement the selection sort algorithm we discussed in class. Note that both BubbleSort and SelectionSort must sort the cubes in the input array in ascending order of the volumes of the cubes.

//I ALSO NEED TO CREATE THE METHOD FOR RUNNER1 THAT WILL EXECUTE THE REQUIREMENT BELOW

Demonstrate that your code works by generating a random list of 20 cubes (in an array), printing out the list before you sort it, and then printing out the list again after it is sorted using BubbleSort. For an identical list of 20 cubes (copied right after the generation of the array that was given to BubbleSort as an input), show the list before and after the use of the SelectionSort method.

Write a class named Runner2 that will test the speed of your BubbleSort and SelectionSort methods by recording the time needed to sort an array of different number of cubes (For example, 2000, 3000, 4000, 5000, 6000, and 7000 cubes)

THESE ARE THE CODES I GOT

//Cube.java

public class Cube {

public double length; //stores length

public double width; //stores width

public double height; //stores height

//calculate volume of cube & return it

public double calculateVol(){

double vol = length*width*height; //stores volume

return vol;

}

}

//Runner1.java

import java.util.Random;

public class Runner1 {

Cube cube[]; //declaration of variable

Cube newcube[];

//return array of cubes

public Cube[] generateRandom(int size){

cube = new Cube[size];

Random rn = new Random(); //for generation of random number

for(int i=0; i

cube[i] = new Cube();

cube[i].length = rn.nextDouble()*100; //random length

cube[i].width = (rn.nextDouble()*100 + rn.nextDouble()*100)%101; //random width

cube[i].height = rn.nextDouble()*100; //random height

}

return cube;

}

//prints cube's length, height, width and volume.

public void printCubes(){

for(int i=0; i

System.out.print("Cube "+i+": "+"length: "+cube[i].length+" width: "+cube[i].width+" height: "+cube[i].height);

System.out.print(" Volume: "+cube[i].calculateVol()); //print volume of cube

System.out.println();//print new line

}

}

//method to create new array

public void copyArray(){

newcube = new Cube[cube.length];

for(int i=0; i

newcube[i] = new Cube(); //creating object of cube class

newcube[i].length = cube[i].length;

newcube[i].width = cube[i].width;

newcube[i].height = cube[i].height;

}

}

//Bubble sort method

public void bubbleSort(){

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

for(int i=0; i

System.out.print("Cube "+i+": "+"length: "+cube[i].length+" width: "+cube[i].width+" height: "+cube[i].height);

System.out.print(" Volume: "+cube[i].calculateVol()); //print volume of cube

System.out.println("");//print double new line

}

System.out.println("");

Cube temp = new Cube();

boolean swapped;

for(int i=0; i

swapped = false;

for(int j=0; j

if(cube[j].calculateVol() > cube[j+1].calculateVol()){ //swapping in element > next element

temp = cube[j];

cube[j].length = cube[j+1].length;

cube[j].height = cube[j+1].height;

cube[j].width = cube[j+1].width;

cube[j+1].length = temp.length;

cube[j+1].height = temp.height;

cube[j+1].width = temp.width;

swapped = true;

}

}

// IF no two elements were swapped by inner loop, then break

if (swapped == false)

break;

}

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

for(int i=0; i

System.out.print("Cube "+i+": "+"length: "+cube[i].length+" width: "+cube[i].width+" height: "+cube[i].height);

System.out.print(" Volume: "+cube[i].calculateVol()); //print volume of cube

System.out.println("");//print new line

}

System.out.println("");

}//end of bubble sort

//selection sort method

public void selectionSort(){

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

for(int i=0; i

System.out.print("Cube "+i+": "+"length: "+newcube[i].length+" width: "+newcube[i].width+" height: "+newcube[i].height);

System.out.print(" Volume: "+newcube[i].calculateVol());

//print volume of cube

System.out.println("");//print new line

}

System.out.println("");

Cube temp = new Cube();

int min_idx; //stores index of cube of min volume

for(int i=0; i

//Find the minimum element in unsorted array

min_idx = i;

for (int j = i+1; j < newcube[i].length; j++)

if (newcube[i].calculateVol() < newcube[min_idx].calculateVol())

min_idx = j;

// Swap the found minimum element with the first element

temp = newcube[min_idx];

newcube[i].length = newcube[min_idx].length;

newcube[i].height = newcube[min_idx].height;

newcube[i].width = newcube[min_idx].width;

newcube[min_idx].length = temp.length;

newcube[min_idx].height = temp.height;

newcube[min_idx].width = temp.width;

}

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

for(int i=0; i

System.out.print("Cube "+i+": "+"length: "+newcube[i].length+" width: "+newcube[i].width+" height: "+newcube[i].height);

System.out.print(" Volume: "+newcube[i].calculateVol()); //print volume of cube

System.out.println("");//print new line

}

System.out.println("");

}//end of selection sort

}

//Runner2.java

import java.util.Scanner;

//Definition of Runner2 class

/*Note: System.currentTimeMillis() method can be used to get the current time then

*subtract when the method finishes executing which result speed of a method.

* */

public class Runner2 {

public static void main(String []args){

Runner1 r1 = new Runner1(); //creating object of runner1 class

System.out.println("-------Runner2Class------");

System.out.println("Enter -1 to quit: ");

while(true){

Scanner scan = new Scanner(System.in);

System.out.print("Enter numer of cubes of generate : ");

int num = scan.nextInt();

if(num < 0)

System.exit(0);

r1.generateRandom(num); //generate random array of cubes

r1.copyArray(); //create copy of cube array to newcube array

//calculate time taken by bubble Sort algo to execute

long startT = System.currentTimeMillis(); //stores start time

r1.bubbleSort();

long endT = System.currentTimeMillis(); //stores end time

long BRuntime = (endT - startT);

//calculate time taken by Selection sort algo to execute

startT = System.currentTimeMillis(); //stores start time

r1.selectionSort();

endT = System.currentTimeMillis(); //stores end time

long SRuntime = (endT - startT);

System.out.println("");

System.out.println("Bubble Sort took "

+BRuntime + " milliseconds");

System.out.println("Selection Sort took "

+ SRuntime + " milliseconds");

System.out.println("");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions

Question

Why does ATM use small, fixed-length cells?

Answered: 1 week ago

Question

What has been your desire for leadership in CVS Health?

Answered: 1 week ago

Question

Question 5) Let n = N and Y Answered: 1 week ago

Answered: 1 week ago