• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiating a user defined object  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • 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



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 291
3
  • Mark post as helpful
  • send pies
  • 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
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks carey and how could i use userinput to define each instance of a new house?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try returning this, which points to the current object.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.

 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 291
3
  • Mark post as helpful
  • send pies
  • 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: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!