List ADT Question: Code Included
From: da Vinci (blank_at_blank.com)
Date: 11/27/04
- Next message: Joe: "Re: OOP question. Are these Objects right?"
- Previous message: Andrew Thompson: "Re: Null pointer exception with Java 1.4.2, ok with 1.5"
- Next in thread: Oscar kind: "Re: List ADT Question: Code Included"
- Reply: Oscar kind: "Re: List ADT Question: Code Included"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 27 Nov 2004 18:13:19 GMT
I have a question regarding the List ADT.
Now, my code has no errors and it works just fine. However, I do not
think I am using the List ADT method (part of the assignment) to do
this right.
The assigment says to use the List ADT to store 100 float values and
then calculate the average.
The problem I had is instantiating 100 objects. I looked through the
Google Groups and found how to do it quickly with a for loop using an
array. So I switched over to doing it via an array with a loop and had
no more problems in this area.
The problem is in doing it this way, I think I went more along the
route of using arrays instead of lists. I searched more through the
google groups and internet and couldnt find anything that talked about
what I am working with...
The main reason I think it is wrong is because if you remove the area
in NumberPlay marked with stars below, it still works fine. So I know
it is working off arrays instead of Lists.
Any advice or pointers (no pun intended) would be great.
Code (multiple .java files):
public class NumberPlayNode
{
private NumberPlayNode next = null;
private float value = 0.0f;
// GET RANDOM FLOAT VALUE FOR OBJECT
public NumberPlayNode()
{
value = (float)(Math.random());
}
// PUBLIC ACCESS METHOD TO DATA
public float getData()
{
return value;
}
// USED FOR LINKING OBJECTS
public void setNext(NumberPlayNode nextPtr)
{
next = nextPtr;
}
}
public class NumberPlay
{
public static void main(String argv[])
{
NumberPlayNode node[] = new NumberPlayNode[100];
float total = 0.0f;
// INSTANTIATE 100 OBJECTS OF NUMBERPLAYNODE CLASS
for (int i = 0; i < 100; i++)
node[i] = new NumberPlayNode();
// ***LINK ALL OBJECTS TOGETHER USING LIST ADT***
for (int i = 0; i < 99; i++)
node[i].setNext(node[i+1]);
// GRAB VALUES OF EACH AND FIND A TOTAL
for (int i = 0; i < 100; i++)
total += node[i].getData();
// OUTPUT AVERAGE VALUE
NumberPlayOutput.showOutput(total);
}
}
public class NumberPlayOutput
{
// GENERATE GENERIC ASCII OUTPUT
public static void showOutput(float totalValue)
{
System.out.println("\n\n\n");
System.out.println("**************************************");
System.out.println("* The average of the float values is *");
System.out.println("* " + totalValue + " *");
System.out.println("**************************************\n\n\n");
}
- Next message: Joe: "Re: OOP question. Are these Objects right?"
- Previous message: Andrew Thompson: "Re: Null pointer exception with Java 1.4.2, ok with 1.5"
- Next in thread: Oscar kind: "Re: List ADT Question: Code Included"
- Reply: Oscar kind: "Re: List ADT Question: Code Included"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]