JAVA编程,在线等答案

写一个接口Number表示一个抽象的数,该接口有三个函数:
1、int compare(Number n); //比较自己和另外一个抽象数n的大小,返回1表示前者大,0表示相等,
-1表示自己比n小
2、double getValue(); //取得自己代表的实际值
3、String toString(); //输出自己内部存储的数

写一个类Fraction实现Number接口,用来表示一个分数,分子和分母分别用两个int成员存储,如分子
为3,分母为5,就表示存储3/5这个分数。getValue时,返回0.6

写一个类Complex实现Number接口,用来表示一个复数,实部和虚部分别用两个int成员存储,如实部
为3,虚部为4,就表示存储3+4i这个复数。getValue时,返回5.0(即该复数的模)
数之间比较大小,直接按照getValue的值来决定它们之间的大小。
写一个测试类,测试上述的两个类。注意测试类要测试到所有成员函数,并且类的取名,要取成“JXXX”。

模板:
interface Number {
}
class Fraction implements Number{
}
class Complex implements Number{
}
public class Jxxxx {
public static void main(String[] args) {
}
}

下面是你要的代码,并且为你提供了单元测试代码.

package test;

import junit.framework.TestCase;

public class Jtest extends TestCase {

Number fraction;
Number complex;

@Override
protected void setUp() throws Exception {
fraction=new Fraction(5,3);
complex=new Complex(3,4);
}

@Override
protected void tearDown() throws Exception {
fraction=null;
complex=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 testComplexCompare(){
assertEquals(5.0, complex.getValue());
}
public void testComplexToString(){
assertEquals(-1, complex.compare(new Complex(7,8)));
}
public void testComplexGetValue(){
assertEquals("3+4i", complex.toString());
}
}

interface Number{
int compare(Number n); //比较自己和另外一个抽象数n的大小,返回1表示前者大,0表示相等,-1表示自己比n小
double getValue(); //取得自己代表的实际值
String toString(); //输出自己内部存储的数
}

class Fraction implements Number{

private int denominator;
private int numerator;

public Fraction() {
super();
}

public Fraction(int denominator, int numerator) {
super();
this.denominator = denominator;
this.numerator = numerator;
}

public int compare(Number n) {
double result=this.getValue()-n.getValue();
return result==0?0:(result>0?1:-1);
}

public double getValue() {
return ((double)numerator)/denominator;
}

public int getDenominator() {
return denominator;
}

public void setDenominator(int denominator) {
if(denominator==0){
throw new ArithmeticException("The denominator can't be zero.");
}else{
this.denominator = denominator;
}
}

public int getNumerator() {
return numerator;
}

public void setNumerator(int numerator) {
this.numerator = numerator;
}

@Override
public String toString() {
return this.getNumerator()+"/"+this.getDenominator();
}

}

class Complex implements Number{

int actualNumber;
int imaginaryNumber;

public Complex() {
super();
}

public Complex(int actualNumber, int imaginaryNumber) {
super();
this.actualNumber = actualNumber;
this.imaginaryNumber = imaginaryNumber;
}

public int compare(Number n) {
double result=this.getValue()-n.getValue();
return result==0?0:(result>0?1:-1);
}

public double getValue() {
return Math.sqrt(actualNumber*actualNumber+imaginaryNumber*imaginaryNumber);
}

public int getActualNumber() {
return actualNumber;
}

public void setActualNumber(int actualNumber) {
this.actualNumber = actualNumber;
}

public int getImaginaryNumber() {
return imaginaryNumber;
}

public void setImaginaryNumber(int imaginaryNumber) {
this.imaginaryNumber = imaginaryNumber;
}

@Override
public String toString() {
return this.getActualNumber()+"+"+this.getImaginaryNumber()+"i";
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-07-12
没明白你的toString方法是想要最终值值还是两个成员变量的值,我写的是最终值

public class Ttest {
public static void main(String[] args) {
Number n1 = new Fraction(3,4);
Number n2 = new Fraction(4,5);
Number n3 =new Complex(1,2);
Number n4 = new Complex(7,8);
System.out.println("n1:"+n1.toString());
System.out.println("n2:"+n2.toString());
System.out.println("n3:"+n3.toString());
System.out.println("n4:"+n4.toString());

System.out.println("n1与n2比较:"+n1.compare(n2));
System.out.println("n2与n1比较:"+n2.compare(n1));
System.out.println("n3与n4比较:"+n3.compare(n4));
System.out.println("n2与n3比较:"+n2.compare(n3));
}
}

--------------------------------------------------------------------------------------------------
public interface Number {
public int compare(Number n);
public double getValue();
public String toString();
}
------------------------------------------------------------------------------------------
import java.math.BigDecimal;
public class Fraction implements Number {

private int molecular;//分子
private int denominator;//分母

public Fraction(int molecular ,int denominator){
this.molecular= molecular;
this.denominator = denominator;
}
public int compare(Number n) {
if(this.getValue()>n.getValue())
return 1;
else if(this.getValue()==n.getValue())
return 0;
else
return -1;
}
public double getValue() {
BigDecimal b1 = new BigDecimal(this.molecular);
BigDecimal b2 = new BigDecimal(this.denominator);
return b1.divide(b2).doubleValue();
}

public String toString(){
return String.valueOf(this.getValue());
}
}

----------------------------------------------------------------------------------------------------
import java.math.BigDecimal;
public class Complex implements Number {

private int realPart ;//实部
private int illusoryPart;//虚部

public Complex(int realPart ,int illusoryPart){
this.realPart = realPart;
this.illusoryPart = illusoryPart;
}
public int compare(Number n) {
if(this.getValue()>n.getValue())
return 1;
else if(this.getValue()==n.getValue())
return 0;
else
return -1;
}
public double getValue() {
BigDecimal b1 = new BigDecimal(this.realPart);
BigDecimal b2 = new BigDecimal(this.illusoryPart);
//BigDecimal没有现成的开发方法,现使用Math的,可能会溢出
//可以在网上搜到BigDecimal的开发实现
return Math.sqrt(b1.pow(2).add(b2.pow(2)).doubleValue());
}

public String toString(){
return String.valueOf(this.getValue());
}
}
第2个回答  2012-07-12
package mytest;
interface Number
{
int compare(Number n);
double getValue();
String toString();
}
class Fraction implements Number
{
int n1;//分子
int n2;//分母
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
public String toString()
{
System.out.println(this.getValue());
return "";
}
@Override
public int compare(Number n) {
if(this.getValue()>n.getValue())
return 1;
else if(this.getValue()<n.getValue())
return -1;
return 0;
}
@Override
public double getValue() {
double result = 0.0;
if(n2 != 0)
{
result = (double)n1 / n2;
}
return result;
}
// public String toString()
// {
//
// }
}
class Complex implements Number{
int n1;//实部
int n2;//虚部
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
@Override
public int compare(Number n) {
if(this.getValue()>n.getValue())
return 1;
else if(this.getValue()<n.getValue())
return -1;
return 0;
}

public String toString()
{
System.out.println(this.getValue());
return "";
}

@Override
public double getValue() {
return Math.pow((Math.pow(n1, 2)+Math.pow(n2, 2)), 0.5);
}
}
public class Jxxxx {
public static void main(String[] args)
{
//测试分数
Fraction f1 = new Fraction();
f1.setN1(3);
f1.setN2(5);
Fraction f2 = new Fraction();
f2.setN1(1);
f2.setN2(2);
double val1 = f1.getValue();
double val2 = f2.getValue();
f1.toString();
f2.toString();
int result = f1.compare(f2);
System.out.println(result);

//测试复数
Complex c1 = new Complex();
c1.setN1(3);
c1.setN2(4);
Complex c2 = new Complex();
c2.setN1(5);
c2.setN2(12);
double v1 = c1.getValue();
double v2 = c2.getValue();
c1.toString();
c2.toString();
int res = c1.compare(c2);
System.out.println(res);
System.out.println(c1.toString());
}
}本回答被提问者采纳
第3个回答  2012-07-12
这个也要来这里问?
相似回答