Aright,我想我已经开始了。但现在我遇到了接口方法的问题。接口方法使用泛型类型和1个对象。但是赋值要求添加两个对象。我现在陷入困境。我不知道如何编写add(T o)接口,因为我只能将一个对象发送到方法中。我尝试过使用value1.add(value2)但是当我测试值时,只有值1的值似乎出现了。我不知道value2在哪里这是我的类和界面
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);
}
答案 0 :(得分:1)
interface
有点合同,sais&#34;无论是谁强制我必须包含所有在我声明的方法的实现&#34 ;。每class
implements
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;
}
}
都明确表示合同,因此必须拥有拥有这些方法的实施。
在您的课程中,您缺少整个界面的实现,这就是您的代码未编译的原因。你的课应该是这样的:
{{1}}
答案 1 :(得分:0)
请退后一步,考虑一下到目前为止的解决方案。您的班级MyFraction
包含四个列表 - 为什么? “我的”MyFraction
将由分子,分母和标志组成。
我会选择Character
来存储标志吗?&gt;我不知道。但你应该考虑分子和分母的负值,例如分子是-4,分母-2。当符号输入为“ - ”时,MyFraction
的值为。{/ p>
要创建MyFraction
实例,您应该能够使用像这样的构造函数
public MyFraction(Integer theNumerator, Integer theDenominator, Character theSign)
请记住实现界面的所有方法。到目前为止,您只获得了方法add
,并且由于缺少实现,代码将无法编译。