0

I'm still new to java and writing/reading code, so I'm not quite sure what my professor wants. All I need is some reinforcement of what I should be doing. The assignment is as follows:

Specify and then implement a method (of some class X) that is passed a NumberList and that returns an array containing the values from the NumberList.

(The NumberList is not changed by your method. Your method is NOT a member of NumberList. You won't be able to test your method by running it since I am not providing the NumberList class to you.)

If you need it, here are the public methods. The one method that I use is:

public int size() //returns number of items in this NumberList

So, as I understand, all I am doing is taking the NumberList and creating an array of the values. Easy enough. Is this handling the work that is asked?

public double [] arrayNL(NumberList list){
    //pre: NL is not empty
    //post: array with NL values is returned
    double [] arrayNL = new double [list.size()];
    for(int x=0;x<list.size();x++){
        arrayNL[x]=list.nextDouble;
    }
    return arrayNL;
}

Just uncertain about list.size() and list.nextDouble... and that is if I'm correct in understanding the problem. Really haven't done enough object coding to be familiar/confident with it and I heavily rely on testing, so I'm questioning everything. Any help would be great, I just have trouble following this professor's instructions for some reason.

JJ Dubz
  • 3
  • 2
  • Where are you getting nextDouble from? Perhaps that should be get(x)? It is difficult to do this perfectly without running the code. It looks like the public methods of NumberList are very similar to ArrayList, so perhaps you could test your function by passing it an ArrayList? – Ian McGowan Jan 24 '17 at 02:31
  • I was thinking the same about an ArrayList. I was wondering if it had a private ArrayList data member that stored the double list – Coop Jan 24 '17 at 02:37
  • I hesitate to check it using arraylist since it kind of goes against the spirit of the assignment. But i think you are onto something with using get(x). Maybe could just use list.get(x) in place of list.nextDouble to move through the numberlist and assign values to the array. Been using Scanner a lot recently, so that is where I got nextDouble, wasn't even thinking about it. – JJ Dubz Jan 24 '17 at 02:39

3 Answers3

0

Not sure I understand the question. Is the goal to write the code that copies the list to the array, or to implement the methods in the NumberList class based on the pre- and post- conditions?

Coop
  • 309
  • 1
  • 9
  • Goal is to copy the list and return an array, without changing number list. The pre and post conditions, in the chunk of code, are what I added. The assignment is only the "as follows:" part. – JJ Dubz Jan 24 '17 at 02:35
0

I believe that one of the goals of this exercise is to teach how to read an API (Application program interface) and implements its method just by reading the documentation, without reading the actual code behind it.

This is an important practice since as a future developer you will have to use other people's methods and you won't be able to implement everything by yourself.

As for your code, I'm not sure where you've seen the nextDouble method as I don't see it in the documentation. Unless it was given to you, I suggest you'll stick to the documentation of NumberList() and other basic coding features.

Instead of using nextDouble you can use: public double get(int index) so your for loop would look something like this:

 for(int i = 0; i < list.size() ;i++){
    arrayNL[i]= list.get(i);
}

The rest of your code is basically fine.

DMEM
  • 1,573
  • 8
  • 25
  • 43
  • Sure thing. I came across get() with the help of Ian and Coop, read right passed it when I was going through the list of methods. NextDouble was what I was using due to not seeing get(), and using Scanner and its methods heavily for the last couple weeks; it was guess work at best. – JJ Dubz Jan 24 '17 at 03:14
-1

Your code is basically all there, although next double is undefined in the NumberList class so that may give you trouble. Here's what each part is doing:

public double [] arrayNL(NumberList list){

    // Initialize an array of doubles containing the same # of elements 
    // as the NumberList
    double [] arrayNL = new double [list.size()];

    // Iterate through the NumberList
    for(int x=0;x<list.size();x+) { 

        // Copy the double from the NumberList object to the double array 
        // at the current index.  Note "nextDouble" is undefined, but
        // NumberList does have a method you can use instead.
        arrayNL[x]=list.nextDouble; 
    } 

    // After iterating through the whole list, return the double array
    return arrayNL; 
}

Sorry for any formatting issues. Typed this on my phone

Coop
  • 309
  • 1
  • 9
  • Thanks a bunch for the help. I usually have more trouble with definitions and understanding assignments than anything else. Found get(x) in the NumberList public methods, so list.get(x) should be used in place of list.nextDouble. – JJ Dubz Jan 24 '17 at 02:53
  • No problem. You had the right thought process, just a simple mistake really – Coop Jan 24 '17 at 03:39