Question

In: Computer Science

In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be...

In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be using them. In your driver class: • 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

Below is our code for the given problem

1. Main.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Main {

static Scanner scan = new Scanner(System.in);
public static String boysFile = "C:/Users/mohammadasif/Desktop/java/BoysNames.txt";
public static String girlsFile = "C:/Users/mohammadasif/Desktop/java/GirlsNames.txt";
  
public static String name = null ;
public static void main(String[] args) {

  
selectFromOption();
  
System.out.println("Is name acceptable for newBorn [Press Y if Yes] ?");
  
String userChoice = scan.next();
  
if(userChoice.equalsIgnoreCase("Y"))
{
System.out.print("Enter the last name : ");
String lastName = scan.next();
  
System.out.print("Enter the expected DOB (dd/MM/yyyy) : ");
String dob = scan.next();
  
NewBorn newborn = new NewBorn(name, lastName, dob);
  
System.out.println("First Name : "+ newborn.getFirstName() + "\nLast Name : "+ newborn.getLastName() + "\nDOB : "+ newborn.getDateofBirth());
  
System.out.println("Press R to Repeat Or Press Any Other Button to Exit");
userChoice = scan.next();
  
if(userChoice.equalsIgnoreCase("R"))
{
main(null);
}
  
}
else
{
main(null);
}
  
}

private static void selectFromOption() {
  
System.out.println("1. Check a Name ");
System.out.println("2. Get Suggested name ");
System.out.println("3. Check all name starts with specific letter ");

int choice = scan.nextInt();

switch(choice)
{
case 1:
enteredName();
break;
case 2:
getSuggestedName();
break;

case 3:
checkNamewithLetter();
break;

default :
System.out.println("Wrong Choice, try Again");
selectFromOption();
}

}

private static void enteredName() {
System.out.print("Enter name : ");
name = scan.next();
boolean popular = true;
File file1 = new File(boysFile);
File file2 = new File(girlsFile);
FileReader fr1, fr2;
try {
fr1 = new FileReader(file1);
fr2 = new FileReader(file2);
BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);
String boys = null;
String girls = null;
while((boys = br1.readLine()) != null || (girls = br2.readLine()) != null){

if(name.equalsIgnoreCase(boys))
{
System.out.println(name + " is a popular name among Boys");
popular = false;
break;
}
else if(name.equalsIgnoreCase(girls))
{
System.out.println(name + " is a popular name among Girls");
popular = false;
break;
}
  
}
  
if(popular)
{
System.out.println(name + " is not a popular name ");
}
  

} catch (FileNotFoundException ex) {

System.out.println("No Fle Found");
} catch (IOException ex) {

System.out.println("Unale to read the file");
}


}
  
  
private static void getSuggestedName() {
  
System.out.println("1. For Boys");
System.out.println("2. For Girls");

String choice = scan.next();
File file1 = new File(boysFile);
File file2 = new File(girlsFile);
FileReader fr1, fr2;
ArrayList<String> allNames = new ArrayList<String>();
Random r = new Random();


if("1".equals(choice))
{
try {
fr1 = new FileReader(file1);
BufferedReader br1 = new BufferedReader(fr1);
String boys = null;
while((boys = br1.readLine()) != null ){

allNames.add(boys);
}
  
String randomName = allNames.get(r.nextInt(allNames.size()));
System.out.println(randomName);
  
} catch (Exception ex) {

System.out.println("No Fle Found");
}
}
else if("2".equals(choice))
{
try {
fr2 = new FileReader(file2);
BufferedReader br1 = new BufferedReader(fr2);
String girls = null;
while((girls = br1.readLine()) != null ){

allNames.add(girls);
}
  
String randomName = allNames.get(r.nextInt(allNames.size()));
System.out.println(randomName);
  
} catch (Exception ex) {

System.out.println("No Fle Found");
}

}
else {
  
System.out.println("Wrong Input, Try Again");
getSuggestedName();
}
}

  
private static void checkNamewithLetter() {

System.out.println("1. For Boys");
System.out.println("2. For Girls");

String choice = scan.next();

System.out.print("Enter Letter : ");
String name = scan.next();



File file1 = new File(boysFile);
File file2 = new File(girlsFile);
FileReader fr1, fr2;

try {
fr1 = new FileReader(file1);
fr2 = new FileReader(file2);
BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);
String boys = null;
String girls = null;
  
if(choice.equals("1"))
{
name = name.substring(0,1).toUpperCase();
while((boys = br1.readLine()) != null)
{
if(boys.startsWith(name))
{
System.out.println(boys);
}
}
}
else if( choice.equals("2"))
{
name = name.substring(0,1).toLowerCase();
while((girls = br2.readLine()) != null)
{
if(girls.startsWith(name))
{
System.out.println(girls);
}
}
}
  
main(null);
return;
} catch (Exception ex) {

System.out.println("No File Found");
}
}

  
  
}

