java-pair programming: fractional integer, number of operators, operation range, addition, subtraction, multiplication and division

Keywords: SQL Java JSP Database

Links to the original text: https://www.mk2048.com/blog/blog.php?id=ic00hjiib&title=java-%E7%BB%93%E5%AF%B9%E7%BC%96%E7%A8%8B%EF%BC%9A%E5%88%86%E6%95%B0%E6%95%B4%E6%95%B0%EF%BC%8C%E8%BF%90%E7%AE%97%E7%AC%A6%E4%B8%AA%E6%95%B0%EF%BC%8C%E8%BF%90%E7%AE%97%E8%8C%83%E5%9B%B4%EF%BC%8C%E5%8

Paired Development Team Members: Zhao Ziming and Zhou Baohui

Design ideas:

The program involves the jsp javabean structure in the simple mvc framework, which writes the classes of integers and fractionals into entity package in src, and writes some operations about database into sql package; JSP has three interfaces, the first one is to select some simple parameters, such as integers or fractions, the range of the data, and calculate The number of expression operators, the number of formula subclasses, etc.) pass these parameters to the second jsp. In the second jsp, the user input parameters are obtained, and the corresponding arithmetic questions are generated. In the third jsp, the right and wrong are judged. In the jsp, the classes in the package are called, and the fractional integer classes in. java are directly used. In some database operations, too. It directly calls the functions in the encapsulated sql package to connect the database and insert data.

Among them, the calculation of arithmetic is realized recursively, such as the calculation of three numbers, which can be first calculated into the calculation of two numbers, thus the result of the calculation can be realized recursively.

Source code:

entity package

Fenshu.java

 

package entity;

public class Fenshu {
    int a;
    int b;
    
    public static Fenshu jisuanjieguo(Fenshu a[],int b[],int n){
        Fenshu jie=new Fenshu();
        if(n==1){jie=jisuan(a[0],b[0],a[1]);}
        else {
            int ii=0;boolean z=false;
            for(int i=0;i<n;i  ){
                if(b[i]==3||b[i]==4){
                    ii=i;
                    z=true;
                    break;
                }
            }
            if(z==true){
                a[ii]=jisuan(a[ii],b[ii],a[ii 1]);
                if(ii!=n-1){
                    for(int i=ii;i<n-1;i  ){
                        b[i]=b[i 1];
                        a[i 1]=a[i 2];
                    }
                }
                jie=jisuanjieguo(a,b,n-1);
            }
            else {
                a[0]=jisuan(a[0],b[0],a[1]);
                for(int i=0;i<n-1;i  ){
                    b[i]=b[i 1];
                    a[i 1]=a[i 2];
                }
                jie=jisuanjieguo(a,b,n-1);
            }
        }
        return jie;
    }
    public static Fenshu jisuan(Fenshu aaa,int bbb,Fenshu ccc){
        Fenshu d=new Fenshu();
        int a1=1,b1=1;
        if(bbb==1){
            b1=aaa.b*ccc.b;
            a1=aaa.a*ccc.b aaa.b*ccc.a;
        }
        else if(bbb==2){
            b1=aaa.b*ccc.b;
            a1=aaa.a*ccc.b-aaa.b*ccc.a;
        }
        else if(bbb==3){
            b1=aaa.b*ccc.b;
            a1=aaa.a*ccc.a;
        }
        else {
            b1=aaa.b*ccc.a;
            a1=aaa.a*ccc.b;
        }
        d.set(a1,b1);
        d.yue();
        return d;
    }
    public void set(int aa,int bb){
        a=aa;
        b=bb;
    }
    public int zuidayin(){
        int c=0,d=1;
        if(a<b) c=a;
        else c=b;
        for(int i=1;i<=c;i  ){
            if(a%i==0&&b%i==0){
                d=i;
            }
        }
        return d;
    }
    public void yue(){
        int y=this.zuidayin();
        if(y!=1){
            a=a/y;
            b=b/y;
        }
    }
    public String dai(){
        String p="";
        if(a>=b){
            int q=a/b;
            int w=a-b*q;
            return p q "'" w "/" b;
        }
        else {
            return p a "/" b;
        }
    }
    public String fanhui(){
        String r="";
        r="(" a "/" b ")";
        return r;
    }
}

 

 

 

Fenshude.java

 

package entity;

public class Fenshude {
    Fenshu as[];
    int fu[];
    int fanwei;
    int shu;
    public void getshu(int n){
        shu=n;
        as=new Fenshu [n 1];
        fu=new int [n];
    }
    public void getfanwei(int a){
        fanwei=a;
    }
    public void shengcheng(int pp){
        for(int i=0;i<shu;i  ){
            if(pp==1)
                fu[i]=(int)(Math.random()*4 1);
            else 
                fu[i]=(int)(Math.random()*2 1);
        }
        for(int i=0;i<shu 1;i  ){
            int ii=0;
            as[i]=new Fenshu();
            while(ii==0){
                as[i].b=(int)(Math.random()*(fanwei-1) 1);
                as[i].a=(int)(Math.random()*(fanwei-1) 1);
                if(as[i].a<as[i].b){ii=1;}
            }
        }
    }
    public String zhuan(int a){
        if(a==1) return " ";
        else if(a==2) return "-";
        else if(a==3)return "*";
        else return "/";
    }
    public String show(){
        String uuu="";
        for(int i=0;i<shu;i  ){
            uuu="(" as[i].a "/" as[i].b ") " zhuan(fu[i]);
            System.out.print("(" as[i].a "/" as[i].b ") " zhuan(fu[i]) " ");
        }
        uuu=uuu "(" as[shu].a "/" as[shu].b ")";
        System.out.print("(" as[shu].a "/" as[shu].b ")");
        System.out.println(" =");
        return uuu;
    }

    public Fenshu jieguo(){
        Fenshu jieguo=new Fenshu();
        jieguo=Fenshu.jisuanjieguo(as,fu,shu);
        return jieguo;
    }
    public Fenshu showjieguo(){
        Fenshu ee=jieguo();
        System.out.println("   The result is:" ee.a "/" ee.b);
        return ee;
    }
    public String showshizi(){
        String r="";
        for(int i=0;i<shu;i  ){
            r=r as[i].fanhui() zhuan(fu[i]);
        }
        r=r as[shu].fanhui();
        return r;
    }
    public String showdaan(){
        String r="";
        Fenshu ee=jieguo();
        r=r ee.a "/" ee.b;
        if(ee.b==1)
            r="" ee.a;
        return r;
    }
    
}

 

 

 

Panduan.java

 

package entity;

public class Panduan{
    
    public static int panduan1(String b)
    {
        if(b.equals("2"))
            return 2;
        else if(b.equals("3"))
            return 3;
        else if(b.equals("4"))
            return 4;
        else if(b.equals("5"))
            return 5;
        else
            return 0;
    }
    public static int panduan2(String b)
    {
        if(b.equals("10"))
            return 10;
        else if(b.equals("50"))
            return 50;
        else if(b.equals("100"))
            return 100;
        else 
            return 0;
    }
    public static void main(String[]arg)
    {
        String a="2";
        int m=panduan1(a);
        System.out.println(m);
        int n=panduan2("10");
        System.out.println(n);
    }

}

 

 

 

Suan.java

 

package entity;

public class Suan {
    int fanwei;
    int shu;
    int zheng[];
    int fu[];
    public void getshu(int n){
        shu=n;
        zheng=new int [n 1];
        fu=new int [n];
    }
    public void getfanwei(int a){
        fanwei=a;
    }
    public void shengcheng(int pp){
        for(int i=0;i<shu;i  ){
            if(pp==1)
                fu[i]=(int)(Math.random()*4 1);
            else 
                fu[i]=(int)(Math.random()*2 1);
        }
        for(int i=0;i<shu 1;i  ){
            zheng[i]=(int)(Math.random()*(fanwei-1) 1);
        }
        for(int i=0;i<shu;i  ){
            int aa=1;
            while(aa==1){
                if(fu[i]==2){
                    if(zheng[i]>=zheng[i 1]){
                        aa=0;
                    }
                    else{
                        zheng[i]=(int)(Math.random()*(fanwei-1) 1);
                        zheng[i 1]=(int)(Math.random()*(fanwei-1) 1);
                    }
                }
                else if(fu[i]==4){
                    if(zheng[i]<zheng[i 1]){
                        aa=0;
                    }
                    else{
                        zheng[i]=(int)(Math.random()*(fanwei-1) 1);
                        zheng[i 1]=(int)(Math.random()*(fanwei-1) 1);
                    }
                }
                else{
                    aa=0;
                }
            }
        }
    }
    public String zhuan(int a){
        if(a==1) return " ";
        else if(a==2) return "-";
        else if(a==3)return "*";
        else return "/";
    }
    public String show(){
        String uuu="";
        for(int i=0;i<shu;i  ){
            uuu=uuu zheng[i] zhuan(fu[i]);
            System.out.print(zheng[i] " " zhuan(fu[i]) " ");
        }
        uuu=uuu zheng[shu];
        System.out.print(zheng[shu]);
        System.out.println(" =");
        return uuu;
    }

