Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

10L: I need help with a JAVA program. I need a simple program to this question.

ID: 662456 • Letter: 1

Question

10L: I need help with a JAVA program. I need a simple program to this question.

(a) Create a Pet class. The class should have a name for the pet and two methods: speak and locomotion.

(b) Extend the class to have Cat, Dog, Fish. Appropriate behavior should be exhibited.

(c) Create a main program, that has an array of Pets. Write a for loop that iterates over the array and exhibits the speak and locomotion behavior. Use an enhanced for loop.

thank you for your help. please keep the program as simple as possible,

Explanation / Answer

package com.tutai;

class Pet{
   private String name;

   Pet(String name){
       this.name=name;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }  
   public void speak(){}
   public void locomotion(){}
}

class Cat extends Pet{
   Cat(String name){
       super(name);
   }
   public void speak(){
       System.out.println("Cat speak");
   }
   public void locomotion(){
       System.out.println("Cat locomotion");
   }
}

class Dog extends Pet{
   Dog(String name){
       super(name);
   }
   public void speak(){
       System.out.println("Dog speak");
   }
   public void locomotion(){
       System.out.println("Dog locomotion");
   }
}

class Fish extends Pet{
   Fish(String name){
       super(name);
   }
   public void speak(){
       System.out.println("Fish speak");
   }
   public void locomotion(){
       System.out.println("Fish locomotion");
   }
}

public class PetDriver {  
   public static void main(String[] args){
       Pet[] pets=new Pet[3];
       pets[0]=new Cat("Cat 1");
       pets[1]=new Dog("Dog 1");
       pets[2]=new Fish("Fish 1");
       for(Pet p:pets){
           System.out.println("-------------------------");
           p.getName();
           p.speak();
           p.locomotion();
           System.out.println("-------------------------");
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote