Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. //Alvin Cao
  2. //2nd Period
  3. public class Name
  4. {
  5.  
  6. private String first;
  7. private String middle;
  8. private String last;
  9.  
  10. public Name()
  11. {
  12. first = null;
  13. middle = null;
  14. last = null;
  15. }
  16.  
  17. public Name(String first, String middle, String last)
  18. {
  19. this.first = first;
  20. this.middle = middle;
  21. this.last = last;
  22. }
  23. public String getFirst()
  24. {
  25. return first;
  26. }
  27. public String getMiddle()
  28. {
  29. return middle;
  30. }
  31. public String getLast()
  32. {
  33. return last;
  34. }
  35. public String firstMiddleLast()
  36. {
  37. String fml = first + " " + middle + " " + last;
  38. return fml;
  39. }
  40. public String lastFirstMiddle()
  41. {
  42. String lmf = last + ", " + first + " " + middle;
  43. return lmf;
  44. }
  45. public boolean equals(Name otherName)
  46. {
  47. return (otherName.getFirst().equalsIgnoreCase(first)
  48. && otherName.getMiddle().equalsIgnoreCase(middle)
  49. && otherName.getLast().equalsIgnoreCase(last) );
  50. }
  51. public String initials()
  52. {
  53. String initial = first.toUpperCase().substring(0,1) + middle.toUpperCase().substring(0,1) + last.toUpperCase().substring(0,1);
  54. return initial;
  55. }
  56. public int length()
  57. {
  58. String noSpaces = first + middle + last;
  59. return noSpaces.length();
  60. }
  61. }
  62.  
  63. ------------------------ Tester ----------------------------------------
  64.  
  65. //Alvin Cao
  66. //2nd Period
  67. import java.util.Scanner;
  68. public class NameTester {
  69. public static void main (String[] args)
  70. {
  71. Scanner scan = new Scanner(System.in);
  72.  
  73. String first, middle, last;
  74.  
  75. System.out.println("Enter First Name..");
  76. first = scan.nextLine();
  77. System.out.println("Enter Middle Name..");
  78. middle = scan.nextLine();
  79. System.out.println("Enter Last Name..");
  80. last = scan.nextLine();
  81.  
  82. Name name1 = new Name(first, middle, last);
  83. System.out.println("first name: " + name1.getFirst() + "\nmiddle name: " + name1.getMiddle()
  84. + "\nlast name: " + name1.getLast());
  85.  
  86.  
  87. String first2, middle2, last2;
  88. System.out.println("Enter The Same First Name With A Different Case..");
  89. first2 = scan.nextLine();
  90. System.out.println("Enter The Same Middle Name With A Different Case..");
  91. middle2 = scan.nextLine();
  92. System.out.println("Enter The Same Last Name With A Different Case..");
  93. last2 = scan.nextLine();
  94.  
  95. Name name2 = new Name(first2, middle2, last2);
  96. System.out.println("first name: " + name2.getFirst() + "\nmiddle name: " + name2.getMiddle()
  97. + "\nlast name: " + name2.getLast());
  98.  
  99.  
  100. String first3, middle3, last3;
  101. System.out.println("Enter First Name..");
  102. first3 = scan.nextLine();
  103. System.out.println("Enter Middle Name..");
  104. middle3 = scan.nextLine();
  105. System.out.println("Enter Last Name..");
  106. last3 = scan.nextLine();
  107.  
  108. Name name3 = new Name(first3, middle3, last3);
  109. System.out.println("first name: " + name3.getFirst() + "\nmiddle name: " + name3.getMiddle()
  110. + "\nlast name: " + name3.getLast());
  111.  
  112.  
  113. System.out.println("The first name is " + name1.firstMiddleLast());
  114. System.out.println("Also written as " + name1.lastFirstMiddle());
  115. System.out.println("The initials are " + name1.initials());
  116. System.out.println("The length is " + name1.length());
  117. System.out.println();
  118. System.out.println("The second name is " + name2.firstMiddleLast());
  119. System.out.println("Also written as " + name2.lastFirstMiddle());
  120. System.out.println("The initials are " + name2.initials());
  121. System.out.println("The length is " + name2.length());
  122. System.out.println();
  123. System.out.println("The last name is " + name3.firstMiddleLast());
  124. System.out.println("Also written as " + name3.lastFirstMiddle());
  125. System.out.println("The initials are " + name3.initials());
  126. System.out.println("The length is " + name3.length());
  127. System.out.println();
  128. System.out.println("Comparing the first and second name : ");
  129. if(name1.equals(name2))
  130. {
  131. System.out.println("The two names are the same");
  132. }
  133. else
  134. {
  135. System.out.println("The two names are NOT the same");
  136. }
  137. System.out.println("Comparing the first and third name : ");
  138. if(name1.equals(name3))
  139. {
  140. System.out.println("The two names are the same");
  141. }
  142. else
  143. {
  144. System.out.println("The two names are NOT the same");
  145. }
  146. System.out.println("Comparing the second and third name : ");
  147. if(name2.equals(name3))
  148. {
  149. System.out.println("The two names are the same");
  150. }
  151. else
  152. {
  153. System.out.println("The two names are NOT the same");
  154. }
  155.  
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement