Difference between revisions of "Shape"
(Created page with "=Motivation= Experience with utilizing fields and implementing interfaces. =Mistakes To Avoid= {{warning | Do NOT Parallelize}} =Code to Investigate= <pre>package shape.cor...") |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Motivation= | =Motivation= | ||
− | Experience | + | Experience implementing interfaces. |
=Mistakes To Avoid= | =Mistakes To Avoid= | ||
Line 6: | Line 6: | ||
=Code to Investigate= | =Code to Investigate= | ||
+ | ==Shape== | ||
<pre>package shape.core; | <pre>package shape.core; | ||
Line 14: | Line 15: | ||
=Code to Implement= | =Code to Implement= | ||
+ | We will build 2 classes: Rectangle and Circle which implement the interface Shape which forces the implementation of the area() method. | ||
+ | |||
+ | You will need to declare multiple fields to track the internal state of each instance. The fields you declare should be private to encapsulate the state and final for immutability. | ||
+ | |||
+ | [https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html member variables] | ||
+ | |||
==Rectangle== | ==Rectangle== | ||
<pre>public class Rectangle implements Shape</pre> | <pre>public class Rectangle implements Shape</pre> | ||
− | {{CodeToImplement|Rectangle|constructor<br/>area()<br/>getX()<br/>getY()<br/>getWidth()<br/>getHeight()|shape.warmup}} | + | {{CodeToImplement|Rectangle|constructor<br/>area()<br/>getX()<br/>getY()<br/>getWidth()<br/>getHeight()<br/>area()|shape.warmup}} |
− | ==constructor== | + | ===constructor=== |
{{Sequential|public Rectangle(double x, double y, double width, double height)}} | {{Sequential|public Rectangle(double x, double y, double width, double height)}} | ||
− | ==getX()== | + | ===getX()=== |
{{Sequential|public double getX()}} | {{Sequential|public double getX()}} | ||
− | ==getY()== | + | ===getY()=== |
{{Sequential|public double getY()}} | {{Sequential|public double getY()}} | ||
− | ==getWidth()== | + | ===getWidth()=== |
{{Sequential|public double getWidth()}} | {{Sequential|public double getWidth()}} | ||
− | ==getHeight()== | + | ===getHeight()=== |
{{Sequential|public double getHeight()}} | {{Sequential|public double getHeight()}} | ||
+ | |||
+ | ===area()=== | ||
+ | {{Sequential|public double area()}} | ||
+ | |||
+ | ==Circle== | ||
+ | <pre>public class Circle implements Shape</pre> | ||
+ | |||
+ | {{CodeToImplement|Circle|constructor<br/>area()<br/>getX()<br/>getY()<br/>getRadius()<br/>area()|shape.warmup}} | ||
+ | |||
+ | ===constructor=== | ||
+ | {{Sequential|public Circle(double x, double y, double radius)}} | ||
+ | |||
+ | ===getX()=== | ||
+ | {{Sequential|public double getX()}} | ||
+ | |||
+ | ===getY()=== | ||
+ | {{Sequential|public double getY()}} | ||
+ | |||
+ | ===getRadius()=== | ||
+ | {{Sequential|public double getRadius()}} | ||
+ | |||
+ | ===area()=== | ||
+ | {{Sequential|public double area()}} | ||
+ | |||
+ | <math>\pi * r ^ 2</math> | ||
=Testing Your Solution= | =Testing Your Solution= | ||
==Correctness== | ==Correctness== | ||
{{TestSuite|ShapeTestSuite|shape.warmup}} | {{TestSuite|ShapeTestSuite|shape.warmup}} |
Latest revision as of 07:17, 21 January 2020
Contents
Motivation
Experience implementing interfaces.
Mistakes To Avoid
Warning: Do NOT Parallelize |
Code to Investigate
Shape
package shape.core; public interface Shape { double area(); }
Code to Implement
We will build 2 classes: Rectangle and Circle which implement the interface Shape which forces the implementation of the area() method.
You will need to declare multiple fields to track the internal state of each instance. The fields you declare should be private to encapsulate the state and final for immutability.
Rectangle
public class Rectangle implements Shape
class: | Rectangle.java | |
methods: | constructor area() getX() getY() getWidth() getHeight() area() |
|
package: | shape.warmup | |
source folder: | student/src/main/java |
constructor
method: public Rectangle(double x, double y, double width, double height)
(sequential implementation only)
getX()
method: public double getX()
(sequential implementation only)
getY()
method: public double getY()
(sequential implementation only)
getWidth()
method: public double getWidth()
(sequential implementation only)
getHeight()
method: public double getHeight()
(sequential implementation only)
area()
method: public double area()
(sequential implementation only)
Circle
public class Circle implements Shape
class: | Circle.java | |
methods: | constructor area() getX() getY() getRadius() area() |
|
package: | shape.warmup | |
source folder: | student/src/main/java |
constructor
method: public Circle(double x, double y, double radius)
(sequential implementation only)
getX()
method: public double getX()
(sequential implementation only)
getY()
method: public double getY()
(sequential implementation only)
getRadius()
method: public double getRadius()
(sequential implementation only)
area()
method: public double area()
(sequential implementation only)
Testing Your Solution
Correctness
class: | ShapeTestSuite.java | |
package: | shape.warmup | |
source folder: | testing/src/test/java |