SHARE
TWEET

Untitled

a guest Feb 27th, 2020 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######### FRAME CLASS ############
  2.  
  3. public class OurFrame extends java.awt.Frame {
  4.  
  5.     double a, b, c;
  6.     String n1, n2;
  7.    
  8.     /**
  9.      * Creates new form OurFrame
  10.      */
  11.     public OurFrame() {
  12.         initComponents();
  13.     }
  14.  
  15.     /**
  16.      * This method is called from within the constructor to initialize the form.
  17.      * WARNING: Do NOT modify this code. The content of this method is always
  18.      * regenerated by the Form Editor.
  19.      */
  20.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  21.     private void initComponents() {
  22.  
  23.         Text1 = new java.awt.Label();
  24.         Text2 = new java.awt.Label();
  25.         textbox2 = new java.awt.TextField();
  26.         textbox1 = new java.awt.TextField();
  27.         add_button = new java.awt.Button();
  28.         div_button = new java.awt.Button();
  29.         sub_button = new java.awt.Button();
  30.         mul_button = new java.awt.Button();
  31.         label1 = new java.awt.Label();
  32.  
  33.         addWindowListener(new java.awt.event.WindowAdapter() {
  34.             public void windowClosing(java.awt.event.WindowEvent evt) {
  35.                 exitForm(evt);
  36.             }
  37.         });
  38.  
  39.         Text1.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
  40.         Text1.setText("Second Number");
  41.  
  42.         Text2.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
  43.         Text2.setText("First Number");
  44.  
  45.         textbox2.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
  46.         textbox2.addActionListener(new java.awt.event.ActionListener() {
  47.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  48.                 textbox2ActionPerformed(evt);
  49.             }
  50.         });
  51.  
  52.         textbox1.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
  53.         textbox1.addActionListener(new java.awt.event.ActionListener() {
  54.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  55.                 textbox1ActionPerformed(evt);
  56.             }
  57.         });
  58.  
  59.         add_button.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
  60.         add_button.setLabel("Add");
  61.         add_button.addActionListener(new java.awt.event.ActionListener() {
  62.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  63.                 add_buttonActionPerformed(evt);
  64.             }
  65.         });
  66.  
  67.         div_button.setActionCommand("Division");
  68.         div_button.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
  69.         div_button.setLabel("Divide");
  70.         div_button.addActionListener(new java.awt.event.ActionListener() {
  71.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  72.                 div_buttonActionPerformed(evt);
  73.             }
  74.         });
  75.  
  76.         sub_button.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
  77.         sub_button.setLabel("Subtract");
  78.         sub_button.addActionListener(new java.awt.event.ActionListener() {
  79.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  80.                 sub_buttonActionPerformed(evt);
  81.             }
  82.         });
  83.  
  84.         mul_button.setActionCommand("Multiplication");
  85.         mul_button.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
  86.         mul_button.setLabel("Multiply");
  87.         mul_button.addActionListener(new java.awt.event.ActionListener() {
  88.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  89.                 mul_buttonActionPerformed(evt);
  90.             }
  91.         });
  92.  
  93.         label1.setAlignment(java.awt.Label.CENTER);
  94.         label1.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
  95.         label1.setText("Result");
  96.  
  97.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  98.         this.setLayout(layout);
  99.         layout.setHorizontalGroup(
  100.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  101.             .addGroup(layout.createSequentialGroup()
  102.                 .addContainerGap()
  103.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  104.                     .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  105.                     .addComponent(div_button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  106.                     .addComponent(add_button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  107.                     .addComponent(sub_button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  108.                     .addComponent(mul_button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  109.                     .addGroup(layout.createSequentialGroup()
  110.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  111.                             .addComponent(Text1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  112.                             .addComponent(Text2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  113.                         .addGap(18, 18, 18)
  114.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  115.                             .addComponent(textbox1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
  116.                             .addComponent(textbox2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
  117.                 .addContainerGap())
  118.         );
  119.         layout.setVerticalGroup(
  120.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  121.             .addGroup(layout.createSequentialGroup()
  122.                 .addContainerGap()
  123.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  124.                     .addComponent(Text2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  125.                     .addComponent(textbox1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  126.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  127.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  128.                     .addComponent(textbox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  129.                     .addComponent(Text1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  130.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  131.                 .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  132.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  133.                 .addComponent(add_button, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
  134.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  135.                 .addComponent(sub_button, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
  136.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  137.                 .addComponent(mul_button, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
  138.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  139.                 .addComponent(div_button, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
  140.                 .addContainerGap())
  141.         );
  142.  
  143.         div_button.getAccessibleContext().setAccessibleName("Division");
  144.  
  145.         pack();
  146.     }// </editor-fold>//GEN-END:initComponents
  147.  
  148.     /**
  149.      * Exit the Application
  150.      */
  151.     private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
  152.         System.exit(0);
  153.     }//GEN-LAST:event_exitForm
  154.  
  155.     private void textbox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textbox1ActionPerformed
  156.         // TODO add your handling code here:
  157.        
  158.     }//GEN-LAST:event_textbox1ActionPerformed
  159.  
  160.     private void add_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add_buttonActionPerformed
  161.         // TODO add your handling code here:
  162.         n1 = textbox1.getText();
  163.         a = Double.valueOf(n1);
  164.         n2 = textbox2.getText();
  165.         b = Double.valueOf(n2);
  166.         Addition add = new Addition(a, b);
  167.         Double x = add.getAdd();
  168.         String i = x.toString();
  169.         label1.setText(a + " + " + b + " = " + i);
  170.        
  171.     }//GEN-LAST:event_add_buttonActionPerformed
  172.  
  173.     private void sub_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sub_buttonActionPerformed
  174.         // TODO add your handling code here:
  175.         n1 = textbox1.getText();
  176.         a = Double.valueOf(n1);
  177.         n2 = textbox2.getText();
  178.         b = Double.valueOf(n2);
  179.         Subtraction sub = new Subtraction(a, b);
  180.         Double x = sub.getSub();
  181.         String i = x.toString();
  182.         label1.setText(a + " - " + b + " = " + i);
  183.     }//GEN-LAST:event_sub_buttonActionPerformed
  184.  
  185.     private void mul_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mul_buttonActionPerformed
  186.         // TODO add your handling code here:
  187.         n1 = textbox1.getText();
  188.         a = Double.valueOf(n1);
  189.         n2 = textbox2.getText();
  190.         b = Double.valueOf(n2);
  191.         Multiply mul = new Multiply(a, b);
  192.         Double x = mul.getMul();
  193.         String i = x.toString();
  194.         label1.setText(a + " x " + b + " = " + i);
  195.     }//GEN-LAST:event_mul_buttonActionPerformed
  196.  
  197.     private void div_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_div_buttonActionPerformed
  198.         // TODO add your handling code here:
  199.         n1 = textbox1.getText();
  200.         a = Double.valueOf(n1);
  201.         n2 = textbox2.getText();
  202.         b = Double.valueOf(n2);
  203.         if(a < 1 || b < 1) {
  204.             label1.setText("ERROR: Division by Zero");
  205.         } else {
  206.             Division div = new Division(a, b);
  207.             Double x = div.getDiv();
  208.             String i = x.toString();
  209.             label1.setText(a + " รท " + b + " = " + i);
  210.         }
  211.     }//GEN-LAST:event_div_buttonActionPerformed
  212.  
  213.     private void textbox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textbox2ActionPerformed
  214.         // TODO add your handling code here:
  215.     }//GEN-LAST:event_textbox2ActionPerformed
  216.  
  217.     /**
  218.      * @param args the command line arguments
  219.      */
  220.     public static void main(String args[]) {
  221.         java.awt.EventQueue.invokeLater(new Runnable() {
  222.             public void run() {
  223.                 new OurFrame().setVisible(true);
  224.             }
  225.         });
  226.     }
  227.  
  228.  
  229.     // Variables declaration - do not modify//GEN-BEGIN:variables
  230.     private java.awt.Label Text1;
  231.     private java.awt.Label Text2;
  232.     private java.awt.Button add_button;
  233.     private java.awt.Button div_button;
  234.     private java.awt.Label label1;
  235.     private java.awt.Button mul_button;
  236.     private java.awt.Button sub_button;
  237.     private java.awt.TextField textbox1;
  238.     private java.awt.TextField textbox2;
  239.     // End of variables declaration//GEN-END:variables
  240. }
  241.  
  242.  
  243.  
  244.  
  245. ###########################################################################################################################
  246.  
  247. ####### CALCULAT CLASS(superclass) #########
  248.  
  249. public class Calculat {
  250.    
  251.     double num1, num2;
  252.    
  253.     public Calculat(double num1, double num2) {
  254.         this.num1 = num1;
  255.         this.num2 = num2;
  256.     }
  257.    
  258.    
  259. }
  260.  
  261.  
  262.  
  263.  
  264. ###########################################################################################################################
  265.  
  266. ######### ADDITION CLASS #############
  267.  
  268. public class Addition extends Calculat {
  269.    
  270.     public Addition(double num1, double num2) {
  271.         super(num1, num2);
  272.     }
  273.    
  274.     public double getAdd() {
  275.         return num1 + num2;
  276.     }
  277.    
  278.    
  279. }
  280.  
  281.  
  282.  
  283. ##########################################################################################################################
  284.  
  285. ####### SUBTRACTION CLASS ###########
  286.  
  287. public class Subtraction extends Calculat {
  288.    
  289.     public Subtraction(double num1, double num2) {
  290.         super(num1, num2);
  291.     }
  292.    
  293.     public double getSub() {
  294.         return num1 - num2;
  295.     }
  296.    
  297. }
  298.  
  299. ##########################################################################################################################
  300.  
  301. ######### MULTIPLICATION CLASS ###########
  302.  
  303. public class Multiply extends Calculat {
  304.    
  305.     public Multiply(double num1, double num2) {
  306.         super(num1, num2);
  307.     }
  308.    
  309.     public double getMul() {
  310.         return num1 * num2;
  311.     }
  312.    
  313. }
  314.  
  315.  
  316.  
  317. ###########################################################################################################################
  318.  
  319. ######### DIVISION CLASS ###########
  320.  
  321. public class Division extends Calculat {
  322.    
  323.     public Division(double num1, double num2) {
  324.         super(num1, num2);
  325.     }
  326.    
  327.     public double getDiv() {
  328.         return num1 / num2;
  329.     }
  330.    
  331. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top