Running NameTester.java
10 Expected: 10 Steve P Jobs Expected: Steve P Jobs SPJ Expected: SPJ 9 Expected: 9 Bill Gates Expected: Bill Gates BG Expected: BG
CheckStyle
Name.java:
Okpass
Student files
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | /**
* Models a person's name
*/
public class Name
{
private String f;
private String m;
private String l;
/**
* Constructs a Name given first,middle,
* and last names
* @param theFirst the first name in the Name
* @param theMiddleInitial the middle inital in
* the Name
* @param theLast the last name in the Name
*/
public Name(String theFirst, String theMiddleInitial, String theLast)
{
f = theFirst;
m = theMiddleInitial;
l = theLast;
}
/**
* Constructs a Name given first,middle,
* and last names
* @param theFirst the first name in the Name
* @param theLast the last name in the Name
*/
public Name(String theFirst, String theLast)
{
f = theFirst;
m = "";
l = theLast;
}
/**
* Gets the first name of this Name
* @return the first name of this Name
*/
public String getFirst()
{
return f;
}
/**
* Gets the middle initial name of this Name
* @return the middle initial name of this Name
*/
public String getMiddleInitial()
{
return m;
}
/**
* Gets the last name of this Name
* @return the last name of this Name
*/
public String getLast()
{
return l;
}
/**
* Gets the number of characters in the full name
* - not counting spaces
* @return the number of characters in the full name
* - not counting spaces
*/
public int numberOfCharacters()
{
return f.length() + m.length()
+ l.length();
}
/**
* Gets the full name with spaces
* separating the names
* @return the full name with spaces separating the names
*/
public String getFullName()
{
return f + " " + m + " "
+ l;
}
/**
* Gets the initials of this Name (first
* inital, middle inital, last initial)
* with no spaces
* @return the initials of this Name
*/
public String getInitials()
{
return f.substring(0,1) + m
+ l.substring(0,1);
}
}
|
Provided files
NameTester.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 |
/**
* Test the Name class
*/
public class NameTester
{
public static void main(String[] args)
{
Name name1 = new Name("Steve", "P", "Jobs");
Name name2 = new Name("Bill", "Gates");
System.out.println(name1.numberOfCharacters());
System.out.println("Expected: 10");
System.out.println(name1.getFullName());
System.out.println("Expected: Steve P Jobs");
System.out.println(name1.getInitials());
System.out.println("Expected: SPJ");
System.out.println(name2.numberOfCharacters());
System.out.println("Expected: 9");
System.out.println(name2.getFullName());
System.out.println("Expected: Bill Gates");
System.out.println(name2.getInitials());
System.out.println("Expected: BG");
}
}
|
Score
7/7