import java.text.DecimalFormat;
public class Cylinder
{
// Instance Variables
private String label = "";
private double radius;
private double height;
/**
*@param labelIn Represents label value
*@param radiusIn Represents radius value
*@param heightIn Represents height value
*/
public Cylinder(String labelIn, double radiusIn, double heightIn)
{
label = labelIn.trim();
radius = radiusIn;
height = heightIn;
}
/**
*@return String representation of label.
* Accepts no parameters and returns
* a string representing the label field.
*/
public String getLabel()
{
return label;
}
/**
* Takes string parameter and returns boolean.
*@param labelIn Command Line used
*@return checks to see if labelIn exist.
*/
public boolean setLabel(String labelIn)
{
if (labelIn == null)
{
return false;
}
else
{
label = labelIn.trim();
return true;
}
}
/**
*@return a method to display a double radius.
* Accepts no parameter and returns double representing
* radius.
*/
public double getRadius()
{
return radius;
}
/**
*@param radiusIn Command Line used.
* Accepts a double parameter, sets radius field, and
* returns nothing.
*/
public void setRadius(double radiusIn)
{
radius = radiusIn;
}
/**
*@return a method to display a double height.
* Accepts no parameters and returns double
* representing the height field.
*/
public double getHeight()
{
return height;
}
/**
*@param heightIn Command Line used.
* Accepts a double parameter, sets radius field, and
* returns nothing.
*/
public void setHeight(double heightIn)
{
height = heightIn;
}
/**
*@return displays diameter when method is called.
* Accepts no parameters and returns double value
* for diameter calculated using radius.
*/
public double diameter()
{
return radius * 2;
}
/**
*@return displays circumference when method is called.
* Accepts no parameters and returns double value
* for circumference calculated using Math.PI and radius.
*/
public double circumference()
{
return Math.PI * radius * 2;
}
/**
*@return displays area when method is called.
* Accepts no parameters and returns double value
* for surface area calculated using Math.PI, radius, and height.
*/
public double area()
{
return 2 * Math.PI * radius * radius + 2 * Math.PI * radius * height;
}
/**
*@return displays volume when method is called.
* Accepts no parameters and returns double value
* for volume calculated using Math.PI, radius, and height.
*/
public double volume()
{
return Math.PI * radius * radius * height;
}
/**
*@return displays cylinder information.
* Returns a string containing the information about
* the cylinder object.
*/
public String toString()
{
DecimalFormat fmt = new DecimalFormat("#,##0.0##");
return "\"" + label
+ "\" is a cylinder with radius = " + fmt.format(getRadius())
+ " units and height = " + fmt.format(getHeight()) + " units,"
+ "\nwhich has diameter = " + fmt.format(diameter())
+ " units, circumference = " + fmt.format(circumference())
+ " units,"
+ "\narea = " + fmt.format(area()) + " square units,"
+ " and volume = " + fmt.format(volume()) + " cubic units.";
}
}
我需要为每种方法编写测试用例,但我不确定如何做到这一点。我试过了..
@Test public void getLabelTest() {
Cylinder c = new String("Cyl");
但我得到incompatible type error
。任何帮助将不胜感激。
答案 0 :(得分:0)
由于您使用的是@Test
注释,我假设您应该使用JUnit测试。
检查here,了解它是如何完成的。
您正在尝试将字符串分配给您的Cylinder,不兼容类型错误来自哪里。我认为你打算使用
Cylinder c = new Cylinder("Cyl", 1.0, 1.0); // label = "Cyl", radius = 1.0, height = 1.0
通常,您在一个名为 JUnit Test Case 的单独Java文件中创建(JUnit)-Test,并且您将为使用@Test注释的方法创建单个测试。
这是一个简单的例子,这个 JUnit测试用例的外观如何(与你的setLabel方法有关):
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class JUnitTest {
private Cylinder c;
@Before
public void setUp() {
// initializing Cylinder instance to work on
c = new Cylinder("Cyl", 1.0, 1.0);
}
@Test
public void setLabelTest() {
// parameter is null, return value should be false
assertFalse(c.setLabel(null));
// parameter is NOT null, return value should be true
assertTrue(c.setLabel("Foo"));
}
}
同时检查Assert doc是否有不同的断言。