In: Computer Science
In java language how would I write the following?
1. Enter a name and the application must display a message indicating if the name is among the most popular (found on either array)
2. Option to suggest a name. Program must suggest/display a name to the user by randomly selecting one from the appropriate array depending if the user wants a boy’s or girl’s name
3. Option to display all names starting with a specific letter. Display all names starting
with a specific letter (e.g. all names starting with the letter R) depending if the user
wants a boy’s or girl’s name
After a user either enters a name from (1) in the menu or is suggested a name from (2), the program must ask if the name is acceptable for the newborn or not
1. If the name is not acceptable, user must be given the option to exit the program or repeat the procedure. Either ask for a new name to search if user is in (1), or suggest a new name randomly if in (2).
2. If the name is acceptable, program must ask the user to enter the newborn’s last name and expected date of birth.
3. Then, the program must instantiate an object of the class called NewBorn using its constructor with the accepted name for the newborn, last name, and expected date of birth.
4. Finally, program must display the state of the NewBorn object
CODE:
package naming;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
//class NewBorn
class NewBorn{
//private variables
private final String firstname;
private final String lastname;
private final String e_dob;
//Constructor
public NewBorn(String firstname,String lastname,String
e_dob){
this.firstname = firstname;
this.lastname = lastname;
this.e_dob = e_dob;
}
//toString() - method
@Override
public String toString(){
return "FirstName :"+firstname+"\nLastName : "+lastname+"\nExpected
date of birth : "+e_dob+"\n";
}
}
public class Naming {
public static void main(String[] args) throws FileNotFoundException
{
File file1;
Scanner fscan;
int count = 0,choice = 1;
String firstname = null;
Scanner scan = new Scanner(System.in);
//reading names from BoyNames.txt and Storing them to boyNames
String array
file1= new File("BoyNames.txt");
fscan= new Scanner(file1);
while(fscan.hasNextLine()){
//counting number of names in the file to create boyNames[]
count++;
fscan.nextLine();
}
String[] boyNames= new String[count];
count = 0;
fscan= new Scanner(file1);
while(fscan.hasNextLine()){
//reading and storing
boyNames[count++] = fscan.nextLine();
}
//reading names from GirlNames.txt and Storing them to girlNames
String array
count = 0;
file1 = new File("GirlNames.txt");
fscan = new Scanner(file1);
while(fscan.hasNextLine()){
fscan.nextLine();
//counting number of names in the file to create girlNames[]
count++;
}
String[] girlNames = new String[count];
count = 0;
fscan= new Scanner(file1);
while(fscan.hasNextLine()){
//reading and storing
girlNames[count++] = fscan.nextLine();
}
//use this to print the names in both the files
//System.out.println(Arrays.toString(boyNames));
//System.out.println(Arrays.toString(girlNames));
while(choice != 4){
printMenu();
choice = scan.nextInt();
switch(choice){
//Menu 1
case 1:{
int flag = 0;
char choice1;
System.out.print("Enter Name to Search(Starting letter Capital)
:");
String name = scan.next();
if(Arrays.asList(boyNames).contains(name)){
flag = 1;
System.out.println("Entered name is one of the popular boy's
names.");
}
if(Arrays.asList(girlNames).contains(name)){
flag =1;
System.out.println("Entered name is one of the popular girl's
names.");
}
if(flag == 0){
System.out.println("Name not found in the list!");
}
System.out.print("Is this Name is Acceptable for the newborn?\n(y -
yes/n - No) :");
choice1 = scan.next().charAt(0);
if(choice1 == 'y'){
firstname = name;
//making choice to 4 exits the while loop
choice = 4;
}
break;
}
//Menu 2
//read gender of the newborn
//suggest a random name based on the gender using Random()
case 2:{
char Gender,choice1;
int flag=0 ;
String name = null;
System.out.print("B - Boy's Name\nG - Girl's Name\nEnter your
choice :");
Gender = scan.next().charAt(0);
if(Gender == 'B'){
flag = 1;
//generating random number between o and length of the boyNames
array
name = boyNames[new Random().nextInt(boyNames.length)];
System.out.println("Suggested Boy Name : "+name);
}
else if(Gender == 'G'){
flag = 1;
//generating random number between o and length of the girlNames
array
name = girlNames[new Random().nextInt(girlNames.length)];
System.out.println("Suggested Boy Name : "+name);
}
else{
System.out.println("Invalid Input!");
}
if(flag == 1){
System.out.print("Is this Name is Acceptable for the newborn?\n(y -
yes/n - No) :");
choice1 = scan.next().charAt(0);
if(choice1 == 'y'){
firstname = name;
//making choice to 4 exits the while loop
choice = 4;
}
}
break;
}
//Menu 3
//read Gender and Starting letter of the newborn
//suggest names based on the starting letter and gender
case 3:{
char Gender,letter;
int flag = 0;
System.out.print("B - Boy's Name\nG - Girl's Name\nEnter your
choice :");
Gender = scan.next().charAt(0);
System.out.print("Enter the Starting letter(Caps) : ");
letter = scan.next().charAt(0);
if(Gender == 'B'){
System.out.println("Suggested Names :");
for(String boyName : boyNames){
if(letter == boyName.charAt(0)){
System.out.println(boyName);
flag = 1;
}
}
if(flag == 0)
System.out.println("Not Found!");
}
else if(Gender == 'G'){
System.out.println("Suggested Names :");
for(String girlName : girlNames){
if(letter == girlName.charAt(0)){
System.out.println(girlName);
flag = 1;
}
}
if(flag == 0)
System.out.println("Not Found!");
}
else{
System.out.println("Invalid Input!");
}
break;
}
//Menu 4
case 4:{
System.out.println("Exiting Menu!");
}
default:System.out.println("Invalid Input!");
}
}
//creating and instatiating object for NewBorn class
if(firstname != null){
System.out.print("Enter the lastname of the newborn :");
String lastname = scan.next();
System.out.print("Enter the expected date of birth of the
newborn(MM/dd/yyyy) :");
String dateString = scan.next();
NewBorn newborn = new NewBorn(firstname,lastname,dateString);
System.out.println("NewBorn Details:\n"+newborn.toString());
}
else{
System.out.println("Program terminated!");
}
}
//static method to print Menu
public static void printMenu(){
System.out.println("Menu");
System.out.print("1.Search Name\n2.Suggest a Name\n3.Search Names
based on Starting letter\n4.Exit\nEnter your Choice :");
}
}
//Screenshot of Code:
//Sample I/O:
//BoyNames.txt
//GirlNames.txt
//append more names if you want.
//Make sure to keep both the files in the same folder as the .java file.
//Hope this helps.Give thumbs up.Thank you!
//Comment if you got any doubt.