Results 1 to 15 of 15
Like Tree3Likes
  • 1 Post By Zyril
  • 1 Post By awinston
  • 1 Post By Tolls

Thread: Constructor/Object Help

  1. #1
    MastaChief11 is offline Member
    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0

    Default Constructor/Object Help

    A couple days ago, I asked help on a mini RPG I was making. I've continued to develop it, but now I'm trying to not hard code the whole thing. I want the class you choose to be set up in the Character class so it will be easier to code later on. This is my primary class:

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class adventure {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//str = strength
    		//def = defense
    		//mmagic = defense
    		//t = temporary
    		
    		int character;
    		int decision;
    		int fight;
    		Scanner input = new Scanner (System.in);
    		Random r = new Random();
    		
    		int str = 1;
    		int def = 2;
    		int magic = 3;
    		int speed = 4;
    		int health = 5;
    		
    		//Temporary attributes
    		int t_str = 1;
    		int t_def = 2;
    		int t_magic = 3;
    		int t_speed = 4;
    		int t_health = 5;
    		
    		//Mage attributes
    		int m_str = 13;
    		int m_def = 9;
    		int m_magic = 12;
    		int m_speed = 27;
    		int m_health = 55;
    		
    		//Samurai attributes
    		int s_str = 17;
    		int s_def = 5;
    		int s_magic = 5;
    		int s_speed = 20;
    		int s_health = 51;
    		
    		//Knight attributes
    		int k_str = 20;
    		int k_def = 3;
    		int k_magic = 2;
    		int k_speed = 15;
    		int k_health = 47;
    		
    		//Enchanted Dummy attributes
    		int d_str = 12;
    		int d_def = 5;
    		int d_magic = 1;
    		int d_speed = 4;
    		int d_health = 30;
    		String d_name = "Enchanted Dummy";
    		
    		//Racaria attributes
    		int r_str = 14;
    		int r_def = 8;
    		
    		//Random CPU fight choice
    		int random_num = r.nextInt(2);
    		
    		System.out.println("Hello, and welcome to Craig's Mini RPG. \nPre-alpha 0.1 \nPlease pick a class: \n \n<1> Mage: \n \nStrength: " + m_str + "\nDefense: " + m_def + "\nMagic: " + m_magic + "\nSpeed: " + m_speed + "\nHealth: " + m_health + "\n \n<2> Samurai: \n \nStrength: " + s_str + "\nDefense: " + s_def + "\nMagic: " + s_magic + "\nSpeed: " + s_speed + "\nHealth: " + s_health + "\n \n<3> Knight: \n \nStrength: " + k_str + "\nDefense: " + k_def + "\nMagic: " + k_magic + "\nSpeed: " + k_speed + "\nHealth: " + k_health);
    		int characterOption = input.nextInt();
    		Character player;
    		
    		switch(character){
    		
    		case 1:
    			player = new Character(13, 9, 12, 27, 55);
    			break;
    		
    		}
    		
    		if(character == 1){
    			System.out.println("You have chosen the Mage class. What would you like to do now? <1> Go to the arena <2> Go to the store");
    			decision = input.nextInt();
    			str = m_str;
    			def = m_def;
    			magic = m_magic;
    			speed = m_speed;
    			health = m_health;
    			
    			t_str = m_str;
    			t_def = d_def;
    			t_magic = m_magic;
    			t_speed = m_speed;
    			t_health = m_health;
    			
    			if(decision == 1){
    				System.out.println("Who would you like to fight? \n \n<1> Enchanted Dummy: \n \nStrength: " + d_str + "\nDefense: " + d_def + "\nMagic: " + d_magic + "\nSpeed: " + d_speed + "\nHealth: " + d_health + "\n \n<2> Racaria");
    				decision = input.nextInt();
    				
    			if(decision == 1){//Fight Enchanted Dummy
    				
    				while(d_health > 0){
    					
    					System.out.println("You must now fight the " + d_name + "\n \nMage stats: \n \nStrength: " + t_str + "\nDefense: " + t_def + "\nMagic: " + t_magic + "\nSpeed: " + t_speed + "\nHealth: " + t_health + "\n \n" + d_name + "stats: \n \nStrength: " + d_str + "\nDefense: " + d_def + "\nMagic: " + d_magic + "\nSpeed: " + d_speed + "\nHealth: " + d_health + "\nWhat would you like to do? <1> Attack <2> Magic");
    					fight = input.nextInt();
    					
    					if(fight == 1){
    						d_health -= (t_str - d_def);
    					}
    					
    					System.out.println("Now the " + d_name + " has " + d_health);
    					
    					if(random_num == 0){//CPU Attack
    						
    					}
    					
    					if(d_health < 0){
    						break;
    					}
    				
    				}
    				
    				System.out.println("Copngratulations! You beat the " + d_name + "!");
    				
    			}
    				
    			}
    			
    			else if(decision == 2){
    				System.out.println("Welcome to the store! What would you like to buy?");
    			}
    			
    		}
    		
    		if(character == 2){
    			System.out.println("You have chosen the Samurai class. What would you like to do now? <1> Go to the arena <2> Go to the store");
    			decision = input.nextInt();
    			str = s_str;
    			def = s_def;
    			magic = s_magic;
    			speed = s_speed;
    			health = s_health;
    			
    			t_str = s_str;
    			t_def = s_def;
    			t_magic = s_magic;
    			t_speed = s_speed;
    			t_health = s_health;
    			
    			if(decision == 1){
    				System.out.println("Who would you like to fight? <1> Enchanted Dummy <2> Racaria");
    				decision = input.nextInt();
    				
    				if(decision == 1){
    					System.out.println("You must now fight the " + d_name + "\n \nSamurai stats: \n \nStrength: " + t_str + "\nDefense: " + t_def + "\nMagic: " + t_magic + "\nSpeed: " + t_speed + "\nHealth: " + t_health);
    				}
    				
    			}
    			
    			else if(decision == 2){
    				System.out.println("Welcome to the store! What would you like to buy?");
    			}
    			
    		}
    		
    		if(character == 3){
    			System.out.println("You have chosen the Knight class. What would you like to do now? <1> Go to the arena <2> Go to the store");
    			decision = input.nextInt(); 
    			str = k_str;
    			def = k_def;
    			magic = k_magic;
    			speed = k_speed;
    			health = k_health;
    			
    			t_str = k_str;
    			t_def = k_def;
    			t_magic = k_magic;
    			t_speed = k_speed;
    			t_health = k_health;
    			
    			if(decision == 1){
    				System.out.println("Who would you like to fight? <1> Enchanted Dummy <2> Racaria");
    				decision = input.nextInt();
    				
    				if(decision == 1){
    					System.out.println("You must now fight the " + d_name + "\n \nKnight stats: \n \nStrength: " + t_str + "\nDefense: " + t_def + "\nMagic: " + t_magic + "\nSpeed: " + t_speed + "\nHealth: " + t_health);
    				}
    				
    			}
    			
    			else if(decision == 2){
    				System.out.println("Welcome to the store! What would you like to buy?");
    			}
    			
    		}
    
    	}
    
    }
    This is the code for my secondary class:

    Java Code:
    public class Character {
    
    	int strength;
    	int defense;
    	int magic;
    	int speed;
    	int health;
    
    	void Class(int strength, int defense, int magic, int speed, int health){
    	// The constructor, used to populate the attributes of this class
    	this.strength = strength;
    	this.defense = defense;
    	this.magic = magic;
    	this.speed = speed;
    	this.health = health;
    
    	}
    	
    }
    Help is greatly appreciated!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Constructor/Object Help

    And what problem are you having?
    Are you saying you would like to create a CharacterClass class?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    MastaChief11 is offline Member
    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0

    Default Re: Constructor/Object Help

    Quote Originally Posted by Tolls View Post
    And what problem are you having?
    Are you saying you would like to create a CharacterClass class?
    Right here I am getting an error that says I need to remove arguments to match Character(). So I think I have to make an object or constructor (not sure which) to fix that problem. Currently, I have 2 classes, 1 class for the game, and another class for the class/character that you want to play as (Mage, Samurai, or Knight).

    Java Code:
    switch(character){
             
            case 1:
                player = new Character(13, 9, 12, 27, 55);
                break;
             
            }

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Constructor/Object Help

    You want to create a new object of the Character class, that is what your code is saying, but without a constructor for the class you want to create an object of, this can't be done. Since you are missing the constructor, this is the first step to fixing your code.

    Initialize your variable "character" that is used for the switch as well, you don't seem to do that anywhere and this will give you a compilation error.

    Java Code:
    int character = 1;
    
    --------
    
        /**
         * Constructor for the class character.
         * @param strength
         * @param defense
         * @param magic
         * @param speed
         * @param health
         */
        public Character(int strength, int defense, int magic, int speed, int health){
        // The constructor, used to populate the attributes of this class
        this.strength = strength;
        this.defense = defense;
        this.magic = magic;
        this.speed = speed;
        this.health = health;
        }
    Now your code should work and run as you wish.
    Last edited by Zyril; 08-10-2012 at 05:20 PM.
    MastaChief11 likes this.

  5. #5
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Constructor/Object Help

    First off, the name of a constructor needs to be the name of its class. In your case, that would be "Character."

    Secondly, don't specify a return type for a constructor.

    More info: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Zyril likes this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  6. #6
    MastaChief11 is offline Member
    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0

    Default Re: Constructor/Object Help

    Quote Originally Posted by Zyril View Post
    You want to create a new object of the Character class, that is what your code is saying, but without a constructor for the class you want to create an object of, this can't be done. Since you are missing the constructor, this is the first step to fixing your code.

    Initialize your variable "character" that is used for the switch as well, you don't seem to do that anywhere and this will give you a compilation error.

    Java Code:
    int character = 1;
    
    --------
    
        /**
         * Constructor for the class character.
         * @param strength
         * @param defense
         * @param magic
         * @param speed
         * @param health
         */
        public Character(int strength, int defense, int magic, int speed, int health){
        // The constructor, used to populate the attributes of this class
        this.strength = strength;
        this.defense = defense;
        this.magic = magic;
        this.speed = speed;
        this.health = health;
        }
    Now your code should work and run as you wish.
    Edit: Okay, I tried it and it worked. Now, how do I get the stats? For example, in a fight, how would I call on the players defense, health, etc?
    Last edited by MastaChief11; 08-10-2012 at 05:27 PM.

  7. #7
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Constructor/Object Help

    Read what awinston wrote. How is a constructor built, and how do you use it?

    Notice that the code I posted is actually YOUR code, only that I have changed the name and made it into a constructor. That is all you have to do really. As awinston also said, a constructor doesn't have a return type so there is no need for void as you see.

    About initializing the variable character; where is it declared and when is it used? Think about when the variable has to be initialized so your code will run.

  8. #8
    MastaChief11 is offline Member
    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0

    Default Re: Constructor/Object Help

    Quote Originally Posted by Zyril View Post
    Read what awinston wrote. How is a constructor built, and how do you use it?

    Notice that the code I posted is actually YOUR code, only that I have changed the name and made it into a constructor. That is all you have to do really. As awinston also said, a constructor doesn't have a return type so there is no need for void as you see.

    About initializing the variable character; where is it declared and when is it used? Think about when the variable has to be initialized so your code will run.
    I changed the code to this like you said, fixing one error:

    Java Code:
    public class Character {
    	
    	int character = 1;
    	
    	int strength;
    	int defense;
    	int magic;
    	int speed;
    	int health;
    
    	public Character(int strength, int defense, int magic, int speed, int health){
    	// The constructor, used to populate the attributes of this class
    	this.strength = strength;
    	this.defense = defense;
    	this.magic = magic;
    	this.speed = speed;
    	this.health = health;
    
    	}
    	
    }
    However, here: when I type in 1, it doesn't say anything back to me.

    Java Code:
    System.out.println("Hello, and welcome to Craig's Mini RPG. \nPre-alpha 0.1 \nPlease pick a class: \n \n<1> Mage: \n \nStrength: " + m_str + "\nDefense: " + m_def + "\nMagic: " + m_magic + "\nSpeed: " + m_speed + "\nHealth: " + m_health + "\n \n<2> Samurai: \n \nStrength: " + s_str + "\nDefense: " + s_def + "\nMagic: " + s_magic + "\nSpeed: " + s_speed + "\nHealth: " + s_health + "\n \n<3> Knight: \n \nStrength: " + k_str + "\nDefense: " + k_def + "\nMagic: " + k_magic + "\nSpeed: " + k_speed + "\nHealth: " + k_health);
    		int characterOption = input.nextInt();
    		Character player;
    		
    		switch(character){
    		
    		case 1:
    			player = new Character(13, 9, 12, 27, 55);
    			System.out.println("Test");
    			break;
    		
    		}

  9. #9
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Constructor/Object Help

    Shouldn't the argument of your switch statement be characterOption, not character? There isn't even a variable named "character" within the scope of class adventure.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  10. #10
    MastaChief11 is offline Member
    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0

    Default Re: Constructor/Object Help

    Quote Originally Posted by awinston View Post
    Shouldn't the argument of your switch statement be characterOption, not character? There isn't even a variable named "character" within the scope of class adventure.
    Thanks, that fixed one warning right here:

    Java Code:
    int characterOption = input.nextInt();
    Now this needs to be fixed:

    Java Code:
    Character player;
    This is all the code in that area:

    Java Code:
    System.out.println("Hello, and welcome to Craig's Mini RPG. \nPre-alpha 0.1 \nPlease pick a class: \n \n<1> Mage: \n \nStrength: " + m_str + "\nDefense: " + m_def + "\nMagic: " + m_magic + "\nSpeed: " + m_speed + "\nHealth: " + m_health + "\n \n<2> Samurai: \n \nStrength: " + s_str + "\nDefense: " + s_def + "\nMagic: " + s_magic + "\nSpeed: " + s_speed + "\nHealth: " + s_health + "\n \n<3> Knight: \n \nStrength: " + k_str + "\nDefense: " + k_def + "\nMagic: " + k_magic + "\nSpeed: " + k_speed + "\nHealth: " + k_health);
    		int characterOption = input.nextInt();
    		Character player;
    		
    		switch(characterOption){
    		
    		case 1:
    			player = new Character(13, 9, 12, 27, 55);
    			System.out.println("Test");
    			break;
    		
    		}

  11. #11
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Constructor/Object Help

    What exactly needs to be fixed? Are you receiving an error?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  12. #12
    MastaChief11 is offline Member
    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0

    Default Re: Constructor/Object Help

    Quote Originally Posted by awinston View Post
    What exactly needs to be fixed? Are you receiving an error?
    I fixed all the errors in the program, so now it works correctly. Now, I need help converting my old code to be shorter and use the constructor:

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class adventure {
    
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    //str = strength
    //def = defense
    //mmagic = defense
    //t = temporary
    
    int character = 0;
    int decision;
    int fight;
    Scanner input = new Scanner (System.in);
    Random r = new Random();
    
    int str = 1;
    int def = 2;
    int magic = 3;
    int speed = 4;
    int health = 5;
    
    int t_str = 1;
    int t_def = 2;
    int t_magic = 3;
    int t_speed = 4;
    int t_health = 5;
    
    //Mage attributes
    int m_str = 13;
    int m_def = 9;
    int m_magic = 12;
    int m_speed = 27;
    int m_health = 55;
    
    //Samurai attributes
    int s_str = 17;
    int s_def = 5;
    int s_magic = 5;
    int s_speed = 20;
    int s_health = 51;
    
    //Knight attributes
    int k_str = 20;
    int k_def = 3;
    int k_magic = 2;
    int k_speed = 15;
    int k_health = 47;
    
    //Enchanted Dummy attributes
    int d_str = 12;
    int d_def = 5;
    int d_magic = 1;
    int d_speed = 4;
    int d_health = 30;
    String d_name = "Enchanted Dummy";
    
    //Racaria attributes
    int r_str = 14;
    int r_def = 8;
    
    //Random CPU fight choice
    int random_num = r.nextInt(2);
    
    System.out.println("Hello, and welcome to Craig's Mini RPG. \nPre-alpha 0.1 \nPlease pick a class: \n \n<1> Mage: \n \nStrength: " + m_str + "\nDefense: " + m_def + "\nMagic: " + m_magic + "\nSpeed: " + m_speed + "\nHealth: " + m_health + "\n \n<2> Samurai: \n \nStrength: " + s_str + "\nDefense: " + s_def + "\nMagic: " + s_magic + "\nSpeed: " + s_speed + "\nHealth: " + s_health + "\n \n<3> Knight: \n \nStrength: " + k_str + "\nDefense: " + k_def + "\nMagic: " + k_magic + "\nSpeed: " + k_speed + "\nHealth: " + k_health);
    int characterOption = input.nextInt();
    Character player;
    
    switch(characterOption){
    
    case 1:
    player = new Character(13, 9, 12, 27, 55);
    System.out.println("Test");
    break;
    
    }
    
    if(character == 1){
    System.out.println("You have chosen the Mage class. What would you like to do now? <1> Go to the arena <2> Go to the store");
    decision = input.nextInt();
    
    if(decision == 1){
    System.out.println("Who would you like to fight? \n \n<1> Enchanted Dummy: \n \nStrength: " + d_str + "\nDefense: " + d_def + "\nMagic: " + d_magic + "\nSpeed: " + d_speed + "\nHealth: " + d_health + "\n \n<2> Racaria");
    decision = input.nextInt();
    
    if(decision == 1){//Fight Enchanted Dummy
    
    while(d_health > 0){
    
    System.out.println("You must now fight the " + d_name + "\n \nMage stats: \n \nStrength: " + str + "\nDefense: " + def + "\nMagic: " + magic + "\nSpeed: " + speed + "\nHealth: " + health + "\n \n" + d_name + "stats: \n \nStrength: " + d_str + "\nDefense: " + d_def + "\nMagic: " + d_magic + "\nSpeed: " + d_speed + "\nHealth: " + d_health + "\nWhat would you like to do? <1> Attack <2> Magic");
    fight = input.nextInt();
    
    if(fight == 1){
    d_health -= (str - d_def);
    }
    
    System.out.println("Now the " + d_name + " has " + d_health);
    
    if(random_num == 0){//CPU Attack
    
    }
    
    if(d_health < 0){
    break;
    }
    
    }
    
    System.out.println("Copngratulations! You beat the " + d_name + "!");
    
    }
    
    }
    
    else if(decision == 2){
    System.out.println("Welcome to the store! What would you like to buy?");
    }
    
    }
    
    if(character == 2){
    System.out.println("You have chosen the Samurai class. What would you like to do now? <1> Go to the arena <2> Go to the store");
    decision = input.nextInt();
    str = s_str;
    def = s_def;
    magic = s_magic;
    speed = s_speed;
    health = s_health;
    
    t_str = s_str;
    t_def = s_def;
    t_magic = s_magic;
    t_speed = s_speed;
    t_health = s_health;
    
    if(decision == 1){
    System.out.println("Who would you like to fight? <1> Enchanted Dummy <2> Racaria");
    decision = input.nextInt();
    
    if(decision == 1){
    System.out.println("You must now fight the " + d_name + "\n \nSamurai stats: \n \nStrength: " + t_str + "\nDefense: " + t_def + "\nMagic: " + t_magic + "\nSpeed: " + t_speed + "\nHealth: " + t_health);
    }
    
    }
    
    else if(decision == 2){
    System.out.println("Welcome to the store! What would you like to buy?");
    }
    
    }
    
    if(character == 3){
    System.out.println("You have chosen the Knight class. What would you like to do now? <1> Go to the arena <2> Go to the store");
    decision = input.nextInt(); 
    str = k_str;
    def = k_def;
    magic = k_magic;
    speed = k_speed;
    health = k_health;
    
    t_str = k_str;
    t_def = k_def;
    t_magic = k_magic;
    t_speed = k_speed;
    t_health = k_health;
    
    if(decision == 1){
    System.out.println("Who would you like to fight? <1> Enchanted Dummy <2> Racaria");
    decision = input.nextInt();
    
    if(decision == 1){
    System.out.println("You must now fight the " + d_name + "\n \nKnight stats: \n \nStrength: " + t_str + "\nDefense: " + t_def + "\nMagic: " + t_magic + "\nSpeed: " + t_speed + "\nHealth: " + t_health);
    }
    
    }
    
    else if(decision == 2){
    System.out.println("Welcome to the store! What would you like to buy?");
    }
    
    }
    
    }
    
    }

  13. #13
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Constructor/Object Help

    Quote Originally Posted by MastaChief11 View Post
    I fixed all the errors in the program, so now it works correctly. Now, I need help converting my old code to be shorter and use the constructor:
    Since I don't know what you want to do with this game, I can't really help you with such a general request. If you have a specific issue, feel free to ask.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  14. #14
    MastaChief11 is offline Member
    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0

    Default Re: Constructor/Object Help

    Quote Originally Posted by awinston View Post
    Since I don't know what you want to do with this game, I can't really help you with such a general request. If you have a specific issue, feel free to ask.
    Alright then. I'll work on it, and if I need help I'll ask.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Constructor/Object Help

    At least you used code tags, but you do really need to indent your code properly.

    Can I suggest creating my aforementioned CharacterClass class?
    Just something to hold all those different attribute sets.
    You could then have a Map<Integer, CharacterClass> to map the option numbers to the class.

    It would be another step in the right direction for breaking your code up into smaller chunks.
    Zyril likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. initializing InetAddress object in constructor
    By sixxvirus in forum Networking
    Replies: 1
    Last Post: 05-20-2012, 07:23 AM
  2. Error Passing Object through a constructor
    By mpankhurst in forum New To Java
    Replies: 9
    Last Post: 12-15-2011, 11:12 AM
  3. Getting the calling object in a constructor?
    By Koala in forum New To Java
    Replies: 8
    Last Post: 10-31-2011, 04:27 AM
  4. Create an object without calling the constructor?
    By sublixt in forum New To Java
    Replies: 7
    Last Post: 10-28-2011, 01:49 PM
  5. No main method, constructor/object not working
    By sinista in forum New To Java
    Replies: 9
    Last Post: 10-14-2009, 03:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •