活跃农民
- 积分
- 355
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-3-23
- 最后登录
- 1970-1-1
|
前两天毫无干劲……本来以为今天晚上完不成,没想到还是完成了。这次不难,但是大量的时间都花在脑残的笔误上- /*
- * 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.awt.*;
- import java.util.ArrayList;
- public class NameSurfer extends Program implements NameSurferConstants {
- /* 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() {
- // You fill this in, along with any helper methods //
- graph=new NameSurferGraph();
- add(graph);
- data=new NameSurferDataBase("names-data.txt");
- setLayout(new GridLayout(1,1));
- Graph=new JButton("Graph");
- Clear=new JButton("Clear");
- tf=new JTextField(10);
- tf.addActionListener(this);
- add(new JLabel("Name"),SOUTH);
- add(tf,SOUTH);
- add(Graph,SOUTH);
- add(Clear,SOUTH);
- addActionListeners();
-
-
- }
- /* 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) {
- // You fill this in //
- String cmd=e.getActionCommand();
-
- if(cmd.equals("Graph")){
- graph.update();
- graph.addEntry(data.findEntry(tf.getText()));
-
- graph.addLine();
-
-
-
- //println("Graph: "+data.findEntry(tf.getText()).toString());
-
- }
- if(cmd.equals("Clear")){
- graph.update();
- graph.setNull();
- //println("Clear");
- }
-
-
- }
-
- //private NameSurferGraph[] graphs=new NameSurferGraph[5];
- //private ArrayList<NameSurferGraph> graphs=new ArrayList<NameSurferGraph>();
- private NameSurferGraph graph;
- private NameSurferDataBase data;
- private JTextField tf;
- private JButton Graph;
- private JButton Clear;
- }
-
复制代码- /*
- * File: NameSurferGraph.java
- * ---------------------------
- * This class represents the canvas on which the graph of
- * names is drawn. This class is responsible for updating
- * (redrawing) the graphs whenever the list of entries changes or the window is resized.
- */
- import acm.graphics.*;
- import java.awt.event.*;
- import java.util.*;
- import java.awt.*;
- public class NameSurferGraph extends GCanvas
- implements NameSurferConstants, ComponentListener {
- /**
- * Creates a new NameSurferGraph object that displays the data.
- */
- public NameSurferGraph() {
-
- addComponentListener(this);
- temp = 0;
-
-
-
-
-
-
- // You fill in the rest //
- }
- public String year(int i){
- if (i==0) return "1900";
- else if(i==1) return "1910";
- else if(i==2) return "1920";
- else if(i==3) return "1930";
- else if(i==4) return "1940";
- else if(i==5) return "1950";
- else if(i==6) return "1960";
- else if(i==7) return "1970";
- else if(i==8) return "1980";
- else if(i==9) return "1990";
- else return "2000";
- }
-
- /**
- * Clears the list of name surfer entries stored inside this class.
- */
- public void clear() {
- // You fill this in //
- }
-
- /* Method: addEntry(entry) */
- /**
- * Adds a new NameSurferEntry to the list of entries on the display.
- * Note that this method does not actually draw the graph, but
- * simply stores the entry; the graph is drawn by calling update.
- */
- public void addEntry(NameSurferEntry entry) {
- if(temp<5){
- entries[temp]=entry;
- temp++;
- }
- else{
- entries[0]=entry;
- temp=0;
- }
-
- // You fill this in //
-
-
-
- }
-
-
-
- /**
- * Updates the display image by deleting all the graphical objects
- * from the canvas and then reassembling the display according to
- * the list of entries. Your application must call update after
- * calling either clear or addEntry; update is also called whenever
- * the size of the canvas changes.
- */
- public void update() {
- removeAll();
- add(new GLine(0,GRAPH_MARGIN_SIZE,getWidth(),GRAPH_MARGIN_SIZE));
- add(new GLine(0,getHeight()-GRAPH_MARGIN_SIZE,getWidth(),getHeight()-GRAPH_MARGIN_SIZE));
- for(int h=0;h<NDECADES;h++){
- add(new GLine(getWidth()/NDECADES*(h),0,getWidth()/NDECADES*(h),getHeight()));
- add(new GLabel(year(h),getWidth()/NDECADES*(h),getHeight()));
- }
- // You fill this in //
-
-
- }
- public void addLine(){
- /*
- GLine line=new GLine(0,GRAPH_MARGIN_SIZE+entries[0].getRank(1900)*(getHeight()-2*GRAPH_MARGIN_SIZE)/1000,
- getWidth(),GRAPH_MARGIN_SIZE+entries[0].getRank(1910)*(getHeight()-2*GRAPH_MARGIN_SIZE)/1000);
- add(line);
- line.setColor(Color.RED);
- */
-
- for(int i=0;i<5;i++){
- for(int j=0;j<11;j++){
-
- if(entries[i]!=null){
- int y1=GRAPH_MARGIN_SIZE+entries[i].getRank(1900+j*10)*(getHeight()-2*GRAPH_MARGIN_SIZE)/1000;
- int y2=GRAPH_MARGIN_SIZE+entries[i].getRank(1900+(j+1)*10)*(getHeight()-2*GRAPH_MARGIN_SIZE)/1000;
- if(y1==GRAPH_MARGIN_SIZE)y1=getHeight()-GRAPH_MARGIN_SIZE;
- if(y2==GRAPH_MARGIN_SIZE)y2=getHeight()-GRAPH_MARGIN_SIZE;
- if(j!=10){
-
-
- GLine line=new GLine(j*getWidth()/NDECADES,y1,(j+1)*getWidth()/NDECADES,y2);
- add(line);
- if(i==1) line.setColor(Color.RED);
- if(i==2) line.setColor(Color.ORANGE);
- if(i==3) line.setColor(Color.BLUE);
- if(i==4) line.setColor(Color.GREEN);
- }
-
- String rank=Integer.toString(entries[i].getRank(1900+j*10));
- if(rank.equals("0")) rank="*";
- GLabel label=new GLabel(entries[i].getName()+" "+rank,j*getWidth()/NDECADES+1,y1+1);
- add(label);
- if(i==1) label.setColor(Color.RED);
- if(i==2) label.setColor(Color.ORANGE);
- if(i==3) label.setColor(Color.BLUE);
- if(i==4) label.setColor(Color.GREEN);
-
-
- }
-
- }
- }
- }
-
-
-
- public void setNull(){
- for(int i=0;i<5;i++){
- entries[i]=null;
- }
- }
-
- /* Implementation of the ComponentListener interface */
- public void componentHidden(ComponentEvent e) { }
- public void componentMoved(ComponentEvent e) { }
- public void componentResized(ComponentEvent e) { update(); }
- public void componentShown(ComponentEvent e) { }
-
- private int temp;
- //private ArrayList<NameSurferEntry> entries=new ArrayList<NameSurferEntry>();
- NameSurferEntry[] entries=new NameSurferEntry[5];
-
- }
复制代码- /*
- * 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 {
- /* 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) {
-
-
- // You fill this in //
- int nameEnd=line.indexOf(" ");
- name=line.substring(0,nameEnd);
- int first=line.indexOf(" ",nameEnd)+1;
- int second=line.indexOf(" ",first)+1;
- int third=line.indexOf(" ",second)+1;
- int forth=line.indexOf(" ",third)+1;
- int fifth=line.indexOf(" ",forth)+1;
- int sixth=line.indexOf(" ",fifth)+1;
- int seventh=line.indexOf(" ",sixth)+1;
- int eighth=line.indexOf(" ",seventh)+1;
- int ninth=line.indexOf(" ",eighth)+1;
- int tenth=line.indexOf(" ",ninth)+1;
- int eleventh=line.indexOf(" ",tenth)+1;
-
- /*
- one=line.substring(first,second-1);
- two=line.substring(second,third-1);
- three=line.substring(third,forth-1);
- four=line.substring(forth,fifth-1);
- five=line.substring(fifth,sixth-1);
- six=line.substring(sixth,seventh-1);
- seven=line.substring(seventh,eighth-1);
- eight=line.substring(eighth,ninth-1);
- nine=line.substring(ninth,tenth-1);
- ten=line.substring(tenth,eleventh-1);
- eleven=line.substring(eleventh+1);
- */
-
- one=Integer.parseInt(line.substring(first,second-1));
- two=Integer.parseInt(line.substring(second,third-1));
- three=Integer.parseInt(line.substring(third,forth-1));
- four=Integer.parseInt(line.substring(forth,fifth-1));
- five=Integer.parseInt(line.substring(fifth,sixth-1));
- six=Integer.parseInt(line.substring(sixth,seventh-1));
- seven=Integer.parseInt(line.substring(seventh,eighth-1));
- eight=Integer.parseInt(line.substring(eighth,ninth-1));
- nine=Integer.parseInt(line.substring(ninth,tenth-1));
- ten=Integer.parseInt(line.substring(tenth,eleventh-1));
- eleven=Integer.parseInt(line.substring(eleventh));
- //eleven=line.substring(eleventh);
-
-
- }
- /* Method: getName() */
- /**
- * Returns the name associated with this entry.
- */
- public String getName() {
- // You need to turn this stub into a real implementation //
- return name;
- //return null;
- }
- /* 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) {
- // You need to turn this stub into a real implementation //
- if(decade==1900) return one;
- else if(decade==1910) return two;
- else if(decade==1920) return three;
- else if(decade==1930) return four;
- else if(decade==1940) return five;
- else if(decade==1950) return six;
- else if(decade==1960) return seven;
- else if(decade==1970) return eight;
- else if(decade==1980) return nine;
- else if(decade==1990) return ten;
- else return eleven;
-
- }
- /* Method: toString() */
- /**
- * Returns a string that makes it easy to see the value of a
- * NameSurferEntry.
- */
- public String toString() {
- // You need to turn this stub into a real implementation //
- //return (name+" ["+one+" "+two+" "+three+" "+four+" "+five+" "+six+" "+seven+" "+eight+" "+nine+" "+ten+" ]");
- return (name+" ["+one+" "+two+" "+three+" "+four+" "+five+" "+six+" "+seven+" "+eight+" "+nine+" "+ten+" "+eleven+"]");
- }
-
- private int one, two, three, four, five, six, seven, eight, nine, ten, eleven;
- //private int one, two, three, four, five, six, seven, eight, nine, ten, eleven;
- private String name;
- }
复制代码- /*
- * 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.
- */
- import acm.util.*;
- import acm.program.*;
- import java.io.*;
- import java.util.*;
- import java.awt.*;
- public class NameSurferDataBase implements NameSurferConstants {
-
- /* 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) {
- // You fill this in //
- try{
- BufferedReader rd=new BufferedReader(new FileReader(filename));
- while(true){
- String line=rd.readLine();
- if(line==null)break;
- entry=new NameSurferEntry(line);
- entries.add(entry);
-
- }
- 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) {
- // You need to turn this stub into a real implementation //
- int temp=-1;
- for(int i=0;i<entries.size();i++){
- if(name.equals(entries.get(i).getName())){
- temp=i;
-
- }
- }
-
- if(temp==-1)return(null);
- else {
- return(entries.get(temp));
- }
- }
- private ArrayList<NameSurferEntry> entries=new ArrayList<NameSurferEntry>();
- private NameSurferEntry entry;
- private String nameref;
- private String Name;
- }
复制代码 |
|