Help with objects in Java - Techist - Tech Forum

Go Back   Techist - Tech Forum > Computer Software > Programming
Click Here to Login
Closed Thread
 
Thread Tools Display Modes
 
Old 06-13-2007, 04:34 PM   #1 (permalink)
True Techie
 
Join Date: Nov 2006
Posts: 111
Default Help with objects in Java

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.
__________________

__________________
PnkFloyd27 is offline  
Old 06-13-2007, 04:41 PM   #2 (permalink)
Software Developer
 
Join Date: Mar 2006
Location: USA
Posts: 632
Default Re: Help with objects in Java

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.
__________________

__________________
jaeusm is offline  
Old 06-13-2007, 05:27 PM   #3 (permalink)
True Techie
 
Join Date: Nov 2006
Posts: 111
Default Re: Help with objects in Java

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?
__________________
PnkFloyd27 is offline  
Old 06-13-2007, 08:41 PM   #4 (permalink)
Software Developer
 
Join Date: Mar 2006
Location: USA
Posts: 632
Default Re: Help with objects in Java

Quote:
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?
__________________
jaeusm is offline  
Old 06-14-2007, 06:42 AM   #5 (permalink)
True Techie
 
Join Date: Nov 2006
Posts: 111
Default Re: Help with objects in Java

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.
__________________
PnkFloyd27 is offline  
Old 06-14-2007, 08:01 AM   #6 (permalink)
Software Developer
 
Join Date: Mar 2006
Location: USA
Posts: 632
Default Re: Help with objects in Java

Quote:
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).
__________________
jaeusm is offline  
Old 06-14-2007, 01:08 PM   #7 (permalink)
Monster Techie
 
Join Date: May 2004
Location: USA
Posts: 1,178
Default Re: Help with objects in Java

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.
__________________
Vormund is offline  
Old 06-14-2007, 01:22 PM   #8 (permalink)
True Techie
 
Join Date: Nov 2006
Posts: 111
Default Re: Help with objects in Java

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?
Attached Files
File Type: txt Robot1.txt (491 Bytes, 78 views)
File Type: txt GameManager.txt (4.0 KB, 81 views)
__________________
PnkFloyd27 is offline  
Old 06-14-2007, 01:42 PM   #9 (permalink)
True Techie
 
Join Date: Nov 2006
Posts: 111
Default Re: Help with objects in Java

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.
Attached Files
File Type: txt Welcome.txt (238 Bytes, 82 views)
File Type: txt Robot5.txt (488 Bytes, 82 views)
File Type: txt Robot4.txt (488 Bytes, 76 views)
File Type: txt Robot3.txt (488 Bytes, 76 views)
File Type: txt Robot2.txt (489 Bytes, 66 views)
__________________
PnkFloyd27 is offline  
Old 06-14-2007, 04:40 PM   #10 (permalink)
Monster Techie
 
Join Date: May 2004
Location: USA
Posts: 1,178
Default Re: Help with objects in Java

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.)
__________________

__________________
Vormund is offline  
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Question PnkFloyd27 Programming 4 06-07-2007 03:30 PM
Java SCript Help NEEDED! kellee91 Internet Software and Browsers 1 06-05-2007 11:20 AM
Java problem JHC130 Internet Software and Browsers 7 04-19-2007 05:02 PM
how do you install java in ubuntu 6.10 straightv6 Linux and Open Source 1 04-10-2007 10:15 PM
n00b - java array mitzi Programming 3 03-27-2007 06:36 PM



Copyright 2002- Social Knowledge, LLC All Rights Reserved.

All times are GMT -5. The time now is 10:25 AM.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2018, vBulletin Solutions, Inc.