包裝測試;
導入JUnit . framework . test case;
公共類Jtest擴展測試用例{
數字分數;
復數;
@覆蓋
受保護的void設置()引發異常{
分數=新分數(5,3);
復數=新的復數(3,4);
}
@覆蓋
受保護的void tearDown()引發異常{
分數=空;
復數= null
}
public void testFractionCompare(){
assertEquals(0.6,fraction . getvalue());
}
public void testFractionToString(){
assertEquals(1,fraction.compare(new Fraction(6,3)));
}
public void testFractionGetValue(){
assertEquals("3/5 ",fraction . tostring());
}
public void testcomplex compare(){
assertEquals(5.0,complex . getvalue());
}
public void testcomplex tostring(){
assertEquals(-1,complex.compare(new Complex(7,8)));
}
public void testComplexGetValue(){
assertEquals("3+4i ",complex . tostring());
}
}
接口號{
int compare(數量n);//把自己和另壹個抽象數n比較,返回1表示前者更大,0相等,-1表示妳比n小。
double getValue();//獲取您表示的實際值。
string toString();//輸出內部存儲的數字。
}
課件分數實現數{
private int分母;
private int分子;
公共分數(){
super();
}
公共分數(整數分母,整數分子){
super();
this .分母=分母;
this.numerator =分子;
}
公共int比較(數字n) {
double result = this . getvalue()-n . getvalue();
返回結果==0?0:(結果& gt0?1:-1);
}
public double getValue() {
return((雙)分子)/分母;
}
public int getDenominator() {
返回分母;
}
public void set denominator(int denominator){
if(分母==0){
拋出new ArithmeticException("分母不能為零。");
}否則{
this .分母=分母;
}
}
public int get enumerator(){
返回分子;
}
public void set enumerator(int enumerator){
this.numerator =分子;
}
@覆蓋
公共字符串toString() {
返回this . get enumerator()+"/"+this . get denominator();
}
}
類復雜實現數量{
int actualNumber
int imaginaryNumber
公共綜合體(){
super();
}
public Complex(int actualNumber,int imaginaryNumber) {
super();
this . actualNumber = actualNumber;
this . imaginary number = imaginary number;
}
公共int比較(數字n) {
double result = this . getvalue()-n . getvalue();
返回結果==0?0:(結果& gt0?1:-1);
}
public double getValue() {
返回math . sqrt(actual number * actual number+imaginary number * imaginary number);
}
public int getActualNumber() {
返回實際數字;
}
public void set actual number(int actual number){
this . actualNumber = actualNumber;
}
public int getImaginaryNumber() {
返回imaginaryNumber
}
public void setimaginary number(int imaginary number){
this . imaginary number = imaginary number;
}
@覆蓋
公共字符串toString() {
返回this . getactualnumber()+"+"+this . getimaginarynumber()+" I ";
}
}