6 Replies - 2073 Views - Last Post: 03 October 2007 - 03:36 PM Rate Topic: -----

#1 moses316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 13-September 07

Using Abstract Classes and Methods

Posted 02 October 2007 - 06:22 PM

I have been able to make all the classes compile, but for some reason the verbage does not show up when you run the AbstractTest.java. Any help would be greatly appeciated. Please keep in mind that these are 5 separate .java files.





public class AbstractTest
{
	public static void main(String[] args)
	{
		Cat cat = new Cat("Kitty", "Angora");
		Robin bird = new Robin("Rockin");

		System.out.println("For the cat:");
		System.out.print("This is:	 \n"); cat.describe();
		System.out.print("Sound:	   \n"); cat.sound();
		System.out.print("Sleeping:	\n"); cat.sleep();
		System.out.print("Moving:	  \n"); cat.move();
		System.out.println("\n");

		System.out.println("For the robin:");
		System.out.print("This is:			 \n"); bird.describe();
		System.out.print("Sound:			   \n"); bird.sound();
		System.out.print("Sleeping:			\n"); bird.sleep();
		System.out.print("Moving:			  \n"); bird.move();
		System.out.print("\n");

		System.out.println("End of program.");
	}
}



public abstract class Animal
{
	String type;

	public Animal(String type)
	{
	  this.type = type;
	}

	public abstract String describe();

	public abstract String sound();

	public abstract String sleep();

	public abstract String move();
}



public abstract class Bird extends Animal
{
	protected String breed;

	public Bird()
	{
	  super("Bird");
	}

	public abstract String move();


}



public class Cat extends Animal
{

	private String name;
	protected String breed;

	public Cat(String name, String breed)
	{
		 super("Cat");
		 this.name=name;
		 this.breed=breed;
	}

	public Cat()
	{
		super("Cat");
	}

	public String describe()
	{
		return new String(",a breed of Cat called");
	}

	public String sound()
	{
		return new String("Meow");
	}

	public String sleep()
	{
		return new String("Kitty is having purfect dreams!");
	}

	public String move()
	{
		return new String("This little Kitty moves fast!");
	}
}


public class Robin extends Bird
{
	private String name;
	protected String breed;

	public Robin(String name)
	{

	}


	public String describe()
	{
		return new String(",a breed of Bird called");
	}

	public String sound()
	{
		return new String("Tweet Tweet!");
	}

	public String sleep()
	{
		return new String("Rockin the Robin Sleeps with one eye open for the cat!");
	}

	public String move()
	{
		return new String("This Robin flies up and away!");
	}
}
[edit]: code tags PB

This post has been edited by PennyBoki: 02 October 2007 - 06:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using Abstract Classes and Methods

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Using Abstract Classes and Methods

Posted 02 October 2007 - 07:08 PM

Hi there, not 100 % sure what are you trying to do, but I made some changes in the code so that things will be more clear, so the modifications I made are in the main() function. This is the code:

public class AbstractTest
{
	public static void main(String[] args)
	{
		Cat cat = new Cat("Kitty", "Angora");
		Robin bird = new Robin("Rockin");

		System.out.println("For the cat:");
		System.out.print("This is:	 \n"+cat.describe()); 
		System.out.print("Sound:	   \n"+cat.sound()); 
		System.out.print("Sleeping:	\n"+cat.sleep()); 
		System.out.print("Moving:	  \n"+cat.move()); 
		System.out.println("\n");

		System.out.println("For the robin:");
		System.out.print("This is:			 \n"+bird.describe()); 
		System.out.print("Sound:			   \n"+bird.sound()); 
		System.out.print("Sleeping:			\n"+bird.sleep()); 
		System.out.print("Moving:			  \n"+bird.move()); 
		System.out.print("\n");

		System.out.println("End of program.");
	}
}


All I did was concatenating the string you get from the methods with the string that should be printed, was that what you needed?

PS please use code tags when posting code.

This post has been edited by PennyBoki: 02 October 2007 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 moses316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 13-September 07

Re: Using Abstract Classes and Methods

Posted 02 October 2007 - 07:39 PM

Well that helped a little, but on the first part of the program it isn't pulling the name of the animal and it's type. Also, the lines aren't lining up like they should. All of the descriptions should be across from one another. Example: For the cat:
This is: Kitty, a breed of Cat call Angora
Sound: Meow
Sleeping: Kitty is having purrfect dreams!
Moving: This little kitty moves fast!

