Hi everyone I have a issue where I have written the entire program but couple little tweaks are needed for me to get the program done.

I have my super class here "Pet". I have created couple objects in Main, "myRobin" , "myCow", "myBlackMamba" and passed some arguments to them. Im now trying to send those parameters and have them printed to the output via the toString method I have. Im learning my way around Java ..so Im wondering what am I missing that could make this possible....

Thanks everyone.

public class Pet 
{
    //office wants to store info regarding the animal it treats

    /*
     * Done  
     */

    ///variables 
    /*Diet
     * Nocturnal
     * poisionous 
     * ability to fly
     */

    /*
     * Done 
     */

    //Classes 
    /*SuperClass pet 
     * subClass: 
     * robin, cow, black mamaba 
     */

    /*
     * Done 
     */

    ///Characteristics
    /*Num. of Legs 
     *  wings or no
     *  feathers , skin or fur
     *  nocturnal
     */
    private int numOfLegs;
    private String wings; 
    private String skin;
    private String fur; 
    private String nocturnal;
    private String diet; 
    private String poison;

    public Pet(int l , String w , String s, String n, String d, String p)
    {
        numOfLegs = l;
        wings = w;
        skin = s;
        nocturnal = n;
        diet = d; 
        poison = p;
    }

    //will allow me to create subclasses 
    public Pet() 
    {

    }

    //main start 
    public static void Main(String[] args)
    {
        //return what each different class does 
        Robin myRobin = new Robin(2, "Two Wings" , "feathers" , "No I sleep" , "berries" , "I am not poisonous!" );
        myRobin.toString();


        Cow myCow = new Cow(4,"No Wings", "little fur" , "I sleep, moo!", "I love to eat hay and grass", "Im not poisionous :(");
        myCow.toString();

        BlackMamba myBlackMamba = new BlackMamba(0, "No Wings","skin","awake at night","I love eating rodents","Of course Im poisonous");
        myBlackMamba.toString();

        Pet p = new Pet(); 

    }

    //might delete this 
    public void Report() 
    {
        System.out.println("This is the information for the pet."); 

    }

    ///Generate getters and setters to access private data fields 
    public String getWings() {
        return wings;
    }

    public void setWings(String wings) {
        this.wings = wings;
    }

    public String getSkin() {
        return skin;
    }

    public void setSkin(String skin) {
        this.skin = skin;
    }

    public String getFur() {
        return fur;
    }

    public void setFur(String fur) {
        this.fur = fur;
    }

    public String getNocturnal() {
        return nocturnal;
    }

    public void setNocturnal(String nocturnal) {
        this.nocturnal = nocturnal;
    }

    public String getDiet() {
        return diet;
    }

    public void setDiet(String diet) {
        this.diet = diet;
    }

    public String getPoison() {
        return poison;
    }

    public void setPoison(String poison) {
        this.poison = poison;
    }

    public void setNumOfLegs(int numOfLegs) {
        this.numOfLegs = numOfLegs;
    }

    public int getNumOfLegs()
    {
        return numOfLegs;
    }
    ////end getters and setters 



    public String toString()
    {
        return("Robin:" + wings );

    }



    ///Test all subclasses.

    //start with

}

and then my other three classes "Robin, Cow, BlackMamba" . Since these three subclasses are pretty much similar except for the names I will post the Robin class here:

package com.Java.CS300;


public class Robin extends Pet 
{

    ///Determine whether this constructor is needed 
    public Robin(int l , String w , String s, String n, String d, String p) 
    {

    }

    //myPet is of type "Pet"
    public Pet myPet;

    ///list the characteristics here and return a message which concludes what the animal does 

    //these are the methods supplied to the animal 

    public void Legs(Pet p)
    {
        myPet = p;
    }

    public Pet doIFly() 
    {
        return myPet;

    }

    public Pet mySkin() 
    {
        return myPet; 
    }

    public Pet poison()
    {
        return myPet;
    }

    public Pet nocturnal() 
    {
        return myPet;
    }

    public Pet diet() 
    {
        return myPet;
    }

    //method for animal to report its characteristics
    ////might delete this 
    public void infoRobin(int l , String w , String f, String s, String n, String d, String p)
    {
        //call from main Robin object


    } // end of report on Robin

}

You are not too far from what you want to acomplish.

You will not need the myPet variable in Robin , nor most of the functions you gave it.

First its gonna be important that you keep separated what is comon to all pets, and what is specific to a certain subclass. Everything that is common should go in the class Pet. Right now, your subclasses are not adding anything specific to them to the Pet class , while the personalised robin tostring method is in the Pet class.

The good news is you are correctly inherithing from the Pet class. so lets start from there.

public class Vehicule{
    private int nbPassengers;

    public Vehicule(int passengers){
        nbPassengers = passengers;
    }

    public int getNbPassengers(){
        return nbPassengers;
    }

    public void setNbPassengers(int passengers){
        nbPassengers = passengers;
    }
}

public class Car extends Vehicule{
    private int horsePower; //adds something specific to car that doesnt relate to all vehicules

    public Car(int passengers , int hp){
        super(passengers); //calls Vehicule constructor
        horsePower = hp;
    }

    public int getHorsePower(){
        return horsePower;
    }

    public void setHorsePower(int hp){
        horsePower = hp;
    }
}

code not actually tested just typed this to demonstrate oop

as for the toString method, each class will have their own , and the subclass' toString will overwrite the Vehicules method.

public class Vehicule{
    private int nbPassengers;

    public Vehicule(int passengers){
        nbPassengers = passengers;
    }

    public int getNbPassengers(){
        return nbPassengers;
    }

