软件工程结对作业01
Posted 耶路撒冷有多冷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件工程结对作业01相关的知识,希望对你有一定的参考价值。
设计思想:
主要功能:
生成试题:在html页面输入题目选项(有无括号,数值范围,题目个数等属性)点击提交按钮使用jQuery的响应按钮的点击事件发送ajax post请求,将题目选项参数传到servlet,servlet获取参数值,调用生成试题的方法生成指定选项的试题,并将试题信息存入到session中已备判断用户的答案的正确性,将试题信息返回,在ajax成功接收到试题信息之后将试题信息显示在页面上。并将输入题目选项的块隐藏,将显示试题的块显示。
判断结果:点击交卷按钮后,jQuery响应按钮点击事件,将利用ajax将答案信息传入到servlet,servlet获取答案,并从session中获取题目信息,将用户的答案与正确答案进行比较,并调用操作数据库的方法将试题信息存储到数据库,将结果传回,ajax收到传回的数据后将结果显示在页面上。
编程总结和体会:
(1)程序的设计非常重要,在具体编码之前要把程序有什么功能怎么写设计好,有了一个清晰的设计思路,才能高效正确地写出代码。
(2)要将整体的框架分清楚,合理地进行封装,可以方便地进行改动。
运行结果截图:
输入做题选项界面:
答题界面:
判断结果界面:
查询历史试题界面:
查询结果页面:
PSP表格
实现程序之前估计各个模块所耗费时间
实现程序之后各个模块实际花费的时间
源程序代码:
package beans; import java.util.Stack; import func.FenShu; import func.MyException; public class ShiTi { private int id,userScore, length,shiJuanID; private String tiMu, rightAnswer, userAnswer, username,logicOrder; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getShiJuanID() { return shiJuanID; } public void setShiJuanID(int shiJuanID) { this.shiJuanID = shiJuanID; } public int getUserScore() { return userScore; } public void setUserScore(int userScore) { this.userScore = userScore; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public String getTiMu() { return tiMu; } public void setTiMu(String tiMu) { this.tiMu = tiMu; try { expressCalculate(); } catch (MyException e) { // TODO Auto-generated catch block e.printStackTrace(); }// 计算答案 this.length = (tiMu.split(" ").length + 1) / 2; } public String getRightAnswer() { return rightAnswer; } public void setRightAnswer(String rightAnswer) { this.rightAnswer = rightAnswer; } public String getUserAnswer() { return userAnswer; } public void setUserAnswer(String userAnswer) { this.userAnswer = userAnswer; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getLogicOrder() { return logicOrder; } public void setLogicOrder(String logicOrder) { this.logicOrder = logicOrder; } public ShiTi() { } // 表达式计算,参数为字符串类型的运算式 private void expressCalculate() throws MyException { if(this.tiMu == null) { throw new MyException("试题无效"); } String express = this.tiMu; Stack<String> num = new Stack<String>(); Stack<String> symbolS = new Stack<String>(); symbolS.push("#"); express += "#"; String order = ""; char ch; int i = 0; ch = express.charAt(i); while ((!symbolS.peek().equals("#")) || (ch != \'#\')) {// while循环开始 if (isNumber(ch)) {// 读到的不是空格,说明开始读运算数 String readNumStr = ""; while (true) { readNumStr += ch; ch = express.charAt(++i); if (ch == \' \' || ch == \'#\' || ch == \')\') {// 读到的是空格,或,说明运算数结束 break; } } num.push(readNumStr); } else if (ch == \' \') { if ((i + 1) < express.length()) {// 未到字符串末尾 ch = express.charAt(++i); } }else {// 读到的是运算符 char compare = priorityCompare(symbolS.peek(), ch + ""); if (compare == \'=\') {// 若优先级相等,则说明ch是右括号,栈顶为左括号,此时将栈顶弹出,读取下一个字符 symbolS.pop(); ch = express.charAt(++i); } else if (compare == \'>\') {// ch的优先级小于栈顶的优先级,要说明栈顶的运算符应该先计算,所以应弹栈运算 // 弹出两个运算数,弹出一个运算符 String bStr = num.pop(); String aStr = num.pop(); String symbolT = symbolS.pop(); // 计算该字表达式 String c = yunSuan(aStr, bStr, symbolT); if (c.equals("ERROR")) {// 如果计算函数返回error则说明计算过程出现了负数,说明该运算式不符合要求,停止计算,计算结果为error,返回; this.rightAnswer = "ERROR"; return; } else {// 计算过程正常,则将计算结果压栈 order += aStr + "," + symbolT + "," + bStr + ",";// 将运算的子表达式加进运算顺序字符串中,操作数和操作符用逗号隔开 num.push(c); } } else if(compare == \'E\') { this.rightAnswer = "ERROR"; return; } else {// 说明ch优先级大于栈顶元素的优先级,则应将ch压栈,读取下一个运算符 symbolS.push(ch + ""); if ((i + 1) < express.length()) { ch = express.charAt(++i); } } } } this.rightAnswer = num.pop(); this.logicOrder = order; } // 判断ch是否为数字 private boolean isNumber(char ch) { if (ch >= \'0\' && ch <= \'9\') { return true; } return false; } /* * 子表达式计算,参数为两个运算数的字符串形式,和一个运算符,也为字符串类型 返回计算结果的字符串形式 * 如果减法运算出现负数,或除数为0,或分数的分母为0则返回ERROR * */ private String yunSuan(String aStr, String bStr, String symbol) throws MyException { if(aStr == null || bStr == null || symbol == null) { throw new MyException("子表达式出现错误!"); } int adivIndex = aStr.indexOf("/"); int bdivIndex = bStr.indexOf("/"); if ((adivIndex == -1) && (bdivIndex == -1)) {// a.b都是整数 int a = Integer.parseInt(aStr); int b = Integer.parseInt(bStr); switch (symbol.charAt(0)) { case \'+\': return a + b + ""; case \'-\': { if (a < b) { return "ERROR"; } return a - b + ""; } case \'*\': { return a * b + ""; } case \'/\': { if (b == 0) { return "ERROR"; } else if (a % b == 0) { return a / b + ""; } return new FenShu(a, b).toString(); } default: return "ERROR"; } } else {// a,b中存在分数,则将a,b都当做分数进行运算 FenShu a = new FenShu(aStr); FenShu b = new FenShu(bStr); switch (symbol.charAt(0)) { case \'+\': return a.add(b).toString(); case \'-\': { FenShu c = a.subtract(b); if(c.getNumerator() < 0) { return "ERROR"; } return c.toString(); } case \'*\': return a.multiply(b).toString(); case \'/\': return a.divide(b).toString(); default: return "ERROR"; } } } // 判断运算符优先级 private char priorityCompare(String a, String b) { char[][] priority = { { \'>\', \'>\', \'<\', \'<\', \'<\', \'>\', \'>\' }, { \'>\', \'>\', \'<\', \'<\', \'<\', \'>\', \'>\' }, { \'>\', \'>\', \'>\', \'>\', \'<\', \'>\', \'>\' }, { \'>\', \'>\', \'>\', \'>\', \'<\', \'>\', \'>\' }, { \'<\', \'<\', \'<\', \'<\', \'<\', \'=\', \'>\' }, { \'>\', \'>\', \'>\', \'>\', \' \', \'>\', \'>\' }, { \'<\', \'<\', \'<\', \'<\', \'<\', \' \', \'=\' } }; int a_index = index_symbol(a); int b_index = index_symbol(b); if(a_index == -1 || b_index == -1) { return \'E\'; } return priority[a_index][b_index]; } // 获取运算符对应的下标 private int index_symbol(String a) { String p = "+-*/()#"; // System.out.println("判断运算符对应的下标:" + a); return p.indexOf(a); } }
package beans; import java.util.Date; public class ShiJuan { private int id,shiTiNum, grade,shiJuanID; private String username; private Date date; public int getShiJuanID() { return shiJuanID; } public void setShiJuanID(int shiJuanID) { this.shiJuanID = shiJuanID; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getShiTiNum() { return shiTiNum; } public void setShiTiNum(int shiTiNum) { this.shiTiNum = shiTiNum; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public ShiJuan() { } }
package beans; public class UserInfo { private String username, password; private int shiTiNum, shiJuanNum,rightNum; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getShiTiNum() { return shiTiNum; } public void setShiTiNum(int shiTiNum) { this.shiTiNum = shiTiNum; } public int getShiJuanNum() { return shiJuanNum; } public void setShiJuanNum(int shiJuanNum) { this.shiJuanNum = shiJuanNum; } public int getRightNum() { return rightNum; } public void setRightNum(int rightNum) { this.rightNum = rightNum; } public UserInfo() { } }
package dao; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; public class DBUtil { private static String driverName; private static String username; private static String password; private static String url; // static // { // driverName = "com.mysql.jdbc.Driver"; // url = "jdbc:mysql://localhost:3306/siZeWeb?useSSL=false&useUnicode=true&characterEncoding=UTF-8"; // username = "root"; // password = "490272"; // } //从属性文件加载参数(URL 用户名 密码) static { Properties prop = new Properties(); try { prop.load(DBUtil.class.getResourceAsStream("/db.properties")); driverName = prop.getProperty("driverName"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { Connection conn = getDBConnection(); closeDB(conn, null, null); } //获取数据库连接 public static Connection getDBConnection() { Connection conn = null; try { Class.forName(driverName); conn = DriverManager.getConnection(url,username,password); } catch (SQLException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //释放资源 public static void closeDB(Connection conn,PreparedStatement pstmt,ResultSet rs) { try { if(rs != null) rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(pstmt != null) pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import beans.ShiJuan; public class ShiJuanDAO { //插入一条试卷记录到数据库 public void insert(ShiJuan sj) throws SQLException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; conn = DBUtil.getDBConnection(); // System.out.println(sj.getShiTiNum() + " " + sj.getUsername() + " " + sj.getDate() + " " + sj.getUserCurrectSum()); pstmt = conn.prepareStatement("insert into shiJuanKu(shiTiNum,username,date,shiJuanID) values(?,?,?,?)"); pstmt.setInt(1, sj.getShiTiNum()); pstmt.setString(2, sj.getUsername()); pstmt.setTimestamp(3, new Timestamp(sj.getDate().getTime())); pstmt.setInt(4, sj.getShiJuanID()); pstmt.executeUpdate(); DBUtil.closeDB(conn, pstmt, rs); } //根据用户名查询试卷信息,返回值类型为list public List<ShiJuan> selectByUsername(String username) throws SQLException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; List<ShiJuan> list = new ArrayList<ShiJuan>(); conn = DBUtil.getDBConnection(); pstmt = conn.prepareStatement("select * from shiJuanKu where username=?"); pstmt.setString(1, username); rs = pstmt.executeQuery(); while(rs.next()) { ShiJuan sj = new ShiJuan(); sj.setId(rs.getInt("id")); sj.setShiTiNum(rs.getInt("shiTiNum")); sj.setUsername(rs.getString("username")); sj.setGrade(rs.getInt("grade")); sj.setDate(rs.getTimestamp("date")); sj.setShiJuanID(rs.getInt("shiJuanID")); list.add(sj); } DBUtil.closeDB(conn, pstmt, rs); return list; } //更新试卷信息,设置试卷题目,根据用户名和用户当前试卷总数 public void update(ShiJuan sj) throws SQLException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; conn = DBUtil.getDBConnection(); pstmt = conn.prepareStatement("update shiJuanKu set grade=? where username=? and shiJuanID=?"); pstmt.setInt(1,sj.getGrade()); pstmt.setString(2, sj.getUsername()); pstmt.setInt(3, sj.getShiJuanID()); pstmt.executeUpdate(); DBUtil.closeDB(conn, pstmt, rs); } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import beans.ShiTi; public class ShiTiDAO { //将list中的试题插入到数据库, public void insert(List<ShiTi> list) throws SQLException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; conn = DBUtil.getDBConnection(); pstmt = conn.prepareStatement("insert into shiTiKu(tiMu,rightAnswer,username,logicOrder,length,shiJuanID) values(?,?,?,?,?,?)"); for(ShiTi st : list) { pstmt.setString(1, st.getTiMu()); pstmt.setString(2, st.getRightAnswer()); pstmt.setString(3, st.getUsername()); pstmt.setString(4, st.getLogicOrder()); pstmt.setInt(5, st.getLength()); pstmt.setInt(6, st.getShiJuanID()); pstmt.executeUpdate(); } DBUtil.closeDB(conn, pstmt, rs); } //根据用户名和用户的试题ID查询试题,返回结果为list public List<ShiTi> selectByUserNameANDShiJuanID(String username,int shiJuanID) throws SQLException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; List<ShiTi> list = new ArrayList<ShiTi>(); conn = DBUtil.getDBConnection(); pstmt = conn.prepareStatement("select * from shiTiKu where username=? and shiJuanID=?"); pstmt.setString(1, username); pstmt.setInt(2, shiJuanID); rs = pstmt.executeQuery(); while(rs.next()) { ShiTi st = new ShiTi(); st.setId(rs.getInt("id")); st.setTiMu(rs.getString("tiMu")); st.setRightAnswer(rs.getString("rightAnswer")); st.setUserAnswer(rs.getString("userAnswer")); st.setUserScore(rs.getInt("userScore")); list.add(st); } DBUtil.closeDB(conn, pstmt, rs); return list; } //根据用户名和试题对错查询试题,返回结果为list public List<ShiTi> selectByUserNameANDScore(String username,int score) throws SQLException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; List<ShiTi> list = new ArrayList<ShiTi>(); conn = DBUtil.getDBConnection(); pstmt = conn.prepareStatement("select * from shiTiKu where username=? and userScore=?"); pstmt.setString(1, username); pstmt.setInt(2, score); rs = pstmt.executeQuery(); while(rs.next()) { ShiTi st = new ShiTi(); st.setId(rs.getInt("id")); st.setTiMu(rs.getString("tiMu")); st.setRightAnswer(rs.getString("rightAnswer")); st.setUserAnswer(rs.getString("userAnswer")); st.setUserScore(rs.getInt("userScore")); list.add(st); }以上是关于软件工程结对作业01的主要内容,如果未能解决你的问题,请参考以下文章