Question: Java Help I have class below. I need a seperate class that contains a test case for each method form the ellipse class. It should
Java Help
I have class below. I need a seperate class that contains a test case for each method form the ellipse class. It should be able to read values from a given unit tester class and be able to succeed or fail if it does not fall within the parameters.
public class Ellipse { private double x,y,width,height; private boolean rectMode; private String color; //declare instance variables public Ellipse(double x, double y, double width, double height) { x = 0.0; y = 0.0; width = 0.0; height = 0.0; rectMode = false; color = "None"; } /*Constructor checks if parameters are valid through the isValid method and sets * ellipse to default settings if not. */ Ellipse(double x, double y, double height, double width, String color, boolean rectMode) { if(isValid(x,y,width,height,rectMode,color)) { this.x=x; this.y=y; this.height = height; this.width = width; this.color = color; this.rectMode = rectMode; } else { this.x = 0; this.y = 0; this.width = 1; this.height = 1; this.rectMode= false; this.color= "red"; } } //returns x public double getX() { return x; } //returns y public double getY() { return y; } //returns width public double getWidth() { return width; } //returns height public double getHeight() { return height; } //returns (x,y) public boolean getRectMode() { return rectMode; } //Returns color public String getColor() { return color; } //Checks if X is within the bounding box public void setX(double x) { if(x>=-100 && x<=100) this.x = x; } //Checks if Y is within the bounding box public void setY(double y) { if(y>=-100 && y<=100) this.y = y; } //Checks if height is within the bounding box public void setHeight(double height) { if(height>=0 && height<=1) this.height = height; } //Checks if width is within the bounding box public void setWidth(double width) { if(width>=0 && height<=1) this.width = width; } // sets rectMode to m public void setRectMode(boolean m) { this.rectMode = m; } //sets the color to c public void setColor(String c) { if(c.equalsIgnoreCase("black")||c.equalsIgnoreCase("red")||c.equalsIgnoreCase("blue")||c.equalsIgnoreCase("green")) this.color = c; } //returns all instance variables @Override public String toString(){ return "X: " + x + " " + "Y: " + y + " " + "Width: " + width + " " + "Height: " + height + " " + "rectMode: " + getRectMode() + " " + "Color: " + getColor(); } //returns true if e is the same as the object, false otherwise public boolean equals(Ellipse e) { if(this.equals(e)) return true; else return false; } //Computes & returns perimenter of ellipse public double computePerimeter() { double perimeter = 0.0; double radius = 0.0; radius = Math.sqrt((Math.pow(x,2)+Math.pow(y,2))/2); perimeter = 2 * Math.PI * radius;
return perimeter; } //Computes & returns area of ellipse public double computeArea() { double area = 0.0; area=Math.PI*x*y; return area; } // Returns a booleans if the ellipse contains a point public boolean containsPoint(double x, double y) { if(this.x == x && this.y==y) return true; else return false; } //returns a true value if the ellipse satisfies all the bounds public static boolean isValid(double x, double y, double w, double h, boolean m, String c) { //returns rectMode m as true if at x,y if(x==0 && y==0){ m=false; } else{ m=true; } //returns other variables as true if they satisfy bounds. if((h>=0 &&h<=1)&&(w>=0 && w <=1) && (x>=-100 && x<=100)&& (y>=-100 && y<=100)&&(c.equalsIgnoreCase("black")||c.equalsIgnoreCase("red")|| c.equalsIgnoreCase("green")||c.equalsIgnoreCase("blue"))) { return true; } else return false; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
