insertInOrder method

I am asked to write a method with the following rules:

Add method insertInOrder to SortedBag<E> so all elements are inserted in ascending order according to their compareTo method. Assume the thpe <E> has the compareTo method such as Integer, Double, and String. How do I write this method? I can't use a compareTo method to a integer right?

[367 byte] By [aditya15417a] at [2007-11-26 17:42:47]
# 1

i had to do a priority queue method, heres the code i used to insert the elements in order buut i dont really think im answering your question but i cant delete this

public void add(String x)

{

super.length++;

int Stringvalue=x.length();

int index=super.length;

for(int w=0;w<length;w++)

{

if(super.data[w].length()><=Stringvalue)

{

index=w;

break;

}

}

int y;

for(y=super.length;y>index;y--)

{

super.data[y]=super.data[y-1];

}

super.data[index]=x;

}

Message was edited by:

StaceyMach

StaceyMacha at 2007-7-9 0:10:58 > top of Java-index,Java Essentials,Java Programming...
# 2
can you explain what does super here? and how does the compareTo method works for integer if say I have the integer 2 and 12 what will compareTo return?
aditya15417a at 2007-7-9 0:10:58 > top of Java-index,Java Essentials,Java Programming...
# 3

oh ignore the "super" it is just there because i had to access instance fields from the superclass

as for your CompareTo method, i suppose you are using strings, right? well in my code i am comparing the lengths of the words, instead of putting "if(data[w].length()<=Stringvalue)" like i did

say

if(data[w].compareTo(input)<0)

StaceyMacha at 2007-7-9 0:10:58 > top of Java-index,Java Essentials,Java Programming...
# 4
well because my teacher gave me the test method and in the test method he uses string. That's why I asked what is the result of compareTo using strings
aditya15417a at 2007-7-9 0:10:58 > top of Java-index,Java Essentials,Java Programming...
# 5
You cannot do it for integers because they are primitives You can do it for the Integer class though. See the capitalisation.
floundera at 2007-7-9 0:10:58 > top of Java-index,Java Essentials,Java Programming...
# 6

I am sorry, I mean for the Integer class. How do I do this for the Integer class? What will compare to return if I have the integer object 5 and 12? anyone? Here's my code so far, do you think it works?

int temp = 0;

//n is the number of elements in the array

if (n==0){

data[o] = element;

n++;

else

for (int i = n-1; i>= 0; i--){

if (element.compareTo(data[i])<0){

temp = data[i];

data[i] = element;

data[i+1] = element;

}

n++;

}

}

Message was edited by:

aditya15417

Message was edited by:

aditya15417

aditya15417a at 2007-7-9 0:10:58 > top of Java-index,Java Essentials,Java Programming...
# 7

> What will compare to return if I have the integer object 5 and 12

Perhaps you should go and read about the compareTo method in Integer.

Your code has a bug. Lets say you have the following list (using ints to easily display): 1 2 5 6 7 8. Using your code to insert 4 you would end up with: 1 2 4 4 7 8.

floundera at 2007-7-9 0:10:58 > top of Java-index,Java Essentials,Java Programming...
# 8
data[i+1] = element;Actually the above line has the potential to raise an ArrayOutOfBoundsException
floundera at 2007-7-9 0:10:59 > top of Java-index,Java Essentials,Java Programming...
# 9
okay I think I know how to fix this, it's a typo error.. should be data[i+1] = temp; not element. and by the way data[i+i] will not get an index out of bound because n is the number of elements in the array, not the capacity of the array
aditya15417a at 2007-7-9 0:10:59 > top of Java-index,Java Essentials,Java Programming...
# 10
and what happens when the array is full?
floundera at 2007-7-9 0:10:59 > top of Java-index,Java Essentials,Java Programming...
# 11
there's another method that will control this so that the array wont be full. besides I already declared the array size to be 1000 the first time, maybe I should just put the size must be below 1000.
aditya15417a at 2007-7-9 0:10:59 > top of Java-index,Java Essentials,Java Programming...
# 12
You might be confident that it will never raise the exception but I would test it just in case. Make the initial size 3 and try insert 4 numbers.
floundera at 2007-7-9 0:10:59 > top of Java-index,Java Essentials,Java Programming...
# 13
I've added some code in this method so it won't exceed the array capacity. How about the other part of this code, is it fine?
aditya15417a at 2007-7-9 0:10:59 > top of Java-index,Java Essentials,Java Programming...
# 14
I don't know. Are we supposed to be your beta testers?
floundera at 2007-7-9 0:10:59 > top of Java-index,Java Essentials,Java Programming...
# 15
I was just asking if my algorithm is right, you don't have to test it to see whether it works or not. I just want to know whether my logic is right or not
aditya15417a at 2007-7-21 17:11:48 > top of Java-index,Java Essentials,Java Programming...
# 16
My point is, the easiest way for you to find out is to compile and run your code. Did it produce the correct results?
floundera at 2007-7-21 17:11:48 > top of Java-index,Java Essentials,Java Programming...
# 17
actually I only wrote this code on a piece of paper and If I would want to test it I would have to write one page long code along with the class and other stuff inside eclipse. this is just a written homework assignment. but it's okay, I appreciate for the help.
aditya15417a at 2007-7-21 17:11:48 > top of Java-index,Java Essentials,Java Programming...