Array help

• March 6th, 2010, 03:48 PM
mlan
Array help
Code :

```package lab6;   import java.sql.Array; import java.util.Random;   /** * A list of floats that can grow to be as long as necessary. * @author Melissa, Colorado School of Mines. */ public class FloatList {   /** * Constructs an empty list of floats. */ public FloatList() { // TODO: initialize private fields. // Construct the array a with initial length 8, so that // we can append eight floats without growing the array. for (int i=0; i<a.length; ++i) this.n = 0; this.a = new float[8]; }   /** * Appends the specified float to this list. * @param x the float to append. */ public void append(float x) { // TODO: implement this method using the following algorithm: // If the number of floats in this list equals a.length // (such that there is no more room in the array a), then // 1) make a new array t that is twice as long as the array a, // 2) copy elements from the array a to the new array t, and // 3) set a = t // In any case, store x in the array a and increment n. Random r = new Random(); for (float a=r.nextFloat(); x>0.01f; x=r.nextFloat()) append(x); float[] t = new float[]; a = t; }   /** * Returns the number of floats in this list. * @return the number of floats. */ public int countFloats() { for (int i=0; i<n; ++i) // TODO: implement this method. return n; // incorrect! }   /** * Gets a copy of all of the floats in this list. The number of * floats in the returned array equals the number of floats in * this list. * @return array of floats in this list. */ public float[] getFloats() { // TODO: implement this method as specified. // DO NOT simply return the array a of floats used to contain // this list. That array is private and may have a length that // exceeds the current number of floats in this list. Instead, // 1) construct a new array c with the proper length, // 2) copy values from the array a to the array c, and // 3) return the array c.   FloatList c = new FloatList(); float[] a = c.getFloats(); ArrayPlot.show("random floats",a); return a; // incorrect! }   /////////////////////////////////////////////////////////////////////////// // private (do not add any other fields to this class)   private int n; // number of floats in this list private float[] a; // array of floats such that n <= a.length }```

I don't understand why the countFloat and the append won't work.
• March 6th, 2010, 07:38 PM
jasonm
Re: Array help
Can you provide an example using your FloatList? Indicate what you expect to happen as well as what actually happens. That should provide a good starting point for getting this coded up.
• March 7th, 2010, 10:59 AM
mlan
Re: Array help
Quote:

Originally Posted by jasonm
Can you provide an example using your FloatList? Indicate what you expect to happen as well as what actually happens. That should provide a good starting point for getting this coded up.

Example code using FloatList:

Code :

```package lab6;   import java.util.Random;   /** * Demonstrates use of the classes FloatList and ArrayPlot. You need not * modify this class; you need only answer the questions in the comments. * @author Melissa, Colorado School of Mines */ public class FloatListDemo {   public static void main(String[] args) {   // Construct an empty list of floats. FloatList fl = new FloatList();   // Append random floats in the range [0.01,1) to the list. // Stop when we generate a random float not in this range. // What is the probability that the list will be empty? // What happens if the list is empty? Random r = new Random(); for (float x=r.nextFloat(); x>0.01f; x=r.nextFloat()) fl.append(x);   // Get and plot the floats in the list. float[] a = fl.getFloats(); ArrayPlot.show("random floats",a); } }```

I hope this is what you meant.
• March 7th, 2010, 12:52 PM
jasonm
Re: Array help
Okay, so now what do you expect to happen when you run this, and what actually happens? For simplicity, maybe change your example to eliminate the randomness (in the for(...) loop) for the time being. That might be what your assignment calls for, but for now you just want to be able to verify your class is working correctly.
• March 7th, 2010, 04:23 PM
mlan
Re: Array help
Quote:

Originally Posted by jasonm
Okay, so now what do you expect to happen when you run this, and what actually happens? For simplicity, maybe change your example to eliminate the randomness (in the for(...) loop) for the time being. That might be what your assignment calls for, but for now you just want to be able to verify your class is working correctly.

I don't understand how this will help me understand why the countFloats and append don't work.
• March 7th, 2010, 04:40 PM
jasonm
Re: Array help
What I am trying to get you to do is show me how you expect your class to work, as well as how it is working now. Without being able to do this, how do you know if your code is right or wrong?

From there, you can work through your test example to understand why you are getting the wrong answer and it should become more clear how to fix it. If you need help with syntax or anything like that, we can tackle that after you have a better idea what is wrong.