2. NewBorn.java

public class NewBorn {
  
private String name;
private String lastName;
private String dob;

NewBorn(String name, String lastName, String dob) {

this.name = name ;
this.lastName = lastName;
this.dob = dob;
}
  
  
public String getFirstName()
{
return name;
}
  
public String getLastName()
{
return lastName;
}
  
public String getDateofBirth()
{
return dob;
}
  
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------

Here is some of the output that I run for the above code, if you feel any problem in the code feel free to ask


Related Solutions

JAVA There is a folder named Recursive folder at the same location as these below files,...
JAVA There is a folder named Recursive folder at the same location as these below files, which contains files and folders. I have read the data of the Recursive folder and stored in a variable. Now they are required to be encrypted and decrypted. You may copy past the files to three different files and see the output. I am confused on how to write code for encrypt() and decrypt() The names of files and folders are stored in one...
I need this in java on textpad. There are two files, both have instructions in them...
I need this in java on textpad. There are two files, both have instructions in them on what to add in the code. They are posted below. public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[5]; } public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[capacity]; } public MyArrayForDouble(double[] nums1) { nums =...
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors.
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors. In each case, determine the problem and fix the program. After you correct the errors, save each file using the same filename preceded with Fix. For example, DebugFifteen1.java will become FixDebugFifteen1.java. a. DebugFifteen1.java b. DebugFifteen2.java c. DebugFifteen3.java d. DebugFifteen4.java    
In JAVA : There are two text files with the following information stored in them: The...
In JAVA : There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a Java program that reads these text files and provides user with the following menu: 1. Enter the instructor ID and I...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java,...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java, BubbleSortTestCaseMaker.java, and Statistician.java. PART 1: Implementing BubbleSorter Implement a very simple BubbleSorter class that records how many array visits and how many swaps are performed. Look at the starter file before reading on. This class has an instance variable called “a”. Its type is int[]. This is the array that will be bubble-sorted in place. Usually a single letter is a bad variable name,...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named...
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named Exercisei_j, where i and j are digits. Write a program that pads a 0 before j if j is a single digit. For example, a file named Exercise2_1 in a directory will be renamed to Exercise2_01. In Java, when you pass the symbol * from the command line, it refers to all files in the directory (see Supplement III.V). Use the following command to...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program. Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number. In the main() method, declare an int named myPick3choice. Create a prompt for myPick3choice as "Enter your Pick 3...
Write a C++ app to read both files, store them into parallel vectors, sort the list...
Write a C++ app to read both files, store them into parallel vectors, sort the list of people in alphabetical order, display the new sorted list of names with their corresponding descriptions. Use the Bubble Sort strategy to rearrange the vector(s). File 1: Marilyn Monroe Abraham Lincoln Nelson Mandela John F. Kennedy Martin Luther King Queen Elizabeth II Winston Churchill Donald Trump Bill Gates Muhammad Ali Mahatma Gandhi Margaret Thatcher Mother Teresa Christopher Columbus Charles Darwin Elvis Presley Albert Einstein...
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT