dcsimg
Need advice with extending and implementing an Abstract class
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dianne_Calhoun
Posted On:   Sunday, October 7, 2001 04:46 PM

Can you help me figure out why I'm getting all the errors that are at the end of my code. Hi, I'm working on a program for class, and having problems getting it to compile. There are several files. Here are my instructions: [Instructions] Create the following interfaces and classes: Interface: Speaker public String speak(); Interface: Flyer public double getMaximumSpeed(); Abstract Class: Animal // private attributes private double weight; private double height; // public methods public double getWeight(); public double getHeight(); publi   More>>

Can you help me figure out why I'm getting all the errors that are at the end of my code.



Hi, I'm working on a program for class, and having problems getting it to compile.

There are several files.

Here are my instructions:


[Instructions]
Create the following interfaces and classes:



Interface: Speaker

public String speak();


Interface: Flyer

public double getMaximumSpeed();


Abstract Class: Animal


// private attributes

private double weight;

private double height;



// public methods

public double getWeight();

public double getHeight();

public void setWeight(double weight);

public void setHeight(double height);


Class: Dog

Dog extends Animal and implements Speaker


Class: Cat

Cat extends Animal and implements Speaker


Class: Bird

Bird extends Animal and implements Speaker and Flyer


Class: Bat

Bat extends Animal and implements Flyer



Create a TestAnimal.java driver program that creates an
array of animals that are dogs, cats, birds, bats.


Loop through array and print out the weight and
height of the animal.

If the animal is a speaker,
print out its sound.

If the animal is a flyer print
out its maximum speed.



Hint: You will need to use instanceof operator to know when to
call speak() and getMaximumSpeed().
[/end Instructions]


			
Animal.java

// This program is the Abstract Animal class

import java.lang.String.*;

public abstract class Animal
{
// private attrributes
private double weight;
private double height;

// public methods
public double getWeight();
public double getHeight();
public void setWeight(double weight);
public void setHeight(double height);

public Animal()
{
System.out.println("Creating Animal Object...");
}

public abstract void getWeight();
{
System.out.println("The Animal's weight is...");
}

public abstract void getHeight();
{
System.out.println("Your Animal's height is...");
}
}

			
Bird.java

// This program is Bird extends Animal and implements Speaker and Flyer

import java.lang.String.*;

public class Bird extends Animal and implements Speaker, Flyer
{
public Bird()
{
System.out.println("Creating Bird...");
speak = String("tweet tweet");
getMaximunSpeed = 10;
}

public String speak();
{
System.out.println("Bird says "+speak+);
}

public double getMaximunSpeed();
{
System.out.println("Bird flies "+getMaximumSpeed+);
}

public static void main(String[] args)
{
Bird myBird= new Bird();
myBird.speak();
myBird.getMaximumSpeed();
}
}

			
Bat.java
// This program is Bat extends Animal and implements Flyer

import java.lang.String.*;

public class Bat extends Animal and implements Flyer
{
public Bat()
{
System.out.println("Creating Bat...");
getMaximunSpeed = 15;
}
public double getMaximunSpeed();
{
System.out.println("Bat flies "+getMaximumSpeed+);
}

public static void main(String[] args)
{
Bat myBat= new Bat();
myBat.getMaximumSpeed();
}
}

			
Cat.java
// This program is Cat extends Animal and implements Speaker

import java.lang.String.*;

public class Cat extends Animal and implements Speaker
{
public Cat()
{
System.out.println("Creating Cat...");
speak = String("meow");
}
public String speak();
{
System.out.println("Cat says "+speak+);
}

public static void main(String[] args)
{
Cat myCat= new Cat();
myCat.speak();
}
}

			
Dog.java
// This program is Dog extends Animal and implements Speaker

import java.lang.String.*;

public class Dog extends Animal implements Speaker
{
public Dog()
{
System.out.println("Creating Dog...");
speak = String("bow wow");
}
public String speak();
{
System.out.println("Dog says "+speak+);
}

public static void main(String[] args)
{
Dog myDog= new Dog();
myDog.speak();
}
}

			
Flyer.java
// This program is the definition of interface Flyer

public interface Flyer
{
// getMaximumSpeed
public abstract double getMaximumSpeed();

// return Flyer getMaximumSpeed
public abstract String getMaximumSpeed();
}

			
Speaker.java
// This program is the definition of interface Speaker

