-
Notifications
You must be signed in to change notification settings - Fork 0
/
Name.java
81 lines (62 loc) · 1.59 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
71
72
73
74
75
76
77
78
79
80
81
import java.util.Scanner;
public class Name {
private static Scanner scan = new Scanner(System.in);
private String first;
private char middle;
private String last;
//constructors
public Name() {
makeName();
}
public Name(String first, char middle, String last) {
setFirst(first);
setMiddle(middle);
setLast(last);
}
//end constructors
//setters and getters
public String getFirst() {
return first;
}
public void setFirst(String first) {
first = first.substring(0, 1).toUpperCase() + first.substring(1);
this.first = first;
}
public char getMiddle() {
return middle;
}
public void setMiddle(char middle) {
this.middle = middle;
}
public String getLast() {
return last;
}
public void setLast(String last) {
last = last.substring(0, 1).toUpperCase() + last.substring(1);
this.last = last;
}
//end setters and getters
public String toString() {
String result = "Name: " + first + " ";
if(middle == '.') {
result += last;
} else {
result += middle + ". " + last;
}
return result;
}//end toString
public void makeName() {
//first name
System.out.println("Enter your first name: ");
first = scan.next();
setFirst(first);
//middle name
System.out.println("Enter your middle initial: (enter'.' (period) if you don't have a middle name): ");
middle = scan.next().toUpperCase().charAt(0);
setMiddle(middle);
//last name
System.out.println("Enter your last name: ");
last = scan.next();
setLast(last);
}//end makeName
}//end Name Class