Question

What is wrong with this Code? Fix the code errors to run correctly without error. There...

What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed.

Does not need Debugging:

public class Bird extends Pet {

public Bird(String name)
{
super(name);
}

public void saySomething(){}

}

public class Cat extends Pet {
  
public Cat(String name)
{
super(name);
}

@Override
public void saySomething()
{
System.out.println("Meow.");
}

@Override
public String toString()
{
return "Hello, I'm " + name + " the cat.";
}
}

public class Dog extends Pet {

public Dog (String name)
{
super(name);
}

@Override
public void saySomething()
{
System.out.println("Ruff, ruff!");
}

@Override
public String toString()
{
return "Hello, I'm " + name + " the dog.";
}
}

public class Parrot extends Bird {

public Parrot (String name)
{
super(name);
}

@Override
public void saySomething()
{
System.out.println("Pauly want a craker?");
}

@Override
public String toString()
{
return "Hello, I'm " + name + " the parrot.";
}

}

Needs to be Fixed:

public abstract class Pet {

String name;

public Pet(String name){
this.name = name;
}
}

import java.util.ArrayList;

public class PetTest {

public static void main(String[] args)
{
ArrayList<Pet> myPets = new ArrayList<Pet>();
  
myPets.add(new Parrot("Boss"));
myPets.add(new Cat("Oreo"));
myPets.add(new Dog("Riley"));
  
for (Pet p: myPets)
{
p.saySomething();
System.out.println(p);
}
}
}

public class Speak {
public void saySomething();
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the complete code below>>

Pet.java

package classes16;

public abstract class Pet {

   String name;

   public Pet(String name){
       this.name = name;
   }
  
   public abstract void saySomething();
}

Bird.java

package classes16;

public class Bird extends Pet {

public Bird(String name)
{
super(name);
}

public void saySomething(){}

}


Cat.java

package classes16;

public class Cat extends Pet {

   public Cat(String name)
   {
       super(name);
   }

   @Override
   public void saySomething()
   {
       System.out.println("Meow.");
   }

   @Override
   public String toString()
   {
       return "Hello, I'm " + name + " the cat.";
   }
}

Dog.java

package classes16;

public class Dog extends Pet {

   public Dog (String name)
   {
       super(name);
   }

   @Override
   public void saySomething()
   {
       System.out.println("Ruff, ruff!");
   }

   @Override
   public String toString()
   {
       return "Hello, I'm " + name + " the dog.";
   }
}

Parrot.java

package classes16;

public class Parrot extends Bird {

public Parrot (String name)
{
super(name);
}

@Override
public void saySomething()
{
System.out.println("Pauly want a craker?");
}

@Override
public String toString()
{
return "Hello, I'm " + name + " the parrot.";
}

}

PetTest.java

package classes16;
import java.util.ArrayList;

public class PetTest {
   public static void main(String[] args)
   {
       ArrayList<Pet> myPets = new ArrayList<Pet>();

       myPets.add(new Parrot("Boss"));
       myPets.add(new Cat("Oreo"));
       myPets.add(new Dog("Riley"));

       for (Pet p: myPets)
       {
           p.saySomething();
           System.out.println(p);
       }
   }
}

output:

Console <terminated> PetTest [Java Application] C\Program Files Pauly want a Hello, Im Boss the parrot. Meow craker? Hello,

Add a comment
Know the answer?
Add Answer to:
What is wrong with this Code? Fix the code errors to run correctly without error. There...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with this Java question in 1 hour please JAVA Using the files from the...

    Need help with this Java question in 1 hour please JAVA Using the files from the in-class assignment add two additional animals of your choice. For each animal add two additional methods appropriate to your particular animal. As the majority of the code for this program exists you will not need an algorithm. Use the 3 java classes below. Thank you //Animal.java public class Animal {    public Animal() {    System.out.println("A new animal has been created!");    }   ...

  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are two parts for one code (one question) the two parts branch off, one part has 2 sheets of code, the other has 3 sheets of code, all are small code segments for one question. Pt.1 - 2 sheets of code: public class Computer { private String make; private String type; private String modelNumber; private double cpuSpeed_GHz; private int ramSize_GB; private int hardDriveSize_GB; private...

  • This is the question about object-oriend programming(java) please show the detail comment and prefect code of...

    This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** *           Program:          PRG/421 Week 1 Analyze Assignment *           Purpose:         Analyze the coding for...

  • The method m() of class B overrides the m() method of class A, true or false?...

    The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...

  • What will be result of each method call in the main method for the codes shown...

    What will be result of each method call in the main method for the codes shown below? public class Pett public void speak) System.out.println ("speaking!") public void jump) System.out.println ("jumping!") public void walk)i System.out.println ("walking!") public class Dog extends Pet t public class SimpleInheritance i public static void main (String args [1) Dog myDognew Dog ); myDog.speak) myDog.walk) myDog.jump )

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Can you turn this code into a C++ class diagram? Animals.h #1 fndef ANIMALS-H #define ANIMALSH...

    Can you turn this code into a C++ class diagram? Animals.h #1 fndef ANIMALS-H #define ANIMALSH #include <string> class Animal { - public: Animal (const std::string&name: fName( name virtual void Speak O const0 th protected: std::string fName; !; // class Animal class Dogpublic Animal public: Dog (const std::string& nameAnimal ( name virtual void SpeakO const; //class Dog class Cat public Animal f public: Cat ( const std::string& nameAnimal(name) t virtual void Speak O const; 1; // class Cat class Tiger:public...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT