Write a program (RadixSortAnimation.java) that animates the radix sort algorithm
ID: 3913556 • Letter: W
Question
Write a program (RadixSortAnimation.java) that animates the radix sort algorithm.
Create an array that consists of 20 random integers from 0 to 999, inclusive. The array
elements are displayed, as shown in Figure 0. Clicking the Stepbutton causes the
program to place a number in a bucket. The number that has just been placed is displayed
in red, as shown in Figure 1. Once all the numbers are placed in the buckets, clicking the
Step button collects all the numbers from the buckets and moves them back to the array,
as shown in Figure 2. When the algorithm is finished, clicking the Step button displays a
message to inform the user, as shown in Figure 3. Clicking the Reset button creates a new
random array for a new start.
Submit the source code file - RadixSortAnimation.java
Explanation / Answer
///Hiii....Sorry to say ..
//Bcs of time constraint I have not added sample outputs...
//Letmeknow if you found any difficult..i ill help...
import javattgeometry.Insets;
import javattapplicationApplication;
import javatcscene.controlLabel;
import javatteventEventHandler;
import javatteventActionEvent;
import javattgeometry.Pos;
import java.utilArrayList;
import javatcscene.Scene;
import javatcscene.controlautton;
import javatcscene.layoutHBox;
import javatcscene.layoutBorderPane;
import javatcscene.paint.Color;
import javatcscene.layoutPane;
import javatcscene.tenText;
import javatcscene.shape.Rectangle;
import javatcstage.Stage;
pubic class Main extends Application
{
double radius = 2;
ArrayList[] myBucket = new ArrayList[10];
@Override
public void start(Stage myStage)
{
RadixPane myPane;
myPane = new RadixPane();
myPane.setPadding(new Insets(5,5,5,5));
myPane.setStyle("-fx-border-color: black");
myStage.setHeight(355);
myStage.setWidth(855);
//Buttons creating
Button Step_btn;
Step_btn = new Button("Step");
Button Reset_btn;
Reset btn = new Button('Reset");
// Putting buttons in HBox
HBox myBox;
myBox = new HBox(5);
boolean addAll = myBox.getChildren().
addAll(Step_bin, Reset_btn);
myBox.setAlignment(Pos.CENTER);
BorderPane mainPane;
mainPane = new BorderPane();
mainPane.setPadding(new Insets(5,5,5,5));
mainPane.setCenter(myPane);
mainPane.setBottom(myBox);
//Label to display the status of sorting
Label Sort;
Sort = new Label();
mainPane.setTop(Son);
BorderPane.setAlignment(
Sort, Pos.CENTER);
Scene myScene;
myScene = new Scene(mainPane, 400, 250);
myStage.setTitle("Exercise23_17: Radix Sort);
myStage.setScene(myScene);
myStage.show();
//intializing the array
initArr();
//Drawing the buckets
drawBuckets();
//displaying everything on radix sort pane.
myPane.redraw():
//Calling Event handler when Step button is pressed
Step_btn.setOnActon(new EventHandlerImpl1 (myPane, Sort));
//Callig Event handler when Reset button is pressed
Reset_btn.setOnAction(new EventHandierlmpl (Sort, myPane));
}
//void method for launching the application
public static void main(String[] values)
{
launch(values);
}
public void initArr() {
for (int loopvar = 0;
loopvar < myListlength; loopvar++)
{
myList[loopvar] = 1+(int)
(Math.random()* 1000);
}}
//create buckets for rad& sort
pubic void drawBuckets()
{
for (int loopvar = 0;
loopvar < myBucket.length; loopvar++)
{
myBucket[loopvar] = new ArrayList<>0;
} }
//delete all values in all buckets
public void cleanBuckets()
{
for(Arraytist<Integer> bucket1: myBucket)
{
bucket1.clear();
}
}
//reset buckets and reinitialize array.
pubic void ResetAll()
{
cleanBuckets();
index = 0;
pos = 0;
initArr();
}
//convert and store values from bucket to list
public void bucketToList()
int indx;
indx = 0;
for(Arraylist<Integer> bucket1 : myBucket)
for (int loop_j
loop_j < bucket1.size(); loop_J++)
{
myList[indx++] = bucket1.get(loop_j);
}
}
}
public final static int MAX_ARRAY_SIZE = 20;
private int pos = 0;
private int index = 0;
private int elem_key;
private final int[] myList= new int(MAX_ARRAY_SIZE);
// Each step of radix sort done when the button is
// pressed
pubic boolean eachStep()
{
if (index < myList.length)
{
elem_key = returnKey(myList[index], pos);
myBucket[elem_key].add(myList[index]);
index++;
return false;
}
else if (pos < 2)
{
bucketToList();
cleanBuckets();
index = 0;
pos++;
return false;
}
else
{
bucketToList();
cleanBuckets();
return true;
} }
public static int returnKey(int number, int position)
{
int result= 1;
for (int i = 0; i < position; i++)
result *= 10;
return (number/ result) % 10;
}
//radix pane that displays the GUI using rectangles to
//show the array and the buckets.
class RadixPane extends Pane
{
private final int originX;
private final in originY;
private final int Width_Arr;
private final int Height_Arr;
RadixPane() {
this.Height_Arr = 20;
this.originX = 20;
this.originY = 20;
this.Width_Arr = 30;
}
//takes values from buckets and arrays and redraws
//the GUI whenever the button is pressed.
public void redraw()
{
this.getChildren().clear();
int x;
x= 10 + originX;
int y;
y= 10 + originY;
for (int loopvar = 0;
loopvar < myListlength; loopvar++)
{
Rectangle myRect = new Rectangle(x, y, Width_Arr, Height_Arr);
myRect.setFill(Color.WHITE);
myRect.setStroke(Color.BLACK);
this.getChildren().add(myRect);
Text newLabel = new Text (x + 5, y+ 15, myList[loopvar] + "");
this.getChildren().add(newLabel);
if (loopvar == index - 1)
{
newLabelsetFill(Color.RED);
}
else
{
newLabel.setFill(Color.BLACK);
}
x = x + Width_Arr;
}
x= 10 + originX;
y=y+40;
int bucket_Width;
bucket_width = 40;
int bucket_Height;
bucket_Height = 130;
int bucket_Distance;
bucket_Distance =60;
for (int loopvar = 0; loopvar < myBucket.length; loopvar++)
{
Rectangle myRect;
myRect = new Rectangle(x + 10, y, bucket_Width, bucket_Height);
myRect.setFill(Color.WHITE);
myRect.setStroke(Color.BLACK);
this.getChildren().add(myRect);
for (int loop_j = 0; loop_j < myBucket[loopvar.size(); loop_j++)
{
this.getChildren().add(new Text
(x + 10 + 5, y+ (loop_j + 1)* 20,
myBucket[loopvar].get(loop_j) + ""));
}
if (loopvar == elem_key &&
myBucket[loopvar].size() > 0)
{
Text newLabel_2;
newLabel2 = new Text(x + 10 + 5,
y + myBucket[loopvar].size() * 20, myBucket[loopvar].get(
myBucket[loopvar].size() - 1) + "");
newLabel2.setFill(Color.RED);
this.getChildren().add(newLabel_2);
}
Text newLabel_3;
newLabel_3 = new Text(x + 5, y + bucket_Height + 25,
"bucket[" + loopvar + "]");
getChildren().add(newLabel_3);
x = x + bucket_Distance;
} } }
//Event handier for Reset button.
private class EventHandlerImpl
implements EventHandler<ActionEvent>
{
private final Label Sort;
private final RadixPane myPane;
pubic EventHandlerImpl
(Label Sort, RadixPane myPane)
{
this.Sort = Sort;
this.myPane = myPane;
@Override
public void handle(ActionEvent e)
ResetAll(); Sort.setText("");
myPane.redraw():
}
}
//Event Handler for Step Button
private class EventHandlerImpl1
implements EventHandler<ActionEvent>
{
private final RadixPane myPane;
private final Label Sort;
pubic EventHandlerImpl1
(RadixPane myPane, Label Sort)
{
this.myPane = myPane;
this.Sort = Sort;
}
@Override
public void handle(ActionEvent e)
{
if (eachStep())
{
myPane.redraw();
Sort.setText("Sorted");
}
else
{
myPane.redraw();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.