Java 实验5

 /*
 * @(#)Circle
 *
 * Copyright 2008 School of Software, Yunnan University.
 *                                  All rights reserved
 */
package cn.edu.ynu.sei.Java_Labs.Lab5;

/**
 * 实验5 根据给出的圆的半径计算该圆的直径和面积
 * 本程序计算时采用的圆周率为3.14
 * @version 1.0.0.0 Jan 24, 2008
 * @author eleven
 */
public class Circle {

    private double Radius;

    /**
     * 设置半径
     */
    public double getRadius() {
        return Radius;
    }

    /**
     * 获取半径
     */
    public void setRadius(double Radius) {
        this.Radius = Radius;
    }

    /**
     * 计算直径
     */
    public double computeDiameter(double Radius) {
        double diameter = 2 * Radius;
        return diameter;
    }

    /**
     * 计算面积
     */
    public double computeArea(double Radius) {
        double area = 3.14 * Radius * Radius;
        return area;
    }

    public static void main(String[] args) {
        Circle c = new Circle();
        c.setRadius(5);
        double Radius1 = c.getRadius();
        double diameter1 = c.computeDiameter(Radius1);
        System.out.println("The diameter of a is " + diameter1);
        double area1 = c.computeArea(Radius1);
        System.out.println("The area of a is " + area1);

        c.setRadius(8);
        double Radius2 = c.getRadius();
        double diameter2 = c.computeDiameter(Radius2);
        System.out.println("The diameter of b is " + diameter2);
        double area2 = c.computeArea(Radius2);
        System.out.println("The area of b is " + area2);
    }
}


------------------------
测试:
/*
 * @(#)CircleTest
 *
 * Copyright 2008 School of Software, Yunnan University.
 *                                  All rights reserved
 */
package cn.edu.ynu.sei.Java_Labs.Lab5;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @version 1.0.0.0 Jan 24, 2008
 * @author eleven
 */
public class CircleTest {

    public CircleTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of getRadius method, of class Circle.
     */
    @Test
    public void getRadius() {
        System.out.println("getRadius");
        Circle instance = new Circle();
        double result = instance.getRadius();
    }

    /**
     * Test of setRadius method, of class Circle.
     */
    @Test
    public void setRadius() {
        System.out.println("setRadius");
        double Radius = 10.0;
        Circle instance = new Circle();
        instance.setRadius(Radius);
    }

    /**
     * Test of computeDiameter method, of class Circle.
     */
    @Test
    public void computeDiameter() {
        System.out.println("computeDiameter");
        double Radius = 10.0;
        Circle instance = new Circle();
        double expResult = 20.0;
        double result = instance.computeDiameter(Radius);
        assertEquals(expResult, result);
    }

    /**
     * Test of computeArea method, of class Circle.
     */
    @Test
    public void computeArea() {
        System.out.println("computeArea");
        double Radius = 10.0;
        Circle instance = new Circle();
        double expResult = 314.0;
        double result = instance.computeArea(Radius);
        assertEquals(expResult, result);
    }

    /**
     * Test of main method, of class Circle.
     */
    @Test
    public void main() {
        System.out.println("main");
        String[] args = null;
        Circle.main(args);
    }
}
阅读更多
个人分类: Java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