#Java Input for Objects

26 messages · Page 1 of 1 (latest)

cerulean canyon
#

How do you assign inputs to an object? I'm trying to do that for the first part of this project and I can't exactly figure it out.

rain hollyBOT
#

This post has been reserved for your question.

Hey @cerulean canyon! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

thick pike
#

Normally I'd say however you want. And yet, you should probably avoid protocols and user inputs inside constructors

#

you could first demand the user enters the needed input into some variables, and then once that's done, you can build the object with these variables

cerulean canyon
#

What about the UML diagram?

#

Doesn’t it show that the variables need to be in the constructor?

#

@thick pike

thick pike
#

You're allowed to have other variables. Exclusivity isn't all that important in programming

cerulean canyon
#

so make a main class?

thick pike
#

Or anything else you see fit, just, probably not in the constructor

cerulean canyon
#

it doesnt seem i can have the inputs outside the constructor

thick pike
#

Honestly not sure what you mean

cerulean canyon
#

i tried having the variables outside the constructor and it doesn't look it goes well

thick pike
#

So. As I said. In addition to the variables you have in the class and the constructor, you're allowed to have other variables too

cerulean canyon
#

hmm

#

not to be mean but ur kinda confusing me with that

thick pike
#

So, first go back to what you had initially

#

Then. When you want to have variables to store user input, it doesn't need to be these variables. You can have other variables, these other ones won't be in the constructor, nor even in this class

#

And using these other variables, you'll call the constructor, to transmit the value to that class

cerulean canyon
#

like this @thick pike ?

thick pike
#

toString()? How would that relate

#

Okay, for now on, you'll make as if this class doesn't exist

#

Now you'll make a method, elsewhere, that asks the user to input 3 things: A name, a price and an amount

cerulean canyon
thick pike
#

I meant the whole class