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

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 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...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
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...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Java programming language Array - Single identifier which can store multiple values Example initialized with values:...
Java programming language Array - Single identifier which can store multiple values Example initialized with values: int[] mySimpleArray = {4, 17, 99}; Example with fixed length but no values: int[] myFixedArray = new int[11]; The brackets identify the index. Each value has its own index number. NOTE: First element is the zeroth element: mySimpleArray[0] is 4, [1] is 17 Make a new Netbeans project called ArraysAndLoops Create the simple array with 4, 17, 99. Use System.out.println with the variable with...
Using JAVA Write a program that uses a 2-D array to store the highest and lowest...
Using JAVA Write a program that uses a 2-D array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods with their appropriate parameters: a.) getData: This method reads and stores the data in the 2-D array. b.) averageHigh: This method calculates and returns the average high temperature of the year. c.)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT