Question

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 the caller to create pets by specifying

○ Name

○ Acres of land where pets will live

● The # of acres of land determines what kind of pet is assigned

○ 1​ ​acre​ ​or​ ​less:​ ​parakeet

○ 4​ ​acre​ ​or​ ​less:​ dog

○ 9​ ​acre​ ​or​ ​less:​ lion

● Smaller pets are always preferred over larger ones

○ For example if the caller has 2 acres, then a new Dog should be returned.

*Your​ ​solution​ ​should​ ​minimally​ ​include​ ​6​ ​classes:

● The abstract Pet and concrete classes (4 classes)

● Factory class (1 class)

● An additional Test class

○ Should demonstrate getting pets for the following acre sizes: 0, 3, 7

○ For each pet retrieved, print out its sound (to the output).

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

Hello
Your required program is here.

abstract class Pet
{
String Name,Sound;
public void show();
}

class Parakeet
{
String Name,Sound;
Parakeet()
{
Name="parakeet";
Sound="Tweet tweet";
}
public void show()
{
System.out.println("Sound of Parakeet is "+Sound);
}
}

class Dog
{
String Name,Sound;
Dog()
{
Name="dog";
Sound="Woof woof";
}
public void show()
{
System.out.println("Sound of Dog is "+Sound);
}
}

class Lion
{
String Name,Sound;
Lion()
{
Name="lion";
Sound="Roar roar";
}
public void show()
{
System.out.println("Sound of Lion is "+Sound);
}
}

class Factory
{
String Name;
int acres;

public void createPets(int acres)
{
this.acres=acres;
if(acres==0)
   Name="";
if(acres<=1)
   Name="parakeet";
else if(1<acres<=4)
   Name="dog";
else if(4<acres<=9)
   Name="lion";
}
public String showName()
{
String s=Name;
return s;
}
}

class Test
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Factory f1=new Factory();
System.out.println("Enter how much acres of land:");
a=Integer.parseInt(br.readLine().trim());
f1.createPets(a);
String name=f1.showName();
if(name=="parakeet")
{
   Parakeet p=new Parakeet();
   p.show();
}
else if(name=="dog")
{
   Dog d=new Dog();
   d.show();
}
else if(name=="lion")
{
   Lion l=new Lion();
   l.show();
}
else               //if acres=0
{
   System.out.println("No pet");
}
}
}

//Hope you will give positive feedback for this

Add a comment
Know the answer?
Add Answer to:
Program in Java: Use​ ​the​ ​Factory​ ​pattern​ ​to​ ​address​ ​the​ ​following​ ​challenge... Your job is to...
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
  • 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...

  • Use Java and please try and show how to do each section. You are creating a 'virtual pet' program...

    Use Java and please try and show how to do each section. You are creating a 'virtual pet' program. The pet object will have a number of attributes, representing the state of the pet. You will need to create some entity to represent attributes in general, and you will also need to create some specific attributes. You will then create a generic pet class (or interface) which has these specific attributes. Finally you will make at least one subclass of...

  • Write and submit one complete Java program to solve the following requirements. Your program will employ...

    Write and submit one complete Java program to solve the following requirements. Your program will employ packages (that is, source directories), and contain multiple source files. Because you are using packages, your code should be in a directory named “Greenhouse.” You should be able to compile your code using the command “javac Greenhouse\*.java” from a directory just below the Greenhouse directory. In the program for this assignment, class names have been specified. You mustuse the supplied class name for both...

  • Requirements Create an Address Book class in Java for general use with the following behaviors: 1....

    Requirements Create an Address Book class in Java for general use with the following behaviors: 1. Constructor: public Address Book Construct a new address book object. • A contact has four fields: first name, last name, email and phone. (There could be more information for a real contact. But these are sufficient for the assignment.) . The constructor reads from the disk to retrieve previously entered contacts. If previous contacts exist, the address book will be populated with those contacts...

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

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