??


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: ??

  1. #1
    foolguy Guest

    ??


    public class Animal
    {

    public Animal(String aType)
    {
    type=new String(aType);
    }

    public String toString()
    {
    return "This is a " + type;
    }

    public void sound(){}



    private String type;
    }

    //other class

    public class Cat extends Animal
    {
    public Cat(String aName)
    {
    super("Cat");//call the base constructor
    name=aName;
    breed="Unknown";
    }

    public Cat(String aName, String aBreed)
    {
    super("Cat");
    name=aName;
    breed=aBreed;
    }

    public String toString()
    {
    return super.toString()+"\nIt's"+name+"the"+breed;
    }

    public void sound()
    {
    System.out.println("Miiaooww");
    }

    private String name;
    private String breed;
    }
    //<the other class also same like Cat>

    //main
    import java.util.Random;

    public class Trypoly
    {
    public static void main(String[] args)
    {
    Animal[] theAnimals={
    new Dog("Rover","Poodle"),
    new Cat("Max","Abyssinian"),
    new Duck("Daffy","Aylesbury"),
    new human("mkyong1","mkyong2")
    };

    Animal Choice;

    Random select=new Random();

    for(int i=0;i<6;i++)
    {
    Choice = theAnimals[select.nextInt(theAnimals.length)];

    System.out.println("\nYour choice:\n"+Choice);

    Choice.sound();
    }
    }
    }

    when i run , i got this message

    C:\JDK1.3\MKYONG>javac Trypoly.java
    Trypoly.java:24: cannot resolve symbol
    symbol : method sound ()
    location: class Animal
    Choice.sound();
    ^
    1 error

    How do solve this <i'm a newcomer to java>
    please help....

  2. #2
    Niklas Junel Guest

    Re: ??


    Since I didn't have all the classes I took the lines that tries to make an
    object of dog... away. Anyway, You cant't have more than one public class
    in each source-file. So I made the other classes non-public. The main-method
    should exist in the public class (otherwise the system won't see the method).
    As You only had the main-method in the last class, I took it away,and placed
    the main-method in the Animal-class,this way I got it to work. Of course
    You could have all the classes in different files, that way they can all
    be public.

    Niklas

    import java.util.Random;
    public class Animal
    {

    public Animal(String aType)
    {
    type=new String(aType);
    }

    public String toString()
    {
    return "This is a " + type;
    }

    public void sound(){}



    private String type;

    //put Your main-method in the public class

    public static void main(String[] args)
    {
    Animal[] theAnimals={new Cat("Max","Abyssinian")};

    Animal Choice;

    Random select=new Random();

    for(int i=0;i<6;i++)
    {
    Choice = theAnimals[select.nextInt(theAnimals.length)];

    System.out.println("\nYour choice:\n"+Choice);

    Choice.sound();
    }
    }
    }

    //other class

    class Cat extends Animal
    {
    public Cat(String aName)
    {
    super("Cat");//call the base constructor
    name=aName;
    breed="Unknown";
    }

    public Cat(String aName, String aBreed)
    {
    super("Cat");
    name=aName;
    breed=aBreed;
    }

    public String toString()
    {
    return super.toString()+"\nIt's"+name+"the"+breed;
    }

    public void sound()
    {
    System.out.println("Miiaooww");
    }

    private String name;
    private String breed;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center