• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i've got to create a constructor that accepts the size of an array. I've come up with this

Where sa is the name of the array, it has been consructed in another class called student
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


hope this helps
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if this is where you're going ...

Can you fill in the missing line in the constructor?
 
Iain Palmer
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In answer to Stans question would it be?
 
Iain Palmer
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also thanksnow Miguel it works fine now
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Iain Palmer:
In answer to Stans question would it be?



No, not quite. myArray hasn't been defined yet, so you'll actually get a compiler error. Try again, keeping in mind what you want to do -- define myArray as an integer array of size size.
 
Iain Palmer
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how about
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There seems to be some confusion over what exactly you're trying to do.

Do you already have an array created, and you just want to find out the length of that already-existing array? And then you want to pass that length to the constructor of another class?

If so, then Migeul's code is right on the money. He's creating an array (in this case, it has 17 elements), and he's asking that array how long it is, using the length() method. That method will return 17 of course, and that result of 17 is therefore what gets passed to the constructor. The constructor then simply stores that value in an instance variable in your new object.

Okay, so on to the second possiblity:

Do you not already have an array created? And do you want to pass an arbitrary number (could be 17, could be 5, could be anything) to the constructor of a class? And do you then want that construction to create a new array right there on the spot, using the number you gave it (17, or 5, or whatever) to determine the size of this newly-created array?

If so, then Stan's question is leading you in the right direction. He's set it up so that you can pass any number to a constructor. It could be anything--that all depends on how you call the constructor. It's intended to be used like this:

Demo myDemo1 = new Demo(17);

Demo myDemo2 = new Demo(5);

See, when we use "new" to create a new instance of Stan's Demo class, we pass a number to the constructor. The question he's asking is, how would you get your construction to create a brand-new array of the size we've asked for?

Remember, the constructor doesn't know how big it will be asked to make the array, so you can't just type in a number like this:

myArray = new int[17];

...because you don't know that 17 is the correct size. But you do know that, whatever the size should be, that size will be passed as an argument and stored in that integer variable called "size".

Hopefully that helps.

- Jeff
 
Iain Palmer
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Miguels answer helped me fine but I thought that Stan asked a question and I would have a go at answering it. I think in the end I've learnt more by trying to answer Stan's question. Many thanks
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was after a different problem description perhaps. I guessed the assignment is: "Create an object that contains an array. We'll tell the object how big the array should be through the constructor." So my missing line would be something that creates a new array of the specified size.

How do you make a new array? How do you tell it how big to be?

Your answer might be: That wasn't the assignment at all! Then I'll just crawl away.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from sun java tutorial:

Be careful: Programmers new to the Java programming language are tempted to follow length with an empty set of parenthesis. This doesn't work because length is not a method. length is a property provided by the Java platform for all arrays.

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and yet, for Strings, "length" is a method instead of a property. this has tripped me up on more than one occasion!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic