How do I go about testing this code completely (in a class called TestCircle)? i
ID: 3772218 • Letter: H
Question
How do I go about testing this code completely (in a class called TestCircle)?
import java.lang.Math;
public class Circle {
private int x;
private int y;
private int size;
@SuppressWarnings("unused")
private Circle(){
}
public Circle(int X, int Y){
set(X, Y, 1);
}
public Circle(int X, int Y, int s){
set(X, Y, s);
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getSize(){
return size;
}
public void set(int X, int Y, int R){
x=X;
y=Y;
size=R;
}
public void setSize(int s){
size = s;
}
@Override
public String toString(){
return "Point: [(x:"+x+"), (y:"+y+"), (size:"+size+")]" ;
}
@Override
public boolean equals(Object obj){
if(obj instanceof Circle){
Circle parm = (Circle) obj;
return (parm.x==this.x)
&& (parm.y==this.y)
&& (parm.size==this.size);
}
else return false;
}
public double area(){
double area;
area = Math.PI*Math.pow(size, 2);
return area;
}
public static boolean intersect(Circle a, Circle b){
double distance;
distance = Math.sqrt(Math.pow(b.getX() - a.getX(), 2) + Math.pow(b.getY() - a.getY(), 2));
if(distance < a.getSize() + b.getSize())
return true;
return false;
}
public static boolean enclose(Circle a, Circle b){
double distance;
distance = Math.sqrt(Math.pow(b.getX() - a.getX(), 2) + Math.pow(b.getY() - a.getY(), 2));
if(distance + b.getSize() > a.getSize())
return false;
return true;
}
}
Explanation / Answer
import java.util.Scanner;
public class TestCircle{
public static void main(String[] args){
Circle circle1 = new Circle();
Circle circle2 = new Circle(2,2);
Circle circle3 = new Circle(4,2,2);
circle1.set(2,2,1);
if(circle1.getX()==2 && circle1.getY()==2 && circle1.getSize()==1)
System.out.println("First test passed: set function and Constructor1 working");
if(circle2.getX()==2 && circle2.getY()==2 && circle2.getSize()==1)
System.out.println("Second test passed: Constructor2 working");
if(circle3.getX()==4 && circle3.getY()==2 && circle3.getSize()==2)
System.out.println("Third test passed: Constructor3 working");
if(circle1.equals(circle2)==true)
System.out.println("Fourth test passed: equals method working");
if(circle1.area()==(Math.PI*Math.pow(1, 2)))
System.out.println("Fifth test passed: area method working");
if(circle1.intersect(circle3))
System.out.println("Sixth test passed: intersect method working");
circle1.set(4,2,1);
if(circle3.enclose(circle1))
System.out.println("Seventh test passed: enclose method working");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.