Your current code (as posted in comments)
private void read() {
System.out.println(
"Enter the amount of liquid in the bottle (0-100): ");
volume = keyboard.nextDouble();
if (volume > MAX || volume < MIN) { read(); } }
}
This initializes your object by setting volume
.
You can call this from the class constructor:
public Lotion(){
read();
}
This way, when you create instances, it will automatically ask for user input and set the answer to an instance field.
Some design notes (but don't get into a fight over it with your instructor):
- "heavy lifting" like user input should probably not be done as part of the constructor.
- if you are going to call methods from the constructor, making them private is a good idea. Otherwise they can be overridden, and then will be called on a not completely constructed object instance.
read
could return a double
instead of updating the object state directly.