Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Name
- {
- private String first;
- private String last;
- /**
- Constructs a name from a first and last name.
- @param aFirst the first name
- @param aLast the last name
- */
- public Name(String aFirst, String aLast)
- {
- first = aFirst;
- last = aLast;
- }
- /**
- Returns the first name of this name
- @return the first name
- */
- public String getFirst()
- {
- return first;
- }
- /**
- Returns the last name of this name
- @return the last name
- */
- public String getLast()
- {
- return last;
- }
- /**
- Checks whether this name should come before another name
- when sorted by last name, then by first name.
- @param other another name
- @return true if this name should come before other
- */
- public boolean comesBefore(Name other)
- {
- String thisLast = this.getLast();
- String otherLast = other.getLast();
- for(int i = 0; i < thisLast.length(); i++)
- {
- if(thisLast.charAt(i) == otherLast.charAt(i)){continue;}
- else if((int)thisLast.charAt(i) < (int)otherLast.charAt(i)){return true;}
- return false;
- }
- String thisFirst = this.getFirst();
- String otherFirst = other.getFirst();
- for(int i = 0; i < thisFirst.length(); i++)
- {
- if(thisFirst.charAt(i) == otherFirst.charAt(i)){continue;}
- else if((int)thisFirst.charAt(i) < (int)otherFirst.charAt(i)){return true;}
- return false;
- }
- return false;
- }
- // this method is used to check your work
- public static boolean check(String first1, String last1,
- String first2, String last2)
- {
- Name name1 = new Name(first1, last1);
- Name name2 = new Name(first2, last2);
- return name1.comesBefore(name2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement