Question
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
public ArrayList
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started