Stanford CS106A NameSurfer Solution
I am really really excited that I was able to get through the Stanford CS106A class so far. Only one assignment left!!!! And I’m extremely excited that I actually like programming (I probably wouldn’t have come this far if I didn’t).
Anyway, without more interruptions, here is the solution to the NameSurfer class. If you’re interested in the different parts of this solution, make sure to check out:
- Stanford CS106A NameSurferEntry Class Solution
- Stanford CS106A NameSurferDataBase Class Solution
- Stanford CS106A NameSurferGraph Class Solution
You can also find the full code on github.
Here is my solution to the NameSurfer Class, which pulls the whole program together.
NameSurfer.java
/* * File: NameSurfer.java * --------------------- * When it is finished, this program will implements the viewer for * the baby-name database described in the assignment handout. */ import acm.program.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; import acm.util.*; public class NameSurfer extends Program implements NameSurferConstants { /* Private instance variables*/ private JLabel label; private JTextField Name; private JButton Graph; private JButton Clear; private NameSurferDataBase namesdb; private NameSurferGraph graph; /* Method: init() */ /** * This method has the responsibility for reading in the data base * and initializing the interactors at the bottom of the window. */ public void init() { //Set up initial display with ineractors and canvas label = new JLabel("Name "); add(label, SOUTH); Name = new JTextField(20); add(Name, SOUTH); Name.addActionListener(this); Graph = new JButton("Graph"); add(Graph, SOUTH); Clear = new JButton("Clear"); add(Clear, SOUTH); graph = new NameSurferGraph(); add(graph); addActionListeners(); //reads the file of names and adds to the NameSurferDataBase namesdb = new NameSurferDataBase(NAMES_DATA_FILE); } /* Method: actionPerformed(e) */ /** * This class is responsible for detecting when the buttons are * clicked, so you will have to define a method to respond to * button actions. */ public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("Clear")) { graph.clear(); graph.update(); } else { String enteredName = Name.getText(); NameSurferEntry rankings = namesdb.findEntry(enteredName); if(rankings != null) { graph.addEntry(rankings); graph.update(); } } } }
NameSurferDataBase.java
mport java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; import acm.util.*; import java.util.*; /* * File: NameSurferDataBase.java * ----------------------------- * This class keeps track of the complete database of names. * The constructor reads in the database from a file, and * the only public method makes it possible to look up a * name and get back the corresponding NameSurferEntry. * Names are matched independent of case, so that "Eric" * and "ERIC" are the same names. */ public class NameSurferDataBase implements NameSurferConstants { /* Private instance variables */ private Map namesdb = new HashMap (); /* Constructor: NameSurferDataBase(filename) */ /** * Creates a new NameSurferDataBase and initializes it using the * data in the specified file. The constructor throws an error * exception if the requested file does not exist or if an error * occurs as the file is being read. */ public NameSurferDataBase(String filename) { getNameData(filename); } private void getNameData(String filename) { try{ BufferedReader rd = new BufferedReader(new FileReader(filename)); while(true) { String line = rd.readLine(); if(line == null) break; NameSurferEntry nameEntry = new NameSurferEntry(line); namesdb.put(nameEntry.getName(), nameEntry); } rd.close(); } catch(IOException ex) { throw new ErrorException(ex); } } /* Method: findEntry(name) */ /** * Returns the NameSurferEntry associated with this name, if one * exists. If the name does not appear in the database, this * method returns null. */ public NameSurferEntry findEntry(String name) { char ch = name.charAt(0); if(Character.isLowerCase(ch) == true) { ch = Character.toUpperCase(ch); } String otherLetters = name.substring(1); otherLetters = otherLetters.toLowerCase(); name = ch + otherLetters; if(namesdb.containsKey(name)) { return namesdb.get(name); } else{ return null; } } }
NameSurferEntry.java
/* * File: NameSurferEntry.java * -------------------------- * This class represents a single entry in the database. Each * NameSurferEntry contains a name and a list giving the popularity * of that name for each decade stretching back to 1900. */ import acm.util.*; import java.util.*; public class NameSurferEntry implements NameSurferConstants { /*private instance variables*/ private String Name; private int[] rankings = new int[NDECADES]; /* Constructor: NameSurferEntry(line) */ /** * Creates a new NameSurferEntry from a data line as it appears * in the data file. Each line begins with the name, which is * followed by integers giving the rank of that name for each * decade. */ public NameSurferEntry(String line) { parseLine(line); } private void parseLine(String line) { //gets the name int nameEnd = line.indexOf(" "); Name = line.substring(0, nameEnd); //gets the popularity ranking and puts it into an array String numbers = line.substring(nameEnd + 1); StringTokenizer tokenizer = new StringTokenizer(numbers); for(int count = 0; tokenizer.hasMoreTokens(); count++) { int popularityRank = Integer.parseInt(tokenizer.nextToken()); rankings[count] = popularityRank; } } /* Method: getName() */ /** * Returns the name associated with this entry. */ public String getName() { return Name; } /* Method: getRank(decade) */ /** * Returns the rank associated with an entry for a particular * decade. The decade value is an integer indicating how many * decades have passed since the first year in the database, * which is given by the constant START_DECADE. If a name does * not appear in a decade, the rank value is 0. */ public int getRank(int decade) { return rankings[decade]; } /* Method: toString() */ /** * Returns a string that makes it easy to see the value of a * NameSurferEntry. */ public String toString() { String value = """ + Name + " ["; for(int i = 0; i<NDECADES; i++) { value += getRank(i) + " "; } value += "]""; return value; } }