public interface Speaker
{
// Speaker
public abstract double speak();

// return Speaker name
public abstract String speak();
}

			
TestAnimal.java
/* This program creates an array of animals that are dogs, cats, birds, bats.
* Loops through an array and prints out the weight and height of the animal.
* If the animal is a speaker, print out its sound. If the animal is a flyer
* print out its maximum speed.
*/

public class TestAnimal
{
public static void main( String args[] )
{
Animal[] animalArray;
animalArray = new Animal[4];

for()
{
animal a = animal [i];
a.getweight;
a.getheight;

if (a instance of Speaker)

((Speaker)a).speak();

if (a instance of getMaximumSpeed)

((getMaximumSpeed)a).getMaximumSpeed();

}
}
}



Begin errors

errors

Microsoft Windows 2000 [Version
5.00.2195]

(C) Copyright 1985-2000 Microsoft Corp.


C:myjava>cd assignment3


C:myjavaassignment3>cd problem2


C:myjavaassignment3problem2>javac
TestAnimal.java

TestAnimal.java:17: illegal start of
expression

for()

^

TestAnimal.java:31: ';' expected

}

^

TestAnimal.java:32: illegal start of
expression

}

^

.Animal.java:25: getWeight() is
already defined in Animal

public abstract void getWeight();

^

.Animal.java:30: getHeight() is
already defined in Animal

public abstract void
getHeight();

^

.Animal.java:15: missing method body, or declare abstract

public double getWeight();

^

.Animal.java:16: missing method body, or declare abstract

public double getHeight();

^

.Animal.java:17: missing method body, or declare abstract

public void setWeight(double weight);

^

.Animal.java:18: missing method body, or declare abstract

public void setHeight(double height);

^

9 errors


C:myjavaassignment3problem2>

errors



So, as you can see it's a lengthy problem, and I would appreciate any help you could offer. Thanks

   <<Less

Re: Need advice with extending and implementing an Abstract class

Posted By:   Jeff_Hubbach  
Posted On:   Sunday, October 7, 2001 07:59 PM


::::::::::::::
Animal.java
::::::::::::::
public abstract class Animal
{
// private attrributes
private double weight;
private double height;
// public methods
public double getWeight()
{ return weight; }
public double getHeight()
{ return height; }
public void setWeight(double weight)
{ this.weight = weight; }
public void setHeight(double height)
{ this.height = height; }
}
::::::::::::::
Bat.java
::::::::::::::
public class Bat extends Animal implements Flyer
{
public Bat()
{
setWeight(2.3);
setHeight(1.2);
}
public double getMaximumSpeed()
{ return (3.4); }
}
::::::::::::::
Bird.java
::::::::::::::
public class Bird extends Animal implements Speaker, Flyer
{
Bird()
{
setWeight(3.4);
setHeight(2.6);
}
public String speak()
{ return new String("tweet tweet"); }
public double getMaximumSpeed()
{ return 12.0; }
}
::::::::::::::
Cat.java
::::::::::::::
public class Cat extends Animal implements Speaker
{
public Cat()
{
setWeight(10.5);
setHeight(3.2);
}
public String speak()
{ return new String("Meow"); }
}
::::::::::::::
Dog.java
::::::::::::::
public class Dog extends Animal implements Speaker
{
public Dog()
{
setWeight(20.5);
setHeight(10.5);
}
public String speak()
{ return new String("Woof"); }
}
::::::::::::::
Flyer.java
::::::::::::::
public interface Flyer
{
public double getMaximumSpeed();
}
::::::::::::::
Speaker.java
::::::::::::::
public interface Speaker
{
public String speak();
}
::::::::::::::
TestAnimal.java
::::::::::::::
public class TestAnimal
{
public static void main( String args[] )
{
Animal[] animalArray = { new Bird(), new Bat(), new Dog(), new Cat()};
for(int i = 0; i < animalArray.length; i++)
{
System.out.println("Weight is "+animalArray[i].getWeight());
System.out.println("Height is "+animalArray[i].getHeight());
if (animalArray[i] instanceof Speaker)
{
System.out.println("This animal says "+((Speaker)animalArray[i]).speak());
}
if (animalArray[i] instanceof Flyer)
{
System.out.println("This animal's maximum speed is "+((Flyer)animalArray[i]).getMaximumSpeed());
}
}
}
}




There are alot more problems with your code than just not understanding abstracts. Some of your methods that you defined as returning a String aren't returning anything, but instead you're doing System.out.println in it. Also, the empty for() will never work. The minimal for() loop is:

for(;;){}



Jeff.
About | Sitemap | Contact