Tuesday, November 30, 2010

Lab 22 - Record Class


1) Remove quotes around Record

public class Record implements Comparable<"Record">
{
 
private String name;
 
private int age;
 
 
public Record()
 
{
 
this.setName("");
 
this.setAge(0);
 
}
 
 
public Record(String n, int a)
 
{
 
this.setName(n);
 
this.setAge(a);
 
}

 
public String getName() {
 
return name;
 
}

 
public void setName(String name) {
 
this.name = name;
 
}

 
public int getAge() {
 
return age;
 
}

 
public void setAge(int age) {
 
this.age = age;
 
}
 
 
public int compareTo(Record that)
   
{
     
int returnValue;

     returnValue
= 0;

     
if(this.getName().compareTo(that.getName()) < 0)
     
{
       returnValue
= -1;
     
}
     
else if(this.getName().compareTo(that.getName()) > 0)
     
{
       returnValue
= +1;
     
}
     
else
     
{
     
if(this.getAge() < that.getAge())
       returnValue
= -1;
     
else if(this.getAge() > that.getAge())
       returnValue
= +1;
     
else
       returnValue
= 0;
     
}

     
return returnValue;
   
}
 
 
public String toString()
 
{
 
String s;
  s
= String.format("Name: %s , Age: %d" , this.getName(), this.getAge());
 
return s;
 
}
 
 
}

No comments: