• 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

Instantiating a user defined object

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

Merry Christmas!!!

just creating a program that asks the user a couple of questions
house price
house size
contact person (to start,will add more)
then i would like to create house objects with the user input and add the house object to an array
here is what i have so far:

Office class,which is just the main class really



and the house class:



i have been experimenting with the constructor,but just cant seem to get this right...any guidance would be appreiated



 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you didn't define a constructor, you have the default one:



You can keep it like that and set the instance variables directly. Or you can write your own constructor. Which are are you trying to do?
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jeanne,

i want the user to be able to set the instance variables
then add the object to the arraylist
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't have the ArrayList inside the House class. Put it, in your case, main().
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks carey and how could i use userinput to define each instance of a new house?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try returning this, which points to the current object.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that your method is not good practice.  You shouldn't have the IO in the House class.  What if you wanted to add House to a GUI?

What you should do is either add a constructor with the three arguments or add three setters for them.  Either way, you should add three getters so the data can be read.  Then in another class, get the data, create the object, and add it to the List.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, add constructor

Then create some helper methods to display a prompt and then get and return the user input.

Then, using the helpers, get the initializers for a House instance.

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Please note that your method is not good practice.  You shouldn't have the IO in the House class.



And it makes no sense for a House to ask for those things. In a normal object-oriented design it would be the house's owner which determines those things and configures the house accordingly.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi carrey,

thanks for the help.

just regarding this code:



ok,so i understand this is a method called promptInt that requires an integer to be returned,but also requires a String called prompt as input?
i dont understand the String prompt in the brackets...

also your get input...is that where the scanner would go? ...should this method be in the House class or my User class (changed Office to User) as another user on this post said the
questions should be answered by the right post.

thanks everybody for your help...appreciated
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the example, promptInt(String prompt) is called twice and the only thing that differentiates them is the message to be displayed to the user.

Yes, "... get input" is where the scanner stuff goes.

If you are defining the helper methods inside the User class then you can create the scanner once inside that class.

requires a String called prompt as input?


"prompt" is a "parameter", not to be confused with the input that is received from scan.nextInt().
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

just regarding this code:



ok,so i understand this is a method called promptInt that requires an integer to be returned,but also requires a String called prompt as input?
i dont understand the String prompt in the brackets...


I think you have it.  The method promtInt takes a String as an argument.  That argument is call prompt in the method body.  So you might call the method like this:

also your get input...is that where the scanner would go?


Correct.

...should this method be in the House class or my User class (changed Office to User) as another user on this post said the
questions should be answered by the right post.


Each class should be about one thing.  The House class is not a good place for promptInt because it's not about a House.  The same goes for User.  In a small project you might put this method in a "driver" class, the one that has the main() method in it, but really the best place for it is in its own class that is all about getting user input from a non-GUI.
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic