Good morning to everyone. I have been asked to derive a SortedLinkedList from the java.util.LinkedList which will sort members of a gym based on their last names. In addition will calculate the BMI of each member, the members will be added from a text file.
Here is my code.

1.Class Member

import java.util.*;

public class Member implements Comparable {
	
    // instance variables - replace the example below with your own
    private String firstName;                   //First Name
    private String lastName;                    //Last Name
    private double height;                      //Member's Height
    private double weight;                      //Member's Weight

    /**
     * Constructor for objects of class Member.
     * Default constructor with no parameters.
     */
    public Member() 
    {
    	// initialise instance variables
        firstName = new String();
        lastName = new String();
        height = (double) new Double(0.0).doubleValue();
        weight = (double) new Double(0.0).doubleValue();

    }
    
    /**
     * Constructor for objects of class Member
     * with parameters to pass values and to set
     * the private fields.
     */
    public Member(String FN, String LN, double H, double W) //double BMI,String BMIGroup
    {
    	 this.firstName = FN;
         this.lastName = LN;
         this.height = H;
         this.weight = W;
         //this.BMI = BMI;
         //this.BMIGroup = BMIGroup;
    }
    
    /** The following method setFirstName() allows
     *  the contents of first name to be changed and
     *  to store different String value.
     */
    public void setFirstName(String FN)
    {
        firstName = FN;
    }
    
     /** The following method setLasttName() allows
     *   the contents of last name to be changed and
     *   to store different String value.
     */
    public void setLastName(String LN)
    {
        lastName = LN;
    }
    
     /** The following method setHeight() allows
     *   the contents of height to be changed and
     *   to store different Double value.
     */
    public void setHeight(double H)
    {
        height = H;
    }
    
     /** The following method setWeight() allows
     *   the contents of weight to be changed and
     *   to store different Double value.
     */
    public void setWeight(double W)
    {
        weight = W;
    }
    
     /** The foollowing mathod getFirstName() is
     *  returning a String with the contents of 
     *  variable firstName to whoever object calls
     *  it.
     */
    public String getFirstName()
    {
        return firstName;
    }
    
     /** The foollowing mathod getLastName() is
     *  returning a String with the contents of 
     *  variable lastName to whoever object calls
     *  it.
     */
    public String getLastName()
    {
        return lastName;
    }
    
     /** The foollowing mathod getHeight() is
     *  returning a Double with the contents of 
     *  variable height to whoever object calls
     *  it.
     */
    public double getHeight()
    {
        return height;
    }
    
     /** The foollowing mathod getWeight() is
     *  returning a Double with the contents of 
     *  variable weight to whoever object calls
     *  it.
     */
    public double getWeight()
    {
        return weight;
    }
    
    /**
     * Method groupBMI() which place each
     * Member in the correct group based
     * on their bmi.
     */
    public String groupBMI()
    {          
            if(calculateBMI() <= 18.5)
            {
                return "GROUP 1";
            }else if((calculateBMI() > 18.5) && (calculateBMI() <= 24.9))
            {
                return "GROUP 2";
            }else if((calculateBMI() > 24.9) && (calculateBMI() <= 29.9))
            {
                return "GROUP 3";
            }else if(calculateBMI() > 29.9)
            {
                return "GROUP 4";
            }
            
            return "Not In Groups";
    }
    
    /** 
     *  Method calculateBMI() which calculates 
     *  each member's their bmi.
     */
    public double calculateBMI()
    {
          double bmi = 0.0;
          double h = getHeight();
          double w = getWeight();
            
          bmi = (w/(h * h));
            
          return bmi;
    }
    
    
    public int compareTo(Member otherMember)
    {
    	Member m = (Member) otherMember;
    	
    	int x;
    	
    	if(getLastName().compareTo(getLastName()) < 0)
    	{
    		x = -1;
    		return x;
    		
    	}
    	else if(getLastName().compareTo(getLastName()) == 0)
    	{
    		x = 0;
    		return x;
    	}
    	else
    	{
    		x = 1;
    		return x;
       	}
    	
    	
    }
    
