华南理工大学Java语言程序设计课堂作业答案

上传人:2127513****773577... 文档编号:64632881 上传时间:2022-03-21 格式:DOC 页数:24 大小:42.50KB
收藏 版权申诉 举报 下载
华南理工大学Java语言程序设计课堂作业答案_第1页
第1页 / 共24页
华南理工大学Java语言程序设计课堂作业答案_第2页
第2页 / 共24页
华南理工大学Java语言程序设计课堂作业答案_第3页
第3页 / 共24页
资源描述:

《华南理工大学Java语言程序设计课堂作业答案》由会员分享,可在线阅读,更多相关《华南理工大学Java语言程序设计课堂作业答案(24页珍藏版)》请在装配图网上搜索。

1、精选优质文档-倾情为你奉上华南理工大学Java语言程序设计课堂作业答案homework01 2011-02-21 13:59 1. 编写一个类(控制台), 输入你的名字, 回车后, 向屏幕输出信息欢迎你, *类似字样 2. 使用命令行模式编译,执行该程序, 将class文件指定输出到class目录 3. 给类添加 move(), turnLeft(), pickPeeper(), putPeeper() 等方法, 并在Main中调用显示相关信息 4. 给类和各方法添加注释, 并使用javadoc指令输出代码文档 Homework01 1. 编写一个类(控制台), 输入你的名字, 回车后, 向屏

2、幕输出信息欢迎你, *类似字样; () 2. 使用命令行模式编译,执行该程序, 将class文件指定输出到class目录; (现class文件夹在D:/目录下,控制台命令输入:javac -d D:class ) 3. 给类添加 move(), turnLeft(), pickPeeper(), putPeeper() 等方法, 并在Main中调用显示相关信息; 4. 给类和各方法添加注释, 并使用javadoc指令输出代码文档. (现新建doc文件夹在D:下以存储文档,控制台输入:javadoc -d D:doc ) homework02 2011-02-27 22:32 修路工: 请使用分

