7 Replies - 542 Views - Last Post: 24 July 2009 - 04:46 PM Rate Topic: -----

#1 Gsirtak  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 30-June 09

An instanceof needed

Posted 24 July 2009 - 08:17 AM

I would really appreciate it if someone can help me finalize this code.

I need to incude 1 instanceof and 1 casting object. I've benn fiddling with this for 2 hours.

Thank you!
public abstract class NflPlayer {
	public static void main(String[] args) {
		Runningback myRunningback = new Runningback();
		Quarterback myQuarterback = new Quarterback();
		Linebacker myLinebacker = new Linebacker();

		myNflPlayer.run();
		myNflPlayer.scramble();
		myNflPlayer.tackle();
		myNflPlayer.slashes();

		myRunningback.run();
		myRunningback.scramble();

		myQuarterback.run();
		myQuarterback.scramble();

		myLinebacker.run();
		myLinebacker.scramble();
		myLinebacker.tackle();
		myLinebacker.slashes();

	}
	  
	public void run() {
		System.out.println("All NFL Players run!");
	}
	public void scramble() {
		System.out.println("All NFL Players scramble!");
	}
	public void tackle() {
		System.out.println("All NFL Players tackle when needed!\n");
	}
 
}


Is This A Good Question/Topic? 0
  • +

Replies To: An instanceof needed

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: An instanceof needed

Posted 24 July 2009 - 10:12 AM

Didnt you have an InstanceOf done before? Do you have a main Object class which your other classes derive from? Somthing like NFLplayer, then you could just do somthing like
if(NFLplayer InstanceOf LineBacker)

Was This Post Helpful? 1
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: An instanceof needed

Posted 24 July 2009 - 10:26 AM

View Postnick2price, on 24 Jul, 2009 - 11:12 AM, said:

if(NFLplayer InstanceOf LineBacker)


Just make sure you don't capitalize any letters in instanceof. ;)

This post has been edited by Locke: 24 July 2009 - 10:26 AM

Was This Post Helpful? 1
  • +
  • -

#4 Gsirtak  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 30-June 09

Re: An instanceof needed

Posted 24 July 2009 - 03:48 PM

View Postnick2price, on 24 Jul, 2009 - 09:12 AM, said:

Didnt you have an InstanceOf done before? Do you have a main Object class which your other classes derive from? Somthing like NFLplayer, then you could just do somthing like
if(NFLplayer InstanceOf LineBacker)


Thank you. I'm sorry but I forgot to tell you I have 3 subclasses of my Abstarct class. So would I still place my instanceof in my abstract class?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 Gsirtak  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 30-June 09

Re: An instanceof needed

Posted 24 July 2009 - 03:54 PM

View PostGsirtak, on 24 Jul, 2009 - 02:48 PM, said:

View Postnick2price, on 24 Jul, 2009 - 09:12 AM, said:

Didnt you have an InstanceOf done before? Do you have a main Object class which your other classes derive from? Somthing like NFLplayer, then you could just do somthing like
if(NFLplayer InstanceOf LineBacker)


Thank you. I'm sorry but I forgot to tell you I have 3 subclasses of my Abstarct class. So would I still place my instanceof in my abstract class?

Thanks!

Here are my other clasesses.
public class Runningback extends NflPlayer {

	public static void main(String[] args) {

		Runningback myRunningback = new Runningback();
		myRunningback.run();
		myRunningback.scramble();
	}

	public void run(){
		System.out.println("Runningbacks run!");
	}
	public void scramble(){
		System.out.println("Runningbacks scramble.\n");
	}
}


public class Quarterback extends NflPlayer {

		String name;
		String color;

	public static void main(String[] args) {

		Quarterback myQuarterback = new Quarterback();
		myQuarterback.scramble();
		myQuarterback.tackle();
		myQuarterback.fast();
		myQuarterback.slashes();
	}
	public void run(){
		System.out.println("Quarterbacks run.");
	}
	public void scramble(){
		System.out.println("Quarterbacks scramble.");
	}
	public void fast() {
		System.out.println("Quarterbacks need quickness.");
	}
	public void slash() {
		System.out.println("Quarterbacks slash through players!\n.");
	}

