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
  • 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 four parts for one code, all are small code segments for one question. public class StudentTest { public static void main(String[] args){ // Part Time Student Test Student ft1 = new PartTimeStudent("George", "Bush", "123-12-5678", 1, 2 ); System.out.println(ft1); Student ft2 = new PartTimeStudent("Abraham", "Lincoln", "001-90-5323", 6, 22 ); System.out.println(ft2); // Full Time Student Test Student pt1 = new FullTimeStudent("Nikola", "Tesla", "442-00-0998", 8, 26...

  • To do: Now add code to Pet and PetPlay to complete the comments in the code....

    To do: Now add code to Pet and PetPlay to complete the comments in the code. import java.util.ArrayList; public class PetPlay {    //-----------------------------------------------------------------    // Stores and modifies a list of pets    //----------------------------------------------------------------- public static void main (String[] args)    { // always make your array lists generic ArrayList pets = new ArrayList(); pets.add(new Pet("dog", "Bella")); pets.add(new Pet("cat","Blackie")); pets.add(new Pet("bird", "Babs")); pets.add(new Pet("dog", "Lassie")); pets.add(new Pet("horse", "Sam")); pets.add(new Pet("dog", "Blackie")); pets.add(new Pet("turtle", "Joe")); pets.add(new Pet("bird","Annie")); pets.add(new Pet("bird", "Alex"));...

  • 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...

  • Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes...

    Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes named Cat.java and Dog.java. Cat.java should add attributes for color and breed. Dog.java should add attributes for breed and size (use String for small, medium, large). Add appropriate constructors, getter/setter methods and toString() methods.   In a separate file named PetDriver.java, create a main. In the main, create an ArrayList of Pet. Add an instance of Cat and an instance of Dog to the ArrayList....

  • 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 )

  • Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the...

    Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the TA class to represent the course the student is TA'ing. 1.3) Provide a constructor for the class so the code in the PeopleFactory will work as provided. 1.4) Provide a toString() method for the TA  class so that TA's will output as shown in the sample code. Provide a class Staff so that you can un-comment the lines of code in the PeopleFactory class that...

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