    public Fenshu jieguo(){
        Fenshu qq[]=new Fenshu[shu 1];
        for(int i=0;i<shu 1;i  ){
            qq[i]=new Fenshu();
            qq[i].set(zheng[i],1);
        }
        Fenshu jieguo=new Fenshu();
        jieguo=Fenshu.jisuanjieguo(qq,fu,shu);
        return jieguo;
    }
    public Fenshu showjieguo(){
        Fenshu ee=jieguo();
        System.out.println("   The result is:" ee.a "/" ee.b);
        return ee;
    }
    public String showdaan(){
        String r="";
        Fenshu ee=jieguo();
        r=r ee.a "/" ee.b;
        if(ee.b==1)
            r="" ee.a;
        return r;
    }
    public String showshizi(){
        String r="";
        for(int i=0;i<shu;i  ){
            r=r zheng[i] zhuan(fu[i]);
        }
        r=r zheng[shu];
        return r;
    }
    public String fanhui(){
        String aq="";
        for(int i=0;i<shu;i  ){
            aq=aq "" zheng[i] zhuan(fu[i]);
        }
        aq=aq zheng[shu];
        return aq;
    }
}

 

 

 

sql package

Sql.java

 

package sql;

import java.sql.*;

public class Sql {
    static Connection con=null;
    static Statement stmt=null;
    static PreparedStatement psmt=null;
    public static Statement get(){
        return stmt;
    }
    public static void lianjie(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/web_number","root","baohui");
            stmt=con.createStatement();
            System.out.print("Successful database connection");
            //out.print("<p>");
        }
        catch(Exception e)
        {
            System.out.print(e);
        }
        
    }
    public static void insert (int a,String b,String c)throws Exception{
        
        try
        {
            String sql2="insert into web (number,shizi,jieguo) values (?,?,?)";
            psmt=con.prepareStatement(sql2);
            psmt.setInt(1,a);
            psmt.setString(2,b);
            psmt.setString(3,c);
            psmt.executeUpdate();
            //out.print("Insert Successfully! "";    
        }
        catch(Exception e)
        {
             //out.print(e);
        }
        
    }
    public static void delete()throws Exception{
        String sql1="delete from web";
        stmt.execute(sql1);
    }
    public static void guan()throws Exception{
        psmt.close();
        stmt.close();
        con.close();
    }
    public static void main(String arg[]){
        lianjie();
    }
    
}

 

 

 

jsp

index.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>
<%@page import="entity.*"%>
<%@page import="sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Issue</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <form id=form1 method="post" action="judge.jsp" >
    <% 
    request.setCharacterEncoding("utf-8");
      String operator=request.getParameter("operator");
      String operator1=request.getParameter("operator1");
      String number1=request.getParameter("number1");
      String number2=request.getParameter("number2");
  
    int a=Panduan.panduan1(number1);
      int b=Panduan.panduan2(number2);
      
      int c=2;
      if(operator.equals("yes"))
      {
          c=1;
      }
      
      if(operator1.equals("integer"))
      {
          Suan xx[]=new Suan[10];
        int wei=b;
        int shu=a;
        
        String pp="";
        String pp1="";
        Sql.lianjie();
        Sql.delete();
        for(int i=0;i<10;i  ){
            xx[i]=new Suan();
            xx[i].getfanwei(wei);
            xx[i].getshu(shu);
            xx[i].shengcheng(c);
            pp=xx[i].show();
            pp1=xx[i].showjieguo().dai();
            out.print(xx[i].showshizi() "=  ");
            //out.print(xx[i].showdaan());
            out.print("<input type=text id=text1 name=name" i ">");
            out.print("<p>");
            Sql.insert(i 1,xx[i].showshizi(),xx[i].showdaan());
        }
        Sql.guan();
      }
      
      else
      {
          request.setCharacterEncoding("utf-8");
          Fenshude xx[]=new Fenshude[10];
        int wei=b;
        int shu=a;
        
        String pp="";
        String pp1="";
        Sql.lianjie();
        Sql.delete();
        for(int i=0;i<10;i  ){
            xx[i]=new Fenshude();
            xx[i].getfanwei(wei);
            xx[i].getshu(shu);
            xx[i].shengcheng(c);
            //pp=xx[i].show();
            //pp1=xx[i].showjieguo().dai();
            out.print(xx[i].showshizi() "=  ");
            //out.print(xx[i].showdaan());
            out.print("<input type=text id=text1 name=name" i ">");
            out.print("<p>");
            Sql.insert(i 1,xx[i].showshizi(),xx[i].showdaan());
        }
        Sql.guan();
      
      }
      //out.print(a "<p>");
      //out.print(b "<p>");
      
    %>
   <input type=submit name=submit value=Submission >
   </form>
  </body>
