Lab_Name
可以参考一下,虽然我没加注释吧。。。。 凑活一下。。。 LAB Requirement
The Name Class
public class Name{
String first,middle,last;
public Name(String f, String m, String l){
first=f;
middle=m;
last=l;
}
public String getFirst(){
return first;
}
public String getMiddle(){
return middle;
}
public String getLast(){
return last;
}
public String firstMiddleLast(){
return first+" "+middle+" "+last;
}
public String lastFirstMiddle(){
return last+", "+first+" "+middle;
}
public boolean equals(Name o){
if(first.equalsIgnoreCase(o.getFirst())&&middle.equalsIgnoreCase(o.getMiddle())&&last.equalsIgnoreCase(o.getLast())){
return true;
}
else{
return false;
}
}
public String initials(){
return first.substring(0,1).toUpperCase()+middle.substring(0,1).toUpperCase()+last.substring(0,1).toUpperCase();
}
public int length(){
return first.length()+middle.length()+last.length();
}
}
The TestName Class
import java.util.*;
public class TestName{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.println("Enter the name of the first person. Format: firstname middlename lastname");
Name person1=new Name(scan.next(),scan.next(),scan.next());
System.out.println("Enter the name of the second person. Format: firstname middlename lastname");
Name person2=new Name(scan.next(),scan.next(),scan.next());
System.out.println(person1.firstMiddleLast());
System.out.println(person1.lastFirstMiddle());
System.out.println(person1.initials());
System.out.println(person1.length());
System.out.println(person2.firstMiddleLast());
System.out.println(person2.lastFirstMiddle());
System.out.println(person2.initials());
System.out.println(person2.length());
if(person1.equals(person2)==true){
System.out.println("They have the same name.");
}
else{
System.out.println("They don't have the same name");
}
}
}