斯坦福 cs106A Assignment7
来源:互联网 发布:软件开发报价清单 编辑:程序博客网 时间:2024/05/15 04:11
在学斯坦福cs106A时编程任务没有习题答案,百度上也难找到4-7的答案, 所有放上自己写的代码(能运行并符合题目要求,因为初学,写的不好看,)供人参考,
Assignment6
/* * File: FacePamphlet.java * ----------------------- * When it is finished, this program will implement a basic social network * management system. * @author zyw23 */import acm.program.*;import acm.graphics.*;import acm.util.*;import java.awt.event.*;import javax.swing.*;public class FacePamphlet extends Program implements FacePamphletConstants {/** * This method has the responsibility for initializing the * interactors in the application, and taking care of any other * initialization that needs to be performed. */public void init() {initButton();canvas = new FacePamphletCanvas();//addButton//changeStatusButtonadd(canvas);println(canvas.getWidth() + "#" + canvas.getHeight()); } /** * This class is responsible for detecting when the buttons are * clicked or interactors are used, so you will have to add code * to respond to these actions. */ public void actionPerformed(ActionEvent e) {// You fill this in as well as add any additional methods} //按钮初始化 private void initButton() { nameField = new JTextField(TEXT_FIELD_SIZE);nameField.setActionCommand("Add");nameField.addActionListener(this);addButton = new JButton("Add");deleteButton = new JButton("Delete");lookupButton = new JButton("Lookup");add(new JLabel("Name"), NORTH);add(nameField, NORTH);add(addButton, NORTH);add(deleteButton, NORTH);add(lookupButton, NORTH);changeStatusField = new JTextField(TEXT_FIELD_SIZE);changeStatusField.setActionCommand("changeStatus");changeStatusField.addActionListener(this);changeStatusButton = new JButton("changeStatus");add(changeStatusField, WEST);add(changeStatusButton, WEST);add(new JLabel(EMPTY_LABEL_TEXT), WEST);changePictureField = new JTextField(TEXT_FIELD_SIZE);changePictureField.setActionCommand("changePicture");changePictureField.addActionListener(this);changePictureButton = new JButton("changePicture");add(changePictureField, WEST);add(changePictureButton, WEST);add(new JLabel(EMPTY_LABEL_TEXT), WEST);addFriendField = new JTextField(TEXT_FIELD_SIZE);addFriendField.setActionCommand("addFriend");addFriendField.addActionListener(this);addFriendButton = new JButton("addFriend");add(addFriendField, WEST);add(addFriendButton, WEST);addActionListeners(); } /* private instance variables */ private FacePamphletCanvas canvas;private JButton addButton;private JButton deleteButton;private JButton lookupButton;private JButton changeStatusButton;private JButton changePictureButton;private JButton addFriendButton;private JTextField nameField;private JTextField changeStatusField;private JTextField changePictureField;private JTextField addFriendField;}/* * File: FacePamphletCanvas.java * ----------------------------- * This class represents the canvas on which the profiles in the social * network are displayed. NOTE: This class does NOT need to update the * display when the window is resized. * @author zyw23 */import java.awt.*;import acm.graphics.*;import java.util.*;public class FacePamphletCanvas extends GCanvas implements FacePamphletConstants {/** * Constructor * This method takes care of any initialization needed for * the display */public FacePamphletCanvas() {init();}/** * This method displays a message string near the bottom of the * canvas. Every time this method is called, the previously * displayed message (if any) is replaced by the new message text * passed in. */public void showMessage(String msg) {messageLabel.setLabel(msg);add(messageLabel, (getWidth() - messageLabel.getWidth()) / 2, getHeight() - BOTTOM_MESSAGE_MARGIN - messageLabel.getDescent());}/** * This method displays the given profile on the canvas. The * canvas is first cleared of all existing items (including * messages displayed near the bottom of the screen) and then the * given profile is displayed. The profile display includes the * name of the user from the profile, the corresponding image * (or an indication that an image does not exist), the status of * the user, and a list of the user's friends in the social network. */public void displayProfile(FacePamphletProfile profile) {removeAll();nameLabel.setLabel(profile.getName());add(nameLabel, LEFT_MARGIN, TOP_MARGIN + nameLabel.getAscent());image = profile.getImage();add(imageRange, IMAGE_MARGIN, TOP_MARGIN + IMAGE_MARGIN + nameLabel.getAscent() + nameLabel.getDescent());if (image == null) {add(noImage, IMAGE_MARGIN + (IMAGE_WIDTH - noImage.getWidth()) / 2,TOP_MARGIN + IMAGE_MARGIN + nameLabel.getAscent() + nameLabel.getDescent() + (IMAGE_HEIGHT + noImage.getAscent() - noImage.getDescent()) / 2);} else {add(image, IMAGE_MARGIN, TOP_MARGIN + IMAGE_MARGIN + nameLabel.getAscent() + nameLabel.getDescent());}status = profile.getStatus();if (status == null) {add(new GLabel("No current status"), STATUS_MARGIN, TOP_MARGIN + IMAGE_MARGIN * 2 + nameLabel.getHeight() + IMAGE_HEIGHT + statusLabel.getAscent());} else {statusLabel.setLabel(status);add(statusLabel, STATUS_MARGIN, TOP_MARGIN + IMAGE_MARGIN * 2 + nameLabel.getHeight() + IMAGE_HEIGHT + statusLabel.getAscent());}friends = profile.getFriends();add(friendsLabel, getWidth() / 2, TOP_MARGIN + IMAGE_MARGIN + nameLabel.getAscent() + nameLabel.getDescent());int nFriends = 1;while (friends.hasNext()) {add(new GLabel(friends.next()), getWidth() / 2, nFriends * friendsLabel.getHeight() + TOP_MARGIN + IMAGE_MARGIN + nameLabel.getAscent() + nameLabel.getDescent());nFriends++;}}//初始化界面private void init() {nameLabel = new GLabel("");nameLabel.setFont(PROFILE_NAME_FONT);image = null;imageRange = new GRect(IMAGE_WIDTH, IMAGE_HEIGHT);noImage = new GLabel("No Image");noImage.setFont(PROFILE_IMAGE_FONT);status = null;statusLabel = new GLabel("");statusLabel.setFont(PROFILE_STATUS_FONT);friends = null;friendsLabel = new GLabel("Friends:");friendsLabel.setFont(PROFILE_FRIEND_LABEL_FONT);add(friendsLabel, getWidth() / 2, TOP_MARGIN + IMAGE_MARGIN + nameLabel.getAscent() + nameLabel.getDescent());messageLabel = new GLabel("");messageLabel.setFont(MESSAGE_FONT);//add(messageLabel, (getWidth() - messageLabel.getWidth()) / 2, //getHeight() - BOTTOM_MESSAGE_MARGIN - messageLabel.getDescent());}/* private instance variables */private GRect imageRange; private GImage image;private GLabel noImage; private GLabel nameLabel; private GLabel statusLabel; private String status; private Iteratorfriends; private GLabel friendsLabel; private GLabel messageLabel;}/* * File: FacePamphletConstants.java * -------------------------------- * This file declares several constants that are shared by the * different modules in the FacePamphlet application. Any class * that implements this interface can use these constants. * @author zyw23 */public interface FacePamphletConstants {/** 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 = 500;/** Number of characters for each of the text input fields */public static final int TEXT_FIELD_SIZE = 15;/** Text to be used to create an "empty" label to put space * between interactors on EAST border of application. Note this * label is not actually the empty string, but rather a single space */public static final String EMPTY_LABEL_TEXT = " ";/** Name of font used to display the application message at the * bottom of the display canvas */public static final String MESSAGE_FONT = "Dialog-18";/** Name of font used to display the name in a user's profile */public static final String PROFILE_NAME_FONT = "Dialog-24";/** Name of font used to display the text "No Image" in user * profiles that do not contain an actual image */public static final String PROFILE_IMAGE_FONT = "Dialog-24";/** Name of font used to display the status in a user's profile */public static final String PROFILE_STATUS_FONT = "Dialog-16-bold";/** Name of font used to display the label "Friends" above the * user's list of friends in a profile */public static final String PROFILE_FRIEND_LABEL_FONT = "Dialog-16-bold";/** Name of font used to display the names from the user's list * of friends in a profile */public static final String PROFILE_FRIEND_FONT = "Dialog-16";/** The width (in pixels) that profile images should be displayed */public static final double IMAGE_WIDTH = 200;/** The height (in pixels) that profile images should be displayed */public static final double IMAGE_HEIGHT = 200;/** The number of pixels in the vertical margin between the bottom * of the canvas display area and the baseline for the message * text that appears near the bottom of the display */public static final double BOTTOM_MESSAGE_MARGIN = 20;/** The number of pixels in the hortizontal margin between the * left side of the canvas display area and the Name, Image, and * Status components that are display in the profile */public static final double LEFT_MARGIN = 20;/** The number of pixels in the vertical margin between the top * of the canvas display area and the top (NOT the baseline) of * the Name component that is displayed in the profile */public static final double TOP_MARGIN = 20;/** The number of pixels in the vertical margin between the * baseline of the Name component and the top of the Image * displayed in the profile */public static final double IMAGE_MARGIN = 20;/** The number of vertical pixels in the vertical margin between * the bottom of the Image and the top of the Status component * in the profile */public static final double STATUS_MARGIN = 20;}/* * File: FacePamphletDatabase.java * ------------------------------- * This class keeps track of the profiles of all users in the * FacePamphlet application. Note that profile names are case * sensitive, so that "ALICE" and "alice" are NOT the same name. */import java.util.*;public class FacePamphletDatabase implements FacePamphletConstants {/** * Constructor * This method takes care of any initialization needed for * the database. */public FacePamphletDatabase() {FacePamphletDB = new HashMap ();}/** * This method adds the given profile to the database. If the * name associated with the profile is the same as an existing * name in the database, the existing profile is replaced by * the new profile passed in. */public void addProfile(FacePamphletProfile profile) {FacePamphletDB.put(profile.getName(), profile);}/** * This method returns the profile associated with the given name * in the database. If there is no profile in the database with * the given name, the method returns null. */public FacePamphletProfile getProfile(String name) {return FacePamphletDB.get(name);}/** * This method removes the profile associated with the given name * from the database. It also updates the list of friends of all * other profiles in the database to make sure that this name is * removed from the list of friends of any other profile. * * If there is no profile in the database with the given name, then * the database is unchanged after calling this method. */public void deleteProfile(String name) {FacePamphletDB.remove(name);}/** * This method returns true if there is a profile in the database * that has the given name. It returns false otherwise. */public boolean containsProfile(String name) {return FacePamphletDB.containsKey(name); }/* private instance variables */private HashMap FacePamphletDB;}/* * File: FacePamphletProfile.java * ------------------------------ * This class keeps track of all the information for one profile * in the FacePamphlet social network. Each profile contains a * name, an image (which may not always be set), a status (what * the person is currently doing, which may not always be set), * and a list of friends. */import acm.graphics.*;import java.util.*;public class FacePamphletProfile implements FacePamphletConstants {/** * Constructor * This method takes care of any initialization needed for * the profile. */public FacePamphletProfile(String name) {uniqueName = name;statusName = null;imageName = null;friends = new ArrayList ();}/** This method returns the name associated with the profile. */ public String getName() {return uniqueName;}/** * This method returns the image associated with the profile. * If there is no image associated with the profile, the method * returns null. */ public GImage getImage() {if (imageName != null) return imageName; else return null;}/** This method sets the image associated with the profile. */ public void setImage(GImage image) {imageName = image;}/** * This method returns the status associated with the profile. * If there is no status associated with the profile, the method * returns the empty string (""). */ public String getStatus() {if (statusName != null) return statusName; else return null;}/** This method sets the status associated with the profile. */ public void setStatus(String status) {statusName = status;}/** * This method adds the named friend to this profile's list of * friends. It returns true if the friend's name was not already * in the list of friends for this profile (and the name is added * to the list). The method returns false if the given friend name * was already in the list of friends for this profile (in which * case, the given friend name is not added to the list of friends * a second time.) */public boolean addFriend(String friend) {if (!friends.contains(friend)) {friends.add(friend);return true;} else return false;}/** * This method removes the named friend from this profile's list * of friends. It returns true if the friend's name was in the * list of friends for this profile (and the name was removed from * the list). The method returns false if the given friend name * was not in the list of friends for this profile (in which case, * the given friend name could not be removed.) */public boolean removeFriend(String friend) {return friends.remove(friend);}/** * This method returns an iterator over the list of friends * associated with the profile. */ public Iterator getFriends() {return friends.iterator();}/** * This method returns a string representation of the profile. * This string is of the form: "name (status): list of friends", * where name and status are set accordingly and the list of * friends is a comma separated list of the names of all of the * friends in this profile. * * For example, in a profile with name "Alice" whose status is * "coding" and who has friends Don, Chelsea, and Bob, this method * would return the string: "Alice (coding): Don, Chelsea, Bob" */ public String toString() {String line = uniqueName + " (" + statusName + "): ";for (String name: friends) {line += name + ", ";}return line;}/* private instance variables */private String uniqueName;private String statusName;private GImage imageName;private ArrayList friends;}
0 0
- 斯坦福 cs106A Assignment7
- 斯坦福 cs106A Assignment4
- 斯坦福 cs106A Assignment5
- 斯坦福 cs106A Assignment6
- CS106A Assignment4: Hangman
- CheckerboardKarel For Standord CS106A
- 斯坦福研究所
- 斯坦福学习
- 斯坦福大學將舉辦區塊鏈研討會
- CS106A Assignment3: Breakout——浅谈自上而下的设计体验
- Coding Style 转自Stanford CS106A Handout #7
- 斯坦福大学CS106A公开课笔记--启示环境配置
- cs106a学习笔记 - 搭建Karel the robot环境
- 斯坦福大学公开课:编程方法学 (cs106A) 资源
- 斯坦福iPhone开发课件
- 乔布斯斯坦福演讲
- 乔布斯斯坦福演讲
- 乔布斯斯坦福演讲
- Linux系统文件目录结构详细说明
- test
- BZOJ P3436 小K的农场
- ibatis-dynamic的用法
- 设计模式之builder模式
- 斯坦福 cs106A Assignment7
- Java学习之string类练习
- UVALive
- Ispci命令详解
- python基础之单引号、双引号和三引号
- servlet容器的工具:Filter
- Leetcode 231. Power of Two
- Android里Fragment的相关知识(二)
- win7 生成jar包