Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public interface Animal { void create ( ) ; } public class animal 1 implements Animal { @Override public void create ( ) { System.out.println
public interface Animal void create;
public class animal implements Animal
@Override
public void create
System.out.printlnYouve just created a cheetah!";
public class animal implements Animal
@Override
public void create
System.out.printlnYouve just created a kangaroo!";
public class animal implements Animal
@Override
public void create
System.out.printlnYouve just created a lion!";
public class factory
public Animal getString type
iftype null
return null;
iftypeequalsIgnoreCaseKangaroo
return new animal;
else iftypeequalsIgnoreCaseCheetah
return new animal;
else iftypeequalsIgnoreCaseLion
return new animal;
return null;
public class Main
public static void mainString args
factory factory new factory;
Animal animal factory.getKangaroo;
animalcreate;
Animal animal factory.getCheetah;
animalcreate;
Animal animal factory.getLion;
animalcreate;
Compulsory Task
Follow these steps:
For this task, you are required to refactor the badly written program in the
Factory folder.
The factory pattern is a method that returns one of several possible classes
that share a common superclass. It is particularly useful when you dont
know beforehand what class you need and the decision can only be made
at run time. The superclass that the Factory pattern uses is usually
implemented as an interface. An interface is a special type of class that
acts as a blueprint of classes to come. It is an abstract type that specifies
the behaviour of future classes. An interface will contain method headings
but the methods in an interface dont contain any logic. The logic for the
methods specified in the interface is written in the classes that implement
the interface. In essence, an interface shows a class what to do and not
how to do it
It is not vital for you to be familiar with design patterns for this task, but
you can read more about them here.
For this task be sure to:
Troubleshoot and debug the code first so that it runs correctly.
Fix the indentation and formatting of the code so that it adheres to
the guidelines provided here.
Make sure that all the names of variables, classes, methods, etc.
adhere to the guidelines provided here.
Refactor the code to improve the quality and readability of the code
in other ways highlighted in this task.
Compulsory Task
public interface Frame
public void render;
public class BasicFrame implements Frame
@Override
public void render
System.out.printlnIf this were a real GUI, it'd display the frame";
public class FrameDecorator implements Frame
Frame decoratedFrame;
public FrameDecoratorFrame wrappee
decoratedFrame wrappee;
public void render
decoratedFrame.render;
public class ScalingFrameDecorator extends FrameDecorator
private double scaleFactor ;
public ScalingFrameDecoratorFrame wrappee, double scaleFactor
superwrappee;
this.scaleFactor scaleFactor;
public void render
super.render;
System.out.printlnStringformatImagine making the frame f times bigger!", scaleFactor;
public void scaledouble factorscaleFactor factor;
public class ScrollingFrameDecorator extends FrameDecorator
private int numScrollBars ;
public ScrollingFrameDecoratorFrame wrappee, int numScrollBars
superwrappee;
this.numScrollBars numScrollBars;
public void rendersuperrender;
System.out.printlnStringformatImagine adding d scroll bars to the frame!", numScrollBars;
public void addScrollBarnumScrollBars;
public class Main
public static void mainString args
System.out.printlnThe basic frame";
Frame var new BasicFrame;
varrender;
System.out.println;
FrameDecorator scrollFrame new ScrollingFrameDecoratorvar;
scrollFrame.render;
System.out.println;
Follow these steps:
For this task, you are required to refactor the badly written program in the
Decorator folder.
The decorator pattern adds something extra to an object that already
exists. It wraps an object in order to add functionality or features to the
object. The purpose of the pattern is to make it easy to add or remove
functions if needed. This is done by wrapping a single object not class to
add new behaviour. It has an attribute for that object and has all the same
methods as the wrapped object.
It is not vital for you to be familiar with design patterns for this task, but
you can read more about them here.
For this task be sure to:
Troubleshoot and debug the code first so that it runs correctly.
Fix the indentation and formatting of the code so that it adheres to
the guidelines provided here.
Make sure that all the names of variables, classes, methods, etc.
adhere to the guidelines provided here.
Refactor the code to improve the quality and readability of the code
in other ways highlighted in this task.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started