I have a homework assignment and am having a little bit of trouble. First the assignment is to make a bar graph of various sizes then adjust and sort them with each click of a button. I implemented action listener on the main class then made a secondary class to implement comparable. I have an issue calling the comparable function. It says my array int[] cant be resolved in a comparable method thats looking for comparable[] Any help or tips would be gratly appreciated. Here is my code:
import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TwoSorts extends Applet implements ActionListener
{
private final int APPLET_WIDTH = 600;
private final int APPLET_HEIGHT = 600;
Button sort;
Label sort_label;
String pr_name;
int[] random = new int[20];
int[] sorter = new int[20];
public void init()
{
sort = new Button("Sort");
add(sort);
sort.addActionListener(this);
sort_label = new Label("Orange Selection / Black Bubble");
add(sort_label);
randomGen(random);
sorter = random;
setBackground (Color.white);
setSize (APPLET_WIDTH, APPLET_HEIGHT);
}
private void randomGen (int...random) {
for (int i = 0; i < 20; i++){
random [i] = (int) (20 +(Math.random()*300-20));
}
}
public void paint(Graphics g)
{
for (int i = 0; i < 20; i++ ){
g.setColor(Color.blue);
g.fillRect((int) (10 + (i*50)), 300, 50, ((random[i])));
g.setColor(Color.black);
g.fillRect((int) (10 + (i*50)), 300, 25, (sorter[i]));
}
g.drawRect (20, 30, 130, 50);
sort.setLocation(0,220);
sort_label.setLocation(0,270);
sort_label.setSize(400,30);
}
class action extends TwoSorts implements Comparable {
public void actionPerformed(ActionEvent arg0) {
selectionSort(random);
insertionSort (sort);
repaint;
public static void selectionSort (Comparable[] random) {
int min;
Comparable temp;
for (int index = 0; index < random.length-1; index++)
{
min = index;
for (int scan = index+1; scan < random.length; scan++)
if (random[scan].compareTo(random[min]) < 0)
min = scan;
temp = random[min];
random[min] = random[index];
random[index] = temp;
}
public static void insertionSort (Comparable[] sorter) {
for (int index = 1; index < sorter.length; index ++){
Comparable key = sorter[index];
int position = index;
while (position > 0 && key.compareTo(sorter[position-1]) < 0){
sorter [position] = sorter[position-1];
position--;
}
sorter[position] = key;
}
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
Comparable should be implemented by classes with which you might have some reason to compare with other objects of the same type.
So for instance, If you have a bar graph of rectangles that need to be sorted you might make a class of rectangles which contains the height, width and position of the rectangle. now since this is a class you made up you will need to implement the compareTo function to evaluate which Rectangle is greater or less than another rectangle.
If you look at the compareTo() specification http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Comparable.html you will see:
Returns: a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
so if this object is less than the object passed to compareTo() return - , if equal 0, and + if greater.
Taking that into consideration you might end up with a class that looks something like this
public class MyRect implements Comparable {
int width; //width of the rectangle will probably not change
int height; //this might be the value you want to compare in compareTo()
point position;
...
//getters and setters yada yada
public int getHeight(){
return this.height;
}
...
@Override
public int compareTo(Object otherRect){
// if this rectangle's height is greater than otherRect the difference should
// be positive, if equal 0, and if less than the difference will be negative
// exactly as specification for compareTo() states.
return this.height - (MyRect)otherRect.getHeight();
}
}
Obviously I've left a lot out, but that should get you pointed in the right direction. Play around with it and see what you come up with. Happy coding!