Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the java coding language, please complete the following and adjustments to the GUI when asked. Additional Information and external provided files: Excel document named

Using the java coding language, please complete the following and adjustments to the GUI when asked. Additional Information and external provided files: Excel document named BabyNames with four columns and thousands of datapoints: Name, Gender, count, year Provided GUI with portions that need additional code: -------------------------------------vvvvvvv //********************************************************************************//

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; import java.text.*;

public class BabyNameGUI extends JFrame implements ActionListener{

// FIX ME: Define a BabyNameDatabase variable

// FIX ME: Define buttons

// FIX ME: Define text fields

/** Results text area */ JTextArea resultsArea;

/** menu items */ JMenuBar menus; JMenu fileMenu; JMenuItem quitItem; JMenuItem openItem; JMenuItem countItem;

/***************************************************************** * Main Method ****************************************************************/ public static void main(String args[]){ BabyNameGUI gui = new BabyNameGUI(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setTitle("Baby Names"); gui.pack(); gui.setVisible(true); }

/***************************************************************** * constructor installs all of the GUI components ****************************************************************/ public BabyNameGUI(){

// FIX ME: instantiate an object of type BabyNameDatbase

// set the layout to GridBag setLayout(new GridBagLayout()); GridBagConstraints loc = new GridBagConstraints();

// create results area to span one column and 10 rows resultsArea = new JTextArea(20,20); JScrollPane scrollPane = new JScrollPane(resultsArea); loc.gridx = 0; loc.gridy = 1; loc.gridheight = 10; loc.insets.left = 20; loc.insets.right = 20; loc.insets.bottom = 20; add(scrollPane, loc);

// create Results label loc = new GridBagConstraints(); loc.gridx = 0; loc.gridy = 0; loc.insets.bottom = 20; loc.insets.top = 20; add(new JLabel("Results"), loc);

// create Searches label loc = new GridBagConstraints(); loc.gridx = 1; loc.gridy = 0; loc.gridwidth = 2; add(new JLabel("Searches"), loc);

// FIX ME: create labels, textfields and buttons

// FIX ME: register listeners for the buttons

// hide details of creating menus setupMenus(); }

/***************************************************************** * This method is called when any button is clicked. The proper * internal method is called as needed. * * @param e the event that was fired ****************************************************************/ public void actionPerformed(ActionEvent e){

// extract the button that was clicked JComponent buttonPressed = (JComponent) e.getSource();

// Allow user to load baby names from a file if (buttonPressed == openItem){ openFile(); }

}

/***************************************************************** * open a data file with the name selected by the user ****************************************************************/ private void openFile(){

// create File Chooser so that it starts at the current directory String userDir = System.getProperty("user.dir"); JFileChooser fc = new JFileChooser(userDir);

// show File Chooser and wait for user selection int returnVal = fc.showOpenDialog(this);

// did the user select a file? if (returnVal == JFileChooser.APPROVE_OPTION) { String filename = fc.getSelectedFile().getName(); // FIX ME: change the following line as needed // to use your instance variable name //database.readBabyNameData(filename); } }

/******************************************************* Creates the menu items *******************************************************/ private void setupMenus(){ fileMenu = new JMenu("File"); quitItem = new JMenuItem("Quit"); countItem = new JMenuItem("Counts"); openItem = new JMenuItem("Open..."); fileMenu.add(countItem); fileMenu.add(openItem); fileMenu.add(quitItem); menus = new JMenuBar(); setJMenuBar(menus); menus.add(fileMenu);

// FIX ME: register the menu items with the action listener

} } //***********************************************************//

Provided class called BabyName: ----------------------------------------vvvv

public class BabyName { String nameOfBaby; //Name of baby boolean genderOfBaby; //true or false for female or male int totalBabyName, yearOfBabyName; //total number of names for a baby name, year for specific name

public BabyName(String nob, boolean gob, int count, int yobn) { nameOfBaby = nob; genderOfBaby = gob; totalBabyName = count; yearOfBabyName = yr; }

public boolean isFemale() { if (gender) return genderOfBaby; else return genderOfBaby; }

public String getName(){ return nameOfBaby; }

public int getCount () { return totalBabyName; }

public int getYear(){ return yearOfBabyName; }

public void setName(String nob){ this.nameOfBaby = nob; }

public void setCount (int count) { this.totalBabyName = count; }

public void setYear(int yobn){ this.yearOfBabyName = yobn; }

public String toString() { String string; if (isFemale()) string = totalBabyName + " girls named " + nameOfBaby + " in " + yearOfBabyName; else string = totalBabyName + " boys named " + nameOfBaby + " in " + yearOfBabyName;

return string; }

public static void main(String [] args) { { BabyName name1 = new BabyName("Jessica",true,46473,1990); System.out.println(name1.toString()); System.out.println(name1.isFemale()); System.out.println(name1.toString()); } } }

//****************************************************************//

Partially complete potentially incorrect

Create a class called BabyNamesDatabase

Create a class that maintains an unlimited number of baby names. Use the elegant for-each loop to search the ArrayList.

Class Fields

Define an instance variable that holds an ArrayList of BabyName. You will read the information from a text file.

Constructor

A constructor is a special method with the same name as the class and generally initializes the fields to appropriate starting values. Refer to section 3.7.

public BabyNameDatabase () - instantiate an empty ArrayList for the names. You will not insert the items yet. This method will be one line of code.

Mutator Methods

Mutator methods perform tasks that may modify class fields. Methods that set a field with the parameter value often begin with the prefix set. Refer to section 3.6.

public void readBabyNameData(String filename) - open the provided filename and read all the data. There are thousands of records in BabyNames.csv. We are providing starter code later in this document.

Accessor Methods

An accessor method does not modify class fields. The names for these methods, which simply return the current value of a field, often begin with the prefix get. Refer to section 3.6.

public int countAllNames () - return the number of items in the ArrayList using one line of code.

public int countAllGirls() Use a for-each loop to search the full list and count the total number of girls born since 1880. This number will be much higher than just the number of girl names since each name represents hundreds, if not thousands, of newborn babies.

public int countAllBoys() Use a for-each loop to search the full list and count the total number of boys since 1880. This number will be much higher than just the number of boy names since each name represents hundreds, if not thousands, of newborn babies.

public BabyName mostPopularGirl(int year) search the full list and return the most popular girl name for a specific year.

public BabyName mostPopularBoy(int year) search the full list and return the most popular boy name for a specific year.

public ArrayList searchForName(String name) search the full list and return a new list of baby names that match the requested name (in the parameter). Spelling should match exactly but it is not case sensitive. For example, Angie, angie and ANGIE all match. Hint, check out the String method called equalsIgnoreCase(String str). If there are no matches for the name, the returned list will exist but have zero elements.

public ArrayList searchForYear(int year) search the full list and return a new list of baby names that match the requested year (in the parameter). If there is no match for the year, the returned list will exist but have zero elements.

Generate a Top Ten List

To generate a top ten list for a particular year your solution must be able to sort names in order by number of births. This requires small changes to BabyName and a new method in BabyNameDatabase.

Changes to BabyName

Add two words to the end of the class header (shown below). You will need to import a new java package for this to compile. This allows BabyName objects to be compared using compareTo() and therefore sorted.

public class BabyName implements Comparable{

Also, add the following method. You can try to copy and paste the code but if BlueJ gives unusual compile errors then simply retype it. This method allows two BabyName objects to be compared with respect to the number of births.

IMPORTANT NOTE: For the compareTo code below, we are assuming that the name of the instance variable for the number of births is count.

public int compareTo(Object other){

BabyName b = (BabyName) other;

return (b.count count);

}

Changes to BabyNameDatabase

NOTE: Sorting an ArrayList, called tempList, can be performed with one line of code (i.e. Collections.sort(tempList);)

Now that you can sort search results, sort the temporary lists in searchForYear() and searchForName() before returning. You only need to add one line of code to each method.

public ArrayList topTenNames(int year) search the full list and return a new list of the ten most popular baby names in a given year (in the parameter). The returned list will exist but have zero elements if there are no records for the specified year. Otherwise, it will have ten names. Avoid duplicating code by calling searchByYear() to first select all names in the year. Once you have the list of all names in the year sorted by the count of births, you can remove all items from the temporary list except the first ten before returning the result. Alternatively, you may create another temporary list and add to this new list only the first 10 records from the list of all names in the year. This may take some thought!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions

Question

Why does sin 2x + cos2x =1 ?

Answered: 1 week ago

Question

What are DNA and RNA and what is the difference between them?

Answered: 1 week ago

Question

Why do living creatures die? Can it be proved that they are reborn?

Answered: 1 week ago