Question

In: Computer Science

In java language how would I write the following? Create an array called “boyNames” and store...

In java language how would I write the following?

  • Create an array called “boyNames” and store all names from the BoyNames.txt file
  • Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file
  • Create a text menu that allowing users to:

         1. Enter a name and the application must display a message indicating if the name is   among the most popular (found on either array)

  • If the name is found, tell the user if it is a boy’s or girl’s name

         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

Solutions

Expert Solution

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.


Related Solutions

Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept of a domain name, assuming a domain name has a single attribute: the domain name itself. Include the following: - Constructor: accepts the domain name as an argument. - getDomain: an accessor method for the domain name field. - setDomain: a mutator method for the domain name field. - prefix: a method returning whether or not the domain name starts with www. - extension:...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
In Java: Write a method called copy, which is passed A[], which is an array of...
In Java: Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[]. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of size 4 in a driver class using the following statement: Circle circleArr[] = new Circle[4]; Populate the array with four different radiuses and then, using a for loop from 0 to one less then the length of the array, print the area and the diameter of each of the four circles Circle Class: import java.text.DecimalFormat; public class Circle {    DecimalFormat dec = new...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT