Accenture Interview Question
Java DevelopersCountry: United States
public class Point {
private int x, y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
public Point(){
this.x=0;
this.y=0;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Line {
private final static int NOOFPOINTS = 2;
private Point vertex[];
public Line(Point p1, Point p2){
vertex = new Point[NOOFPOINTS];
vertex[0] = p1;
vertex[1] = p2;
}
public float getLength(){
float length = 0;
float xParam = 0;
float yParam = 0;
for(Point p:vertex){
if(xParam==0 && yParam==0){
xParam = p.getX();
yParam = p.getY();
}
else{
xParam -= p.getX();
yParam -= p.getY();
}
}
length = (float)Math.sqrt(Math.pow(xParam, 2)+Math.pow(yParam, 2));
return length;
}
}
public class Quadrilateral {
private final static int NOOFPOINTS = 4;
private Point vertex[];
public Quadrilateral(Point p1, Point p2, Point p3, Point p4){
vertex = new Point[NOOFPOINTS];
vertex[0] = p1;
vertex[1] = p2;
vertex[2] = p3;
vertex[3] = p4;
}
public List<Line> getAllLines(){
List<Line> result = new ArrayList<Line>();
for(int i=0; i<vertex.length-1; i++){
result.add(new Line(vertex[i], vertex[i+1]));
}
return result;
}
public Line longestSide(){
Line result = null;
float length=0;
for(int i=0; i<vertex.length-1; i++){
Line l = new Line(vertex[i], vertex[i+1]);
float len = l.getLength();
if(len>length){
length = len;
result = l;
}
}
return result;
}
}
Only after solving the question did I read the title...doh! Aw, well. here's some C++!
(I included some << overloads to print out the objects if you want.)
- C++ Just Because Why Not? Okay September 01, 2014