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: invoking method from another class ..

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default invoking method from another class ..

    Hi. I am a beginner still in Java. This is a 'chutes and ladders' game. It has 3 classes:

    Players.java (contains main() )
    Player.java (is basically the blue print for Player objects which will be players in the game, sets up variables for player location, number of turns (ie die rolls) taken)
    Game.java (has a method called move() which contains the switch statements acting upon location which is basically essence of the game).

    I am confused about how to organise the methods and objects. At the moment I am trying to call the move() method from main. I am trying to invoke it upon a player object (Player1). The problem is the move() method is part of the game class. And the Player1 object is derived fromt the Player class. I know this is wrong . But I dont know how better to do it. So, at the moment I am trying to use the move() method upon the Player1 object and passing it (location) as a parameter like this:
    [code=Java]
    if (count % 2 == 0)
    {
    Player1.Game.move(location);
    System.out.println(Player1);
    }
    else
    [\code]

    I know you need to initialize an object from a class to start using methods from that class but I think this is a bit more complex because I am working with three classes. Anyway I look forward to hearing your advice and I will post all three classes now:

    //Players.java
     
    import java.util.Random;
     
    public class Players
    {
      public static final int GOAL_NUMBER = 100;
     
      public static void main (String[] args)
      { 
     
      int location, roll, turns, skipped, count;
     
      Random rand = new Random ();
      Player Player1 = new Player (1, 0, 0);
      Player Player2 = new Player (1, 0, 0);
     
      roll = rand.nextInt (6) + 1;
     
      location += roll;
     
     
      while (location != GOAL_NUMBER)
      {
     
      if (location < GOAL_NUMBER)
      {
        if (count % 2 == 0)
            {
            Player1.Game.move(location);
            System.out.println(Player1);
            }
        else
            {
            Player2.Game.move(location);
            System.out.println(Player2);
            }
      count++;
      }
      else
        location -= roll;
     
      }  //end of while
     
        System.out.println ("Somebody has won");
     
      }
    }

    //Game.java
     
    public class Game
    {
      public int move (int location);
      {
        switch (location)
        {
          case 1:
          location = 38;
          System.out.println("You potted a plant! Climb to 38!");
          break;
          case 5:
          location = 14;
          System.out.println("You made cookies! Climb to 14!");
          break;
          case 9:
          location = 31;
          System.out.println("You mowed the yard! Climb to 31!");
          break;
          case 15:
          location = 6;
          System.out.println("You skipped your homework. Slide down to 6!");
          break;
          case 21:
          location = 42;
          System.out.println("You helped a puppy! Climb to 42!");
          break;
        }
        return location;
      }
    }

    //Player.java
     
    public class Player
    {
     
      int location, turns, skipped;
     
      public Player (int l, int t, int s)
      {
        l = location;
        t = turns;
        s = skipped;
      }
     
      public String toString ()
      {
        String result;
     
       result = "Your Position: " + location;
       result += "\t Number of turns used: " + turns;
       result += "Dived over 100: " + skipped;
     
        return result;
      }
    }


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: invoking method from another class ..

    Here are a few questions for you.
    1. In your player class you have the variables (location, turns, skipped) and then they are repeated in your main. What is the reason for this?
    2. Can you see the mistake in your loop? What happens?
    3. In your main class what does Player.Game.move(location) invoke? Where is this reference?
    4. Where is the default case in your switch?

    I am guessing your question is how do you incorporate the move function within the classes? Well do you know the function of getters and setters? For example when you create your player1 object what happens if you write System.out.println(player1.toString()); This is you invoking one of the classes public methods.

    Post a more specific question to what you are struggling with and what you have attempted so we can have an idea on how to guide you.

  3. The Following User Says Thank You to Ubiquitous For This Useful Post:

    craigjlner (September 28th, 2013)

  4. #3
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: invoking method from another class ..

    1. I see what you mean : when i say Player1 = new Player (1, 0, 0) main should go to the Player class and find that the paraemters reprensent 'int location, int turns, int skipped'. However when I delete these declarations from main (int location, roll, turns) and compile it says 'cannot find symbol' for those variables. But am I doing it right by declaring those things in a seperarte class and then using them in a whiile loop in the main method. It should recognize them should it not?

    2.corrected.

    3.I am trying to invoke the move () method that is inside the game class for the Player1 object and passing it player1's location as a parameter. So that it, i hope, updates the location value for player1.

    4. default: location = location + roll;

    Question 3 is the main thing that bugs me at the moment. I think I can condense the two classes so that the constructor that defines a Player object is in the same class as the move() method which updates the location. And then I just have to make the relationships between that and the main method. I think I can do that. But every guide you get in OOP/Java seems to emphasise how you should make alot of self governing classes to do the work for you. But it seems very hard for me to get more than one other class to work together with the main method.

  5. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: invoking method from another class ..

    In instances where there are multiple players it is good to keep these fields in spearate objects. The reason being is that you can keep track of where both players are easily and without issues. Before taking on the 3rd issue you must understand how an object works and how you have access to its properties.

    As you probably know by now an object contains Constructors, Getters/Settters and methods. The variables declared in objects you want to keep private to prevent headaches down the line. Your constructor initializes the variables you will be using. So in your player class the constructor takes 3 Arguments which initializes location, turns and skipped. Now when you create an object to access it methods you use .

    For example when you create a String object it has methods in its class. It has .charAt(), subString() ...etc

    I cannot stress enough how important it is to understand how to make full use of an object. Once you understand the mechanics behind it stuff will make more sense. You have most if not all the code necessary you just need to implement it correctly and understand what is happening.

    I'll help out in any way possible just let me know if you need clarification.

  6. The Following User Says Thank You to Ubiquitous For This Useful Post:

    craigjlner (September 30th, 2013)

Similar Threads

  1. Confused about invoking methods?
    By ShamelessTeenie in forum Object Oriented Programming
    Replies: 5
    Last Post: April 9th, 2013, 07:50 PM
  2. Invoking JavaScript in Servlet
    By yash188 in forum Java Servlet
    Replies: 3
    Last Post: September 17th, 2012, 10:24 PM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. [SOLVED] Invoking a superclass constructor in my subclass
    By kari4848 in forum Object Oriented Programming
    Replies: 5
    Last Post: April 29th, 2011, 01:12 PM
  5. [SOLVED] Array of objects, invoking constructor for one changes others
    By BigFoot13 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 24th, 2010, 01:30 PM