    public void setNbPassengers(int passengers){
        nbPassengers = passengers;
    }

    public String toString(){
        String s = "";
        s += "Vehicule : \n\tPassengers : " + nbPassengers;

        return s;
    }
}

public class Car extends Vehicule{
    private int horsePower; //adds something specific to car that doesnt relate to all vehicules

    public Car(int passengers , int hp){
        super(passengers); //calls Vehicule constructor
        horsePower = hp;
    }

    public int getHorsePower(){
        return horsePower;
    }

    public void setHorsePower(int hp){
        horsePower = hp;
    }


    public String toString(){
        String s = "";
        s += "Car : \n\tPassengers : " + nbPassengers + "\n\tHorse Power : " + horsePower;

        return s;
    }
}

again code not actually tested just typed this to demonstrate oop

Now the beauty about this , is lets say you had a boat class and a plane class with these. You could build an array of Vehicules, fill it with a mix of Cars, Boats and Planes. Then loop through the array and call the object's toString method. And each object will try to call their respective toString method. If you forget to implement the Plane class' toString method, then it would fall back to Vehicule's toString method.

hope this helps clarify the oop and toString situation, let me know how that works for you :)

I will try this and report back how it goes , thanks for all your wonderful help!

when I have toStrings defined already in the other three subclasses similar to what you have how do I pass each individuals charateristics (Robin, Mamba, Cow) into the toString in the Main Class? How will the program know how to sort it?

Ok so this is what I have so far for the Robin Class:

public class Robin extends Pet 
{
    //come back to this 
    private String wings= "yes";
    private String skin = "feathers";
    private String legs = "two";
    private String nocturnal = "no";
    private String diet = "berries";
    private String poison = "no";



    ///Determine whether this constructor is needed 
    public Robin(int l , String w , String s, String n, String d, String p) 
    {


    }



    public String toString()
    {
        String s = "";
        s +="Pet: \n\tRobin:" + wings + "\n\tLegs:" + legs + "\n\tSkinType:" + skin;

        return s;
    } // end of toString 

}

and my modified Pet Class:

package com.Java.CS300;

public class Pet 
{
    //office wants to store info regarding the animal it treats

    /*
     * Done  
     */

    ///variables 
    /*Diet
     * Nocturnal
     * poisionous 
     * ability to fly
     */

    /*
     * Done 
     */

    //Classes 
    /*SuperClass pet 
     * subClass: 
     * robin, cow, black mamaba 
     */

    /*
     * Done 
     */

    ///Characteristics
    /*Num. of Legs 
     *  wings or no
     *  feathers , skin or fur
     *  nocturnal
     */
    private int numOfLegs;
    private String wings; 
    private String skin;
    private String fur; 
    private String nocturnal;
    private String diet; 
    private String poison;

    public Pet(int l , String w , String s, String n, String d, String p)
    {
        numOfLegs = l;
        wings = w;
        skin = s;
        nocturnal = n;
        diet = d; 
        poison = p;
    }

    //will allow me to create subclasses 
    public Pet() 
    {

    }

    //main start 
    public static void Main(String[] args)
    {
        //return what each different class does 
        Robin myRobin = new Robin(2, "Two Wings" , "feathers" , "No I sleep" , "berries" , "I am not poisonous!" );
        myRobin.toString();


        Cow myCow = new Cow(4,"No Wings", "little fur" , "I sleep, moo!", "I love to eat hay and grass", "Im not poisionous :(");
        myCow.toString();

        BlackMamba myBlackMamba = new BlackMamba(0, "No Wings","skin","awake at night","I love eating rodents","Of course Im poisonous");
        myBlackMamba.toString();

        Pet p = new Pet(); 

    }

    //might delete this 
    public void Report() 
    {
        System.out.println("This is the information for the pet."); 

    }

    ///Generate getters and setters to access private data fields 
    public String getWings() {
        return wings;
    }

    public void setWings(String wings) {
        this.wings = wings;
    }

    public String getSkin() {
        return skin;
    }

    public void setSkin(String skin) {
        this.skin = skin;
    }

    public String getFur() {
        return fur;
    }

    public void setFur(String fur) {
        this.fur = fur;
    }

    public String getNocturnal() {
        return nocturnal;
    }

    public void setNocturnal(String nocturnal) {
        this.nocturnal = nocturnal;
    }

    public String getDiet() {
        return diet;
    }

    public void setDiet(String diet) {
        this.diet = diet;
    }

    public String getPoison() {
        return poison;
    }

    public void setPoison(String poison) {
        this.poison = poison;
    }

    public void setNumOfLegs(int numOfLegs) {
        this.numOfLegs = numOfLegs;
    }

    public int getNumOfLegs()
    {
        return numOfLegs;
    }
    ////end getters and setters 



    public String toString()
    {
        String s = "";
        s += "Pet :  \n\tRobin:" +  ;


        return s;

    }



    ///Test all subclasses.

    //start with

}

im not sure i understand what you want to know.

you define a generic toString for "any pet" in the Pet class, then a more specific toString for each class in their respective class.

then in your main, whatever is the class of the Object you call toString from, it will call the toString method furthest down the inheritence history.

For example, if you do NOT define toString for Robin but you do for Pet and Cow. then calling toString from a Cow object would call the Cow's class toString method, while calling toString from a Robin object would call the Pet's class toString.

Everything extends from close or far the Object Class, and thus if you do not define ANY toString methods in your class you'd eventually be calling the Object class' toString method.

Nevermind I eventually figured it out, the problem was my Main couldnt see any of my variables so I pushed everything into one "classfile" and trimmed the program...
thanks for the help man though you pointed me in the right direction!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.