Part B: Strategy Pattern We've already seen a design pattern, the iterator pattern, that allowed us to iterate over any collection of items without knowing how the collection is actually implemented. In this part of the lab, we'll be looking at another design pattern called the Strategy Pattern. It involves applying a collection of methods (with an interface in common) to a list of objects. Create an interface like this: public interface OpClass { abstract Object op (Object arg); Next, create three classes Square, Cube, and SquareRoot that implement this interface. The Square should convert the arg to a Number, square it, then return the Number as an object. The Cube will do the same thing, expect it will cube the number. The same applies to the SquareRoot which returns the square root of the number. Create a method called apply in another public class called Calculator and implement your main method here. The apply method should accept an ArrayList of Numbers and an OpClass to apply to the list. It should return a new ArrayList with the new values in it. Make use of the ArrayList iterator here as well. Test this out by building a list of integers, and applying the square, cube, then square root objects on the list. Print out the list after each application.
Part B: Strategy Pattern We've already seen a design pattern, the iterator pattern, that allowed us to iterate over any collection of items without knowing how the collection is actually implemented. In this part of the lab, we'll be looking at another design pattern called the Strategy Pattern. It involves applying a collection of methods (with an interface in common) to a list of objects. Create an interface like this: public interface OpClass { abstract Object op (Object arg); Next, create three classes Square, Cube, and SquareRoot that implement this interface. The Square should convert the arg to a Number, square it, then return the Number as an object. The Cube will do the same thing, expect it will cube the number. The same applies to the SquareRoot which returns the square root of the number. Create a method called apply in another public class called Calculator and implement your main method here. The apply method should accept an ArrayList of Numbers and an OpClass to apply to the list. It should return a new ArrayList with the new values in it. Make use of the ArrayList iterator here as well. Test this out by building a list of integers, and applying the square, cube, then square root objects on the list. Print out the list after each application.
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
in Java
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 6 steps with 8 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education