	private void slashes() {
			System.out.println("Quarterbacks slash through players!\n.");
	
	}
}


public class Linebacker extends NflPlayer {

		String fast;
		String slashes;

	public static void main(String[] args) {

		Linebacker myLinebacker = new Linebacker();
		myLinebacker.scramble();
		myLinebacker.tackle();
		myLinebacker.fast();
		myLinebacker.slashes();
	}
  public void run(){
		System.out.println("Linebackers run!");
	}
	public void scramble(){
		System.out.println("Linebackers scramble!");
	}
	public void fast() {
		System.out.println("Linebackers are fast!");
	}
	public void slashes() {
		System.out.println("Linebackers slash!\n.");

	}
}


class myNflPlayer {

	static void run() {
		System.out.println("NFL Players run!.");
	}

	static void scramble() {
		System.out.println("NFL Players scramble!");
	}

	static void slashes() {
		System.out.println("NFL Players slash!");
	}

	static void tackle() {
	   System.out.println("NFL Players tackle!\n.");
	}

}

This post has been edited by Gsirtak: 24 July 2009 - 03:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: An instanceof needed

Posted 24 July 2009 - 03:59 PM

Your design may be a little flawed here. Generally, an abstract class acts as a sort of template for your subclasses. The abstract class shouldnt really have a main method in it, just variables and methods. So, the abstract class should relate to all three of your subclasses, perhaps with variables like playerWeight, playerHeight etc. Your subclasses will then take this further and make things more specific to the type of player the class relates to, for instance, your quatreBack class might have a method hasBall(). Your main should then be placed in a totally seperate class, which is not abstract. You instantiate your subclasses as the abstract class cant be instantiated. you then call whatever methods you have in your subclasses, just like how you have done now. So if i was you, I would rename your NFLplayer class to MainClass and make it not abstract. I would then create a new abstract NFLplayer class and just have a few simple variables and constructor in there. Let the subclasses inherit this, and your good to go.
Was This Post Helpful? 0
  • +
  • -

#7 Gsirtak  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 30-June 09

Re: An instanceof needed

Posted 24 July 2009 - 04:08 PM

View Postnick2price, on 24 Jul, 2009 - 02:59 PM, said:

Your design may be a little flawed here. Generally, an abstract class acts as a sort of template for your subclasses. The abstract class shouldnt really have a main method in it, just variables and methods. So, the abstract class should relate to all three of your subclasses, perhaps with variables like playerWeight, playerHeight etc. Your subclasses will then take this further and make things more specific to the type of player the class relates to, for instance, your quatreBack class might have a method hasBall(). Your main should then be placed in a totally seperate class, which is not abstract. You instantiate your subclasses as the abstract class cant be instantiated. you then call whatever methods you have in your subclasses, just like how you have done now. So if i was you, I would rename your NFLplayer class to MainClass and make it not abstract. I would then create a new abstract NFLplayer class and just have a few simple variables and constructor in there. Let the subclasses inherit this, and your good to go.

Ok, but then I have a class question for my next answer stating: Using the code for the one abstract class with 3 subclasses that inherit the attributes/methods of the superclass, the answer from question #3 which this is the answer for question 3. Using question #3 code include one casting object and include the "instanceof" operator in answer 4.
Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: An instanceof needed

Posted 24 July 2009 - 04:46 PM

Adding them things is simple, as long as your sort out the design. It states

Quote

one abstract class with 3 subclasses that inherit the attributes/methods of the superclass,


Your abstract class doesnt have any attributes which are inherited, but thats not to important for now. Change your class myNflPlayer to be abstract. Take the main methods out of all your classes. Create a new Main class with a main method. Instantiate all your subclasses and call up their methods in your new Main class. See if you can get this to work for now.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1