综合测试


public abstract class Shape
{
    public abstract double perimeter();//计算周长,抽象方法
    public abstract double area();//计算面积,抽象方法
}

import junit.framework.*;

public class ShapeTest {
	public static Test suite() throws Exception {
		TestSuite suite = new TestSuite("Some tests from part 1 ");
		suite.addTestSuite(CircleTest.class);
		suite.addTestSuite(RectangleTest.class);
		suite.addTestSuite(SquareTest.class);
		return suite;
	}

	public static void main(String arg[]) {
		try {
			junit.textui.TestRunner.run(ShapeTest.suite());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

public class Circle extends Shape
{
    private int r;//半径
    public Circle(int x)
    {
        r=x;
    }
    public double perimeter()
    {
        return 2*3.14*r;
    }
    public double area()
    {
        return 3.14*r*r;
    }
}

import junit.framework.TestCase;

public class CircleTest extends TestCase {

	public CircleTest() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void testPerimeter() {
		int r = 8;
		Circle circle = new Circle(r);
		assertEquals(50.24, circle.perimeter(), 0);
//		r = 5;
//		circle = new Circle(r);
//		assertEquals(31.4, circle.perimeter(), 0);
        r=23;
        circle = new Circle(r);
        assertEquals(144.44, circle.perimeter(), 0);
	}

	public void testArea() {
		int r = 5;
		Circle circle = new Circle(r);
		assertEquals(78.5, circle.area(), 0);
		r = 8;
		circle = new Circle(r);
		assertEquals(200.96, circle.area(), 0);
		r=23;
		circle = new Circle(r);
		assertEquals(1661.06, circle.area(), 0);
	}
}

长方形

public class Rectangle extends Shape
{
    int a;//边长
    int b;//边长
    public Rectangle(int x,int y)
    {
        a=x;
        b=y;
    }
    public double perimeter()
    {
        return 2*(a+b);
    }
    public double area()
    {
        return a*b;
    }
}

import junit.framework.TestCase;

public class RectangleTest extends TestCase {

	public RectangleTest() {
		super();
		// TODO Auto-generated constructor stub
	}

	public RectangleTest(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	public void testPerimeter() {
		int x = 4, y = 7;
		Rectangle rectangle = new Rectangle(x, y);
		assertEquals(22, rectangle.perimeter(), 0);
		x = 5;
		y = 9;
		rectangle = new Rectangle(x, y);
		assertEquals(28, rectangle.perimeter(), 0);
	}

	public void testArea() {
		int x = 4, y = 7;
		Rectangle rectangle = new Rectangle(x, y);
		assertEquals(28, rectangle.area(), 0);
		x = 5;
		y = 9;
		rectangle = new Rectangle(x, y);
		assertEquals(45, rectangle.area(), 0);
	}
}

public class Square extends Rectangle
{
	int a;
    public Square(int x,int y)
    {
        super(x,y);
        a=x;
    }
    public double perimeter()
    {
        return 4*a;
    }
    public double area()
    {
        return a*a;
    }
}

import junit.framework.TestCase;

public class SquareTest extends TestCase {

	public SquareTest() {
		super();
		// TODO Auto-generated constructor stub
	}

	public SquareTest(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	public void testPerimeter() {
		int x = 4, y = 4;
		Square square = new Square(x, y);
		if (x == y) {
			assertEquals(16, square.perimeter(), 0);
		}
		x = 7;
		y = 7;
		square = new Square(x, y);
		if (x == y) {
			assertEquals(28, square.perimeter(), 0);
		}
	}

	public void testArea() {
		int x = 4, y = 4;
		Square square = new Square(x, y);
		if (x == y) {
			assertEquals(16, square.area(), 0);
		}
		x = 7;
		y = 7;
		square = new Square(x, y);
		if (x == y) {
			assertEquals(49, square.area(), 0);
		}
	}
}
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公众号
   欢迎关注本站公众号,获取更多程序园信息
开发小院