in java please! public enum Quad{ // An enumeration which models the 4 quadrants
ID: 3803173 • Letter: I
Question
in java please! public enum Quad{ // An enumeration which models the 4 quadrants of the 2D Cartesian // plane. It has exactly four elements: Q1, Q2, Q3, Q4. It is likely // that giving each some fields will make the implementation easier. // A private constructor is also useful. public boolean xPositive(); // true if x-coordinates are positive in the quadrant, false otherwise public boolean yPositive(); // true if y-coordinates are positive in the quadrant, false otherwise public String signPair(); // Return a String which represents the signs of the coordinates in // the Quadrant as // (+,+) for Q1 // (-,+) for Q2 // (-,-) for Q3 // (+,-) for Q4 public Quad flippingX(); // Return the Quadrant that would result from flipping the sign (pos // to neg or neg to pos) of the x-coordinate in this Quadrant..
Explanation / Answer
/**
*
* @author Sam
*/
public final class Quadrant {
int x;
int y;
public enum Quad{
Q1,
Q2,
Q3,
Q4
}
Quad quad;
public Quadrant(int x, int y) {
this.x = x;
this.y = y;
quad = setQuad(xPositive(), yPositive());
}
public boolean xPositive(){
return x>0;
}
public boolean yPositive(){
return y>0;
}
public String signPair(){
if (quad.equals(Quad.Q1))
return "(+,+)";
if (quad.equals(Quad.Q2))
return "(-,+)";
if (quad.equals(Quad.Q3))
return "(-,-)";
return "(+,-)";
}
private Quad setQuad(boolean xIsPos, boolean yIsPos){
if(xIsPos && yIsPos)
return Quad.Q1;
if(!xIsPos && yIsPos)
return Quad.Q2;
if(!xIsPos && !yIsPos)
return Quad.Q3;
return Quad.Q4;
}
public Quad flippingX(){
return setQuad(!xPositive(), yPositive());
}
public static Quad fromInts(int x, int y){
if (x>0 && y>=0)
return Quad.Q1;
else if (x>0 && y<0)
return Quad.Q4;
else if (x<=0 && y>=0)
return Quad.Q2;
else
return Quad.Q3;
}
@Override
public String toString() {
return "Point (" + x + ", " + y + ") has sign " + signPair() +", and present in quadrand: " + quad ;
}
public static void main(String[] args) {
for (int i=0; i<args.length-1; i = i + 2){
try{
System.out.println(new Quadrant(Integer.parseInt(args[i]),Integer.parseInt(args[i+1])));
} catch (NumberFormatException ex){
System.err.println("Some parameter is not a number");
}
}
}
}
I have edited the code for you, please review
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.