3 Replies - 1353 Views - Last Post: 28 March 2010 - 02:27 PM Rate Topic: -----

#1 LiveFlow  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 31-January 10

Using Comparable interface

Posted 28 March 2010 - 11:53 AM

I'm having trouble writing code that will take in an object and compare the objects based on a return from my getScore method. Any help is appreciated. Any additional information can be given on request also.

     public class ShortStirrupRider implements Rider1, Comparable <Rider1> 
   {
       public ShortStirrupRider(String nameofRider, String typeofPony,double heightofFence,double overallScore)
      {
         Rider = nameofRider;
         Pony = typeofPony;
         Fence = heightofFence;
         Score = overallScore;
      }
       public int compareTo(Object Rider1)
      {
      Rider1 
       public String toString(){
         String output = "Name: " + nameofRider + "\n" +
            "Type of Pony: " + typeofPony +"%\n" + 
            "Fence Height:" + heightofFence + "\n" +
            "Overall Score: " + overallScore + "\n";
         return output;
      
      
      
      }
   }



    public class Rider1 implements ShortStirrupRider, LongStirrupRider
   {
   
   
       public int setScore();
      {
      }
       public int getScore();
      {
      }
       public int setHeight();
      {
         double height;
         private static final int inch = 4;
         private static final int hand = 1;
         double equineHeight;
      	
      	
      	
         
      	
         
      }
   
       public String getHeight();
      {
      
      
      } 
   }


This post has been edited by LiveFlow: 28 March 2010 - 11:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using Comparable interface

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9039
  • View blog
  • Posts: 33,537
  • Joined: 27-December 08

Re: Using Comparable interface

Posted 28 March 2010 - 12:09 PM

Since you're implementing the Generic Comparable interface of type Rider1, your param in the compareTo() method should be of type Rider1, not Object. In addition, having a type in the middle of your method as you do here Rider1 is illegal. Lastly, you cannot define methods within methods like you do with toString(). You need to close your compareTo() method before you can begin defining toString().
public int compareTo(Object Rider1) { 
      Rider1  
      public String toString(){ 
         String output = "Name: " + nameofRider + "\n" + 
            "Type of Pony: " + typeofPony +"%\n" +  
            "Fence Height:" + heightofFence + "\n" + 
            "Overall Score: " + overallScore + "\n"; 
         return output; 
         
      }
} 



So your method should look something more like:
public  int compareTo(Rider1 other){
   //code
}


I'm guessing for your ShortStirrupRider class you want to extend Rider1, since you cannot implement a class as you try to do here public class ShortStirrupRider implements Rider1. This should instead be public class ShortStirrupRider extends Rider1.
Was This Post Helpful? 0
  • +
  • -

#3 LiveFlow  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 31-January 10

Re: Using Comparable interface

Posted 28 March 2010 - 02:18 PM

I've made the changes suggested but now I'm getting the following errors:
    public class ShortStirrupRider implements Rider1, Comparable <Rider1> 
   {
       public ShortStirrupRider(String nameofRider, String typeofPony,double heightofFence,double overallScore)
      {
         Rider = nameofRider;
         Pony = typeofPony;
         Fence = heightofFence;
         Score = overallScore;
      }
       public int compareTo(Rider1 other)
      {
         return new Double(getScore()).compareTo(new Double(other.getScore()));
      }
       public String toString(){
         String output = "Name: " + nameofRider + "\n" +
            "Type of Pony: " + typeofPony +"%\n" + 
            "Fence Height:" + heightofFence + "\n" +
            "Overall Score: " + overallScore + "\n";
         return output;
      
      
      
      }
   }




    public class Rider1 implements  ShortStirrupRider, LongStirrupRider
   {
   
   
       public int setScore();
      {
      }
       public int getScore();
      {
      
      }
       public int setHeight();
      {
         double height;
         private static final int inch = 4;
         private static final int hand = 1;
         double equineHeight;
      	
      	
      	
         
      	
         
      }
   
       public String getHeight();
      {
      
      
      } 
   }





 ----jGRASP exec: javac -g C:\Documents and Settings\Rondel\Desktop\ShortStirrupRider.java

Rider1.java:15: illegal start of expression
         private static final int inch = 4;
         ^
Rider1.java:15: illegal start of expression
         private static final int inch = 4;
                 ^
Rider1.java:15: ';' expected
         private static final int inch = 4;
                       ^
Rider1.java:16: illegal start of expression
         private static final int hand = 1;
         ^
Rider1.java:16: illegal start of expression
         private static final int hand = 1;
                 ^
Rider1.java:16: ';' expected
         private static final int hand = 1;
                       ^
Rider1.java:1: interface expected here
    public class Rider1 implements  ShortStirrupRider, LongStirrupRider
                                    ^
Rider1.java:1: cannot find symbol
symbol: class LongStirrupRider
    public class Rider1 implements  ShortStirrupRider, LongStirrupRider
                                                       ^
ShortStirrupRider.java:1: interface expected here
    public class ShortStirrupRider implements Rider1, Comparable <Rider1> 
                                              ^
ShortStirrupRider.java:5: cannot find symbol
symbol  : variable Rider
location: class ShortStirrupRider
         Rider = nameofRider;
         ^
ShortStirrupRider.java:6: cannot find symbol
symbol  : variable Pony
location: class ShortStirrupRider
         Pony = typeofPony;
         ^
ShortStirrupRider.java:7: cannot find symbol
symbol  : variable Fence
location: class ShortStirrupRider
         Fence = heightofFence;
         ^
ShortStirrupRider.java:8: cannot find symbol
symbol  : variable Score
location: class ShortStirrupRider
         Score = overallScore;
         ^
ShortStirrupRider.java:12: cannot find symbol
symbol  : method getScore()
location: class ShortStirrupRider
         return new Double(getScore()).compareTo(new Double(other.getScore()));
                           ^
ShortStirrupRider.java:15: cannot find symbol
symbol  : variable nameofRider
location: class ShortStirrupRider
         String output = "Name: " + nameofRider + "\n" +
                                    ^
ShortStirrupRider.java:16: cannot find symbol
symbol  : variable typeofPony
location: class ShortStirrupRider
            "Type of Pony: " + typeofPony +"%\n" + 
                               ^
ShortStirrupRider.java:17: cannot find symbol
symbol  : variable heightofFence
location: class ShortStirrupRider
            "Fence Height:" + heightofFence + "\n" +
                              ^
ShortStirrupRider.java:18: cannot find symbol
symbol  : variable overallScore
location: class ShortStirrupRider
            "Overall Score: " + overallScore + "\n";
                                ^
Rider1.java:5: missing method body, or declare abstract
       public int setScore();
                  ^
Rider1.java:8: missing method body, or declare abstract
       public int getScore();
                  ^
Rider1.java:12: missing method body, or declare abstract
       public int setHeight();
                  ^
Rider1.java:26: missing method body, or declare abstract
       public String getHeight();
                     ^
22 errors



Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9039
  • View blog
  • Posts: 33,537
  • Joined: 27-December 08

Re: Using Comparable interface

Posted 28 March 2010 - 02:27 PM

View Postmacosxnerd101, on 28 March 2010 - 03:09 PM, said:

I'm guessing for your ShortStirrupRider class you want to extend Rider1, since you cannot implement a class as you try to do here public class ShortStirrupRider implements Rider1. This should instead be public class ShortStirrupRider extends Rider1.


ShortStirrupRider and Rider1 are classes. You cannot implement a class. Also, even after you change the implements to extends, you will have a recursive design- Rider1 is the parent of ShortStirrupRider is the parent of Rider1. This will either fail to compile or give you a StackOverflowError.

public ShortStirrupRider(String nameofRider, String typeofPony,double heightofFence,double overallScore) 
      { 
         Rider = nameofRider; 
         Pony = typeofPony; 
         Fence = heightofFence; 
         Score = overallScore; 
      } 


Where do you declare the variables Rider, Pony, Fence, and Score? Remember- you have to declare variables with type and name before you can initialize them. Also, in your toString() method, you reference the params in the constructor. This is illegal, as parameters are local to the method they are declared in. If you properly declare the variables Rider, Pony, Fence, and Score as instance variables, you can access those in your toString() method.

Here public String getHeight(); as well as in a couple of your other methods, remove the semi-colon for the definitions. The semi-colon implies that your method is abstract, which is false, as you use braces to define a method body.

public int setHeight(); 
      { 
         private static final int inch = 4; 
         private static final int hand = 1; 


You cannot declare variables using the keywords public, private, protected or static inside methods. These keywords imply instance or global variables, which must be declared within the class, but not in any specific method. So for example:
class MyClass{
   private int x; //an instance variable
   private static int y; //a global variable
   public static final int z = 3; //a global constant
}


This post has been edited by macosxnerd101: 28 March 2010 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1