0

菜鸟到java。无法使界面正常工作。在我的界面“{ 预期”中收到一条错误消息,但不知道如何补救,因为有一个。

主文件:

import java.util.Iterator;
import java.util.TreeSet;
import javax.swing.JOptionPane;

public class hw2b 
 {


     public static void main(String[] args)
     {
         TreeSet<Pet> pets = new TreeSet<Pet>();
         pets.add(new Cat("Calico", "5"));
         pets.add(new Cat("Siamese", "10"));
         pets.add(new Dog("Irish Wolfhound", "5"));
         pets.add(new Dog("Border Collie", "10"));

         String choice;

                 choice = JOptionPane.showInputDialog("Enter your choice\n" +
                                                      "Quit\n" +
                                                      "Print\n" +
                                                      "Add Cat\n" +
                                                      "Add Dog");

                 while (! choice.equalsIgnoreCase("Quit"))
                 {
                     if (choice.equalsIgnoreCase("Print"))
                     {
                         print(pets); // List will be output in alphabetical order by breed name
                     }

                     else if (choice.equalsIgnoreCase("Add Cat"))
                     {
                         String breed = JOptionPane.showInputDialog("Enter breed of cat");
                         String years = JOptionPane.showInputDialog("Enter age of cat");
                         pets.add(new Cat(breed, years));
                         System.out.println(breed + years + " added");
                     }

                     else if (choice.equalsIgnoreCase("Add Dog"))
                     {
                         String breed = JOptionPane.showInputDialog("Enter breed of dog");
                         String years = JOptionPane.showInputDialog("Enter age of dog");
                         pets.add(new Dog(breed, years));
                         System.out.println(breed + years + " added");
                     }

                     choice = JOptionPane.showInputDialog("Enter your choice\n" +
                                                          "Quit\n" +
                                                          "Print\n" +
                                                          "Add Cat\n" +
                                                          "Add Dog"); 

                 }

     }

          static private void print(TreeSet data)
          {
              Iterator li = data.iterator();
              System.out.println();
              System.out.println();      
              while (li.hasNext())
              {
                  System.out.println(li.next());
              }
          }

}

接口文件:

public interface Pet implements Comparable  //THE ERROR SAYS THAT THE "{" IS NEEDED ON THIS LINE
{
    public String getBreed();

    public String toString();

    public double getAge();
}

一个实现我的接口的类:

public class Cat implements Pet // Dog would be nearly identical to this
{

    private String breed;

    private String years;



    Cat(String breed, String years)
    {
      this.breed = breed;
      this.years = years;
    }



    public String toString()
    {
        return "Pet's breed is " + getBreed() +
               "\nPet's age is " + getAge() +
               "\n";
    }

    public String getBreed()
    {
      return breed;
    }

    public double getAge()
        {
            double age;

              if (years == "1")
              {
                  age = 15;
              }
              else if (years == "2")
              {
                  age = 24;
              }
              else
              {
                  int yrs;
                  yrs = Integer.parseInt(years);
                  age = ((yrs - 2) * 4) + 24;
              }     

            return age;
        }
}

我的比较器:

import java.util.Comparator;

public class PetComparator implements Comparator
{
    public int compare(Object a, Object b)
    {
      Pet p1 = (Pet) a;
      Pet p2 = (Pet) b;  
      String p1Breed = p1.getBreed();
      String p2Breed = p2.getBreed(); 
      return p1Breed.compareToIgnoreCase(p2Breed);
    }
}

目标是能够从 TreeSet 开始,将 Cat 和/或 Dog 品种和年龄信息添加到列表中,然后按品种名称排序输出列表。一旦我克服了这个错误,我确信我可以解决剩下的挑战,但任何见解或指针都会受到欢迎。请参阅界面代码顶部的注释,了解错误消息说明错误的位置。提前致谢。

4

2 回答 2

0

接口不实现任何东西。上课。

编译器在声明后不会将其识别implements为有效关键字interface,因此(在没有extends关键字的情况下)它需要一个分号,并且没有得到一个。

于 2013-07-12T23:32:55.790 回答
0

一个接口不能实现另一个接口。一个接口只指定一个契约,因此让一个接口“实现”某些东西是没有意义的。

改用extends

public interface Pet extends Comparable {
   ...
}

此外,没有必要在接口中标记方法,public因为所有接口方法都是隐式的public

于 2013-07-12T23:34:13.000 回答