You asked a very general question (why doesn't it work?), I want to narrow that down to specific questions. I am not going to just provide you working implementations of your methods
• March 7th, 2010, 04:54 PM
mlan
Re: Array help
Ok, I understand.
I expect my class to plot a random list of numbers of floats given in an array.
I was given FloatListDemo to base my FloatList off of so that they would work together.

I'm thinking for the append portion of FloatList, I don't need Random, but need to write something that will convert a to a new array t if the present floats equal a.length. But if I tell it a.append, it states that the float is primitive and cannot be used. Whats the difference of primitive float and a normal float?

Then for the countFloats, it should return the number of floats in the present list, and in the beginning I stated that it should have 8 floats, so it should return 8 floats, but it states I must return an int, but I returned n, is n not an int?
• March 7th, 2010, 06:06 PM
jasonm
Re: Array help
Quote:

But if I tell it a.append, it states that the float is primitive and cannot be used. Whats the difference of primitive float and a normal float?
Primitives are simple data types, built into to Java language, and just hold a particular kind of value - they don't have methods. You can read about all of the primitives here. So, "float" is a primitive data type.

However, Java also provides a more powerful data type called "Float". Float is a class built around a "float" primitive type. It can store a "float" value, but in addition it provides a lot of useful methods, like toString() for example. You can read more about the Float here.

Java is case sensitive, so a float is not the same thing as a Float. It sounds to me like you are passing one when your append() method is expecting the other. But I cannot tell where in the code you are referring to when you say you are given an error message. Could you provide more detail?

Quote:

Then for the countFloats, it should return the number of floats in the present list, and in the beginning I stated that it should have 8 floats, so it should return 8 floats, but it states I must return an int, but I returned n, is n not an int?
When you create a new FloatList, internally the object sets aside enough room to hold 8 floats. But no floats have actually been stored in those spaces yet. That is what the following code is doing:

Code :

```this.n = 0; this.a = new float[8];```

So there are two things you need to be keeping track of here - how many spaces you have available to store floats, and how many floats have actually been stored.
• March 7th, 2010, 09:14 PM
mlan
Re: Array help
Quote:

Originally Posted by jasonm

Java is case sensitive, so a float is not the same thing as a Float. It sounds to me like you are passing one when your append() method is expecting the other. But I cannot tell where in the code you are referring to when you say you are given an error message. Could you provide more detail?

I am given an error message at a.append, I've taken in what you've said and changed it to a Float, the a is accepted but the append, now states that it is a float and not a Float, but I changed it to a Float and if I change it back to float then it'll still be an error.
• March 7th, 2010, 09:37 PM
jasonm
Re: Array help
What type is a? Is it the float array that belongs to FloatList? Java arrays are objects. I could be wrong, but I don't think they have an append() method. Could you provide the actual error messages?

The usual way to access a position in an array is using bracket notation. So, for example, a[0] accesses the first position in the array, and a[7] would access the eighth position (because 0 is the first position.)

Here are some examples:
Code :

```float a[]; float x, y; int i;   ...   a[0] = x; // Set the first element in the array to the value in x. y = a[i]; // get the value stored in the location i out of the array // and store it in y.```
• March 7th, 2010, 11:46 PM
mlan
Re: Array help
Quote:

Originally Posted by jasonm
What type is a? Is it the float array that belongs to FloatList? Java arrays are objects. I could be wrong, but I don't think they have an append() method. Could you provide the actual error messages?

I've changed what I had from before for this portion:

Code :

``` Random r = new Random(); for (float a=r.nextFloat(); x>0.01f; x=r.nextFloat()) // the local variable a is never read append(x); }   /** * Returns the number of floats in this list. * @return the number of floats. */ public int countFloats() { for (int i=0; i<n; ++i) // TODO: implement this method. return a; // incorrect! // Type mismatch: cannot convert from float[] to int }```
• March 8th, 2010, 07:07 AM
jasonm
Re: Array help
Code :

``` /** * Returns the number of floats in this list. * @return the number of floats. */ public int countFloats() { for (int i=0; i<n; ++i) // TODO: implement this method. return a; // incorrect! // Type mismatch: cannot convert from float[] to int }```

The type mismatch error is due to the fact that countFloats() returns an int, and you giving it a to return, which is a float[] (array of floats). The type of what you return has to match the type of your method. The comments in your code say what countFloats() should do - it should return the number of floats in the array, not the actual array. If you aren't sure how to do this, take a look
here.

Edit: The link above won't help you with returning the number of items actually in the array, although it can help you find out how big the array is (how many spaces are available). Keeping track of the number of floats that have actually been stored in the array is something you are going to have code up your class to do.