Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: A Question About Merging Many Methods into One

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A Question About Merging Many Methods into One

    Hello all, I've bumped into an interesting coding conundrum recently and seek the wisdom of much more experienced programmers.

    So I've been working on a text adventure (which was formerly written in C++) and I wanted to put together a method that displays the player's stats, location and inventory. In the spirit of compartmentalization I wrote three simple methods spread across two classes so I could create a separate method invoking all three. Now that I've done that I'm having some trouble putting them all together. I can't invoke the methods of an object until it's been created in the main method, but I can't write the combined method unless I can invoke the others.

    I'm not entirely sure what to do and thus I'm looking for advice. Is there a way I can combine these without breaking up the code compartmentalization?

    The two classes are shown below for reference. The three methods I want to combine are the methods displayRoom, displayHealth and displayRace.

    I would be extremely grateful for any assistance.

     package wounded.chapter.pkg1;
     
    /**
     *
     * @author Geoffrey
     */
      public class room{
            //feilds
            public int number;
            public int wind;
            public int hardKnock;
            public int blueBlood;
            public int ura;
     
            // constructor
            // room 1 = clinic
            // room 2 = training room
            // room 3 = lab
            // room 4 = Kitchen
            public room(){
                number = 1;
                wind = 1;
                hardKnock = 2;
                blueBlood = 2;
                ura = 2;
            }
            public void displayRoom(){
               //image for the training room
                if(number == 2){
                    System.out.println("TRAINING ROOM\n"
                            // this is complicated, but it's the best way to do it as far as I can see.
    + "0000000000000000000000000000000000000000000000\n"
    + "0            ()  ()  ()  ()                  0\n"
    + "0              KICKING BAGS                  0\n"
    + "0                                            0\n"
    + "0-------      ______________________  -------0\n"
    + "0       |    |                      | |      0\n"
    + "0 WEAPON|    |                      | |      0\n"
    + "0 LOCKER|    |   EXERCISE MAT       | | COT  0\n"
    + "0       |    |                      | |      0\n");
    if(hardKnock == 2){System.out.println("0       |    |   (HARD KNOCK)       | |      0\n");} 
                 else {System.out.println("0       |    |                      | |      0\n");}
    System.out.println("0       |    |                      | -------0\n"
    + "0-------     |______________________|        |\n"
    + "0                                        DOOR|\n");
    if (ura == 2) {System.out.println("0                      (URA)                 |\n");}
             else {System.out.println("0                                            |\n");}
    if (wind == 2) {System.out.println("0 (WIND)                                     0\n");}
              else {System.out.println("0                                            0\n");}
    if (blueBlood == 2) {System.out.println("0        DOOR              (BLUEBLOOD)       0\n");}
                   else {System.out.println("0        DOOR                                0\n");}
    System.out.println("00000000_____000000000000000000000000000000000\n\n");
     
                }
               //image for the Clinic 
               if(number == 1) {
                   System.out.println("CLINIC"
    + "00000000_____000000000000000000000000000000000"
    + "0       DOOR            U()U       |         0"
    + "0                      WASH BASIN  |         0"
    + "0                                  |CABINET  0");
    if (ura == 1){System.out.println("0                     (URA)        |         0");}
            else {System.out.println("0                                  |         0");}
    System.out.println("0                                  ----------0"
    + "0                                            0"
    + "0                                            0");
    if (blueBlood == 1) {System.out.println("0                (BLUEBLOOD)                 0");}
                   else {System.out.println("0                                            0");}
    System.out.println("0                                            0");
    if (hardKnock == 1) {System.out.println("0-------        --------      (HARD KNOCK)   0");}
                   else {System.out.println("0-------        --------                     0");}
    System.out.println("0      |        |      |                     |");
    if (wind == 1) {System.out.println("0 YOUR | (WIND) |      |                 DOOR|");}
              else {System.out.println("0 YOUR |        |      |                 DOOR|");}
    System.out.println("0 COT  |        | COT  |                     |"
    + "0      |        |      |                     0"
    + "0      |        |      |                     0"
    + "0000000000000000000000000000000000000000000000");
            }
      }
      }

    package wounded.chapter.pkg1;
     
    /**
     *
     * @author Geoffrey
     */
    public class player {
            //has one health feild, one race feild and a feild for each type of item they pick up.
            //I will add items as the code requires starting with the healing potion.
            //and half healing potion.
            public String name;
            public int health;
            public int playerLocation;
            //race= 1 unicorn
            //race= 2 pegasus
            //race= 3 earth pony
            public int race;
            public int healingPotion;
            public int halfHealingPotion;
     
            public player(int startingHealth, int noHealingPotion, int noHalfHealingPotion, int startingRace){
                name = "startingName";
                startingHealth = 1;
                health = startingHealth;
                race = startingRace;
                healingPotion = noHealingPotion;
                halfHealingPotion = noHalfHealingPotion;
     
            }
     
            public void displayRace(){
                switch (race){
                    case 1:
                        System.out.println("UNICORN\n"
    +".................~()~().../.....\n"
    +"..................(~0000/ /............\n"
    +"..............(~0/    _    ............\n"
    +".............(~0/     Q     __.....\n"
    +"............(~0/             _D........\n"
    +"...........(~0/             ___/.........\n"
    +"..........(~0/          __/...................\n"
    +"____________/           /.........................\n"
    +"                       |..........................\n"
    +"                       |...........................\n"
    +"                       |..........................\n"
    +"                       |..........................\n"
    +"                       |.........................\n"
    +"                        .......................\n"
    +"                         .......................\n"
    +"________|     |_____      .......................\n"
    +"........|     |......     ......................\n"
    +"........|     |.......      .........................\n\n");
                break;
                    case 2:
                        System.out.println("PEGASUS\n"
    +"..................~()~()......\n"
    +"..................(~000000............\n"
    +"..............(~0/    _    ............\n"
    +".............(~0/     Q     __.....\n"
    +"............(~0/             _D........\n"
    +"...........(~0/             ___/.........\n"
    +"..........(~0/          __/...................\n"
    +"____________/           /.........................\n"
    +"|||||||||>>             |..........................\n"
    +"|||||||||>>             |...........................\n"
    +"|||||||||>              |..........................\n"
    +"|||||||||               |..........................\n"
    +"|||||||||               |.........................\n"
    +"|||||||||               .......................\n"
    +"|||||||||                .......................\n"
    +"|||||||||     |_____      .......................\n"
    +"--------|     |......     ......................\n"
    +"........|     |.......      .........................\n\n");
               break;
                    case 3:
                        System.out.println("EARTH PONY\n"
    +"..................~()~()......\n"
    +"..................(~000000............\n"
    +"..............(~0/    _    ............\n"
    +".............(~0/     Q     __.....\n"
    +"............(~0/             _D........\n"
    +"...........(~0/             ___/.........\n"
    +"..........(~0/          __/...................\n"
    +"____________/           /.........................\n"
    +"                       |..........................\n"
    +"                       |...........................\n"
    +"                       |..........................\n"
    +"                       |..........................\n"
    +"                       |.........................\n"
    +"                        .......................\n"
    +"                         .......................\n"
    +"        |     |_____      .......................\n"
    +"--------|     |......     ......................\n"
    +"........|     |.......      .........................\n\n");
                }
            }
     
            public void displayHealth(){
            System.out.println("HEALTH: " + health + "\n");
            }
     
            public void setName(String newName){
                name = newName;
            }
     
            public void setRace(int raceChoice){
                race = raceChoice;
            }
     
            public void heal(int healing){
                health += healing;  
            }
     
            public void harm(int damage){
                health -= damage;
            }
     
            public void getHealingPotion(int pickUp) {
                healingPotion += pickUp;
            }
     
            public void getHalfHealingPotion(int pickUp){
                halfHealingPotion += pickUp;
            }
     
            public void dropHealingPotion(int putDown) {
                healingPotion -= putDown;
            }
     
            public void dropHalfHealingPotion(int putDown){
                halfHealingPotion -= putDown;
            }
     
            public void move (int newLocation){
                playerLocation = newLocation;
            }
     
        }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: A Question About Merging Many Methods into One

    A method could just call the three in the methods in the order you wanted to display them.
    public void displayAllThree(Player myPlayer) {
       myPlayer.displayRace();
       myPlayer.displayHealth();
       myPlayer.getRoom().displayRoom();
    }
    ...Where getRoom() would return the room the player is currently in, or by using what ever system you have in place to determine the location...

    (For convention I have used uppercase Player in place of the class name player)
    Edit:
    just for the record, this method name is to suggest the idea, rather than actually be used
    Last edited by jps; October 14th, 2012 at 03:14 AM.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A Question About Merging Many Methods into One

    Quote Originally Posted by jps View Post
    A method could just call the three in the methods in the order you wanted to display them.
    public void displayAllThree(Player myPlayer) {
       myPlayer.displayRace();
       myPlayer.displayHealth();
       myPlayer.getRoom().displayRoom();
    }
    ...Where getRoom() would return the room the player is currently in, or by using what ever system you have in place to determine the location...

    (For convention I have used uppercase Player in place of the class name player)
    Edit:
    just for the record, this method name is to suggest the idea, rather than actually be used
    I'm a bit confused by this answer.

    I'm new to java, but it appears to me from what you've coded that you're creating a new object every time you run that method. That implies that I would have to rig the constructor somehow to give all of the variables in the player class (and the room class where the "public int number" variable is defined) definitions which had been modified by the user over the course of play so that the player and NPCs could move from room to room. Could you give me an example on how you think something like that could be accomplished?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: A Question About Merging Many Methods into One

    you're creating a new object every time you run that method
    Not exactly. The Player object is passed to the method as a parameter. It must be an existing object before calling this method. Besides this was not designed to be a copy-paste-into-code example. Rather just a self-contained example of how to go about combining all three methods. Basically just call the three methods where ever you want all three called. If it is convenient to make a method for doing so, then by all means make one.



    Could you give me an example on how you think something like that could be accomplished?
    I am not sure I know what it is you are trying to accomplish. I just showed an example of what I thought you asked how to do.



    If you still need help, please explain more about what you are having problems doing.

Similar Threads

  1. Newb question: Using the same object over two methods?
    By CitizenSmif in forum Object Oriented Programming
    Replies: 5
    Last Post: July 3rd, 2011, 08:17 PM
  2. Question to do with methods.
    By Dragonkndr712 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 31st, 2011, 11:56 PM
  3. newbie question about Abstract methods
    By FailMouse in forum Java Theory & Questions
    Replies: 3
    Last Post: August 10th, 2010, 11:51 PM
  4. Merging of Trees..
    By Kumarrrr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 23rd, 2010, 11:39 PM
  5. A question about inherited methods
    By joachim89 in forum Object Oriented Programming
    Replies: 1
    Last Post: January 12th, 2010, 09:06 PM