Name, print, read, equals
This commit is contained in:
parent
9aa0600245
commit
2e0a9b667c
Binary file not shown.
@ -1,19 +1,10 @@
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.Scanner;
|
||||
/**
|
||||
This class manages objects characterised by a name, a surname and a title.
|
||||
*/
|
||||
public class Name{
|
||||
|
||||
/**
|
||||
This constructor uses a default title, name and surname.
|
||||
*/
|
||||
public Name(){
|
||||
name="";
|
||||
sname="";
|
||||
title="";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This constructor sets the values of name and surname, and uses a default title.
|
||||
@param name The value of the name.
|
||||
@ -28,11 +19,11 @@ public class Name{
|
||||
|
||||
/**
|
||||
This constructor sets the values of name, surname and title.
|
||||
@param title The value of the title
|
||||
@param name The value of the name.
|
||||
@param sname The value of the surname.
|
||||
@param title The value of the title
|
||||
*/
|
||||
public Name(String name, String sname, String title){
|
||||
public Name(String title, String name, String sname){
|
||||
this.name=name;
|
||||
this.sname=sname;
|
||||
this.title=title;
|
||||
@ -75,12 +66,45 @@ public class Name{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Indicates whether some other object is "equal to" this one.
|
||||
@param n the reference object with which to compare.
|
||||
@return a boolean.
|
||||
**/
|
||||
public boolean equals(Name n){
|
||||
return this.name.equals(n.name) && this.sname.equals(n.sname) && this.title.equals(n.title);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This method prints the value of the istance variables.
|
||||
@param ps The object which must print the values.
|
||||
**/
|
||||
public void print(PrintStream ps){
|
||||
ps.println(getTitleNameSurname());
|
||||
ps.println(title);
|
||||
ps.println(name);
|
||||
ps.println(sname);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This method reads the value of the istance variables.
|
||||
@param sc The Scanner.
|
||||
@return The new Name
|
||||
**/
|
||||
public static Name read(Scanner sc) throws Exception{
|
||||
String title, name, sname;
|
||||
if(sc.hasNext()){
|
||||
title=sc.nextLine();
|
||||
if(sc.hasNext()){
|
||||
name = sc.nextLine();
|
||||
if(sc.hasNext()){
|
||||
sname=sc.nextLine();
|
||||
return new Name(title, name, sname);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
Binary file not shown.
@ -1,18 +1,30 @@
|
||||
import java.io.*;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class NameTester{
|
||||
public static void main(String [] args){
|
||||
public static void main(String [] args) throws Exception{
|
||||
|
||||
Name persona = new Name();
|
||||
persona.setTitle("Sig.");
|
||||
persona.print(System.out); //We expect Sig.
|
||||
Scanner sc = new Scanner(new File("src"));
|
||||
Name persona = Name.read(sc);
|
||||
Name persona1;
|
||||
|
||||
persona = new Name("Mario", "Rossi");
|
||||
System.out.println(persona.getInitials()); //We expect M. R.
|
||||
if(persona!=null)
|
||||
persona.print(System.out);
|
||||
else
|
||||
System.out.println("ERROR");
|
||||
|
||||
persona = new Name("Jon", "Snow", "King in the North");
|
||||
System.out.println(persona.getNameSurname()); //We expect Jon Snow
|
||||
System.out.println(persona.getTitleNameSurname()); //We expect a very big spoiler
|
||||
persona.setTitle("Hand of the Queen");
|
||||
persona.print(System.out);
|
||||
System.out.println(persona.getInitials()); //We expect T. L.
|
||||
|
||||
persona1 = new Name("King in the North", "Jon", "Snow");
|
||||
System.out.println(persona1.getNameSurname()); //We expect Jon Snow
|
||||
System.out.println(persona1.getTitleNameSurname()); //We expect a spoiler
|
||||
|
||||
if(persona.equals(persona1)) //We expect Different
|
||||
System.out.println("Equal");
|
||||
else
|
||||
System.out.println("Different");
|
||||
|
||||
}
|
||||
}
|
||||
|
3
src/Name/src
Normal file
3
src/Name/src
Normal file
@ -0,0 +1,3 @@
|
||||
Ser
|
||||
Tyrion
|
||||
Lannister
|
Loading…
Reference in New Issue
Block a user