Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:35:59+00:00 2026-06-13T22:35:59+00:00

I have a homework assignment and am having a little bit of trouble. First

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:36:00+00:00Added an answer on June 13, 2026 at 10:36 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble with a homework assignment, were we are supposed to make
Me and my friend having a problem while doing a homework assignment. We have
First off this is for homework or... project. I'm having trouble understanding the idea
I have a homework assignment, and i am finished other then one question (see
I have an assignment in C++ and I'm having trouble getting started. The goal
I'm having some trouble writing Pseduocode for a homework assignment in my operating systems
I have a homework assignment where I need to take input from a file
I have a homework assignment to sort an array in ascending order. Obviously, this
Hi I have a homework assignment where I need to implement an intersection of
(this is indirectly a part of a much larger homework assignment) I have something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.