Question

Visual Basic Question In the Chap9 folder of the student sample programs, you will find the...

Visual Basic Question

In the Chap9 folder of the student sample programs, you will find the following files:

GirlNames.txt - This file contains a list of the 200 most popular names given to girls born in the United States from 2000 to 2012

BoyNames.txt - This file contains a list of the 200 most popular names given to boys born in the United States from 2000 to 2012

Create an application that reads the contents of the two files into two seperate arrays or Lists. The user should be able to enter a boy's name, a girl's name, or both, and the application should display messages indicating whether the names were among the most popular.

Note: I can't get the txt documents to load on this question. Let me know if I can do anything to get you those.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Name_Search
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ReadNames(List<string> nameList, string fileName)
        {
            try
            {
                // Open the text file
                StreamReader inputFile = File.OpenText(fileName);

                // Read the names into the list
                while (!inputFile.EndOfStream)
                {
                    nameList.Add(inputFile.ReadLine());
                }

                // Close the file.
                inputFile.Close();
            }
            catch (Exception ex)
            {
                // Display error message.
                MessageBox.Show(ex.Message);
            }
        }

        private bool InputIsValid(out string boyName, out string girlName)
        {
            // Flag variable to indicate whether the input is good.
            bool inputGood = false;

            // Get names from the text boxes
            boyName = txtBoy.Text;
            girlName = txtGirl.Text;

            // Test if there is a name entered in either text box
            if (boyName != "" || girlName != "")
            {
                inputGood = true;
            }
            else
            {
                // Display error message if no name is entered.
                MessageBox.Show("Enter a boy name, a girl name, or both.");

                // Reset focus.
                txtBoy.Focus();
            }

            return inputGood;
        }

        private string SearchNames(List<string> nameList, string name)
        {
            // Declare variables
            string message;
            int position;

            // Change name to have only the first letter uppercase in case
            // the user enters the name a different way.
            name = char.ToUpper(name[0]) + name.Substring(1).ToLower();

            // Search for name
            position = nameList.IndexOf(name);

            // Was name found in the the list?
            if (position != -1)
            {
                message = name + " was among the most popular.";
            }
            else
            {
                message = name + " was not among the most popular.";
            }

            return message;
        }

        private void ConvertNames(List<string> nameList)
        {
            for (int index = 0; index <nameList.Count; index++)
            {
                nameList[index] = char.ToUpper(nameList[index][0]) + nameList[index].Substring(1).ToLower();
            }
        }

        private void DisplayResults(string firstMessage, string secondMessage)
        {
            // Display either both messages or one of the messages
            if (firstMessage != "" && secondMessage != "")
            {
                lblMessage.Text = firstMessage + "\n" + secondMessage;
            }
            else if (firstMessage != "") {
                lblMessage.Text = firstMessage;
            }
            else
            {
                lblMessage.Text = secondMessage;
            }
          
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            // Declare variables
            string boyName;
            string girlName;
            string boyMessage = "";
            string girlMessage = "";
          
            // Create a list to hold boy names
            List<string> boyNamesList = new List<string>();

            // Create a list to hold girl names
            List<string> girlNamesList = new List<string>();

            // Read the names from the files into the lists.
            ReadNames(boyNamesList, "BoyNames.txt");
            ReadNames(girlNamesList, "GirlNames.txt");

            // Convert all names to proper Capitalization
            ConvertNames(boyNamesList);
            ConvertNames(girlNamesList);

            if (InputIsValid(out boyName,out girlName ))
            {
                // Check if a boy's name was entered and compare it to the list of names
                if (boyName != "" )
                {
                    boyMessage = SearchNames(boyNamesList, boyName);
                }

                // Check if a girl's name was entered and compare it to the list of names
                if (girlName != "")
                {
                    girlMessage = SearchNames(girlNamesList, girlName);
                }

                // Display if the names matched or not.
                DisplayResults(boyMessage, girlMessage);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            // Clear the text boxes and label.
            txtBoy.Text = "";
            txtGirl.Text = "";
            lblMessage.Text = "";

            // Reset focus.
            txtBoy.Focus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // Close the form.
            this.Close();
        }
    }
}


Form1.Designer.cs

namespace Name_Search
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lblBoyCaption = new System.Windows.Forms.Label();
            this.lblGirlCaption = new System.Windows.Forms.Label();
            this.txtBoy = new System.Windows.Forms.TextBox();
            this.txtGirl = new System.Windows.Forms.TextBox();
            this.lblMessage = new System.Windows.Forms.Label();
            this.btnCheck = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.btnExit = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // lblBoyCaption
            //
            this.lblBoyCaption.AutoSize = true;
            this.lblBoyCaption.Location = new System.Drawing.Point(32, 15);
            this.lblBoyCaption.Name = "lblBoyCaption";
            this.lblBoyCaption.Size = new System.Drawing.Size(66, 13);
            this.lblBoyCaption.TabIndex = 0;
            this.lblBoyCaption.Text = "Boy\'s Name:";
            //
            // lblGirlCaption
            //
            this.lblGirlCaption.AutoSize = true;
            this.lblGirlCaption.Location = new System.Drawing.Point(35, 41);
            this.lblGirlCaption.Name = "lblGirlCaption";
            this.lblGirlCaption.Size = new System.Drawing.Size(63, 13);
            this.lblGirlCaption.TabIndex = 1;
            this.lblGirlCaption.Text = "Girl\'s Name:";
            //
            // txtBoy
            //
            this.txtBoy.Location = new System.Drawing.Point(104, 12);
            this.txtBoy.Name = "txtBoy";
            this.txtBoy.Size = new System.Drawing.Size(139, 20);
            this.txtBoy.TabIndex = 0;
            //
            // txtGirl
            //
            this.txtGirl.Location = new System.Drawing.Point(104, 38);
            this.txtGirl.Name = "txtGirl";
            this.txtGirl.Size = new System.Drawing.Size(139, 20);
            this.txtGirl.TabIndex = 1;
            //
            // lblMessage
            //
            this.lblMessage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.lblMessage.Location = new System.Drawing.Point(12, 71);
            this.lblMessage.Name = "lblMessage";
            this.lblMessage.Size = new System.Drawing.Size(260, 54);
            this.lblMessage.TabIndex = 4;
            this.lblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // btnCheck
            //
            this.btnCheck.Location = new System.Drawing.Point(23, 141);
            this.btnCheck.Name = "btnCheck";
            this.btnCheck.Size = new System.Drawing.Size(75, 23);
            this.btnCheck.TabIndex = 2;
            this.btnCheck.Text = "Check";
            this.btnCheck.UseVisualStyleBackColor = true;
            this.btnCheck.Click += new System.EventHandler(this.btnCheck_Click);
            //
            // btnClear
            //
            this.btnClear.Location = new System.Drawing.Point(104, 141);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(75, 23);
            this.btnClear.TabIndex = 3;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            //
            // btnExit
            //
            this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnExit.Location = new System.Drawing.Point(185, 141);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(75, 23);
            this.btnExit.TabIndex = 4;
            this.btnExit.Text = "Exit";
            this.btnExit.UseVisualStyleBackColor = true;
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            //
            // Form1
            //
            this.AcceptButton = this.btnCheck;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnExit;
            this.ClientSize = new System.Drawing.Size(284, 180);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.btnCheck);
            this.Controls.Add(this.lblMessage);
            this.Controls.Add(this.txtGirl);
            this.Controls.Add(this.txtBoy);
            this.Controls.Add(this.lblGirlCaption);
            this.Controls.Add(this.lblBoyCaption);
            this.Name = "Form1";
            this.Text = "Name Search";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblBoyCaption;
        private System.Windows.Forms.Label lblGirlCaption;
        private System.Windows.Forms.TextBox txtBoy;
        private System.Windows.Forms.TextBox txtGirl;
        private System.Windows.Forms.Label lblMessage;
        private System.Windows.Forms.Button btnCheck;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Button btnExit;
    }
}


Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Name_Search
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
Visual Basic Question In the Chap9 folder of the student sample programs, you will find the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with the code for this assignment. Python Assignment: List and Tuples We will do...

    Need help with the code for this assignment. Python Assignment: List and Tuples We will do some basic data analysis on information stored in external files. GirNames.txt contains a list of the 200 most popular names given to girls born in US from year 2000 thru 2009: (copy link and create a txt file): https://docs.google.com/document/d/11YCVqVTrzqQgp2xJqyqPruGtlyxs2X3DFWn1YUb3ddw/edit?usp=sharing BoyNames.txt contains a list of the 200 most popular names given to boys born in US from year 2000 thru 2009 (copy link and create...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • The text files boynames.txt and girlnames.txt, which are included in the source code for this boo...

    The text files boynames.txt and girlnames.txt, which are included in the source code for this book, contain lists of the 1,000 most popular boy and girl names in the United States for the year 2005, as compiled by the Social Security Administration. These are blank-delimited files where the most popular name is listed first, the second most popular name is listed second, and so on to the 1,000th most popular name, which is listed last. Each line consists of the...

  • I need help with this question for programming. The language we use is C++ The program...

    I need help with this question for programming. The language we use is C++ The program should ask the user if they want to search for a boy or girl name. It should then ask for the name search the appropriate array for the name and display a message saying what ranking that name received, if any. The name files (BoyNames.txt and GirlNames.txt) are in order from most popular to least popular and each file contains two hundred names. The...

  • In notepad 4. Build a document containing the following: a. Place a heading at the top...

    In notepad 4. Build a document containing the following: a. Place a heading at the top center of the document in all capital letters containing: MY BACKGROUND b. In a paragraph containing complete sentences of correctly spelled words, please describe the following about yourself: (1) Where you were born (city, state, country) (2) What brought you to Middlesex County College (3) What you are studying at Middlesex County College (4) What you like to do in your spare time when...

  • Question I This question carries 20% of the marks for this assignment. You are asked to...

    Question I This question carries 20% of the marks for this assignment. You are asked to develop a set of bash shell script: Write a script that asks for the user's salary per month. If it is less or equals to 800, print a message saying that you need to get another job to increase your income, what you earn is the Minim living cost. If the user's salary is higher than 800 and below 2000, print a message telling...

  • For this project, you are tasked with creating a text-based, basic String processing program that performs...

    For this project, you are tasked with creating a text-based, basic String processing program that performs basic search on a block of text inputted into your program from an external .txt file. After greeting your end user for a program description, your program should prompt the end user to enter a .txt input filename from which to read the block of text to analyze. Then, prompt the end user for a search String. Next, prompt the end user for the...

  • Please answer Question 4 WITH A C# PROGRAM!!!!!! THANK YOU! File Edit View History Bookmarks Tools...

    Please answer Question 4 WITH A C# PROGRAM!!!!!! THANK YOU! File Edit View History Bookmarks Tools Help Share GEP IIIA Moorhead, MN 10-Day Weathe X N NDSU Faculty and Staff | North x D C#.NET - CSCI 213: Modern Sol X VitalSource Bookshelf: Starting x + + → OO - https://bookshelf.vitalsource.com/#/books/9780134400433/cf1/6 90% *** * Cambie Meble alle... S N 10 Crosby Derek Lam ... Alterna Caviar Anti-Ag... U C# Tutorial For Beginn... BE Celsius Fahrenheit con... Charter Club Sweater. Folklorama...

  • Help with java . For this project, you will design and implement a program that analyzes...

    Help with java . For this project, you will design and implement a program that analyzes baby name popularities in data provided by the Social Security Administration. Every 10 years, the data gives the 1,000 most popular boy and girl names for kids born in the United States. The data can be boiled down to a single text file as shown below. On each line we have the name, followed by the rank of that name in 1900, 1910, 1920,...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT