The Artima Developer Community
Sponsored Link

Java Answers Forum
List of Float numbers

1 reply on 1 page. Most recent reply: May 8, 2002 7:24 AM by Lynn Hollerman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Linda

Posts: 2
Nickname: lapo35
Registered: Apr, 2002

List of Float numbers Posted: May 8, 2002 12:49 AM
Reply to this message Reply
Advertisement
Hi,
My name is pat I really have tried so much in vain to work thrrough thsi program but am still unable to get it resolved,The program is supposed to generate 20 random floating numbers and sort in a non ascending order the n print. i have trie dto get help but all in vain Please please help . below is my program.kindly help me
import java.util.*;

public class FloatList extends Frame
{
/*
** preconditions :
** postconditions: Return list
*/

public static void main(String [] args)
{
myList ml = new myList();
ml.setLayout(new flowLayout());
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

/*
** preconditions:
** postconditions:
*/
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

/*
** preconditions:
** postconditions:
*/
public void sort(List list)
{
String tempa;
String tempb;
float k,l;
for(int i =0;i<19,i++)
{
for(int q =1;q<20;q++)
{
tempa = listgetItem(i);
tempb = listgetItem(q);
k = Float.parse(tempa);
l = Float.parse(tempb);

if(k<l)
{
list.remove(i);
list.remove(q);
list.add((new Float(k).toString()),q);
list.add((new Float(y).toString()),i);
}//end if
}//end for 1
}//end for 2

/*
** preconditions:
** postcondition:
*/
public void print(listList)
{
for(int i =0;i<20;i++)
{
list.makeVisible(i);
}//end for
}//end method print
}//end class


Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Re: List of Float numbers Posted: May 8, 2002 7:24 AM
Reply to this message Reply
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.

Flat View: This topic has 1 reply on 1 page
Topic: Base Number Program Previous Topic   Next Topic Topic: download BIG attachfile from servlet, HOW?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use