When you run the AbstractTest.java program this is how it should look in the window. How do I post with tags for future reference?






View PostPennyBoki, on 2 Oct, 2007 - 09:08 PM, said:

Hi there, not 100 % sure what are you trying to do, but I made some changes in the code so that things will be more clear, so the modifications I made are in the main() function. This is the code:

public class AbstractTest
{
	public static void main(String[] args)
	{
		Cat cat = new Cat("Kitty", "Angora");
		Robin bird = new Robin("Rockin");

		System.out.println("For the cat:");
		System.out.print("This is:	 \n"+cat.describe()); 
		System.out.print("Sound:	   \n"+cat.sound()); 
		System.out.print("Sleeping:	\n"+cat.sleep()); 
		System.out.print("Moving:	  \n"+cat.move()); 
		System.out.println("\n");

		System.out.println("For the robin:");
		System.out.print("This is:			 \n"+bird.describe()); 
		System.out.print("Sound:			   \n"+bird.sound()); 
		System.out.print("Sleeping:			\n"+bird.sleep()); 
		System.out.print("Moving:			  \n"+bird.move()); 
		System.out.print("\n");

		System.out.println("End of program.");
	}
}


All I did was concatenating the string you get from the methods with the string that should be printed, was that what you needed?

PS please use code tags when posting code.

Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Using Abstract Classes and Methods

Posted 02 October 2007 - 07:44 PM

well then you give it a try, now that you know how to print them. Hint: make a new method to get the name, and the rest is similar. If you run into trouble you know where to post ;)
Was This Post Helpful? 0
  • +
  • -

#5 moses316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 13-September 07

Re: Using Abstract Classes and Methods

Posted 02 October 2007 - 08:02 PM

I shouldn't have to do a new method though since it is supposed to pull the name from the two new categories above should I?

public static void main(String[] args)
{
Cat cat = new Cat("Kitty", "Angora");
Robin bird = new Robin("Rockin");
[size=7]System.out.println("For the cat:");
System.out.print("This is:Kitty \n"+cat.describe());
System.out.print("Sound: \n"+cat.sound());
System.out.print("Sleeping: \n"+cat.sleep());
System.out.print("Moving: \n"+cat.move());
System.out.println("\n");






View PostPennyBoki, on 2 Oct, 2007 - 09:44 PM, said:

well then you give it a try, now that you know how to print them. Hint: make a new method to get the name, and the rest is similar. If you run into trouble you know where to post ;)

Was This Post Helpful? 0
  • +
  • -

#6 moses316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 13-September 07

Re: Using Abstract Classes and Methods

Posted 03 October 2007 - 02:15 PM

Still need some help with this one please.





View Postmoses316, on 2 Oct, 2007 - 10:02 PM, said:

I shouldn't have to do a new method though since it is supposed to pull the name from the two new categories above should I?

public static void main(String[] args)
{
Cat cat = new Cat("Kitty", "Angora");
Robin bird = new Robin("Rockin");
[size=7]System.out.println("For the cat:");
System.out.print("This is:Kitty \n"+cat.describe());
System.out.print("Sound: \n"+cat.sound());
System.out.print("Sleeping: \n"+cat.sleep());
System.out.print("Moving: \n"+cat.move());
System.out.println("\n");






View PostPennyBoki, on 2 Oct, 2007 - 09:44 PM, said:

well then you give it a try, now that you know how to print them. Hint: make a new method to get the name, and the rest is similar. If you run into trouble you know where to post ;)

Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,845
  • Joined: 18-April 07

Re: Using Abstract Classes and Methods

Posted 03 October 2007 - 03:36 PM

Penny is certainly right about the approach you have to take. Notice that you store the name in your class, but you don't have anything to actually return the name. If you can't get at the name, how can you print it out? Then to print out the lines in order would be just a matter of calling your methods. Here is a nice line for you to munch on as well...

System.out.println("Kitty is a class of: " + cat.getClass().getName());



The above code will get the type of animal that kitty is. So in this case "Cat". It takes the variable "cat" and gets its class, then gets the name of that class. Since the cat variable is of type "Cat" it returns "Cat".

Now isn't programming fun?!?! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1