Comparable 関数で 2 つの住所を比較しようとしています。現在、住所の文字列(通りの名前)を作成中です。これまでのところ、次のコードがあります。最終的には番地も比較できるようにしたいのですが、最初にこのコードを修正できるようにしたいと考えています。このコードをコンパイルしようとすると、次のエラーが発生します。
StreetAddress.java:45: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
StreetAddress add1 = new StreetAddress("cartesian road");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
StreetAddress.java:46: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
StreetAddress add2 = new StreetAddress("cartesian road");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
StreetAddress.java:47: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
StreetAddress add3 = new StreetAddress("n kings street");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
StreetAddress.java:48: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
StreetAddress add4 = new StreetAddress("pioneer parkway");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
StreetAddress.java:49: error: constructor StreetAddress in class StreetAddress cannot be applied to given types;
StreetAddress add5 = new StreetAddress("starry avenue");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
5 errors
public class StreetAddress implements Comparable<StreetAddress>
{
protected int num;
protected String stName;
public void StreetAddress(int n, String s){
num = n;
stName = s;
}
public int getNum(){
// returns the street number
return num;
}
public String getName(){
// returns the street name
return stName;
}
public int compareTo(StreetAddress street) throws ClassCastException{
// exception prevents crash if an address is not compared to
// another address
StreetAddress x = (StreetAddress) street;
int compareName = this.stName.compareTo(street.stName);
if (compareName < 0){
// first address comes after compared address
System.out.println("test<0");
}
else if (compareName == 0){
// same address
System.out.println("test equal");
}
else{
// first address comes before compared address
System.out.println("test > 0");
}
}
public static void main(String args[])
{
StreetAddress add1 = new StreetAddress("cartesian road");
StreetAddress add2 = new StreetAddress("cartesian road");
StreetAddress add3 = new StreetAddress("n kings street");
StreetAddress add4 = new StreetAddress("pioneer parkway");
StreetAddress add5 = new StreetAddress("starry avenue");
add1.compareTo(add2);
add4.compareTo(add1);
add3.compareTo(add3);
add2.compareTo(add5);
}
}