3、附件中的空白项目,装载 sample03_holes_ 背景,修缮1st Street。 /* * File: * - * The SampleKarel subclass as it appears here does nothing. */ import *; /* * Name: * Section Leader: */ public class SampleKarel extends SuperKarel int count = 0; public void run() / You fill in this part while(count if (frontIsBlocked() |

4、 rightIsClear() changeDirection(); move(); public void changeDirection() if (rightIsClear() turnRight(); else if (leftIsBlocked() if (rightIsBlocked() turnRight(); turnRight(); turnRight(); if (rightIsBlocked() turnLeft(); public static void main(String args) String newArgs = new String + 1; (args,

5、0, newArgs, 0, ); public void judgeAndPick() if (beepersPresent() pickBeeper(); count+; newArgs = public String className() return ()1.getCanonicalName(); .className(); (newArgs); homework03 2011-03-07 16:07 使用之前的空白项目,装载*collect* .w 背景,收集全部的Beeper. /* * File: * - * The SampleKarel subclass as it app

6、ears here does nothing. */ import *; public class CollectAllBeepers extends SuperKarel /* * Through the maze* author 黄泽津*/ public void run() collect(); while(frontIsBlocked()&!leftIsBlocked()if(facingEast() turnLeft();move(); turnLeft(); collect(); if(facingWest() turnRight(); move(); turnRight();co

7、llect(); private void collect() while(beepersPresent()pickBeeper(); if(frontIsBlocked() return; elsemove(); collect(); public static void main(String args) String newArgs = new String + 1; (args, 0, newArgs, 0, );newArgs = public String className() return ()1.getCanonicalName(); .className(); (newAr

8、gs); homework04 迷宫收集 2011-03-13 21:47 创建迷宫world并放置一定的Beeper,装载该World后,收集全部的Beeper, 发送时请携带该world地图 int count = 0; public void run() /You fill in this part while(count judgeAndPick(); if (frontIsBlocked() | rightIsClear() changeDirection(); move(); public void changeDirection() if (rightIsClear() turn

9、Right(); else if (leftIsBlocked() turnLeft(); turnLeft(); public void judgeAndPick() if (beepersPresent() pickBeeper(); count+; homework05 迷宫收集 2011-03-26 21:54 1. 从文件中构造二维世界, 文件为文本模式文件, 字符0 1组成 2. 构造该世界最下方地平面处的路面曲线, 以简单直观的方式在文本中打印, 路面可用*表示 3. 可将Ship, Person的行进路线以以简单直观的方式在各自独立的文本中打印, 行进路线可用*表示 /* *

10、*/ package map; import *; import *; /* * author guhonglueying * */ / map生成方法之从文件中读取 public class FileMap extends SubMap catch (IOException e) (); charArr = new char(); for (int i = - 1; i -1; i-) charArri = (); try FileReader fr = new FileReader(s0); BufferedReader bw = new BufferedReader(fr); while

11、 (str = () != null) (); / 重写父类create方法,用于从文件中读取新地图 public void create(String. s) String str = null; Stack stk = new Stack(); /* * */ package map; /* * author guhonglueying * */ / map生成方法之程序中定义map public class SimpleMap extends SubMap / 重写父类create方法,用来从程序中创建新地图 /* * */ package map; charArr = new char

12、 0, 0, 1, 1, 1, 0, 0, 0 , 1, 0, 0, 0, 0, 0, 1, 0 ,; 0, 0, 0, 1, 1, 0, 1, 0 , 0, 1, 0, 0, 0, 0, 0, 0 , 0, 1, 0, 1, 0, 1, 0, 0 , 0, 0, 0, 1, 0, 0, 0, 1 , 1, 1, 0, 0, 0, 1, 0, 1 , 1, 1, 1, 1, 1, 1, 0, 0 public void create(String. s) /* * author guhonglueying * */ / 生成map的抽象类,用来定义map的一些public属性和方法 publi

13、c abstract class SubMap /* * */ package map; public void setCol(int col) = col; int row; int col; char charArr; public void setRow(int row) = row; public int getRow() return row; public int getCol() return col; / 抽象方法,在子类中实现新地图生成的不同实现 public abstract void create(String. s); public char getMap() = ;

14、= charArr0.length; return charArr; import *; import *; /* * author guhonglueying * */ / map生成方法之从标准输入流获取 public class SystemInMap extends SubMap InputStreamReader isr = new InputStreamReader(); BufferedReader bw = new BufferedReader(isr);(请用以下字符输入一方形地图:n1-墙,0-路,-入口,#-出口n每次输入一行回车,输入E结束 while (str = (

15、) != null) if ( break; (); / 重写父类create方法,用于从命令操作符中读取新地图 public void create(String. s) String str = null; Stack stk = new Stack(); try catch (IOException e) (); charArr = new char(); for (int i = - 1; i -1; i-) charArri = (); /* * */ package objectmovable; import util.*; /* * author guhonglueying *

16、*/ / 实现person的走迷宫过程 public class Person extends SubObjectMovable public void Maze(char ch) ( h = new Helper(ch); (); (, 2); /* * */ package objectmovable; import util.*; /* * author guhonglueying * */ / 实现ship的走迷宫过程 public class Ship extends SubObjectMovable public void Maze(char ch) (/* * */ packag

17、e util; /* * author guhonglueying * */ public class Node public Node(int x, int y) = x; = y; public int getX() return x; private int x; private int y; public Node() public void setX(int x) = x; public int getY() return y; public void setY(int y) public boolean equals(Object o) if (!(o instanceof Nod

18、e) = y; return false; Node n = (Node) o; return = x & = y; public String toString() return x + /* * */ package test; import map.*; import objectmovable.*; /* * author guhonglueying * */ public class TestMaze /* * param args */ / 主函数,程序入口 public static void main(String args) /多态实现:三种方式创建迷宫 SubMap sm

19、= new SimpleMap(); (); char c = (); /多态实现不同物体的走迷宫过程 SubObjectMovable iom = new Ship(); (c); homework07 文件处理 2011-04-08 22:19 1. 读文件, 添加或去掉行号后写回 2. 统计一个文件中的词个数(请注意中文) package sample; import *; public class AddRowNum / / / / / / / / / / (return; try /* 开启两个文件, 分别读写 */ BufferedReader reader = new Buffe

20、redReader(new FileReader( BufferedWriter writer = new BufferedWriter( new FileWriter(/* 首先取得总行数, 然后逐行添加写回 */ int nRowNum = 0; String strOneLine = null; while () != null) nRowNum+; public static void main(String args) (); String formator = (nRowNum = 0; reader = new BufferedReader( new FileReader(whi

21、le (strOneLine = () != null) ( %sn+nRowNum, strOneLine); package sample; import *; public class CountWords public static void main(String args) String fileName = try BufferedReader reader = new BufferedReader(new FileReader(fileName); int NumberCount = 0; int LetterCount = 0; int ChineseCharacterCou

22、nt = 0; int a = -1; (new File( (); (); new File(new File( catch (FileNotFoundException e) (找不到指定文件 catch (IOException e) (文件读写错误 Character c; while (a = () != -1) c = (char) a; if (c) NumberCount+; else if (一 ChineseCharacterCount+; else if (c) LetterCount+; (); (文件共包含:(数字:字母: + LetterCount + 汉字:/*

23、林启敏 */ (); reader = new BufferedReader(new FileReader(boolean isWordStart = false; int count = 0; char oneChar = new char1; while (oneChar) != -1) if (oneChar0 = . | oneChar0 = , | oneChar0 = ) if(isWordStart) isWordStart = false; count+; else isWordStart = true; ( catch (FileNotFoundException e) (找

24、不到指定文件 catch (IOException e) (文件读写错误homework08 html 2011-04-11 16:24 1. 使用Html实现登录页面, 需提供身份证号码, Email等相关信息 2. 在提交时, 使用JavaScript做校验, 如果失败, 则提示注册者. 用户注册页面 用户注册页面 注册成功页面 .oneColFixCtrHdr #container width: 780px;background: #FFFFFF; margin: 0 auto; border: 1px solid #; text-align: left; .oneColFixCtrHd

25、r #header background: #DDDDDD; padding: 0 10px 0 20px; .oneColFixCtrHdr #header h1 margin: 0; padding: 10px 0; .oneColFixCtrHdr #mainContent padding: 0 20px; background: #FFFFFF; font-weight: bold; .oneColFixCtrHdr #footer padding: 0 10px; background:#DDDDDD; .oneColFixCtrHdr #footer p margin: 0; pa

26、dding: 10px 0; - 注册成功! 下面自动转入首页. homework09 Swing 2011-04-17 22:26 1. 使用Swing实现之前Html登录页面, 需提供身份证号码, Email等相关信息 2. 同样在提交时, 程序内部做校验, 失败则提示下相关信息. package homework09; import *; import *; import *; import *; import *; public class LoginWindow public static void main(String args) LoginFrame login = new L

27、oginFrame();(用户注册界面(480,360); (null); (_ON_CLOSE);(true); class LoginFrame extends JFrame public LoginFrame() Font defaultFont = new Font(微软雅黑Container loginCon = getContentPane();LoginPanel panel = new LoginPanel(); TitledBorder inputPanelBorder = new TitledBorder(基本资料(defaultFont); (inputPanelBord

28、er);(panel); class LoginPanel extends JPanel JTextField nameField,phoneField,emailField,idCardField; JPasswordField passField,repassField; JButton submit,reset; public LoginPanel() BorderLayout layout = new BorderLayout();setLayout(layout); nameField = new JTextField(10); (); passField = new JPasswo

29、rdField(8); (); repassField = new JPasswordField(8); (); phoneField = new JTextField(12); (); emailField = new JTextField(14); (); idCardField = new JTextField(12); (); submit = new JButton(提交reset = new JButton(重置 Box horizontalBox1 = ();Box horizontalBox2 = ();Box horizontalBox3 = ();Box horizonta

30、lBox4 = ();Box horizontalBox5 = ();Box horizontalBox6 = ();Box horizontalBox7 = (); (35);(new JLabel(用户名:(12);(nameField); (10); (new JLabel(长度为8至16 (new JLabel(设置密码:(12);(passField); (10); (new JLabel(长度至少为6(30);(new JLabel(重复密码:(12);(repassField); (10); (new JLabel(两次输入必须一致(60);(new JLabel(电话号码:(1

31、2);(phoneField);(10); (new JLabel(座机或电话号码 (new JLabel(邮箱地址:(12);(emailField); (20);(new JLabel(身份证信息:(12);(idCardField); (52); (submit); (20);(reset); Box verticalBox = ();(horizontalBox1); (14);(horizontalBox2); (14);(horizontalBox3); (14); (horizontalBox4); (14);(horizontalBox5); (14);(horizontalB

32、ox6); (18);(horizontalBox7); add(verticalBox); (new ActionListener() public void actionPerformed(ActionEvent e) submit(); ); (new ActionListener() public void actionPerformed(ActionEvent e) reset(); ); void submit() String phoneRe = String emailRe = String idCardRe = Pattern phonePat = (phoneRe); Pa

33、ttern emailPat = (emailRe); Pattern idCardPat = (idCardRe); Matcher phoneMat = (); Matcher emailMat = (); Matcher idCardMat = (); String error = if().equals( error = 用户名不能为空! else if().length()16) error = 用户名长度不符合要求! else if().equals( error = 密码不能为空! else if().length()else if(!().equals() error = 两次

34、输入的密码不一致! else if().equals( error = 电话号码不能为空! else if(!() error = 电话号码格式不正确! else if().equals( error = 邮箱地址不能为空! else if(!() error = 邮件地址格式不正确! else if().equals( error = 身份证信息不能为空! else if(!() error =身份证格式不正确! if( else (null, error, 消息提示 _MESSAGE); (null, 注册成功!消息提示 _MESSAGE); reset(); void reset() h

35、omework10 container 2011-04-24 22:46 1. 使用数组构建栈Stack(FILO) (null); (null); (null); (null); (null); (null); 2. 对比Java提供的Stack, 两者有啥区别 3. 使用Stack栈实现行编辑器设立一个输入行冲区, 接受用户逐行的输入, 处理后回显正确的信息 逐行处理过程如下: 有两个特殊字符用来修改该行的错误信息, #表示之前一个字符错误, 请删除, 表示之前字符全部错误, 请删除前面的全部 示例: whli#ilr#e(s#*s) - while(*s) outchaputchar(*

36、s=#+); - putchar(*s+); package mystack; /* * author 电子商务一班 俞国峰 1 */ public class MyStack /currentSize用来记录当前栈中元素个数 private int currentSize; private Object Mylist; /构造函数,创建空栈 public MyStack() Mylist = new Object10; currentSize = 0; /当数组长度不够时,增加数组长度 public void enLargeSize() Object tempList = new Objec

37、t * 2; (Mylist, 0, tempList, 0, ); Mylist = tempList; /判断栈是否为空 public boolean empty() return currentSize = 0; /入栈 public void push(Object o) if (currentSize = ) enLargeSize(); /出栈,返回栈顶元素 public Object pop() /获取栈顶元素 public Object peek() if (currentSize = 0) return null; if (currentSize = 0) return nu

38、ll; currentSize-; if (currentSize + 1 = ) enLargeSize(); return MylistcurrentSize + 1; MylistcurrentSize = new Object(); MylistcurrentSize = o; currentSize+; return MylistcurrentSize - 1; /获取栈大小,返回栈中元素个数 public int getSize() return currentSize; /清空栈 public void clearAll() /重载toString(),以字符串形式返回栈中元素

39、Override public String toString() Mylist = new Object10; currentSize = 0; String tempString = for (int i = 0; i return tempString; package mystack; /* * author 电子商务一班 俞国峰 1 */ import *; public class testMyStack public static void main(String args) MyStack msk = new MyStack(); Scanner sc = new Scanne

40、r(); String input = (nYou may enter in: enter exit or EXIT to exit! (example: outchaputchar(*s=#+);( while (!(input = ().equalsIgnoreCase( char charr = ();(for (int i = 0; i (charri); switch (charri) case #: (); break; case : (); break; default : (charri); break; ( homework11 多线程和网络通信 2011-05-07 21:

41、59 1. 实现UDP模式的客户端和服务器端通信 2. 使用两个线程: 一个线程负责发送消息, 一个线程负责收取并发送消息,两线程间使用LinkedList共享数据 package _yu_guo_feng; /* * author 电子商务一班俞国峰1 * * 功能:c/s结构,实现基于UDP的聊天功能 * 说明:先开启服务器,再接入客户端 * 未完善:服务器多开异常处理,下线提醒 */ import *; import *; import *; import *; import *; import *; public class Server extends JFrame private

42、static final long serialVersionUID = L; private JTextArea jta; /保存当前用户 private LinkedList sclient; private DatagramSocket socket; private DatagramPacket sendPacket, recevicedPacket; private byte buf; /日期处理 private SimpleDateFormat sdf; public static void main(String args) new Server(); public Server

43、() String introduction QQ 730= 寝室版 + 欢迎使用 腾讯=n + + 使用说明:n+ 运行服务器n+ 接入客户端(可接入多个)n+ 功能:上线提醒、群聊和私聊n + 作者:电子商务一班俞国峰1=n + try socket = new DatagramSocket(8888); catch (SocketException e) (); jta = new JTextArea(introduction); (new ImageIcon(腾讯QQ 730寝室版服务器(jta, ); (400, 300); (false); (_ON_CLOSE); (null); (true); sclient = new LinkedList(); /指定时间表示模式 sdf = new SimpleDateFormat(专心-专注-专业

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!