gpt4 book ai didi

java - 作业帮助需要使用 MyMath 的 java 接口(interface)

转载 作者:行者123 更新时间:2023-12-01 13:21:22 25 4
gpt4 key购买 nike

好吧,我想我已经做到了。但现在我遇到了接口(interface)方法的问题。接口(interface)方法使用泛型类型和 1 个对象。但分配要求添加两个对象。我现在被困在这一点上。我不确定如何编写 add(T o) 接口(interface),因为我只能将一个对象发送到该方法中。我尝试过使用 value1.add(value2) 但当我测试值时似乎只出现了 value1 的值。我不知道 value2 去了哪里这是我的类和接口(interface)

public class MyFraction implements MyMath<MyFraction> {
private List<Character> sign = new ArrayList<Character>();
private List<Integer> numerator = new ArrayList<Integer>();
private List<Integer> denominator = new ArrayList<Integer>();

public MyFraction(int numerator, int denominator, char sign) {
this.numerator.add(numerator);
this.denominator.add(denominator);
this.sign.add(sign);

}
public MyFraction(){}

public static void main(String[] args) {
MyFraction run = new MyFraction();
run.start();
}

private void start() {
char sign = JOptionPane.showInputDialog(null, "Enter - for negative + for positive number ").charAt(0);
int numerator = Math.abs(Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a numerator ")));
int denominator = Math.abs(Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a denominator ")));
MyFraction value1 = new MyFraction(numerator, denominator, sign);

sign = JOptionPane.showInputDialog(null, "Enter - for negative + for positive number ").charAt(0);
numerator = Math.abs(Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a numerator ")));
denominator = Math.abs(Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a denominator ")));
MyFraction value2 = new MyFraction(numerator, denominator, sign);

System.out.println("numerator 1: "+value1.getNumerator() );
System.out.println("denominator 1: "+value1.getDenominator() );
System.out.println("sign 1: "+value1.getSign() );
System.out.println();
System.out.println("numerator 2: "+value2.getNumerator() );
System.out.println("denominator 2: "+value2.getDenominator() );
System.out.println("sign 2: "+value2.getSign() );
}


public int getNumerator(){
int value = this.numerator.get(0);
return value;
}
public int getDenominator(){
int value = this.denominator.get(0);
return value;
}
public char getSign(){
char value = this.sign.get(0);
return value;
}


@Override
public MyFraction add(MyFraction o) {

return null;
}
@Override
public MyFraction subtract(MyFraction o) {
// TODO Auto-generated method stub
return null;
}

@Override
public MyFraction divide(MyFraction o) {
// TODO Auto-generated method stub
return null;
}

@Override
public MyFraction multiply(MyFraction o) {
// TODO Auto-generated method stub
return null;
}

}

public interface MyMath<T> {
public T add(T o);

public T subtract(T o);

public T divide(T o);

public T multiply(T o);

}

最佳答案

接口(interface)在某种程度上是一种契约,它说“无论谁实现我,都必须包含我声明的所有方法的实现”。 实现接口(interface)的每个都象征性地签署了该契约,因此必须自己实现这些方法。

在您的类中,您缺少整个接口(interface)的实现,这就是您的代码无法编译的原因。你的类应该如下所示:

public class MyFraction implements MyMath<MyFraction> {
....

}
//interface memebers
@Override
public MyFraction subtract(MyFraction o){
//do subtraction here
return null;
}

@Override
public MyFraction divide(MyFraction o){
//do division here
return null;
}

@Override
public MyFraction multiply(MyFraction o){
//do multiplication here
return null;
}

@Override
public MyFraction add(MyFraction o) {
// do adding here
return null;
}
}

关于java - 作业帮助需要使用 MyMath 的 java 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22008581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com