Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracy marlatt branch #2

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
package planForExtensionExercise;

public class AreaCalculator {
public static double calculateArea(Object[] shapes) {
double area = 0.0;

if (shapes == null) {
return area;
}

for (Object shape : shapes) {
if (shape == null) {
continue;
}
else if (shape instanceof Circle) {
Circle circle = (Circle)shape;
area += Math.PI * circle.getRadius() * circle.getRadius();
}
else if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle)shape;
area += rectangle.getHeight() * rectangle.getWidth();
}
}

return area;
}

// Calculates the aggregate area for an array of Shapes
public static double calculateArea(Shape[] shapes) {

double area = 0.0;

if (shapes == null) {
return area;
}

for (Shape shape : shapes) {
if (shape == null) {
continue;
}
else {
area += shape.getArea();
}
}
return area;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package planForExtensionExercise;

public class Circle {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

public double getRadius() {
return this.radius;
}
public class Circle extends Shape {

private double radius;

public Circle(double radius) {
this.radius = radius;
}

// Computes its own area
public double getArea() {
return (Math.PI * this.radius * this.radius);
}

//Computes its own circumference
public double getPerimeter() {
return (2 * Math.PI * this.radius);
}
}
26 changes: 15 additions & 11 deletions plan-for-extension-exercise/src/planForExtensionExercise/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

public class Main {

public static void main(String[] args) {
Circle circle = new Circle(25);
Rectangle rectangle = new Rectangle(10, 20);
Rectangle square = new Rectangle(10, 10);

Object[] shapes = { circle, rectangle, square };

double totalArea = AreaCalculator.calculateArea(shapes);

System.out.println("The total area is " + totalArea + ".");
}
public static void main(String[] args) {

Circle circle = new Circle(25);
Rectangle rectangle = new Rectangle(10, 20);
Rectangle square = new Rectangle(10, 10);
Triangle triangle = new Triangle(3,4,5);

Shape[] shapes = { circle, rectangle, square, triangle };

double totalArea = AreaCalculator.calculateArea(shapes);
System.out.println("The total area is " + totalArea + ".");

double totalPerimeter = PerimeterCalculator.calculatePerimeter(shapes);
System.out.println("The total perimeter is " + totalPerimeter + ".");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package planForExtensionExercise;

public class PerimeterCalculator {

// Calculates the aggregate perimeter for an array of shapes
public static double calculatePerimeter(Shape[] shapes) {

double perimeter = 0.0;

if (shapes == null) {
return perimeter;
}

for (Shape shape : shapes) {
if (shape == null) {
continue;
}
else {
perimeter += shape.getPerimeter();
}
}
return perimeter;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package planForExtensionExercise;

public class Rectangle {
private double height;
private double width;

public Rectangle(double height, double width) {
this.height = height;
this.width = width;
}

public double getHeight() {
return this.height;
}

public double getWidth() {
return this.width;
}
public class Rectangle extends Shape {

private double height;
private double width;

public Rectangle(double height, double width) {
this.height = height;
this.width = width;
}

// Computes its own area
public double getArea() {
return (this.height * this.width);
}

// Computes its own perimeter
public double getPerimeter() {
return (2*this.height + 2*this.width);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package planForExtensionExercise;

public abstract class Shape {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this abstract class only has methods, it could be an interface instead of an abstract class. Tracy, can you comment on why you made it an abstract class instead of an interface?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used an abstract class largely because the relationship between Shape and
its subclasses is a very strong, hierarchical one so it seemed to make
sense from an interpretability standpoint. Also it is my understanding that
abstract classes are faster than interfaces and may be preferable in cases
where there is no obvious advantage to using an interface.

On Thu, Feb 5, 2015 at 11:11 AM, Spencer Robinson [email protected]
wrote:

In plan-for-extension-exercise/src/planForExtensionExercise/Shape.java
#2 (comment):

@@ -0,0 +1,9 @@
+package planForExtensionExercise;
+
+public abstract class Shape {

Because this abstract class only has methods, it could be an interface
instead of an abstract class. Tracy, can you comment on why you made it an
abstract class instead of an interface?


Reply to this email directly or view it on GitHub
https://github.com/siostechcorp/exercises/pull/2/files#r24173904.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purely for the fun of it, will you please humor me by changing Shape to an interface?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

On Thu, Feb 5, 2015 at 11:33 AM, Spencer Robinson [email protected]
wrote:

In plan-for-extension-exercise/src/planForExtensionExercise/Shape.java
#2 (comment):

@@ -0,0 +1,9 @@
+package planForExtensionExercise;
+
+public abstract class Shape {

Purely for the fun of it, will you please humor me by changing Shape to an
interface?


Reply to this email directly or view it on GitHub
https://github.com/siostechcorp/exercises/pull/2/files#r24175858.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just created a pull request

On Thu, Feb 5, 2015 at 11:34 AM, Tracy Marlatt [email protected]
wrote:

Sure!

On Thu, Feb 5, 2015 at 11:33 AM, Spencer Robinson <
[email protected]> wrote:

In plan-for-extension-exercise/src/planForExtensionExercise/Shape.java
#2 (comment):

@@ -0,0 +1,9 @@
+package planForExtensionExercise;
+
+public abstract class Shape {

Purely for the fun of it, will you please humor me by changing Shape to
an interface?


Reply to this email directly or view it on GitHub
https://github.com/siostechcorp/exercises/pull/2/files#r24175858.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I wasn't very clear. Please update this pull request to include the change, and close the other pull request.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Spencer,

I updated my code in that branch and closed the newest pull request, but
how do I "update" my first pull request?

Thanks, Tracy

On Thu, Feb 5, 2015 at 11:48 AM, Spencer Robinson [email protected]
wrote:

In plan-for-extension-exercise/src/planForExtensionExercise/Shape.java
#2 (comment):

@@ -0,0 +1,9 @@
+package planForExtensionExercise;
+
+public abstract class Shape {

Sorry, I wasn't very clear. Please update this pull request to include the
change, and close the other pull request.


Reply to this email directly or view it on GitHub
https://github.com/siostechcorp/exercises/pull/2/files#r24177110.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make a commit in the branch and the pull request will automatically be
updated.
On Feb 5, 2015 12:07 PM, "TracyMarlatt" [email protected] wrote:

In plan-for-extension-exercise/src/planForExtensionExercise/Shape.java
#2 (comment):

@@ -0,0 +1,9 @@
+package planForExtensionExercise;
+
+public abstract class Shape {

Hi Spencer, I updated my code in that branch and closed the newest pull
request, but how do I "update" my first pull request? Thanks, Tracy
… <#14b5ab621cca8ea0_>
On Thu, Feb 5, 2015 at 11:48 AM, Spencer Robinson <
[email protected]> wrote: In
plan-for-extension-exercise/src/planForExtensionExercise/Shape.java <
https://github.com/siostechcorp/exercises/pull/2#discussion_r24177110>: >
@@ -0,0 +1,9 @@ > +package planForExtensionExercise; > + > +public abstract
class Shape { Sorry, I wasn't very clear. Please update this pull request
to include the change, and close the other pull request. — Reply to this
email directly or view it on GitHub <
https://github.com/siostechcorp/exercises/pull/2/files#r24177110>.


Reply to this email directly or view it on GitHub
https://github.com/siostechcorp/exercises/pull/2/files#r24178946.


// Subclasses implement the following methods
abstract double getArea();
abstract double getPerimeter();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package planForExtensionExercise;

public class Triangle extends Shape {

//Triangle may be scalene
private double side1, side2, side3;

public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

// Computes its own area - using Heron's Formula
public double getArea() {
double s = 0.5 * getPerimeter();
return (Math.sqrt(s * (s-this.side1) * (s-this.side2) * (s-this.side3)));
}

//Computes its own perimeter
public double getPerimeter() {
return (this.side1 + this.side2 + this.side3);
}
}