Results 1 to 9 of 9
  1. #1
    Wazniak is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Gunner class implementation

    Hey i'm having a hard time with this task(Code below):
    I need to write class Gunner which extends abstract class GunnerA. The task of the class is to destroy the target using ShootingRules interface. Gunner class has to destroy the target using information "to close/ to far" and with the least number of shots possible.
    Extra info: I can't modify GunnerA class, Gun and ShootingRules interface. Gunner class have to use no-argument constructor.
    Can you give me any tips how to start ? some examples ?

    Java Code:
    public abstract class GunnerA {
    
      public abstract void weaponry( ShootingRules sr );
      public abstract void destroyTarget();
    
    }
    Java Code:
    public interface Gun {
    
      public double getBulletMinStartingSpeed();
      public double getBulletMaxStartingSpeed();
    // minimum & maximun angle from which the barrel can/will shoot
      public double getBarrelMinAngle();
      public double getBarrelMaxAngle();
      public void setBulletStartingSpeed( double speed );
      public void setBarrelAngle( double angle );
      public void shoot();
    }
    Java Code:
    public interface ShootingRules {
    
      public Gun getGun();
      public int getShootingResult();
    }
    Java Code:
    class Gunner extends GunnerA implements ShootingRules {
    
    	@Override
    	public Gun getGun() {
    
    		return null;
    	}
    
    	@Override
    	public int getShootingResults() {
    		return 0;
    	}
    
    	@Override
    	public void weaponry(ShootingRules sr) {
    			
    	}
    
    	@Override
    	public void destroyTarget() {
    		
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Gunner class implementation

    I can tell you that you need to use composition rather than inheritance. A Gunner logically *is-not* a ShootingRules and thus should not implement this interface. Instead, a Gunner should *contain* a ShootingRules object.

  3. #3
    ieee488 is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Gunner class implementation

    Quote Originally Posted by Fubarable View Post
    A Gunner logically *is-not* a ShootingRules and thus should not implement this interface.
    Can you explain this further?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Gunner class implementation

    Quote Originally Posted by ieee488 View Post
    Can you explain this further?
    Sure. When deciding whether one class should descend from another, the first class must satisfy the is-a rule. So for instance a Dog may descend from a Carnivore class or interface or an Animal class or interface or Pet class or Interface, but there should be no inheritance relationship between Dog and Kennel. Instead we would use the composition rule for this since a Kennel has-a Dog (or likely multiple Dogs).

    Now if I am understanding the original poster correctly, a Gunner is someone who uses a Gun or some other Weapon while ShootingRules would be logical rules that dictate perhaps how the weapon should be used. In no way shape or form does Gunner satisfy the "is-a" rule for ShootingRules. Rather Gunner will likely have a ShootingRules object that it checks before it uses the weapon.

  5. #5
    Wazniak is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Gunner class implementation

    Ok, so if i'm correct my Gunner class "template" should look sth like this?:

    Java Code:
    class Gunner extends GunnerA {
    
    	ShootingRules sr = new ShootingRules() {
    		
    		public int getShootingResults() {
    			
    			return 0;
    		}
    				
    		public Gun getGun() {
    			
    			return null;
    		}
    	};
    		
    	public void weaponry(ShootingRules sr) {
    				
    	}
    	
    	public void destroyTarget() {
    		
    	}
    
    }

  6. #6
    ieee488 is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Gunner class implementation

    Quote Originally Posted by Fubarable View Post
    Sure. When deciding whether one class should descend from another, the first class must satisfy the is-a rule. So for instance a Dog may descend from a Carnivore class or interface or an Animal class or interface or Pet class or Interface, but there should be no inheritance relationship between Dog and Kennel. Instead we would use the composition rule for this since a Kennel has-a Dog (or likely multiple Dogs).

    Now if I am understanding the original poster correctly, a Gunner is someone who uses a Gun or some other Weapon while ShootingRules would be logical rules that dictate perhaps how the weapon should be used. In no way shape or form does Gunner satisfy the "is-a" rule for ShootingRules. Rather Gunner will likely have a ShootingRules object that it checks before it uses the weapon.
    I am surprise that you are using is-a rule for interfaces as well.
    Vaguely, I remember from the only Java class that you can have two dissimilar classes both implement the same interface such as robot and car both implementing makeTurns( ).

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Gunner class implementation

    Quote Originally Posted by ieee488 View Post
    I am surprise that you are using is-a rule for interfaces as well.
    Vaguely, I remember from the only Java class that you can have two dissimilar classes both implement the same interface such as robot and car both implementing makeTurns( ).
    You should not be surprised since interfaces are inherited and both robot and car satisfy the "is-a" rule for "something that can turn".

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Gunner class implementation

    Quote Originally Posted by Wazniak View Post
    Ok, so if i'm correct my Gunner class "template" should look sth like this?:

    Java Code:
    class Gunner extends GunnerA {
    
    	ShootingRules sr = new ShootingRules() {
    		
    		public int getShootingResults() {
    			
    			return 0;
    		}
    				
    		public Gun getGun() {
    			
    			return null;
    		}
    	};
    		
    	public void weaponry(ShootingRules sr) {
    				
    	}
    	
    	public void destroyTarget() {
    		
    	}
    
    }
    I suppose that you can use an anonymous inner class for your ShootingRules object if you wish, but perhaps you'd prefer to make this a settable field instead? That's likely what I would do since it would allow the Gunner to be able to swap ShootingRules if need be.

  9. #9
    ieee488 is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Gunner class implementation

    Quote Originally Posted by Fubarable View Post
    You should not be surprised since interfaces are inherited and both robot and car satisfy the "is-a" rule for "something that can turn".
    I know interfaces are inheritable. Was not taught that they used is-a rule since interfaces are more about being able to do something than being something.

Similar Threads

  1. Cross-Class Implementation
    By FreakyCheeseMan in forum New To Java
    Replies: 1
    Last Post: 08-14-2012, 09:33 AM
  2. Abstract class, implementation, getName()?
    By manalinik in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 02:08 PM
  3. Class within one class implementation
    By gomdohri in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 02:05 AM
  4. Implementation Class for RMI App, Compile-Time Error
    By Jacob1028555 in forum Networking
    Replies: 22
    Last Post: 06-09-2011, 05:14 PM
  5. Help with implementation of the DataSet class
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:47 AM

Tags for this Thread

Posting Permissions

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