Question
Below is a working Name class with the following behavior: Constructor A getFormal method that returns the first name followed by the last name (separated
Constructor
A getFormal method that returns the first name followed by the last name (separated by a space)
A getOfficial method that returns the last name followed by a comma (and space), followed by the first name
toString and equals methods
A class (i.e., static) method named read that is passed a Scanner object and returns a Name object created f rom data (last and first names) read in from the scanner.
A main method that reads in Name objects, prints out the formal and official versions of the name and keeps count of how many names were processed.
import java.io.*;import java.util.*;class Name { public Name(String last, String first) { this.last = last; this.first = first; } public Name(String first) {this(\"\", first);} public String getFormal() {return first + \" \" + last;} public String getOfficial() {return last + \", \" + first;} public boolean equals(Name other) {return first.equals(other.first) && last .equals(other.last);} public String toString() {return first + \" \" + last;} public static Name read(Scanner scanner) { if (!scanner.hasNext()) return null; String last = scanner.next(); String first = scanner.next(); return new Name(last, first); } private String first, last; public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File(\"names.text\")); int count = 0; Name name = read(scanner); while(name != null) { System.out.println(\"formal: \" + name.getFormal()); System.out.println(\"official: \" + name.getOfficial()); System.out.println(); count++; name = read(scanner); } System.out.println(\"---\"); System.out.println(count + \" names processed.\"); }}Your job is to make the following changes:
Add a getInitials method that returnd a string consisting of the first and last initials, each followed by a period ('.').
Add the following logic to main:
Print out the Name object resd in using the toString method
Print out the Name's initials (using the getInitials method).
Add logic so that if two consecutive names match, and error message is printed.
The name of your class should be Name. Plase remove the public attribute from your class definition (CodeLab restriction).
For example, if the file names.text contains:
Edwards Steven Carter Joshua Carter Joshua Jackson Anthony Carter Barbara Evans Edward White Christopher White Christopher Taylor Michelle Baker Melissa Young Anthony Hall Michaelthe program should produce the following output:
name: Steven Edwardsformal: Steven Edwardsofficial: Edwards, Steveninitials: S.E.name: Joshua Carterformal: Joshua Carterofficial: Carter, Joshuainitials: J.C.Duplicate name \"Joshua Carter\" discoveredname: Anthony Jacksonformal: Anthony Jacksonofficial: Jackson, Anthonyinitials: A.J.name: Barbara Carterformal: Barbara Carterofficial: Carter, Barbarainitials: B.C.name: Edward Evansformal: Edward Evansofficial: Evans, Edwardinitials: E.E.name: Christopher Whiteformal: Christopher Whiteofficial: White, Christopherinitials: C.W.Duplicate name \"Christopher White\" discoveredname: Michelle Taylorformal: Michelle Taylorofficial: Taylor, Michelleinitials: M.T.name: Melissa Bakerformal: Melissa Bakerofficial: Baker, Melissainitials: M.B.name: Anthony Youngformal: Anthony Youngofficial: Young, Anthonyinitials: A.Y.name: Michael Hallformal: Michael Hallofficial: Hall, Michaelinitials: M.H.---12 names processed.My code:
import java.io.*;
import java.util.*;
class Name {
public Name(String last, String first) {
this.last = last;
this.first = first;
}
public Name(String first) {this(\"\", first);}
public String getFormal() {return first + \" \" + last;}
public String getOfficial() {return last + \", \" + first;}
public boolean equals(Name other) {return first.equals(other.first) && last .equals(other.last);}
public String toString() {return first + \" \" + last;}
public static Name read(Scanner scanner) {
if (!scanner.hasNext()) return null;
String last = scanner.next();
String first = scanner.next();
return new Name(last, first);
}
public String getInitials() {
return Character.toUpperCase(first.charAt(0)) + \".\" + Character.toUpperCase(last.charAt(0))+\".\";
}
private String first, last;
public static void main(String [] args) throws Exception {
Scanner scanner = new Scanner(new File(\"names.text\"));
int count = 0;
Name name = read(scanner);
while(name != null) {
System.out.println(\"name: \" + name);
System.out.println(\"formal: \" + name.getFormal());
System.out.println(\"official: \" + name.getOfficial());
System.out.println(\"initials: \" + name.getInitials());
System.out.println();
count++;
name = read(scanner);
}
System.out.println(\"---\");
System.out.println(count + \" names processed.\");
}
}
Duplicate name \"Christopher White\" discovered when the same name is scanned.Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started