|
Re: List of Float numbers
|
Posted: May 8, 2002 7:24 AM
|
|
I am *no* expert, but here's what I had time to do:
//import java.util.*;
import java.awt.*; //added
import java.lang.Float; //added
public class FloatList extends Frame
{
public static void main(String [] args)
{
myList ml = new myList();
ml.setLayout(new FlowLayout()); //capitalize f
List list= new List(20);
ml.insert(list);
ml.sort(list);
ml.print(list);
ml.add(list);
ml.setSize(300,600);
ml.setVisible(true);
}//end main method
public void insert(List list)
{
for(int i = 0;i<20;i++)
{
list.add(new Float(Math.random()).toString());
}//end for loop
}//end method insert
public void sort(List list)
{
String tempa;
String tempb;
Float k,l;
int a; // added
for(int i =0;i<19;i++)
{
for(int q =1;q<20;q++)
{
tempa = list.getItem(i);
tempb = list.getItem(q);
//k = Float.parse(tempa); see next set of lines
//l = Float.parse(tempb); for modifications
k = new Float(tempa);
l = new Float(tempb);
a = tempa.compareTo(tempb); //added
//if(k<l) changed to comparison on a
if (a<0)
{
list.remove(i);
list.remove(q);
//list.add((new Float(k).toString()),q);
//list.add((new Float(l).toString()),i); // replace y with l
list.add(tempa,q);
list.add(tempb,i);
}//end if
}//end for q
}//end for i
} //add this (end sort())
public void print(List list) //was "listList"
{
for(int i =0;i<20;i++)
{
list.makeVisible(i);
}//end for
}//end method print
}//end class
This compiles under JDK 1.4 on a Solaris workstation with 2 (of the same) errors concerning "myList" - which I presume you have. Look at the code for the other changes. Without that "myList", I can't tell if the program will actually work as intended.
I couldn't spend all that much time with this today, sorry. But it looks like a lot of the problem is just typos. I haven't had time to go thru a lot of the methods! Perhaps some kind person could do so?
Lynn.
|
|