webdevRefinery Forum: Hit a brick wall with OOP - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is offline TheMaster 

  • Group: Members
  • Posts: 229
  • Joined: 24-May 10
  • LocationAustralia
  • Expertise:HTML,CSS,PHP,Java

Posted 24 January 2012 - 10:50 PM (#1)

Hit a brick wall with OOP


I'll get straight to the point.
I want to be able to "pass" an object, to another object. I'm busy objectifying my to-be "game" (see other thread -> "A bit of Advice"). Anyway, I'll use pseudo code to help you understand what I want to do. Hopefully you can help me out! :D

mainClass:

public class mainClass {

   public static void main(String args[]) {

      classOne objOne = new classOne(5);
      
      //create this object and "pass" the one I created to it.
      classTwo objTwo = new classTwo(objOne);

      //double a in this class
      objOne.doubleA();

     //and then triple it, after retrieving it using the "passed" object

     System.out.prinln(objTwo.tripleTheDouble());

     //should in theory print 30

   }

}





classOne:

public class classOne {

   public int a; 

   //some more properties

   public classOne(int a) {

      this.a = a;

   }

   public int doubleA() {

      this.a = this.a * 2;

      return this.a;
  
   }

   //some more methods

}



classTwo:
public class classTwo {

   classOne one;
   int newA;

   public classTwo(classOne obj) {

      this.one = obj;

   }

   public int tripleTheDouble() {

      newA = one.a * 3;

      return newA;

   }

}


I have NOOOO idea if that code would even work. I just used it as an exmaple of what I want to do. I want to do this sort of thing to pass player objects to board objects, and board objects to game objects etc. etc.

How would I do that?

Thanks in advance! :D

EDIT: I have seen APIs that require you to pass certain objects into class methods (for instance creating a canvas object, and then passing that object to a draw method or something), so I know it can be done, I'm just not sure how.
0


User is offline NeilHanlon 

  • Group: Members
  • Posts: 669
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML,CSS,PHP,Java,Graphics

Posted 24 January 2012 - 10:56 PM (#2)

The main purpose of OOP in this sense would to be able to reference these methods from another file/class.

For example:

Main.class
//import the calculator class from the plugins package (a working example I am coding right now)
import plugins.calculator;
import plugins.someRandomClass;

public class Main {
     public static void main(String args[]) {
          //runs a calculator in GUI
          Calculator.main(null);
          //outputs the sum of 1 and 2
          someRandomClass.addNumbers(1,2);
    }
}


Obviously this is incredibly basic code, but nonetheless, I think it gives a basic understanding of OOP in java.

In your code up there, I'd do something like this:

add
package classes;
to classOne and classTwo (after imports, before class declaration).

add
import classes.*;
to mainClass

Use just like you are.
Thanks,
兄ニール

Website | Blog | @NeilHanlon | About.Me | Facebook | LinkedIn
0


User is online ianonavy 

  • Group: Members
  • Posts: 329
  • Joined: 14-April 10
  • Expertise:HTML,CSS,Java,Javascript,Python

Posted 25 January 2012 - 12:30 AM (#3)

First of all, as a standard Java naming convention, you should have capital CamelCase class names. Second, your classes need to have some meaning.

Naming conventions aside, your above code seems to be more or less correct for what you want to do. This is how I would set up what you are trying to do: https://github.com/i...ext-Gaming-Grid

I kind of got carried away writing up my sample code, so I just decided to throw it up on GitHub for you to download and see how it works. It's not really a game per se, but it could be a good start for you.
if (this.post.isHelpful()) {
    reputation++;
} else {
    response.write(constructiveCriticism);
}
0


User is offline Sephern 

  • Group: Moderators
  • Posts: 840
  • Joined: 04-June 10
  • LocationLeeds, UK
  • Expertise:HTML,CSS,PHP,Python

Posted 25 January 2012 - 08:48 AM (#4)

View PostTheMaster, on 24 January 2012 - 10:50 PM, said:

I'll get straight to the point.
I want to be able to "pass" an object, to another object. I'm busy objectifying my to-be "game" (see other thread -> "A bit of Advice"). Anyway, I'll use pseudo code to help you understand what I want to do. Hopefully you can help me out! :D

Spoiler

I have NOOOO idea if that code would even work. I just used it as an exmaple of what I want to do. I want to do this sort of thing to pass player objects to board objects, and board objects to game objects etc. etc.

How would I do that?

Thanks in advance! :D

EDIT: I have seen APIs that require you to pass certain objects into class methods (for instance creating a canvas object, and then passing that object to a draw method or something), so I know it can be done, I'm just not sure how.


Okay so, the way that you're doing things at the moment seems to be going in the right sort of direction. You'll want to use your classes in the same way as other types in java (like string, int, etc) and pass them as properties between other classes. In general, there's a few different relationships that classes can have with each other.

What you're doing is in essence a has a relationship (or belongs to, depending on which way you look at it).

public class Board {
  private ArrayList<Tile> tiles;

  public Board() {
    //create the array of tiles
  }
}


In this case, we say that a board has a list of tiles. The tiles belong to the board.

Objects can be passed into functions (including the constructor of other objects)

public class Board {
  private Player player1;   

  public Board(Player player) {
    this.player1 = player;
  }
}

public class Player {
  public int position = 0; //some starting point

  public void move(numSquares) {
    this.position += numSquares;
  }

  public int getPosition() {
    return this.position;
  }
}


In this example, the board has a player, and can query that player to see what position they are in. If you wanted to, you could pass the reference to the board into the player too.

There tends to be numerous ways to do things (some obviously better than others). For example, a board could have a list of players, all which control their positions. The player could belong to a board which stores the players position, and tells the player where it is on request. Where the responsibility for moving the player (and indeed, various other questions) falls depends on the application, and on the developer.

The question of how objects interact and which one has what responsibility is the primary design challenge of Object Orientation.
1


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services