斯坦福 cs106A Assignment6
来源:互联网 发布:全国路网shp数据下载 编辑:程序博客网 时间:2024/05/29 13:03
在学斯坦福cs106A时编程任务没有习题答案,百度上也难找到4-7的答案, 所有放上自己写的代码(能运行并符合题目要求,因为初学,写的不好看,)供人参考,
因为是初学,英文又不好,代码中用了很多中文,随着学习的深入,个人觉得还是用中文比较好。
Assignment6
/* * File: NameSurfer.java * --------------------- * When it is finished, this program will implements the viewer for * the baby-name database described in the assignment handout. * @author zyw23 */import acm.program.*;import java.awt.event.*;import javax.swing.*;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() { add(new JLabel("name"), SOUTH);文本输入框 = new JTextField(20);文本输入框.setActionCommand("Graph");add(文本输入框, SOUTH);JButton graphButton = new JButton("Graph");JButton clearButton = new JButton("Clear");add(graphButton, SOUTH);add(clearButton, SOUTH);canvas = new NameSurferGraph();add(canvas);名字排名数据库 = new NameSurferDataBase(NAMES_DATA_FILE);addActionListeners();文本输入框.addActionListener(this);}/* 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("Graph")) {if (名字排名数据库.findEntry(文本输入框.getText()) != null) {canvas.addEntry(名字排名数据库.findEntry(文本输入框.getText()));} else println("没有这个名字的统计数据.");} else if (e.getActionCommand().equals("Clear")) {canvas.clear();}}/* private instance variables */private NameSurferGraph canvas;private JTextField 文本输入框;private NameSurferDataBase 名字排名数据库;}/* * File: NameSurferConstants.java * ------------------------------ * This file declares several constants that are shared by the * different modules in the NameSurfer application. Any class * that implements this interface can use these constants. * @author zyw23 */public interface NameSurferConstants {/** The width of the application window */public static final int APPLICATION_WIDTH = 800;/** The height of the application window */public static final int APPLICATION_HEIGHT = 600;/** The name of the file containing the data */public static final String NAMES_DATA_FILE = "names-data.txt";/** The first decade in the database */public static final int START_DECADE = 1900;/** The number of decades */public static final int NDECADES = 11;/** The maximum rank in the database */public static final int MAX_RANK = 1000;/** The number of pixels to reserve at the top and bottom */public static final int GRAPH_MARGIN_SIZE = 20;}import java.io.*;import java.util.ArrayList;import java.util.HashMap;import acm.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 {/* 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) {int 排名数据数组索引 = 0;try {BufferedReader rd = new BufferedReader(new FileReader(filename));while (true) {String line = rd.readLine();if (line == null) break;int 空格索引位置 = line.indexOf(" ");名字.put(line.substring(0, 空格索引位置), 排名数据数组索引);名字的排名数据.add(line.substring(空格索引位置 + 1));排名数据数组索引++;}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) {if (名字.get(name) != null) {return new NameSurferEntry(name + " " + 名字的排名数据.get(名字.get(name)));} else return null;}/* private instance variables */private ArrayList名字的排名数据 = new ArrayList ();private HashMap 名字 = new HashMap ();}import java.util.ArrayList;import java.util.Scanner;/* * 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. * @author zyw23 */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) {int 空格索引位置 = line.indexOf(" ");名字 = line.substring(0, 空格索引位置);字符型年份排名数据 = line.substring(空格索引位置 + 1); //这个实例变量可以不要,存在增加内存占用,使用toString是增加点性能分拆年份排名数据到数组(字符型年份排名数据); }/* Method: getName() *//** * Returns the name associated with this entry. */public String getName() {return 名字;}/* 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) {//年份对应位置 = (decade - START_DECADE) / 10;return 名字的年份排名数组.get(decade); }/* Method: toString() *//** * Returns a string that makes it easy to see the value of a * NameSurferEntry. */public String toString() {return 名字 + " [" + 字符型年份排名数据 + "]";}//分拆年份排名数据到数组private void 分拆年份排名数据到数组(String str) {名字的年份排名数组 = new ArrayList ();Scanner intScanner = new Scanner(str);while (intScanner.hasNextInt()) {名字的年份排名数组.add(intScanner.nextInt());}intScanner.close();//下面语句功能一样/*名字的年份排名数组 = new ArrayList<>();int 空格索引位置 = -1;for (int i = 0; i < NDECADES; i++) {int 前空格索引位置 = 空格索引位置;空格索引位置 = str.indexOf(" ", 前空格索引位置 + 1);if (空格索引位置 != -1) {名字的年份排名数组.add(Integer.parseInt(str.substring(前空格索引位置 + 1, 空格索引位置)));} else {名字的年份排名数组.add(Integer.parseInt(str.substring(前空格索引位置 + 1)));}}*/}/* private instance variables */private ArrayList 名字的年份排名数组;private String 字符型年份排名数据;private String 名字;}/* * 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 GCanvasimplements NameSurferConstants, ComponentListener {/*** Creates a new NameSurferGraph object that displays the data.*/public NameSurferGraph() {addComponentListener(this);init();}/*** Clears the list of name surfer entries stored inside this class.*/public void clear() {removeAll();排名数据.clear();init();}/* 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) {排名数据.add(entry);画名字排名图表(entry);}/*** 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();init();for (int i = 0; i < 排名数据.size(); i++) {画名字排名图表(排名数据.get(i));}}/* 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) { }/* 创建GCompound图形 */private void 画名字排名图表(NameSurferEntry entry) {GCompound box = new GCompound();int 间隔 = getWidth() / NDECADES;double 排名高度 = getHeight() - GRAPH_MARGIN_SIZE * 2;//int转double计算是,1.0放在除数的位置与放在被除数位置结果不一样.//例如此表达式:1.0 * 排名高度 / MAX_RANK = 0.483, 此表达式:排名高度 / MAX_RANK * 1.0 = 0.0483;double 高度与排名的比率 = 排名高度 / MAX_RANK;for (int i = 0; i < NDECADES; i++) {if ( i < NDECADES - 1) {box.add(new GLine(间隔 * i, GRAPH_MARGIN_SIZE + 排名高度 - GMath.round(高度与排名的比率 * entry.getRank(i)), 间隔 * (i + 1), GRAPH_MARGIN_SIZE + 排名高度 - GMath.round(高度与排名的比率 * entry.getRank(i + 1))));}box.add(new GLabel(entry.getName() + " " + entry.getRank(i)), 间隔 * i, GRAPH_MARGIN_SIZE + 排名高度 - GMath.round(高度与排名的比率 * entry.getRank(i)));}box.setColor(颜色按顺序选择());add(box);颜色顺序++;}//颜色按顺序选择 ,总共10种颜色private Color 颜色按顺序选择() {if (颜色顺序 > 9) 颜色顺序 = 0;switch (颜色顺序) {case 0: return Color.BLACK;case 1: return Color.CYAN;case 2: return Color.DARK_GRAY;case 3: return Color.GRAY;case 4: return Color.GREEN;case 5: return Color.PINK;case 6: return Color.YELLOW;case 7: return Color.ORANGE;case 8: return Color.MAGENTA;case 9: return Color.RED;default: return Color.BLACK;}}//初始化private void init() {颜色顺序 = 0;add(new GLine(0, GRAPH_MARGIN_SIZE, getWidth(), GRAPH_MARGIN_SIZE));add(new GLine(0, getHeight() - GRAPH_MARGIN_SIZE, getWidth(), getHeight() - GRAPH_MARGIN_SIZE));int 间隔 = getWidth() / NDECADES;for (int i = 0; i < NDECADES; i++) {add(new GLine(间隔 * i, 0, 间隔 * i, getHeight()));add(new GLabel((START_DECADE + 10 * i) + ""), 间隔 * i, getHeight());}}/* private instance variables */private ArrayList 排名数据 = new ArrayList ();private int 颜色顺序;}
0 0
- 斯坦福 cs106A Assignment6
- 斯坦福 cs106A Assignment4
- 斯坦福 cs106A Assignment5
- 斯坦福 cs106A Assignment7
- CS106A Assignment4: Hangman
- CheckerboardKarel For Standord CS106A
- 斯坦福研究所
- 斯坦福学习
- 斯坦福大學將舉辦區塊鏈研討會
- CS106A Assignment3: Breakout——浅谈自上而下的设计体验
- Coding Style 转自Stanford CS106A Handout #7
- 斯坦福大学CS106A公开课笔记--启示环境配置
- cs106a学习笔记 - 搭建Karel the robot环境
- 斯坦福大学公开课:编程方法学 (cs106A) 资源
- 斯坦福iPhone开发课件
- 乔布斯斯坦福演讲
- 乔布斯斯坦福演讲
- 乔布斯斯坦福演讲
- Vue.js学习系列(四十四)-- 自定义事件
- 删数问题
- 贪心算法(1)
- 1361 知识排名 (排序)
- 最优分解问题
- 斯坦福 cs106A Assignment6
- 单词字典中对兄弟单词查找算法(C++实现)
- 数据结构——Doubly_Linked_List的代码实现
- CSDN日报20170405 ——《未来的世界没有程序员》
- 最优服务次序问题
- JavaScript之基本包装类型
- 振兴中华
- 第八届蓝桥杯模拟赛本科组算年龄
- 最优合并问题