</html>

 

 

 

judge.jsp

 

<%@ page language="java" import="java.util.*" import ="java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>
<%@page import="entity.*"%>
<%@page import="sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Judge the answer</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <%
    request.setCharacterEncoding("utf-8");
        Statement stmt=null;
        Connection con=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/web_number","root","baohui");
            stmt=con.createStatement();
            //out.print("database connection succeeded");
            //out.print("<p>");
        }
        catch(Exception e)
        {
            out.print(e);
        }
    
    //Sql.lianjie();
    String sql="select * from web";
    ResultSet rs=stmt.executeQuery(sql);
        for(int i=0;i<10;i  )
        {
            //sql statement execution
            int number=0;
            String shizi=null;
            String jieguo=null;
            if(rs.next())
            {
                number=rs.getInt("number");
                shizi=rs.getString("shizi");
                jieguo=rs.getString("jieguo");
            }
            
            //Compare and judge right and wrong
            out.print(shizi "=    ");
            String  name=request.getParameter("name" i);
            out.print(name);
            String name1=jieguo;
            
            if(name.equals(name1))
            {
                out.print("        ");
                out.print("<font color=greeen face=Regular script>Correct</font>");
            }
            else
            {
                out.print("        ");
                out.print("<font color=red face=Regular script>error</font>");
            }
            out.print("<p>");
        }
        stmt.close();
        con.close();
     %>
  </body>
</html>

 

 

 

select.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Selection parameter</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form id=form2 method=post action="index.jsp">
    <table align=center border=0>
    <tr>
        <td>Do you need to multiply and divide?:<td>
        <td>yes <input type=radio name=operator value="yes" checked>
                no <input type=radio name=operator value="no"></td>
    </tr>
    <tr>
        <td>Choose Integer or Score:<td>
        <td>integer<input type=radio name=operator1 value="integer" checked>
               Fraction<input type=radio name=operator1 value="Fraction"></td>
    </tr>
    <tr>
        <td>Number of operators:<td>
        <td><select name=number1 size=1>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>Select the range of operations:<td>
        <td>
            <select name=number2 size=1>
                <option>10</option>
                <option>50</option>
                <option>100</option>
            </select>
        </td>
    </tr>
    <tr>
    <td><input type=submit name=submit value=Next step ></td>
    </tr>
    </table>
    </form>  
    </body>
</html>

 

 

 

Operating results screenshot:

Summary experience:

Firstly, because there are many requirements for topics, it is very important to design ideas before programming, encapsulating them into classes, and some corresponding functions should be written into functions so that they can be flexibly invoked later. The process of calculating results is based on recursive method. Here is the rule of each calculation. Laws, find a similar place, in the implementation of recursive functions, at first there is always no result, but later found that there is no return value, so the return value in recursion is also very important; in the connection of the database, we need to pay attention to the configuration of jdbc. jar file, otherwise the database will fail to drive (it is easy to omit); in jsp, the return value is also very important. When using the loop structure generators, because we need to leave out the text box of the answer questions, we need to pay attention to each output formula in the loop, and output a text box accordingly. The main answer here is convenient, which makes the interface more beautiful and operable. Type conversion is involved in the process of transferring parameters in jsp, and we need to pay attention to it. Using javabean to encapsulate classes makes the idea clear and the structure clear. Most of the statements implemented by java code are written in. Java files. In. JSP files, the main parameters and interface design make the idea more logical.

psp table

Posted by Ifa on Wed, 11 Sep 2019 01:27:00 -0700