    public String toString()
    {
    	return getFirstName() + getLastName() + getHeight() + getWeight() + groupBMI() + calculateBMI();
    }
    

}

2. SortedLinkedList

import java.util.*;

public class SortedLinkedList<E extends Comparable<E>> extends LinkedList<E>   {

    public SortedLinkedList()  
    {  
    }  
  
    public boolean add(E e)  
    {  
        int position = size();  
          
        for (int i = size() - 1; i>=0; i--)  
        {  
            if (e.compareTo(get(i))>0)  
            {  
                break;  
            }  
            position--;  
        }  
        add(position, e);  
          
        return position == size();  
    }  
    
	/**
	 * Method readFile()
	 * This method reads from a file and add to the
	 * SortedLinkedList objects of type Member
	 */
    /**
	public static SortedLinkedList<Member> readFile() throws FileNotFoundException
    {
        //Creation of new object FileReader to read file. Creation of a new Scanner 
        //object to read data from the currentline
        Scanner input = new Scanner(new FileReader("Members.txt"));
        
        int numberOfMembers = 0;
                   
        SortedLinkedList<Member> GymList = new SortedLinkedList(); 
        
         try
         {
            //read line by line
            while(input.hasNextLine())
            {                         
                   numberOfMembers +=1;
                   String line = input.nextLine();
                   String[] split= line.split(" ");
                   String fName = split[0];
                   String lName = split[1];
                   double m_height = Double.parseDouble(split[2]);
                   double m_weight = Double.parseDouble(split[3]);
                   System.out.println( fName + " " + lName + " " + m_height + " " + m_weight);
                   GymList.add(new Member(fName, lName, m_height, m_weight));
                   
                   
                
            }
         }catch( InputMismatchException e )
         {
             System.out.println("Invalid input entered.. please try again");
         }
        input.close();
    }
    */
    public static void main(String[] args)  
    {  
        Member member1= new Member("John", "Mate",1.65 , 65);  
        Member member2= new Member("Marios", "Pavlou",1.75 , 75);   
        Member member3= new Member("Sofoklis", "Andreou",1.70 , 85);    
          
        SortedLinkedList<Member> GymList = new SortedLinkedList();  
          
        GymList.add(member1);  
        GymList.add(member2);  
        GymList.add(member3);  
          
        for (int i = 0; i<GymList.size();i++)  
        {  
            System.out.println(GymList.get(i));  
        }     
    }  
}

I am using eclipse to run my program and I am receiving the following errors:

1. My Member class cannot implement Comparable.

2. The type Member is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type SortedLinkedList<E>.

I appreciate any help.

Recommended Answers

All 7 Replies

Member class cannot implement Comparable.

You need to get the correct error messages from the java compiler. Your posted error message does NOT tell you what the problem is. Use the javac command to get the correct error message.

