OOAD 2019 Makeup solution || BIM STUDY NOTES
Group B 2019 Q.NO 2 Q.no 7
Continue ReadingGroup B 2019 Q.NO 2 Q.no 7
Continue ReadingGroup C Q.no 8 Step 1
1 2 3 |
public interface Shape { void draw(); } |
Create an interface. Shape.java Step 2 Create concrete classes implementing the same interface. Rectangle.java
1 2 3 4 5 6 |
<span class="kwd">public</span> <span class="kwd">class</span> <span class="typ">Rectangle</span> <span class="kwd">implements</span> <span class="typ">Shape</span> <span class="pun">{</span> <span class="lit">@Override</span> <span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> draw</span><span class="pun">()</span> <span class="pun">{</span> <span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Inside Rectangle::draw() method."</span><span class="pun">);</span> <span class="pun">}</span> <span class="pun">}</span> |
Square.java
1 2 3 4 5 6 |
<span class="kwd">public</span> <span class="kwd">class</span> <span class="typ">Square</span> <span class="kwd">implements</span> <span class="typ">Shape</span> <span class="pun">{</span> <span class="lit">@Override</span> <span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> draw</span><span class="pun">()</span> <span class="pun">{</span> <span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Inside Square::draw() method."</span><span class="pun">);</span> <span class="pun">}</span> <span class="pun">}</span> |
Circle.java
1 2 3 4 5 6 |
<span class="kwd">public</span> <span class="kwd">class</span> <span class="typ">Circle</span> <span class="kwd">implements</span> <span class="typ">Shape</span> <span class="pun">{</span> <span class="lit">@Override</span> <span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> draw</span><span class="pun">()</span> <span class="pun">{</span> <span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Inside Circle::draw() method."</span><span class="pun">);</span> <span class="pun">}</span> <span class="pun">}</span> |
Step 3 Create a Factory to generate object of concrete class based on given information. ShapeFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span class="kwd">public</span> <span class="kwd">class</span> <span class="typ">ShapeFactory</span> <span class="pun">{</span> <span class="com">//use getShape method to get object of type shape </span> <span class="kwd">public</span> <span class="typ">Shape</span><span class="pln"> getShape</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> shapeType</span><span class="pun">){</span> <span class="kwd">if</span><span class="pun">(</span><span class="pln">shapeType </span><span class="pun">==</span> <span class="kwd">null</span><span class="pun">){</span> <span class="kwd">return</span> <span class="kwd">null</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">if</span><span class="pun">(</span><span class="pln">shapeType</span><span class="pun">.</span><span class="pln">equalsIgnoreCase</span><span class="pun">(</span><span class="str">"CIRCLE"</span><span class="pun">)){</span> <span class="kwd">return</span> <span class="kwd">new</span> <span class="typ">Circle</span><span class="pun">();</span> <span class="pun">}</span> <span class="kwd">else</span> <span class="kwd">if</span><span class="pun">(</span><span class="pln">shapeType</span><span class="pun">.</span><span class="pln">equalsIgnoreCase</span><span class="pun">(</span><span class="str">"RECTANGLE"</span><span class="pun">)){</span> <span class="kwd">return</span> <span class="kwd">new</span> <span class="typ">Rectangle</span><span class="pun">();</span> <span class="pun">}</span> <span class="kwd">else</span> <span class="kwd">if</span><span class="pun">(</span><span class="pln">shapeType</span><span class="pun">.</span><span class="pln">equalsIgnoreCase</span><span class="pun">(</span><span class="str">"SQUARE"</span><span class="pun">)){</span> <span class="kwd">return</span> <span class="kwd">new</span> <span class="typ">Square</span><span class="pun">();</span> <span class="pun">}</span> <span class="kwd">return</span> <span class="kwd">null</span><span class="pun">;</span> <span class="pun">}</span> <span class="pun">}</span> |
Step 4 Use the Factory to get object of concrete class by […]
Continue Reading2018 q.NO 5
Continue Reading