Question: Question Can you someone help for this: Objectives: Implement the Open/Closed Principle Learn the Observer Design Pattern Learn the Strategy Design Pattern Learn about Functional
Question
Can you someone help for this:
Objectives:
- Implement the Open/Closed Principle
- Learn the Observer Design Pattern
- Learn the Strategy Design Pattern
- Learn about Functional and Non-Functional Requirements
Coding Exercise
See the following code for a sample testing application:
Sure, it works, but the code needs to be modified every time a new Animal class is added. You have to take the classes provided and change them so that the AnimalClassTester class can test new Animal classes without needing to be changed.
You need to write the TreeClimbingOctopus class and use this class to demonstrate that the AnimalClassTester is indeed following the Open/Closed principle.
So, you must:
- refactor (rewrite) the AnimalClassTester class to follow OCP
- refactor the existing Animal classes as needed
- write the TreeClimbingOctopus class and test it using the rewritten AnimalClassTester class to demonstrate OCP
For maximum success on this assignment, follow this path:
- Determine HOW the AnimalClassTester can be modified so that is can be closed for modification.
- Determine HOW the different animal classes are similar.
- Determine HOW you to make the existing animal classes polymorphic.
- Refactor the various animal classes.
- Refactor the AnimalClassTester class to use polymorphism when testing an object.
- Write the TreeClimbingOctopus class.
- You have succeeded if
- If the AnimalClassTester does not need to be modified to test the TreeClimbingOctopus class.
- You have succeeded if
Answers these Questions
The Question report must have the following:
- Describe any problems your group had writing the code
- Did you change the classes that were tested? Why or why not?
- Describe your team's solution. Explain how it meets the criteria given.
- Do you think unit testing is important? Why or why not?
- Name 3 key parts of the Observer pattern
- When would you use the Observer pattern? What problem does it solve?
- Name 3 key parts of the Strategy pattern
- When would you use the Strategy pattern? What problem does it solve?
Code 1
ackage hw7;
public class AnimalClassTester { public boolean testAnimalObject(Object animal) { if (animal instanceof Cow) { Cow c = (Cow) animal; ExpectedCow ex = new ExpectedCow(); if (c.aboutMe().equals(ex.expectedName())) { if (c.diet().equals(ex.expectedFood())) { if (c.produces().equals(ex.expectedInfo())) { return true; } } } } else if (animal instanceof Tyrannosaur) { Tyrannosaur t = (Tyrannosaur) animal; if (t.getName().contentEquals("Tyrannosaurus Rex")) { if (t.roar().equals("Roarrr!")) { if (t.myFood().equals("other dinosaurs")) { return true; } } } } else if (animal instanceof Penguin) { Penguin p = (Penguin) animal; if (p.flightless().equals("Penguin")) { if (p.IEat().equals("mostly fish")) { if (p.movement().equalsIgnoreCase("waddle and swim")) { return true; } } } } return false; }
} Code 2
package hw7;
public class Cow { public String aboutMe() { return "Cow"; } public String diet() { return "grass, hay, and corn"; } public String produces() { return "milk and cheese"; }
}
code 3
package hw7;
public class ExpectedCow { public String expectedName() { return "Cow"; } public String expectedFood() { return "grass, hay, and corn"; } public String expectedInfo() { return "milk and cheese"; }
}
code 4
package hw7;
public class hw7 {
public static void main(String[] args) { AnimalClassTester qa = new AnimalClassTester(); Tyrannosaur clarence = new Tyrannosaur(); if (qa.testAnimalObject((Object) clarence)) { System.out.println("Tyrranosaur class passes"); } else { System.out.println(">>>>Tyrranosaur class Failed!<<<<"); } Penguin opus = new Penguin(); if (qa.testAnimalObject((Object)opus)) { System.out.println("Penguin class passes"); } else { System.out.println(">>>>Penguin class Failed!<<<<"); } Cow mrsolearys = new Cow(); if (qa.testAnimalObject(mrsolearys)) { System.out.println("Cow class passes"); } else { System.out.println(">>>>Cow class Failed!<<<<"); } }
}
code 5
package hw7;
public class Penguin { public String flightless() { return "penguin"; } public String IEat() { return "mostly fish"; } public String movement() { return "waddle and swim"; }
}
code 6
package hw7;
public class Tyrannosaur { public String getName() { return "Tyranosaurus Rex"; } public String roar() { return "Roarrrr!"; } public String myFood() { return "other dinosaurs"; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
