Java Web Classprogram_3

Source: Internet
Author: User
Tags gettext stub

In view of the arithmetic, further modification is made, and the project is disassembled into random number generation and random operator Generation and operation based on the idea of class.

Random numbers are expressed in numerator/denominator, which can be used to represent all types of numbers. such as fractions, integers, decimals

Because of the new data type of operation, it is necessary to write some computational principles.

The code is as follows:

Centrue.class

Package Pro2;

Import Java.awt.BorderLayout;

Import Javax.swing.JFrame;


public class Centrue extends jframe{
Northpanel North;
Public Centrue () {
North=new Northpanel ();
This.setsize (800, 600);
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.add (north, Borderlayout.north);
This.setvisible (TRUE);
}
public static void Main (string[] args) {
Centrue c=new centrue ();

}

}

Count.java

Package Pro2;

Import java.util.LinkedList;
Import Java.util.Random;
Import Java.util.Stack;

public class Count {
int min;
int Max;
int list[];
int bound; Indicates that there are several type numbers in the list
Boolean isbracket; Whether there are parentheses
int number; Digital Range 2-5
Linkedlist<figure> figure;
Char[] operatelist;
String Shizi;
String Daan;
Public Count (int min, int max, int list[], int bound, int number,boolean isbracket) {

This.max = max;
this.min = min;
This.list = list;
This.bound = bound;
This.number = number;
This.isbracket = Isbracket;
Figure=new linkedlist<figure> ();
This.getcompound ();
This.getstring ();
System.out.println (This.shizi);
This.calculation ();
}

Get the evaluation stack, including parentheses
public void Getcompound () {
int braindex[] ={0,0,0,0};
int branum=0;
Random ran = new random ();
if (!isbracket) {
Operatelist=new char[number+1];
}
if (number < 6 && number >= 3 && isbracket) {
Branum = 2;
Operatelist=new char[number+branum+1];

Braindex[0] = Ran.nextint (number-1) + 1;
Operatelist[braindex[0]]= ' (';
BRAINDEX[1] = Ran.nextint (number-braindex[0]) + braindex[0]+2;
if (braindex[0]==1&&braindex[1]==branum+number-1) {
braindex[1]--;
}
Operatelist[braindex[1]]= ') ';
}
Limited Digital insertion Limit
Six-digit parenthesis insertion waiting to be implemented
else if (number >= 6 && number >= 3 && isbracket) {
}
Digital in queue
for (int i = 0; i < number; i++) {
int type = List[ran.nextint (bound)];
Figure.add (New figure (Min, max, type));
}
Operator generation
for (int i = 1; i < Number+branum; i++) {
int type = Ran.nextint (4);
Char a= ' + ';
Switch (type) {
Case 0:
A = ' + ';
Break
Case 1:
A = '-';
Break
Case 2:
A = ' * ';
Break
Case 3:
A = '/';
Break
}
Boolean flag=true;
for (int j=0;j<branum;j++) {
if (braindex[j]==i) {
Flag=false;
}
}
if (flag) {
Operatelist[i]=a;
}
}
Add two magical characters before and after
operatelist[0]= ' # ';
operatelist[number+branum]= ' # ';
}
public void Show () {
SYSTEM.OUT.PRINTLN ("Array of Operators");
for (int i=0;i<operatelist.length;i++) {
System.out.println (Operatelist[i]);
}
for (int i=0;i<figure.size (); i++) {
System.out.println (Figure.get (i));
}
}
public void getString () {
String shi= "";
int i=0;
int j=0;
while (true) {
SHI=SHI+OPERATELIST[J];
if (operatelist[j+1]== ' (') {
j + +;
SHI=SHI+OPERATELIST[J];
}
j + +;
Shi=shi+figure.get (i);
if (operatelist[j]== ') ') {
SHI=SHI+OPERATELIST[J];
j + +;
}
i++;
if (j==operatelist.length-1) {
Break
}
}
Shizi=shi;
}
Public String toString () {
return Shizi;
}
public void calculation () {
Stack<figure> fistack=new stack<figure> (); Figure Data Stack
Stackchar chstack=new Stackchar (); char Data stack
int charindex=0;
int figureindex=0;
#字符压入栈中
Chstack.push (operatelist[charindex++]);
The first character is pressed into a
if (operatelist[charindex]!= ' (') {
Chstack.push (operatelist[charindex++]);
Prevent * (
if (operatelist[charindex]== ' (') {
Chstack.push (operatelist[charindex++]);
}
}
else{
Chstack.push (operatelist[charindex++]);
Chstack.push (operatelist[charindex++]);
}
Two digital presses into the stack
Fistack.add (Figure.get (figureindex++));
Fistack.add (Figure.get (figureindex++));
while (true) {
if (This.compare (Chstack.gettop (), Operatelist[charindex])) {

System.out.println ("Tan stack operation");
System.out.println ("Number of remaining digits" +fistack.size ());
System.out.println ("Number of remaining characters" +chstack.size ());
System.out.println (Figureindex);
Char Ope=chstack.pop ();
if (ope== ' (') {
charindex++;
}
else{
Figure F1=fistack.pop ();
Figure F2=fistack.pop ();
Fistack.push (Simplecount.count (F1, F2, ope));
}
}
else{
Chstack.push (operatelist[charindex++]);

System.out.println ("Into the stack operation");
System.out.println ("Number of remaining digits" +fistack.size ());
System.out.println ("Number of remaining characters" +chstack.size ());
System.out.println (Figureindex);


Fistack.push (Figure.get (figureindex++));
if (operatelist[charindex]== ' (') {
Chstack.push (operatelist[charindex++]);
}
}
if (Fistack.size () ==1&&figureindex==figure.size ()) {
Figure FF;
Ff=fistack.pop ();
ff.type=1;
Ff.huajian ();
Daan=ff.tostring ();
Break
}
}

}
public boolean compare (char A,char b) {
int a1=0,b1=0;
if (a== ' + ' | | a== '-') {
a1=2;
}
if (a== ' * ' | | a== '/') {
a1=3;
}
if (a== ' (') {
A1=1;
}
if (a== ' # ') {
a1=0;
}
if (b== ') ' | | b== ' # ') {
B1=1;
}
if (b== ' + ' | | b== '-') {
b1=2;
}
if (b== ' * ' | | b== '/') {
b1=3;
}
if (b== ' (') {
b1=4;
}
if (A1&GT;=B1) {
return true;
}
else{
return false;
}
}
}

Figure.java

/**
* min Returns the minimum value of the number Max maximum type return number type
* Type=0 returns an integer
*/
Package Pro2;

Import Java.util.Random;

public class Figure {
int Zi;
int mu;

int min;
int Max;

int type;
public figure (int min,int max,int type) {
This.max=max;
This.min=min;
This.type=type;
This.returnfigure ();
}
public figure (int zi,int mu) {
This.zi=zi;
THIS.MU=MU;
}
public void Returnfigure () {
Random ran=new random ();
Switch (type) {
generating integers
Case 0:
zi= (int) ((max-min) *math.random ()) +min;
Mu=1;
Break
Generate scores
Case 1:
Zi=ran.nextint ((int) ((max-min) *math.random ()) +min);
Mu=ran.nextint (zi*10+1) +1;
System.out.println ("zi=" +zi+ "mu=" +MU);
This.huajian ();
Break
Generate double
Case 2:
zi= (int) ((((max-min) *math.random ()) +min) *100);
mu=100;
Break
}
}
public void Huajian () {
int chu=1;
for (int i=1;i<=math.min (Zi, mu); i++) {
if (zi%i==0&&mu%i==0) {
Chu=i;
}
}
Zi=zi/chu;
Mu=mu/chu;
}
Public String toString () {
Switch (type) {
Case 0:
return string.valueof (zi);
Case 1:
return zi+ "%" +MU;
Case 2:
Double ok= ((double) zi)/mu;
return ok+ "";
Default
return null;
}
}
}

Northpanel.java

Package Pro2;

Import Java.awt.BorderLayout;
Import Java.awt.Checkbox;
Import Java.awt.CheckboxGroup;
Import java.awt.Component;
Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;

Import Javax.swing.JButton;
Import Javax.swing.JCheckBox;
Import Javax.swing.JLabel;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;
Import Javax.swing.JRadioButton;
Import Javax.swing.JTextArea;
Import Javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;

public class Northpanel extends JPanel implements actionlistener{

//Topic data
int min=0;
int max=0;
int list[]=new int[4];
int bound=0;//indicates that there are several type numbers in the list
Boolean isbracket=false;//whether there are parentheses
int number=4;//numeric range 2-5


JPanel NORTHJP;
Southpanel South;
Jcheckbox r1=new jcheckbox ("integer", true);
Jcheckbox r2=new jcheckbox ("score", false);
Jcheckbox r3=new jcheckbox ("decimal", false);

Jradiobutton jr=new Jradiobutton ("with parentheses", false);

JLabel jl=new JLabel ("Number of topics");
JTextArea num=new JTextArea ("4");

JLabel jll=new JLabel ("Maximum minimum");
JTextArea mintext=new JTextArea ("10");
JTextArea maxtext=new jtextarea ("100");

JButton jb=new JButton ("Out topic");
Public Northpanel () {
This.setlayout (New BorderLayout ());
Northjp=new JPanel ();
Northjp.setlayout (New GridLayout (2,6));
Northjp.add (R1);
Northjp.add (R2);
Northjp.add (R3);
Northjp.add (JR);
Northjp.add (JL);
Northjp.add (num);
Northjp.add (JLL);
Northjp.add (Mintext);
Northjp.add (Maxtext);
Northjp.add (JB);

This.add (NORTHJP, Borderlayout.north);

Jb.addactionlistener (this);
}
public void Chuti () {
if (south!=null) {
South.setvisible (FALSE);
}
South=new southpanel (min, max, list, bound, number, isbracket);
This.add (south, Borderlayout.south);
This.setvisible (FALSE);
This.setvisible (TRUE);
}
@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
Min=integer.parseint (Mintext.gettext ());
Max=integer.parseint (Maxtext.gettext ());
Number=integer.parseint (Num.gettext ());
Bound=0;
if (jr.isselected ()) {
Isbracket=true;
}
if (r1.isselected ()) {
list[bound++]=0;
}
if (r2.isselected ()) {
List[bound++]=1;
}
if (r3.isselected ()) {
list[bound++]=2;
}

String message = "";
Boolean flag=true;
if (Min>max) {
Message=message+ "; should be left small and big";
Flag=false;
}
if (number<2| | NUMBER&GT;5) {
Message=message+ "; The number should be less than 6 or greater than 2";
Flag=false;
}
if (bound<1) {
Message=message+ "; Select at least one data, such as an integer";
Flag=false;
}
if (flag) {
This.chuti ();
}
else{
Joptionpane.showmessagedialog (null, message);
}
}
}

