Design ideas:
1 Number of questions for input arithmetic
2 display calculation (the ability to generate calculations and results with class encapsulation) (write to database)
3 users fill in the answers
4 Receive answers compared to the answers in the database
5 return results
Source:
Package pers.sun.operateion;
produce a formula, and the corresponding result
public class Operated {
private int resultx;
Private String Formulax;
public int Getresultx () {
return RESULTX;
}
public void Setresultx (int resultx) {
This.resultx = Resultx;
}
Public String Getformulax () {
return formulax;
}
public void Setformulax (String formula) {
This.formulax=formula;
}
Public String calculation () {
int first= (int) (Math.random () *10+1);
int second= (int) (Math.random () *10+1);
int op= (int) (Math.random () *4+1);
char operator = 0;
Switch (OP) {
Case 1:operator= ' + '; resultx=first+second;break;
Case 2:operator= '-'; resultx=first-second;break;
Case 3:operator= ' * '; resultx=first*second;break;
Case 4:operator= '/';
}
is in addition to
if (op==4) {
The denominator is not 0 and can be removed
if (second!=0) {
int Res=first%second;
if (res==0) {
formulax=first+ "" +operator+ "" +second+ "=";
Resultx=first/second;
}
Else
Formulax=null;
}
Else
Formulax=null;
}
Not except
else {
formulax=first+ "" +operator+ "" +second+ "=";
}
return formulax;
}
}
Package pers.sun.operateion;
Import pers.sun.operateion.Operated;
produces n formulas, and results
public class Applyit {
private int[] result;
public string[] make (int n) {
//received container
string formulas[]=new string[n];
result=new Int[n];
//generates a formula
operated Opera=new operated ();
for (int i=0;i<n;) {
string temp=opera.calculation ();
//Determines whether the calculation is null
if (temp!=null) {
formulas[i]=temp;
result[i]=opera.getresultx ();
i++;
}
}
return formulas;
}
public int[] GetResult () {
return result;
}
public void Setresult (int[] result) {
this.result = result;
}
}
Package pers.sun.sql;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
public class DB {
public static Connection getconnection () {
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
String root= "root";
String password= "Sunyu";
String url= "Jdbc:mysql://localhost:3306/user_message";
Connection Con=null;
try {
Con=drivermanager.getconnection (Url,root,password);
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return con;
}
public static void Close (Connection con) {
try {
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public static void Close (PreparedStatement pre) {
try {
if (pre!=null)
Pre.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public static void Close (ResultSet result) {
try {
if (result!=null)
Result.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Package pers.sun.sql;
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 pers.sun.operateion.Operated;
public class SqlTool {
public static void Add (String formula,int result) {
if (formula!=null) {
String sql= "INSERT into Math (formula,result) value (?,?)";
Connection connection=db.getconnection ();
PreparedStatement Preparedstatement=null;
try {
Preparedstatement=connection.preparestatement (SQL);
Preparedstatement.setstring (1,formula);
Preparedstatement.setint (2,result);
Preparedstatement.executeupdate ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally {
Db.close (PreparedStatement);
Db.close (connection);
}
}
}
public static void deleted () {
Connection connection=db.getconnection ();
String sql= "Delete from math";
PreparedStatement Pre=null;
try {
Pre=connection.preparestatement (SQL);
Pre.executeupdate ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public static list<operated> search () {
Connection connection=db.getconnection ();
String sql= "SELECT * from Math";
PreparedStatement Pre=null;
ResultSet Result=null;
list<operated> op= new arraylist<operated> ();
try {
Pre = connection.preparestatement (SQL);
Result=pre.executequery ();
while (Result.next ()) {
Operated temp=new operated ();
Temp.setresultx (Result.getint ("result"));
Temp.setformulax (result.getstring ("formula"));
Op.add (temp);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return op;
}
public static void Updata () {
}
}
<%@ page language= "java" contenttype= "text/html; UTF-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Calculation Preparation </title>
<style type= "Text/css" >
body{
Font-family: "in italics";
Font-size: "10";
}
</style>
<body background= "D:\User\sunyu\Pictures\ background \ character 1.jpg" >
<%
Request.setcharacterencoding ("Utf-8");
String errorx=request.getparameter ("Errorx");
if (errorx!=null) {
%>
<%
}
%>
<form action= "outputbegin.jsp" method= "POST" >
<table align= "left" border= "1" width= "$" height= ">"
<tr>
<td> Enter the number of questions you want to do </td>
<td><input type= "text" name= "number"/></td>
<td><input type= "Submit" value= "OK" name= "submit"/></td>
<td><input type= "reset" value= "reset" name= "reset"/></td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
<% @page import= "Pers.sun.sql.SqlTool"%>
<% @page import= "pers.sun.operateion.Operated"%>
<% @page import= "java.util.*"%>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Judgment Results </title>
<style>
body{
Font-family: "in italics";
}
</style>
<body background= "D:\User\sunyu\Pictures\ background \ character 1.jpg" >
<H1 style= "Color:black" > How was your record? <ul>
<li><a href= "begin.jsp" style= "Color:yellow" > Redo </a> </li>
<li><a href= "ending.jsp" style= "Color:yellow" > End </a></li>
</ul>
<table align= "left" border= "1" width= "$" style= "Color:black" >
<tr>
<td> formulas </td>
<td> Your answers </td>
<td> Right </td> wrong
<td> correct answers </td>
</tr>
<%
int count=0;
List<operated> operation=new arraylist<operated> ();
Operation=sqltool.search ();
int length=operation.size ();
String[] Peoresult1=new string[length];
Peoresult1=request.getparametervalues ("I");
int i=0;
for (operated temp:operation) {
%>
<tr>
<td><%=temp.getformulax ()%></td>
<td><%=peoresult1[i]%></td>
<%
String Correct=null;
if (peoresult1[i]!=null&&! "". Equals (Peoresult1[i].trim ())) {
if (Integer.parseint (Peoresult1[i]) ==temp.getresultx ()) {
Correct= "Right";
count++;
}
Else
correct= "wrong";
}
%>
<td><%=correct%></td>
<td><%=temp.getresultx ()%></td>
</tr>
<%
i++;
}
%>
<tr><td> your score: <%=count%></td></tr>
</table>
<%
Sqltool.deleted ();
%>
</body>
<%@ page language= "java" contenttype= "text/html; UTF-8 "
pageencoding= "UTF-8"%>
<% @page import= "pers.sun.operateion.*"%>
<% @page import= "java.util.*"%>
<% @page import= "Pers.sun.sql.SqlTool"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<title> equation Form </title>
<body background= "D:\User\sunyu\Pictures\ background \ Character 1.jpg" text= "Black" >
<%
String Infor=request.getparameter ("number");
Receive information
if (infor==null| | "". Equals (Infor.trim ())) {
Request.setcharacterencoding ("Utf-8");
%>
<jsp:forward page= "begin.jsp" >
<jsp:param value= "You want to enter an integer" name= "Errorx"/>
</jsp:forward>
<%
Response.sendredirect ("begin.jsp");
}
int Num=integer.parseint (infor);
Applyit apply =new applyit ();
Generate a formula + result
string[] Suanshi=apply.make (num);
int Rightresults[]=apply.getresult ();
Write to Database
for (int i=0;i<num;i++) {
Sqltool.add (Suanshi[i], rightresults[i]);
}
%>
<form action= "handle.jsp" method= "POST" >
<table align= "left" border= "1" width= ">"
<tr>
<td> Serial Number </td>
<TD width= "$" > Formula </td>
<td> Results </td>
</tr>
<%
int i=0;
for (String Stemp:suanshi) {
%>
<tr>
<td><%=i%></td>
<td><%=stemp%></td>
<td><input type= "text" value= "" Name=i/></td>
</tr>
<%
i++;
}
%>
<tr>
<TD colspan= "3" ><input type= "Submit" value= "Submission" name= "Submit"/></td>
</tr>
</table>
</form>
</body>
Results:
Software Engineering Personal Work 03-web version arithmetic