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.