Question

Factory Pattern Uncomment the code below and write the appropriate classes to make it work UNCOMMENT...

Factory Pattern
Uncomment the code below and write the appropriate classes to make it work

UNCOMMENT THIS
Animal goose = AnimalFactory.create("Goose");
Animal dog = AnimalFactory.create("Dog");
Animal cow = AnimalFactory.create("Cow");

goose.makeSound(); // Honk
dog.makeSound(); // Woof
cow.makeSound(); // mooo

0 0
Add a comment Improve this question Transcribed image text
Answer #1
interface Animal {
    public void makeSound();
}

class Goose implements Animal {

    @Override
    public void makeSound() {
        System.out.println("Honk");
    }
}

class Dog implements Animal {

    @Override
    public void makeSound() {
        System.out.println("Woof");
    }
}

class Cow implements Animal {

    @Override
    public void makeSound() {
        System.out.println("mooo");
    }
}

class AnimalFactory {


    public static Animal create(String type) {
        if (type.equalsIgnoreCase(Goose.class.getSimpleName())) {
            return new Goose();
        } else if (type.equalsIgnoreCase(Dog.class.getSimpleName())) {
            return new Dog();
        } else {
            return new Cow();
        }
    }
}

class AnimalTest {

    public static void main(String[] args) {
        Animal goose = AnimalFactory.create("Goose");
        Animal dog = AnimalFactory.create("Dog");
        Animal cow = AnimalFactory.create("Cow");

        goose.makeSound(); // Honk
        dog.makeSound(); // Woof
        cow.makeSound(); // mooo
    }
}

Add a comment
Know the answer?
Add Answer to:
Factory Pattern Uncomment the code below and write the appropriate classes to make it work UNCOMMENT...
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
  • Abstract Classes and Interfaces. Write the code for all the necessary classes and/or interfaces for a...

    Abstract Classes and Interfaces. Write the code for all the necessary classes and/or interfaces for a solution to the problem below. Focus on class structure and interaction. You may implement your solution however you wish, but you will be graded on the appropriateness of your solution to the requirements. Note the use of capit and bold for clarification in the problem. You may use whatever constructors or additional methods you wish. - Define a structure that can represent Animals. -...

  • Program in Java: Use​ ​the​ ​Factory​ ​pattern​ ​to​ ​address​ ​the​ ​following​ ​challenge... Your job is to...

    Program in Java: Use​ ​the​ ​Factory​ ​pattern​ ​to​ ​address​ ​the​ ​following​ ​challenge... Your job is to make it easy to obtain a Pet from a factory-like class: ● Pet is an abstract class that has 2 attributes ○ Name ○ Sound ● You should create 3 concrete types of pets ○ Parakeet (makes the sound "Tweet tweet") ○ Dog (makes the sound "Woof woof") ○ Lion (makes the sound "Roar roar") ● You should create a factory class that allows...

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

  • The files at the links below contain source code for three interrelated classes, a Dog class,...

    The files at the links below contain source code for three interrelated classes, a Dog class, a DogOwner class, and a DogTester class that manipulates objects from the other two classes. DogTester.java DogOwner.java Dog.java "Hotdog" is a 15 kg showdog that's a poodle. Write a statement that creates an owner for "HotDog" named Jill. Reference this person with the DogOwner variable jill in the main method in the DogTester class: public class DogTester { public static void main (String[] arg)...

  • 47 What is printed out by the code listed below the classes (i.e., below the line)?...

    47 What is printed out by the code listed below the classes (i.e., below the line)? class Animal public virtual void whoAreYou () cout << "an Animal\n";h class Insect public Animal public void whoAreYou() cout < "an Insect\n"; class Spider public Insect public void whoAreYou() cout << "a Spider\n"; Animal* a1-new Animal; Animal* a2 = new insect ; Animal* a3 = new spider; al->whoAreYou ); a2->whoAreYou ); ??->whoAreYou ( ) ; an Insect, a Spider, a Spider O an Animal,...

  • [Java] [Visitor Pattern] Given the files below, write all visitor classes so that the demo gives...

    [Java] [Visitor Pattern] Given the files below, write all visitor classes so that the demo gives the following output. Incomplete code: drive.google.com/drive/folders/16dmB8aMlggE54Jv4wK08jCyENRoWlHyJ?usp=sharing Visiting front left wheel Visiting front right wheel Visiting back left wheel Visiting back right wheel Visiting body Visiting engine Visiting car Kicking my front left wheel Kicking my front right wheel Kicking my back left wheel Kicking my back right wheel Waxing my body Starting my engine Starting my car

  • please write c programming for this code below. Write a program to print a pattern of...

    please write c programming for this code below. Write a program to print a pattern of numbers separated by spaces for the given number of rows. At the time of execution, the program should print the message on the console as: Enter number of rows For example, if the user gives the input as: Enter number of rows : 4 then the program should print the result as: 232 34543 4567654

  • 1 Suppose you are designing a set of classes intended to represent a solar system and...

    1 Suppose you are designing a set of classes intended to represent a solar system and the planets and comets within it. Which of the following class structures is a correct representation? public class Comet { ... } public class Planet { ... } public class SolarSystem extends Planet { ArrayList <Comet> comets; ... } public class Planet { ... } public class Comet { ... } public class SolarSystem { ArrayList <Planet> planets; ArrayList <Comet> comets; ... } public...

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

  • Write code for 3 classes described below. - Write abstract class called AbstractPerson which had two...

    Write code for 3 classes described below. - Write abstract class called AbstractPerson which had two methods. One called getSchool which is implemented and returns String "Rutgers". Another abstract method called getType to be implemented by subclasses. - 5 points - Write Faculty class which inherits AbstractPerson and implements getType to return String "Faculty" . - 3 points - Write Student class which inherits AbstractPerson and implements getType to return String "Student" . - 3 points

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