我不断收到方法调用预期错误。我不知道我能做什么。有没有人知道我们如何在不改变它的情况下进行主要运行?
尝试了几个小时不同的方法,但无法弄清楚。
public class UnsignedInteger {
public static void main(String[] args){
UnsignedInteger three = new UnsignedInteger(3); // 3 is "autoboxed"
UnsignedInteger four = new UnsignedInteger(4); // 4 is "autoboxed"
UnsignedInteger sum = three.add(four);
System.out.println(sum); // Should print 7
try
{
UnsignedInteger broken = new UnsignedInteger(-1);
}
catch(IllegalArgumentException e)
{
System.out.println("Negative values are not allowed");
}
int val;
// This is the constructor. It should throw an IllegalArgumentException if a
//negative value is passed in
public UnsignedInteger(Integer value){
if(value<0){
throw new IllegalArgumentException("You have entered a negative number.");
val=value;
}
}
// This method will return the Integer that was passed in via the constructor.
public Integer getRawInteger()
{
return val;
}
// Return the sum of the value stored in this object with the object passed as a parameter (i.e., otherUint)
// This should not mutate the state of our class.
public UnsignedInteger add(UnsignedInteger otherUint)
{
return getRawInteger(this.add(getRawInteger())); }
}
长风秋雁
相关分类