📄 nj.java
字号:
import java.applet.*;import java.awt.*;import java.awt.event.*;import java.math.*;/* Nine-J symbol calculator. */public class NJ extends Applet implements ActionListener, Runnable { Thread playing; // declare our A.M values out here BigRational j1,j2,j3,j4,j5,j6,j7,j8,j9; // Let's allow the user to input the numbers as reals. // a double will be just fine here. double hj1,hj2,hj3,hj4,hj5,hj6,hj7,hj8,hj9; // the approximate answer as a 'double' double approxanswer; // panels for the rows of GUI things: // row one has some text: Panel row1 = new Panel(); Label instructions = new Label ("Enter the values of a.m. in the boxes below..",Label.CENTER); // row two has j1-3 Panel row2 = new Panel(); Label j1label = new Label("j1=",Label.RIGHT); TextField j1field = new TextField(4); Label j2label = new Label("j2=",Label.RIGHT); TextField j2field = new TextField(4); Label j3label = new Label("j3=",Label.RIGHT); TextField j3field = new TextField(4); // row three has j4-6 Panel row3 = new Panel(); Label j4label = new Label("j4=",Label.RIGHT); TextField j4field = new TextField(4); Label j5label = new Label("j5=",Label.RIGHT); TextField j5field = new TextField(4); Label j6label = new Label("j6=",Label.RIGHT); TextField j6field = new TextField(4); // row four has j7-9 Panel row4 = new Panel(); Label j7label = new Label("j7=",Label.RIGHT); TextField j7field = new TextField(4); Label j8label = new Label("j8=",Label.RIGHT); TextField j8field = new TextField(4); Label j9label = new Label("j9=",Label.RIGHT); TextField j9field = new TextField(4); // row five has the buttons Panel row5 = new Panel(); Button go = new Button("Go!"); Button clear = new Button("Clear"); // row six has the answer Panel row6 = new Panel(); TextField outfield = new TextField(80); // row seven has the approximate answer Panel row7 = new Panel(); Label approxlabel = new Label("approx:",Label.RIGHT); TextField approxfield = new TextField(40); public void init() { Font myfont = new Font("SansSerif", Font.PLAIN, 12); setFont(myfont); // set up the layout of the GUI GridLayout mylayout = new GridLayout(6,1,1,1); setLayout(mylayout); FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 1,1); row1.setLayout(layout1); row1.add(instructions); //add(row1); // do "j" row FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER, 1, 1); row2.setLayout(layout2); row2.add(j1label); row2.add(j1field); row2.add(j2label); row2.add(j2field); row2.add(j3label); row2.add(j3field); add(row2); FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 1, 1); row3.setLayout(layout3); row3.add(j4label); row3.add(j4field); row3.add(j5label); row3.add(j5field); row3.add(j6label); row3.add(j6field); add(row3); FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER, 1, 1); row4.setLayout(layout4); row4.add(j7label); row4.add(j7field); row4.add(j8label); row4.add(j8field); row4.add(j9label); row4.add(j9field); add(row4); FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER, 1, 1); row5.setLayout(layout5); row5.add(go); row5.add(clear); add(row5); FlowLayout layout6 = new FlowLayout(FlowLayout.CENTER, 1, 1); row6.setLayout(layout6); outfield.setEditable(false); row6.add(outfield); add(row6); FlowLayout layout7 = new FlowLayout(FlowLayout.CENTER, 1, 1); row7.setLayout(layout7); approxfield.setEditable(false); row7.add(approxlabel); row7.add(approxfield); add(row7); // done setting up the layout. Now add some listeners (to the buttons) go.addActionListener(this); clear.addActionListener(this); } public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if(command == "Go!") { playing = new Thread(this); playing.start(); go.setEnabled(false); clear.setEnabled(false); outfield.setText(""); } if(command == "Clear") clearAllFields(); } void clearAllFields(){ j1field.setText(null); j2field.setText(null); j3field.setText(null); j4field.setText(null); j5field.setText(null); j6field.setText(null); j7field.setText(null); j8field.setText(null); j9field.setText(null); outfield.setText(null); approxfield.setText(null); } public void run() { // This is where the thread starts. final BigRational ONE_R = new BigRational(1); // We get the input from the input boxes and convert to the // BigInteger type. // This can throw an exception if bad input is given, so let's catch // it if we can.. try { hj1 = (new java.lang.Double(j1field.getText())).doubleValue(); hj2 = (new java.lang.Double(j2field.getText())).doubleValue(); hj3 = (new java.lang.Double(j3field.getText())).doubleValue(); hj4 = (new java.lang.Double(j4field.getText())).doubleValue(); hj5 = (new java.lang.Double(j5field.getText())).doubleValue(); hj6 = (new java.lang.Double(j6field.getText())).doubleValue(); hj7 = (new java.lang.Double(j7field.getText())).doubleValue(); hj8 = (new java.lang.Double(j8field.getText())).doubleValue(); hj9 = (new java.lang.Double(j9field.getText())).doubleValue(); } catch (java.lang.NumberFormatException e) { outfield.setText("All input must be numerical"); go.setEnabled(true); clear.setEnabled(true); return; } // convert decimal input to BigRational. j1 = new BigRational(new BigInteger(Long.toString(Math.round(2.*hj1))), new BigInteger("2")); j2 = new BigRational(new BigInteger(Long.toString(Math.round(2.*hj2))), new BigInteger("2")); j3 = new BigRational(new BigInteger(Long.toString(Math.round(2.*hj3))), new BigInteger("2")); j4 = new BigRational(new BigInteger(Long.toString(Math.round(2.*hj4))), new BigInteger("2")); j5 = new BigRational(new BigInteger(Long.toString(Math.round(2.*hj5))), new BigInteger("2")); j6 = new BigRational(new BigInteger(Long.toString(Math.round(2.*hj6))), new BigInteger("2")); j7 = new BigRational(new BigInteger(Long.toString(Math.round(2.*hj7))), new BigInteger("2")); j8 = new BigRational(new BigInteger(Long.toString(Math.round(2.*hj8))), new BigInteger("2")); j9 = new BigRational(new BigInteger(Long.toString(Math.round(2.*hj9))), new BigInteger("2")); // Conditions: the j's must satisfy some triangle relations.. if(Math.abs(hj1-hj2) > hj3 || Math.abs(hj1+hj2) < hj3) { outfield.setText("0 ( j1, j2 and j3 do not satisfy the triangle"+ " relation)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if(Math.abs(hj4-hj5) > hj6 || Math.abs(hj4+hj5) < hj6) { outfield.setText("0 ( j4, j5 and j6 do not satisfy the triangle"+ " relation)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if(Math.abs(hj7-hj8) > hj9 || Math.abs(hj7+hj8) < hj9) { outfield.setText("0 ( j7, j8 and j9 do not satisfy the triangle"+ " relation)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if(Math.abs(hj1-hj4) > hj7 || Math.abs(hj1+hj4) < hj7) { outfield.setText("0 ( j1, j4 and j7 do not satisfy the triangle"+ " relation)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if(Math.abs(hj2-hj5) > hj8 || Math.abs(hj2+hj5) < hj8) { outfield.setText("0 ( j2, j5 and j8 do not satisfy the triangle"+ " relation)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if(Math.abs(hj3-hj6) > hj9 || Math.abs(hj3+hj6) < hj9) { outfield.setText("0 ( j3, j6 and j9 do not satisfy the triangle"+ " relation)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } // and each of the above triads must be able to couple to each other // in the sense that there is not a half-unit of angular momentum // left over. if( (new Double(hj1+hj2+hj3)).intValue()*2 != (new Double(2*(hj1+hj2+hj3))).intValue() ) { outfield.setText("0 (j1 and j2 cannot couple to j3)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if( (new Double(hj4+hj5+hj6)).intValue()*2 != (new Double(2*(hj4+hj5+hj6))).intValue() ) { outfield.setText("0 (j4 and j5 cannot couple to j6)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if( (new Double(hj7+hj8+hj9)).intValue()*2 != (new Double(2*(hj7+hj8+hj9))).intValue() ) { outfield.setText("0 (j7 and j8 cannot couple to j9)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if( (new Double(hj1+hj4+hj7)).intValue()*2 != (new Double(2*(hj1+hj4+hj7))).intValue() ) { outfield.setText("0 (j1 and j4 cannot couple to j7)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if( (new Double(hj2+hj5+hj8)).intValue()*2 != (new Double(2*(hj2+hj5+hj8))).intValue() ) { outfield.setText("0 (j2 and j5 cannot couple to j8)"); approxfield.setText("0.0"); go.setEnabled(true); clear.setEnabled(true); return; } if( (new Double(hj3+hj6+hj9)).intValue()*2 != (new Double(2*(hj3+hj6+hj9))).intValue() ) { outfield.setText("0 (j1 and j2 cannot couple to j3)"); approxfield.setText("0.0");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -