Created
March 10, 2016 18:44
-
-
Save r2dev/be41fbbafe2497226950 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
interface Perimeter<T> { | |
double getPerimeter(T s); | |
} | |
interface Area<T> { | |
double getArea(T s); | |
} | |
public class Picture implements Cloneable{ | |
private ArrayList<Shape> sps; | |
private double sumP; | |
private double sumA; | |
public Picture() | |
{ | |
sps = new ArrayList<Shape>(); | |
} | |
public Picture(ArrayList<Shape> sps) | |
{ | |
this.sps = sps; | |
} | |
public double sumOfPerimeter() | |
{ | |
sumP = 0; | |
Perimeter<Circle> ci = (s) -> (s.getR() * Math.PI * 2); | |
Perimeter<Square> sq = (s) -> (s.getL() * 4); | |
Perimeter<Rectangle> re = (s) -> (2 * s.getL() + 2 * s.getW()); | |
Perimeter<Parallelogram> pa = (s) -> (2 * s.getL() + 2 * s.getW()); | |
Perimeter<Triangle> tr = (s) -> (s.getA() + s.getB() + s.getC()); | |
for (Shape tmp : sps) { | |
Thread t = new Thread(){ | |
public void run() { | |
synchronized(tmp) { | |
switch(tmp.getClass().toString()) { | |
case "Circle": | |
sumP += ci.getPerimeter((Circle)tmp); | |
break; | |
case "Square": | |
sumP += sq.getPerimeter((Square)tmp); | |
break; | |
case "Rectangle": | |
sumP += re.getPerimeter((Rectangle)tmp); | |
break; | |
case "Parallelogram": | |
sumP += pa.getPerimeter((Parallelogram)tmp); | |
break; | |
case "Triangle": | |
sumP += tr.getPerimeter((Triangle)tmp); | |
break; | |
} | |
} | |
} | |
}; | |
t.start(); | |
try { | |
t.join(); | |
} catch (Exception ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
return sumP; | |
} | |
public double sumOfArea() | |
{ | |
sumA = 0; | |
Area<Circle> ci = (s) -> (s.getR() * Math.PI * s.getR()); | |
Area<Square> sq = (s) -> (s.getL() * s.getL()); | |
Area<Rectangle> re = (s) -> (s.getL() * s.getW()); | |
Area<Parallelogram> pa = (s) -> (s.getL() * s.getH()); | |
Area<Triangle> tr = (s) -> (s.getA() + s.getB() + s.getC()); | |
for (Shape tmp : sps) { | |
Thread t = new Thread(){ | |
public void run() { | |
synchronized(tmp) { | |
switch(tmp.getClass().toString()) { | |
case "Circle": | |
sumA += ci.getArea((Circle)tmp); | |
break; | |
case "Square": | |
sumA += sq.getArea((Square)tmp); | |
break; | |
case "Rectangle": | |
sumA += re.getArea((Rectangle)tmp); | |
break; | |
case "Parallelogram": | |
sumA += pa.getArea((Parallelogram)tmp); | |
break; | |
case "Triangle": | |
sumA += tr.getArea((Triangle)tmp); | |
break; | |
} | |
} | |
} | |
}; | |
t.start(); | |
try { | |
t.join(); | |
} catch (Exception ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
return sumA; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((sps == null) ? 0 : sps.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Picture other = (Picture) obj; | |
if (sps == null) { | |
if (other.sps != null) | |
return false; | |
} else if (!sps.equals(other.sps)) | |
return false; | |
return true; | |
} | |
@Override | |
public String toString() { | |
return "Picture [sps=" + sps + "]"; | |
} | |
@Override | |
protected Object clone() throws CloneNotSupportedException | |
{ | |
Picture tmp = (Picture) super.clone(); | |
for (int i = 0; i < sps.size(); i++) { | |
tmp.sps.add(sps.get(i)); | |
} | |
return tmp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment