Help with objects in Java

Status
Not open for further replies.

PnkFloyd27

In Runtime
Messages
111
I'm new to Java and I am trying to make a robot fighting game(I know it's cheesy). What I want to do is have each robot be it's own object. I try to do something like this:

if the player's attack# is <= the robot's attack# then they win

The problem I am having is getting the player's attack number. It is in the main class but I don't know how to refer to it in the robot classes. Please help, I'll try to explain it differently if I didn't do a good job of that. Thank you.
 
You should have a game manager class that will host the game, instantiate player/robot objects, and be responsible for starting and stopping games. As long as each robot object publicly exposes its attack number, the game manager class (or subsystem) should have no problem determining the winner. Maybe each robot should send a message to the game manager when its attack number changes. You'll have to decide what design you like best, but you should use a third party (game manager) to keep track of (or monitor) attack numbers.
 
Would the game manager be the one with the main method?

How do I edit a single variable.
i.e.
if the player wins, then they get $200.
How do I edit the money variable that's in a different class?
 
Would the game manager be the one with the main method?

How do I edit a single variable.
i.e.
if the player wins, then they get $200.
How do I edit the money variable that's in a different class?
Understand that I'm not trying to be offensive, but judging from your questions, I think you might want to concentrate on learning the basics of object oriented programming before trying to make a game. How much programming experience do you have?
 
Well, I tried to learn the basics of OOP in java. i.e I know about the constructor and I know how to make methods and how to call on the methods. I just don't know how to change a variable from a different class. I'm going to keep trying different things and trying to look it up on the internet but if someone could please just simply help me out with that I would appreciate it.
 
I know about the constructor and I know how to make methods and how to call on the methods. I just don't know how to change a variable from a different class.
The simple answer is to create an object and call a method on that object that will update the variable. Make sure to check out some books on Java programming at Amazon.com (or whatever online bookstore you prefer).
 
That answer wasn't so simple at first...;)

But...saying the same thing in a different way:

You have two essential options...either have a public variable, or "helper" methods.

A public variable in your 'Player' class might be,
public static int money = 0;

Then you'd change it like,
Player playerOne = new Player( ); // your instantiated object
playerOne.money += 200;

That is the un-preferred method, for people say giving direct access to variables is bad (they could tamper with them in unwanted ways)...the other would be:

A private variable in your 'Player' class might be,
private static int money = 0;

and you have another method in this class to add money,
public static void addMoney( int amount ) {
money += amount;
}

Then to give a player money, you'd just address it with the method instead,
Player playerOne = new Player( ); // your instantiated object
playerOne.addMoney ( 200 );

And to get the money, you'd have a getter method in the Player class,
public static int getMoney( ) {
return money;
}


Both work. :) ...after looking at my explanation though, it's not so simple either, dang it! Anyway, hope it helps, let me know if ye' need something explained.
 
I've been searching around the internet, it's very confusing. What does the return money do?
I think I understand the rest of it though, I'll try it out. Thank you.

Also, what does the private and public and static stuff do?
 

Attachments

  • Robot1.txt
    491 bytes · Views: 78
  • GameManager.txt
    4 KB · Views: 81
I'm getting an error that says:
unreported exception java.io.IOException; must be caught or declared to be thrown
GameManager.ShopOrBattle();
^

What do I do?

I have the method like this:
public void ShopOrBattle() throws IOException

I don't know where to put that "throws IOException" thing.

Edit: I'm having error after error and I don't know how to debug it, so I'm going to upload the files and if anyone feels that they are so inclined, please look at the code and help me out and teach me what I need to do. (possibly with comments)
I have part of the attachments on this post and the rest on the previous one.
 

Attachments

  • Welcome.txt
    238 bytes · Views: 82
  • Robot5.txt
    488 bytes · Views: 82
  • Robot4.txt
    488 bytes · Views: 76
  • Robot3.txt
    488 bytes · Views: 76
  • Robot2.txt
    489 bytes · Views: 66
This page has a good table for the modifiers private/static/protected/none - but basically it sets the scope of your variables/methods. Public can be viewed by your other classes while private will keep to itself and let nobody else at it (directly).

The 'return money' above would be how your main class gets at the money value for the player object. Since in that example the money variable is private (you cannot access it from the main class), you have to use a method to fetch the value for you.

The exceptions...you have two options, either propagate it (throw it up the chain) or catch it. Generally you want to catch it, so you'd surround the line with:
Code:
try {
  // something that throws an exception
} catch ( e ) {
  // you can display the exception (save it to a log, do a custom System.out, or just ignore it
}

or to propagate it, you simply add the "throws IOException" to the class that you're in. Such as if it's the manager, public class Manager throws IOException { ... that will toss it up to Java.

(I didn't look at your attachments, no time now, but later if things aren't working I can.:))
 
Status
Not open for further replies.
Back
Top Bottom