Lauren S. answered 08/16/23
Lab Tech/Teaching Assistant | Java, Python, Math
Your exact request - to alter the position and climbingUp variables within the class itself - you’re already doing. That’s why you can call Monkey.position in main and get 3 or -2 or whatever number represents what you’ve done with this monkey, rather than getting 0 each time. In your main method, you’re creating an instance of the class with these private variables set originally to 0 and true, and then user input directly changes those variables through the formulas you’ve defined. Your class’s variables already update based on the case formulas - at least that instance of the class.
If you want the user input to affect the class’s structure more generally, though - not just this instance of the class, but future classes as well - this could mean several things. Do you want to be able to close the program entirely, come back, and resume where you left off with the monkey’s position? Or do you want to be able to create multiple monkeys in the same program, one starting where the other leaves off (not always starting at 0 and true)?
If it’s the first option, the easiest way to do this would probably be to write the monkey’s position and direction to an internal text file (either each time it’s updated or when the user chooses the exit option), and then load in that position and direction in the constructor. Here’s an article with different ways to write into a file: https://www.geeksforgeeks.org/java-program-to-write-into-a-file/ - I would recommend FileWriter if you’re only updating on program close, and BufferedWriter if you’re updating more often. This way, if you create an instance of Monkey and make it climb up four units and turn around, and then you close the program to go get lunch and accidentally delete your instance of Monkey, you can open the program again later, create a new instance, and the Monkey will still be in position 4 with climbingUp = false. This is unusual behavior for this type of class, but it's definitely possible.
If instead you just want to be able to create multiple monkeys in the same program, one starting where the other leaves off, I’d recommend adding parameters for position and climbingUp to your constructor, instead of hardcoding them in. It would look like this:
public Monkey(int position, boolean climbingUp)
{
this.position = position;
this.climbingUp = climbingUp;
}
and you would then create your first monkey with
Monkey monkey1 = new Monkey(0, true);
and your second monkey (wherever you want to use that) with
Monkey monkey2 = new Monkey(monkey1.position, monkey1.climbingUp);
For most intents and purposes, you only really need to alter the variables in your own class instance, which you’re already doing. There are ways I’ve described above of changing how future instances are built from this one, so if that’s something you want to implement, there’s that option. Overall, though, your code looks fine as-is.
Let me know if there’s part of this answer I can clarify further, or if I’ve misinterpreted what you’re asking. Thanks for reaching out!