Amazon Interview Question
SDE1sCountry: United States
Interview Type: Phone Interview
Your solution is missing the second requirement. In java your abstract shape class would also have to implement the comparable interface that would compare 2 shapes using the getArea() function so the collection array can use its sorting function
1)create interface Area with method getArea(double ... parameters)
2)have concrete implemetations like rectArea, circArea etc which have getArea method implemented accordingly.
3) Have a base class Shape implements Comparable and which has Area as a member field.
4) Have a constructor like Shape(Area requiredArea){ this.Area = requiredArea) and a method getArea(){ return Area.getArea())
5) Have comparable to act on Area.getArea() of Shape class.
6) Since all are type of Shape class, hence they can be stored in collection of type Shape.
Using Comparable interface :
public class Shape implements Comparable<Shape>{
...
..
@Override
public int compareTo(Shape shape) {
return this.getArea() - shape.getArea();
}
}
Using Comparator :
somewhere in code :
Collections.sort(shapes, new Comparator<Shape> {
@Override
public int compare(Shape s1, Shape s2) {
return s1.getArea() - s2.getArea();
}
})
class shape {
public:
double area;
public:
virtual double getArea(){}
};
class circal:public shape
{
double radius;
public:
double getArea(){return area;}
circal(int r):radius(r){area = pi*radius*radius ;}
};
class rec:public shape
{
double eage;
public:
double getArea(){ return eage*eage;}
rec(int e):eage(e){area = eage*eage;}
};
bool cmp(shape *p1,shape *p2)
{
return p1->area<p2->area;
}
vector<shape *> v;
public abstract class Shape implements Comparable<Shape>{
protected Integer area ;
public abstract Integer calculateArea();
@Override
public int compareTo(Shape shape) {
if(null == shape)
return 0;
return (this.calculateArea() - shape.calculateArea());
}
}
========================================
package design.shapeexample;
public class Recatangle extends Shape{
private Integer length;
private Integer width;
public Recatangle() {
// TODO Auto-generated constructor stub
}
public Recatangle(Integer length, Integer width){
this.length = length;
this.width = width;
}
@Override
public Integer calculateArea() {
this.area = this.length * this.width;
return this.area;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return area.toString();
}
}
=========================================
package design.shapeexample;
public class Square extends Shape{
private Integer dimension;
public Square() {
}
public Square(Integer dimension){
this.dimension = dimension;
}
@Override
public Integer calculateArea() {
area = this.dimension * this.dimension;
return area;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return area.toString();
}
}
Create an abstract "shape" class with an abstract method called "getArea". Create concrete classes for each specific shape that implements the "getArea" method (you could use the factory pattern to handle the creation of these object types). Because all these objects extend "shape" you could you store them all in the same collection type. For example, in Java, Collection<Shape> shapes;
- Anonymous August 07, 2013