-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathName.java
70 lines (59 loc) · 1.76 KB
/
Name.java
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
package com.B5015845.CSC8002.Coursework;
/**
* The Name class creates names, and can store and retrieve individual components of the name, such as first name, last name and
* initials.
* @author Arianna Esposito
*/
public final class Name {
private final String firstName;
private final String lastName;
/**
* Constructs and initialises the objects of the name (first name and last name).
* @param first name
* @param last name
*/
public Name (String firstName, String lastName)
{
if (firstName == null || lastName == null)
{
throw new NullPointerException("Student must have both a first name and a last name");
}
this.firstName = firstName;
this.lastName = lastName;
}
/**
* Retrieves the first name.
* @return this name's first name.
*/
public String getFirstName() { //String is immutable.
return firstName;
}
/**
* Retrieves the last name.
* @return this name's last name.
*/
public String getLastName() { //String is immutable.
return lastName;
}
/**
* @Override
* @return the string representation of names when printed.
*/
@Override
public String toString() //String is immutable.
{
return firstName + " " + lastName;
}
/**
* Retrieves the name's initials.
* Code adapted from: //https://stackoverflow.com/questions/328249/how-to-concatenate-characters-in-java
* @return this name's initials.
*/
public String getInitials() //String is immutable.
{
char firstInitials = getFirstName().charAt(0);
char secondInitials = getLastName().charAt(0);
String initials = new StringBuilder().append(firstInitials).append(secondInitials).toString();
return initials;
}
}