Simplecount.java

Package Pro2;

public class Simplecount {
public static figure count (Figure F1, figure F2, char a) {
Figure value;
int zi=1;
int mu=1;
Switch (a) {
Case ' + ':
Zi=f1.zi*f2.mu+f2.zi*f1.mu;
Mu=f1.mu*f2.mu;
Break
Case '-':
Zi=f2.zi*f1.mu-f1.zi*f2.mu;
Mu=f1.mu*f2.mu;
Break
Case ' * ':
Zi=f1.zi*f2.zi;
Mu=f1.mu*f2.mu;
Break
Case '/':
Zi=f2.zi*f1.mu;
Mu=f2.mu*f1.zi;
Break
}
Value=new figure (Zi, mu);
return value;
}
}

Southpanel.java

Package Pro2;

Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;

Import Javax.swing.JButton;
Import Javax.swing.JLabel;
Import Javax.swing.JPanel;
Import Javax.swing.JTextArea;

public class Southpanel extends JPanel implements actionlistener{
int min;
int Max;
int list[];
int bound; Indicates that there are several type numbers in the list
Boolean isbracket; Whether there are parentheses
int number; Digital Range 2-5
String Count[]=new string[30];
String Result[]=new string[30];

JLabel Jlcount[]=new jlabel[30];
JLabel Jresult[]=new jlabel[30];
JTextArea Janswer[]=new jtextarea[30];

JButton jb=new JButton ("Submit Answer");

Public southpanel (int min, int max, int list[], int bound, int number,boolean isbracket) {
This.max = max;
this.min = min;
This.list = list;
This.bound = bound;
This.number = number;
This.isbracket = Isbracket;
This.setlayout (New GridLayout (16,6));
This.addcount ();
Jb.addactionlistener (this);
This.setvisible (FALSE);
This.setvisible (TRUE);
}

public void Addcount () {

for (int i=0;i<30;i++) {
Count c=new count (min, max, list, bound, number, isbracket);
Count[i]=c.tostring ();
Result[i]=c.daan;
Jlcount[i]=new JLabel (Count[i]);
This.add (Jlcount[i]);
Janswer[i]=new JTextArea ();
This.add (Janswer[i]);
Jresult[i]=new JLabel ("");
This.add (Jresult[i]);
}

This.add (JB);
}
public void result () {
for (int i=0;i<30;i++) {
if (!result[i].equals (Janswer[i].gettext ())) {
Jresult[i].settext (Result[i]);
}
}
}

@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
This.result ();
}
}

Stackchar.java

Package Pro2;

public class Stackchar {
Char a[];
int index;
Public Stackchar () {
A=new CHAR[10];
index=0;
}
public void push (char b) {
A[index]=b;
index++;
}
Public char pop () {
Char K=a[index-1];
index--;
return k;
}
Public Char GetTop () {
return a[index-1];
}
public int size () {
return index;
}
}

Java Web Classprogram_3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.