Q:

a complete example of OOP in java

0

write the following OOP program in java:

  • Class name: Human, contains the following:

         properties of the human: name, eye color, skin color, hair color, gender

         methods of human: breath, talk, walk

         getters and setters for all properties of human

         constructors: 1 no-argument constructor, and 1 full-argument constructor

         static property: define the the counter property, which increases by 1 in each instance initialization.

  • create 2 objects of Human class, and initializa an instances for them , initialize the first instance using the no-argument constrcutor, and the second instance using the full-argument constructor.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

class Creature:

public class Creature {

    private int weight;
    private int height;
    private int bornYear;
    private boolean gender; //true for male, false for female

    //setters
    public int getWeight() {
        return weight;
    }

    public int getHeight() {
        return height;
    }

    public int getBornYear() {
        return bornYear;
    }

    public String getGender() {
        if (gender == true) {
            return "male";
        } else {
            return "female";
        }
    }

    //getters
    public void setWeight(int weight) {
        this.weight = weight;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setBornYear(int bornYear) {
        this.bornYear = bornYear;
    }

    public void setGender(String gender) {
        if (gender == "male") {
            this.gender = true;
        } else if (gender == "female") {
            this.gender = false;
        } else {
            System.out.println("invalid gender!");
        }
    }

    //no-arg constructer
    public Creature() {
    }

    //full-args contructer
    public Creature(int weight, int height, int bornYear, String gender) {
        this.weight = weight;
        this.height = height;
        this.bornYear = bornYear;
        setGender(gender);
    }

    //toString method
    public String toString() {
        return "weight: " + getWeight() + ", height: " + getHeight() + ", bornYear: " + getBornYear() + ", gender: " + getGender();
    }

    //creature methods
    public void breath() {
        System.out.println("creature Breathing");
    }

    public void walk() {
        System.out.println("creature walking");
    }
}

 

Human:

public class Human extends Creature {

    private String name;
    private String skinColor;
    private String eyeColor;
    private static int counter=0;
    //setters

    public void setName(String name) {
        this.name = name;
    }

    public void setSkinColor(String skinColor) {
        this.skinColor = skinColor;
    }

    public void setEyeColor(String eyeColor) {
        this.eyeColor = eyeColor;
    }
    //getters

    public String getName() {
        return name;
    }

    public String getSkinColor() {
        return skinColor;
    }

    public String getEyeColor() {
        return eyeColor;
    }

    //no-arg constructer

    public Human() {
        counter++;
    }
    //full-args contructer
    public Human(String name, String skinColor, String eyeColor, int weight, int height, int bornYear, String gender) {
        super(weight, height, bornYear, gender);
        this.name = name;
        this.skinColor = skinColor;
        this.eyeColor = eyeColor;
        counter++;
    }

    //human methods
    @Override
    public void walk() {
        System.out.println("Human is walking");
    }

    @Override
    public void breath() {
        System.out.println("Human is breathing");
    }

    public void talk() {
        System.out.println("Human is talking");
    }
    
    @Override
    public String toString()
    {
        return "Name :"+getName()+", skin color: "+getSkinColor()+", Hair color: "+getEyeColor()+super.toString();
    }
}

 

Animal:

public class Animal extends Creature {

    private boolean type; //true for Herbivores, and false for Carnivores,,,,,,,,,type of animal either Herbivores or Carnivores (Herbivores are animals that eat only plants. Carnivores are animals that eat only meat)    

    private static int counter = 0;

//setters
    public void setType(String type) {
        if(type=="Herbivores")
            this.type=true;
        else if(type=="Carnivores")
            this.type=false;
    }

    public static void setCounter(int counter) {
        Animal.counter = counter;
    }

    //getters
    public String getType() {
        if (type == true) {
            return "Herbivores";
        } else {
            return "Carnivores";
        }
    }

    public static int getCounter() {
        return counter;
    }
    
    //no-arg constructer
    public Animal() {
        counter++;
    }

    //full-args contructer
    public Animal(String type, int weight, int height, int bornYear, String gender) {
        super(weight, height, bornYear, gender);
        setType(type);
        counter++;
    }

//animal methods
    @Override
    public void walk() {
        System.out.println("Animal is Walking");
    }

    @Override
    public void breath() {
        System.out.println("Animal is Breathing");
    }

    public void animalSound() {
        System.out.println("Animal is making a sound");
    }
    @Override
    public String toString()
    {
        return "Type :"+getType()+super.toString();
    }
}

 

Class OOP_Human:

public class OOP_Human {

    public static void main(String[] args) {
        //create human instance by calling no-args contructer
        Human h1 = new Human();
        //create animal instance by calling no-args contructer
        Animal a1 = new Animal();
        
        //create human instance by calling full-args contructer
        Human h2 = new Human("mohammad","red", "black", 12, 50, 91, "male");

        //create animal instance by calling full-args contructer
        Animal a2 = new Animal("Carnivores", 60, 50, 2017, "female");       

//this is an example of polymorphism: create creatures array, and insert the human object and animal object inside it
        Creature []cArr=new Creature[4];
        cArr[0]=h1;
        cArr[1]=a1;
        cArr[2]=h2;
        cArr[3]=a2;
        
        //printing the creatures information:
        for(int i=0;i<cArr.length;i++)
        {
            System.out.println(cArr[i].toString());
        }
    }   
}

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

OOP Example in java | class, method, constructor, ... >>
<< Object Oriented Programming project Using Java | M...