Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Array help

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array help

    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.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.

  3. The Following User Says Thank You to jasonm For This Useful Post:

    JavaPF (March 7th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array help

    Quote Originally Posted by jasonm View Post
    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:

    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.

  5. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.

  6. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array help

    Quote Originally Posted by jasonm View Post
    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.

  7. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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

  8. #7
    Junior Member
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by mlan; March 7th, 2010 at 05:57 PM.

  9. #8
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Array help

    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?

    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:

    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.

  10. #9
    Junior Member
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array help

    Quote Originally Posted by jasonm View Post

    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.

  11. #10
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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:
    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.

  12. #11
    Junior Member
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array help

    Quote Originally Posted by jasonm View Post
    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:

    	  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
      }

  13. #12
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Array help

      /**
       * 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.
    Last edited by jasonm; March 8th, 2010 at 08:20 AM.

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM