Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Thanked 0 Times in 0 Posts

    Inheritance Class help

    I was given this assignment to coplete, however know nothing about inhetiance in java
    i have this code already but by use of inheritance have to add 2 other classes and a user class, how do i do this?

    the code i have so far is:

    public class Piece {
       // attributes
    		protected String name;
    		protected int posX;
    		protected int posY;
    		protected boolean moves = true;
        // constructors
    			public Piece (String theName, int x, int y)     { 
    			name = theName;
    			posX = x; 
    			posY = y; 
        // methods
    			// Gets the value of the name
    				public String getName() {
    				return name;
    				// Gets the value of the x coordinate
    					public int getPosX() {
    					return posX;
    				// Gets the value of the y coordinate
    					public int getPosY() {
    					return posY;
    					// Gets the value of moves
    					public boolean getMoves() {
    						return moves;
    				// Sets the value of the x coordinate
    					public void setPosX(int x) {
    							posX = x;
    				// Sets the value of the y coordinate
    					public void setPosY(int y) {
    							posY = y;
    // Converts posX and posY into a single integer. For example (2,1) will return 21.
    // This may help defining move methods given a particular position.
    								public int positionCode () { 
    								return (posX*10) + posY; 

    i have to add a treasure and person class as said below

    The pieces have a name, x and y co-ordinates (each between 1 and 3) and
    whether they can be moved or not. Treasure has the value a positive integer. In the person class it must state if the treasure has been found.
    Treasure is placed at one ofnine locations, it cannot move. person is placed at one of the nine locations and searches for the treasure.

    The person can move from its location right, left, up or down (where possible)
    one square, but not diagonally. For example from (1,2) a person could move to either (1,1), (2,2) or (1,3). From the location (3,3) the person could move to (2,3) or (3,2).
    The person can see the treasure if it is in the same row or column (i.e. has the same x coordinates or the same y coordinates). For example if the person is at (1,1) and the treasure is at (1,3) the person can see the treasure. However if the person is at (1,1) and the treasure is at (2,2) the person cannot see the treasure. If the person can see the treasure they move one step towards it. For example if the person is at (1,1) and the treasure is at (1,3) the person moves to (1,2). If they cannot see the treasure they move in some direction one step.

    The program should input the name, co-ordinates and value of the treasure, and the name and co-ordinates of the person. Negative or zero treasure values should be disallowed.
    Co-ordinates not in the range 1-3 should be disallowed. Additionally the program should print out the moves of the person, i.e. the name and location of the person
    and whether the treasure has been found until they find the treasure. You should use inheritance in your solution to obtain the classes Treasure and Person. There should be a user class that asks for input and instantiates Treasure and Person.

  2. #2
    Regular Coder
    Join Date
    Feb 2008
    Edinburgh - Scotland
    Thanked 12 Times in 12 Posts

    I havent read your post fully **lack of time** but I can show you what inheritance is and why you would want it.

    If you were creating a game - with lots of types of monsters in it, you might have a class for each type of monster with their attributes, health, magic power, size, x coord, y coord, power etc etc etc.

    If you have 20 monsters, you then need 20 of the above code repeated 20 times which is a massive waste.

    So you can you use inheritance as follows:

    public class Monster{
    String name;
    int xCoord;
    int yCoord;
    int health;
    public Monster(String name, int xCoord, int yCoord, int health){
    this.name = name;
    this.xCoord = xCoord;
    this.yCoord = yCoord;
    this.health = health;
    public class MonkeyManMonster extends Monster{
    Color color;
    public MonkeyManMonster((String name, int xCoord, int yCoord, int health,Color color){
    super(name,xCoord,yCoord,health); // calls the Monster class constructor
    this.color = color;
    This means creating a new type of Monster which has the same attributes as all other monsters is now 5 lines of code instead of nearly 20 for example. Another good example is a vehicle class being the superclass - with car, taxi, bus being the subclass - because all cars , taxis buses etc have 4 wheels, a registration plate etc.

    Hope this helps

  3. Users who have thanked renegadeandy for this post:

    Beck7223 (12-07-2010)


Posting Permissions

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