OK, a few subtle problems here: class Member implements Comparable { Comparable is a "raw type", so your compareTo method has to take any kind of Object as its parameter. if you say class Member implements Comparable<Member> { then (a) you no longer have a raw type warning, and (b) Member is the correct parameter for compareTo

public int compareTo(Member otherMember) {
   Member m = (Member) otherMember;

This cast is redundant, since the parameter must already be a Member SortedLinkedList<Member> GymList = new SortedLinkedList(); should be SortedLinkedList<Member> GymList = new SortedLinkedList<Member>(); Finally, don't try to summarise Java error messages in your own words (eg "My Member class cannot implement Comparable"). Always quote the complete exact error message

My program runs, but I don't get the results as I expected actually I was expecting to be sorted based on the last names of the members. In a strange way it is sorted based on their Group.
Here is the output from eclipse:

Sofoklis Andreou 1.7 50.0 GROUP 1 17.301038062283737
Marios Pavlou 1.75 75.0 GROUP 2 24.489795918367346
John Mate 1.65 90.0 GROUP 4 33.057851239669425

Class Member:

import java.util.*;

public class Member implements Comparable<Member> {
	
    // instance variables - replace the example below with your own
    private String firstName;                   //First Name
    private String lastName;                    //Last Name
    private double height;                      //Member's Height
    private double weight;                      //Member's Weight

    /**
     * Constructor for objects of class Member.
     * Default constructor with no parameters.
     */
    public Member() 
    {
    	// initialise instance variables
        firstName = new String();
        lastName = new String();
        height = (double) new Double(0.0).doubleValue();
        weight = (double) new Double(0.0).doubleValue();

    }
    
    /**
     * Constructor for objects of class Member
     * with parameters to pass values and to set
     * the private fields.
     */
    public Member(String FN, String LN, double H, double W) //double BMI,String BMIGroup
    {
    	 this.firstName = FN;
         this.lastName = LN;
         this.height = H;
         this.weight = W;
         //this.BMI = BMI;
         //this.BMIGroup = BMIGroup;
    }
    
    /** The following method setFirstName() allows
     *  the contents of first name to be changed and
     *  to store different String value.
     */
    public void setFirstName(String FN)
    {
        firstName = FN;
    }
    
     /** The following method setLasttName() allows
     *   the contents of last name to be changed and
     *   to store different String value.
     */
    public void setLastName(String LN)
    {
        lastName = LN;
    }
    
     /** The following method setHeight() allows
     *   the contents of height to be changed and
     *   to store different Double value.
     */
    public void setHeight(double H)
    {
        height = H;
    }
    
     /** The following method setWeight() allows
     *   the contents of weight to be changed and
     *   to store different Double value.
     */
    public void setWeight(double W)
    {
        weight = W;
    }
    
     /** The foollowing mathod getFirstName() is
     *  returning a String with the contents of 
     *  variable firstName to whoever object calls
     *  it.
     */
    public String getFirstName()
    {
        return firstName;
    }
    
     /** The foollowing mathod getLastName() is
     *  returning a String with the contents of 
     *  variable lastName to whoever object calls
     *  it.
     */
    public String getLastName()
    {
        return lastName;
    }
    
     /** The foollowing mathod getHeight() is
     *  returning a Double with the contents of 
     *  variable height to whoever object calls
     *  it.
     */
    public double getHeight()
    {
        return height;
    }
    
     /** The foollowing mathod getWeight() is
     *  returning a Double with the contents of 
     *  variable weight to whoever object calls
     *  it.
     */
    public double getWeight()
    {
        return weight;
    }
    
    /**
     * Method groupBMI() which place each
     * Member in the correct group based
     * on their bmi.
     */
    public String groupBMI()
    {          
            if(calculateBMI() <= 18.5)
            {
                return " GROUP 1 ";
            }else if((calculateBMI() > 18.5) && (calculateBMI() <= 24.9))
            {
                return " GROUP 2 ";
            }else if((calculateBMI() > 24.9) && (calculateBMI() <= 29.9))
            {
                return " GROUP 3 ";
            }else if(calculateBMI() > 29.9)
            {
                return " GROUP 4 ";
            }
            
            return "Not In Groups";
    }
    
    /** 
     *  Method calculateBMI() which calculates 
     *  each member's their bmi.
     */
    public double calculateBMI()
    {
          double bmi = 0.0;
          double h = getHeight();
          double w = getWeight();
            
          bmi = (w/(h * h));
            
          return bmi;
    }
    
    
    public int compareTo(Member otherMember)
    {
    	Member m = (Member) otherMember;
    	
    	int x;
    	
    	if(getLastName().compareTo(getLastName()) < 0)
    	{
    		x = -1;
    		return x;
    		
    	}
    	else if(getLastName().compareTo(getLastName()) == 0)
    	{
    		x = 0;
    		return x;
    	}
    	else
    	{
    		x = 1;
    		return x;
       	}
    	
    	
    }
    
    public String toString()
    {
    	return getFirstName() + getLastName() + getHeight() + " " + getWeight() + groupBMI() + calculateBMI();
    }
    

}

Class SortedLinkedList:

import java.util.*;

public class SortedLinkedList<E extends Comparable<E>> extends LinkedList<E>   {

    public SortedLinkedList()  
    {  
    }  
  
    public boolean add(E e)  
    {  
        int position = size();  
          
        for (int i = size() - 1; i>=0; i--)  
        {  
            if (e.compareTo(get(i))>0)  
            {  
                break;  
            }  
            position--;  
        }  
        add(position, e);  
          
        return position == size();  
    }  
    
	/**
	 * Method readFile()
	 * This method reads from a file and add to the
	 * SortedLinkedList objects of type Member
	 */
    /**
	public static SortedLinkedList<Member> readFile() throws FileNotFoundException
    {
        //Creation of new object FileReader to read file. Creation of a new Scanner 
        //object to read data from the currentline
        Scanner input = new Scanner(new FileReader("Members.txt"));
        
        int numberOfMembers = 0;
                   
        SortedLinkedList<Member> GymList = new SortedLinkedList(); 
        
         try
         {
            //read line by line
            while(input.hasNextLine())
            {                         
                   numberOfMembers +=1;
                   String line = input.nextLine();
                   String[] split= line.split(" ");
                   String fName = split[0];
                   String lName = split[1];
                   double m_height = Double.parseDouble(split[2]);
                   double m_weight = Double.parseDouble(split[3]);
                   System.out.println( fName + " " + lName + " " + m_height + " " + m_weight);
                   GymList.add(new Member(fName, lName, m_height, m_weight));
                   
                   
                
            }
         }catch( InputMismatchException e )
         {
             System.out.println("Invalid input entered.. please try again");
         }
        input.close();
    }
    */
    public static void main(String[] args)  
    {  
        Member member1= new Member("John ", "Mate ",1.65 ,90);  
        Member member2= new Member("Marios ", "Pavlou ",1.75 , 75);   
        Member member3= new Member("Sofoklis ", "Andreou ",1.70 , 50);    
          
        SortedLinkedList<Member> GymList = new SortedLinkedList<Member>(); 
          
        GymList.add(member1);  
        GymList.add(member2);  
        GymList.add(member3);  
          
        for (int i = 0; i<GymList.size();i++)  
        {  
            System.out.println(GymList.get(i) );  
        }     
    }  
}

Try debugging your code by adding some printlns to the compareTo to show what values it receives and what it returns.

Hi Norm if I understand what you meant is something like the follow lines of code

compareTo()

public int compareTo(Member otherMember)
    {
    	Member m = (Member) otherMember;
    	
    	int x;
    	
    	if(getLastName().compareTo(getLastName()) < 0)
    	{
    		x = -1;
    		System.out.println("name2 follows name1");
    		return x;
    		
    	}
    	else if(getLastName().compareTo(getLastName()) == 0)
    	{
    		x = 0;
    		System.out.println("The strings are equal.");
    		return x;
    	}
    	else
    	{
    		x = 1;
    		System.out.println("name1 follows name2");
    		return x;
       	}
    	
    	
    }

the result:

The strings are equal.
The strings are equal.
Sofoklis Andreou 1.7 50.0 GROUP 1 17.301038062283737
Marios Pavlou 1.75 75.0 GROUP 2 24.489795918367346
John Mate 1.65 90.0 GROUP 4 33.057851239669425

If not is what you asked me to do, could you give me some directions
how to do it?
Thanks

Your print out does not make sense. All three are different.
What are the values that "are equal"?
You need to print out the value of the variables that are being compared so you can see the data that the code is working with.

if(getLastName().compareTo(getLastName())

Of course they are equal, you are comparing the same thing with itself: getLastName()
It would work better if you compared this.getLastName() with otherMember.getLastName()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.