Advertisement
advictoriam

Untitled

Jan 8th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. public class Name
  2. {
  3.    private String first;
  4.    private String last;
  5.  
  6.    /**
  7.       Constructs a name from a first and last name.
  8.       @param aFirst the first name
  9.       @param aLast the last name
  10.    */
  11.    public Name(String aFirst, String aLast)
  12.    {
  13.       first = aFirst;
  14.       last = aLast;
  15.    }
  16.  
  17.    /**
  18.       Returns the first name of this name
  19.       @return the first name
  20.    */
  21.    public String getFirst()
  22.    {
  23.       return first;
  24.    }
  25.  
  26.    /**
  27.       Returns the last name of this name
  28.       @return the last name
  29.    */
  30.    public String getLast()
  31.    {
  32.       return last;
  33.    }
  34.  
  35.    /**
  36.       Checks whether this name should come before another name
  37.       when sorted by last name, then by first name.
  38.       @param other another name
  39.       @return true if this name should come before other
  40.    */
  41.    public boolean comesBefore(Name other)
  42.    {
  43.       String thisLast = this.getLast();
  44.       String otherLast = other.getLast();
  45.       for(int i = 0; i < thisLast.length(); i++)
  46.       {
  47.          if(thisLast.charAt(i) == otherLast.charAt(i)){continue;}
  48.          else if((int)thisLast.charAt(i) < (int)otherLast.charAt(i)){return true;}
  49.          return false;
  50.       }
  51.       String thisFirst = this.getFirst();
  52.       String otherFirst = other.getFirst();
  53.       for(int i = 0; i < thisFirst.length(); i++)
  54.       {
  55.          if(thisFirst.charAt(i) == otherFirst.charAt(i)){continue;}
  56.          else if((int)thisFirst.charAt(i) < (int)otherFirst.charAt(i)){return true;}
  57.          return false;
  58.       }
  59.       return false;
  60.    }
  61.  
  62.  
  63.    // this method is used to check your work
  64.  
  65.    public static boolean check(String first1, String last1,
  66.       String first2, String last2)
  67.    {
  68.       Name name1 = new Name(first1, last1);
  69.       Name name2 = new Name(first2, last2);
  70.       return name1.comesBefore(name2);
  71.    }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement