-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathName.java
More file actions
executable file
·73 lines (63 loc) · 2.83 KB
/
Name.java
File metadata and controls
executable file
·73 lines (63 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
File Name: Name.java
Description: The following program creates a Name object and has all name-related functions.
*/
//This is the declaration of the class.
public class Name{
//These are the declarations of the fields.
private String first;
private String last;
//This is the constructor of the class.
//It takes in a String and creates a Name object.
public Name (String fullName){
//This stores temporarily the different components of a name (first name before last name).
String[] partsOfName = fullName.split(" ");
//This initializes the fields with the different components of the name.
if (partsOfName.length == 1){
first = fullName;
last = "";
}
else{
first = partsOfName[0];
last = partsOfName[1];
}
}
//These are the mutators of the class.
//It takes in a String and returns no value.
public void setFirst(String firstName){
first = firstName;
}
public void setLast(String lastName){
last = lastName;
}
//These are the accessors of the class.
//It takes in no parameters and returns a String.
public String getFirst(){
return first;
}
public String getLast(){
return last;
}
//This is a method that would compare two Name objects lexicographically, starting with the first name.
//It takes in a Name object as the argument and returns a boolean to represent the difference between the two Name objects.
//It returns false if any part of the name is not the same and true if both first and last names are equal.
public boolean equalsToName(Name other){
//This returns the lexicographical difference between the two Names (both first and last names).
return first.equals(other.first) && last.equals(other.last);
}
//This is a method that would compare two Name objects lexicographically, starting with the first name.
//It takes in a Name object as the argument and returns an integer to represent the difference between the two Name objects.
//It returns a negative value if the argument is lexicographically greater, 0 if the two Strings are equal, and a positive value if the argument
//lexicographically less.
public int compareToName(Name other){
//This returns the lexicographical difference between the two Names (both first and last names).
return first.compareTo(other.first) + last.compareTo(other.last);
}
//This overrides the original method in the Object class.
//This is a method that would convert all information of a Name object into a presentable String.
//It takes in no parameters and returns a String.
@Override
public String toString(){
return "First Name: " + first +"\nLast Name: " + last +"\n";
}
}