Help with objects in Java

Status
Not open for further replies.
public class Manager throws IOException
That doesn't work, I tried that before, it just comes up with an error. I do

import java.io.*;

public class GameManager throws IOException {

private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

//my code

}

the error says that it expects a '{' do be right after GameManager and then calls the throw IOException an illegal start of type.
 
Oops, my mistake... I think. You can definitely add the "throws IOException" to a method, hmm, maybe you can just put it in the constructor like this:
Code:
import java.io.*;

public class GameManager {

private static BufferedReader stdin;

  public GameManager( ) throws IOException {
    stdin = new BufferedReader(new InputStreamReader(System.in));
  }

}
Sorry, I haven't touched this type of Java in a couple months...:)
 
I made a bunch of the methods public and static. It compiles now, but when I run Welcome.class, the one with the main method, I get this error:
at Robot1.<init>(Robot1.java:11)
at GameManager.<init>(GameManager.java:19)

over and over and over again. GAH!

Edit: Here are my updates.
 

Attachments

  • GameManager.txt
    4.2 KB · Views: 37
  • Robot1.txt
    611 bytes · Views: 32
  • Robot2.txt
    609 bytes · Views: 35
  • Robot3.txt
    608 bytes · Views: 35
  • Robot4.txt
    608 bytes · Views: 25
Wait, why do you have Robot1 - Robot5? You only need one Robot class, that is the advantage of objects. :) I guess the attacks are different, but you could have a list of some sort and still only use one Robot. Anyhow, your current problem is just a simple oversight...

You usually want to look at the top-most error, in which your error says Robot1.java:11 - so you want to look in Robot1.java on line 11:
Code:
GameManager = new GameManager();
See somethin' missing? There's no reference name after GameManager. :)
 
Would I make all of the different Robots their own method or how would I do that?

update:
Code:
class Welcome {

	public static void main(String[] args){
	
		GameManager GameManager = new GameManager();
		
		System.out.println("::Welcome to RoboMadness::\n");
		GameManager.Status();
		GameManager.ShopOrBattle();
	}
}

Code:
import java.io.*;

class GameManager {

	private static BufferedReader stdin;

	GameManager GameManager = new GameManager();
	Robot Robot = new Robot(1,2,3,4,5,0,1,2,3,4);

	GameManager() throws IOException {
		stdin = new BufferedReader(new InputStreamReader(System.in));
		money = 200;
		attack = 0;
		defense = 0;
		lives = 3;
	}

	void AddAttack(int amount) {
		attack =+ amount;
	}

	void AddDefense(int amount) {
		defense =+ amount;
	}

	void Lives() {
		lives = lives - 1;

		if (lives == 0) {
			System.out.println("Game Over");
		} else {
		}
	}

	void MinusMoney(int amount) {
		money =+ amount;
	}

	void AddMoney(int amount) {
		money += amount;
	}

	void Status() {
	System.out.println("Current Status");
	System.out.println("Lives: "+lives);
	System.out.println("Money: $"+money);
	System.out.println("Attack: "+attack);
	System.out.println("Defense: "+defense);

	}

	void ShopOrBattle() {
	System.out.print("Would you like battle or shop?");
	String ShopBattle = stdin.readLine();

	if (ShopBattle.equals("battle")||ShopBattle.equals("Battle")) {
		System.out.println("Which robot would you like to battle?\n");
		System.out.println("Robot 1");
		System.out.println("Robot 2");
		System.out.println("Robot 3");
		System.out.println("Robot 4");
		System.out.println("Robot 5");

		String robotString = stdin.readLine();
		int robotInt = Integer.parseInt(robotString);

		if (robotInt == 1) {
			System.out.println("\nBattling Robot 1");
			Robot.Robot1();
		} else if (robotInt == 2) {
			System.out.println("\nBattling Robot 2");
			Robot.Robot2();
		} else if (robotInt == 3) {
			System.out.println("\nBattling Robot 3");
			Robot.Robot3();
		} else if (robotInt == 4) {
			System.out.println("\nBattling Robot 4");
			Robot.Robot4();
		} else if (robotInt == 5) {
			System.out.println("\nBattling Robot 5");
			Robot.Robot5();
		} else {
			System.out.println("Please enter the number of the robot you wish to battle");
			GameManager.ShopOrBattle();
		}
	} else if (ShopBattle.equals("shop")||ShopBattle.equals("Shop")) {
		System.out.println("This is the Shop, what would you like to purchase?\n");
		System.out.println("1. Armour $100");
		System.out.println("2. Sword $200");
		System.out.println("3. Chain $300");
		System.out.println("4. Gun $400");
		String buy1 = stdin.readLine();
		int buy = Integer.parseInt(buy1);

			if (buy == 1){
				if (money >= 100) {
					defense = defense++;
					money = money - 100;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			} else if (buy == 2) {
				if (money >= 200) {
					attack = attack ++   		;
					money = money - 200;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			} else if (buy == 3) {
				if (money >= 300) {
					attack = attack + 2;
					money = money - 300;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			} else if (buy == 3) {
				if (money >= 300) {
					attack = attack + 3;
					money = money - 300;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			} else if (buy == 4) {
				if (money >= 400) {
					attack = attack + 4;
					money = money - 400;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			}
	} else {
		System.out.println("Please enter either shop or battle");
		ShopBattle = stdin.readLine();
		GameManager.ShopOrBattle();
	}

}


}

Code:
class Robot {

	GameManager GameManager = new GameManager();
	
	Robot(int Robot1Attack, int Robot2Attack, int Robot3Attack, int Robot4Attack, int Robot5Attack, int Robot1Defense, int 		Robot2Defense, int Robot3Defense, int Robot4Defense, int Robot5Defense) {
	Robot1Attack = 0;
	Robot1Defense = 0;
	Robot2Attack = 0;
	Robot2Defense = 0;
	Robot3Attack = 0;
	Robot3Defense = 0;
	Robot4Attack = 0;
	Robot4Defense = 0;
	Robot5Attack = 0;
	Robot5Defense = 0;
	}
	
	void Robot1(){
		if (GameManager.attack >= Robot1Attack && GameManager.defense >= Robot1Defense) {
			System.out.println("You won the battle and won $200");
			GameManager.AddMoney(200);
		} else {
			System.out.println("You lost");
			GameManager.Lives();
		}
	}
	
	void Robot2() {
		if (GameManager.attack >= Robot2Attack && GameManager.defense >= Robot2Defense) {
			System.out.println("You won the battle and won $300");
			GameManager.AddMoney(300);
		} else {
			System.out.println("You lost");
			GameManager.Lives();
		}
	}
	
	void Robot3(){
		if (GameManager.attack >= Robot3Attack && GameManager.defense >= Robot3Defense) {
			System.out.println("You won the battle and won $400");
			GameManager.AddMoney(400);
		} else {
			System.out.println("You lost");
			GameManager.Lives();
		}
	}
	
	void Robot4(){
		if (GameManager.attack >= Robot4Attack && GameManager.defense >= Robot4Defense) {
			System.out.println("You won the battle and won $500");
			GameManager.AddMoney(500);
		} else {
			System.out.println("You lost");
			GameManager.Lives();
		}
	}
	
	void Robot5(){
		if (GameManager.attack >= Robot5Attack && GameManager.defense >= Robot5Defense) {
			System.out.println("You won the battle and won $400");
			GameManager.AddMoney(400);
		} else {
			System.out.println("You lost");
			GameManager.Lives();
		}
	}
	

}
 
Hmm, there's an endless number of ways to do it, but you wouldn't want that as it's still not taking advantage of the objects (you'd only have one Robot with your above code).

Instead, in Robot you'd probably want something like:

Code:
private class Robot {
  private static int attack;
  private static int defense;

  public Robot( int attack, int defense ) {
    this.attack = attack;
    this.defense = defense;
  }
  
  .
  .
  .
}

Then for each robot you create, it'd just be Robot myRobot = new Robot( 1, 4 ); and you can create a whole army of them!

I'm not sure how you're writing this, but I don't think GameManager will be doing what you think/want it to, but I guess first things first. ;)
 
I don't understand the static stuff. I understand the public and private stuff, but when do I use "static?" I found this but it doesn't make much sense to me:
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object.

I really have no idea what to do with this. Should I change the way I made the constructor?
Code:
public class Shop {


	
	Shop(int PlayerMoney, int PlayerDefense, int PlayerAttack) {
		private static GameManager GameManager = new GameManager();
		PlayerMoney = GameManager.money();
		PlayerDefense = GameManager.defense();
		PlayerAttack = GameManager.attack();
	}
			
		void Market() {	
		
			System.out.println("This is the Shop, what would you like to purchase?\n");
			System.out.println("1. Armour $100");
			System.out.println("2. Sword $200");
			System.out.println("3. Chain $300");
			System.out.println("4. Gun $400");
			String buy1 = stdin.readLine();
			int buy = Integer.parseInt(buy1);
			
			if (buy == 1){
				if (money >= 100) {
					PlayerDefense = PlayerDefense++;
					PlayerMoney = PlayerMoney - 100;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			} else if (buy == 2) {
				if (money >= 200) {
					PlayerAttack = PlayerAttack ++   		;
					PlayerMoney = PlayerMoney - 200;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			} else if (buy == 3) {
				if (money >= 300) {
					PlayerAttack = PlayerAttack + 2;
					PlayerMoney = PlayerMoney - 300;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			} else if (buy == 3) {
				if (money >= 300) {
					PlayerAttack = PlayerAttack + 3;
					PlayerMoney = PlayerMoney - 300;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			} else if (buy == 4) {
				if (money >= 400) {
					PlayerAttack PlayerAttack + 4;
					PlayerMoney = PlayerMoney - 400;
					System.out.println("Purchase Successful");
				} else {
					System.out.println("You do not have enough money");
				}
			}
		}

}

Also, when I want to leave a variable alone when making an object, do I just put the name of the variable? Also, when I change the PlayerAttack, PlayerDefense, and PlayerMoney variables, am I doing it right?

P.S. I appreciate your help very very much. I am trying to look my questions up online but they leave me in the dust sometimes. =x

Another thing, if I don't make those robot methods, where do I put the if statements? Edit: OK, never mind that, I made it into one if statement. But, I want to give players a different amount of money when they win so how do I do that for the different robots?
 
Hmm, I guess first - can players have more then one robot? It sounds like you might want another class to make up:

GameManager
addPlayer()
attack()
Player
name
robots
createRobot()
destroyRobot()
Robot
damage

Obviously stuff is missing, but if you can create a layout like that, it's much easier to write the whole thing (instead of rewriting areas like you are now!).


Re the static stuff, I agree completely...there are no good descriptions that I've found. The best way I know of describing it though (and it's not very good), would be:
if you have:
Code:
public class Test {
   public int myMoney = 0;
 
   public void getMyMoney( ) {
     return 1+ 1;
   }
 }
You can only get at those variables/methods with an instance of the class. That is to say,
Test one = new Test( );
System.out.println( one.myMoney + " " + one.getMyMoney( ) );

However if you had this:
Code:
public class Test {
  public [b]static[/b] int myMoney = 0;

  public [b]static[/b] void getMyMoney( ) {
    return myMoney;
  }
}
You could get at those items without instantiating the object. Instead of having to make a "new" object, you could just:
System.out.println( Test.myMoney );
System.out.println( Test.getMyMoney( ) );

--like the Math class. You can use methods like absolute value without creating an object. That's what the static allows you to do...if that makes any sense!
 
Status
Not open for further replies.
Back
Top Bottom