Advertisement
lgfjj

CS361Lecture1

Jan 27th, 2021
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. Tyheir Brooks
  2. Exercise 1
  3. ___________________________ ___________________________ ___________________________ _________________________
  4.  
  5. import java.awt.BorderLayout;
  6. import java.awt.EventQueue;
  7.  
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10. import javax.swing.border.EmptyBorder;
  11. import javax.swing.JTextField;
  12. import javax.swing.JLabel;
  13. import javax.swing.JButton;
  14. import java.awt.Color;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.ActionEvent;
  17.  
  18. public class Main extends JFrame {
  19.  
  20.     private JPanel contentPane;
  21.     private JTextField opA;
  22.     private JTextField opB;
  23.     private JTextField result;
  24.     private JButton myButton_1;
  25.     private JButton myButton_2;
  26.     private JButton myButton_3;
  27.     private JButton myButton_4;
  28.  
  29.     /**
  30.      * Launch the application.
  31.      */
  32.     public static void main(String[] args) {
  33.         EventQueue.invokeLater(new Runnable() {
  34.             public void run() {
  35.                 try {
  36.                     Main frame = new Main();
  37.                     frame.setVisible(true);
  38.                 } catch (Exception e) {
  39.                     e.printStackTrace();
  40.                 }
  41.             }
  42.         });
  43.     }
  44.  
  45.     /**
  46.      * Create the frame.
  47.      */
  48.     public Main() {
  49.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.         setBounds(100, 100, 450, 300);
  51.         contentPane = new JPanel();
  52.         contentPane.setBackground(Color.LIGHT_GRAY);
  53.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  54.         setContentPane(contentPane);
  55.         contentPane.setLayout(null);
  56.        
  57.         opA = new JTextField();
  58.         opA.setBounds(34, 79, 52, 45);
  59.         contentPane.add(opA);
  60.         opA.setColumns(10);
  61.        
  62.         opB = new JTextField();
  63.         opB.setBounds(146, 79, 52, 45);
  64.         contentPane.add(opB);
  65.         opB.setColumns(10);
  66.        
  67.         JButton myButton = new JButton("=");
  68.         myButton.addActionListener(new ActionListener() {
  69.             public void actionPerformed(ActionEvent arg0) {
  70.                 String x = opA.getText();
  71.                 String y = opB.getText();
  72.                 int a = Integer.parseInt(x);
  73.                 int b = Integer.parseInt(y);
  74.                 int c = a + b;
  75.                 result.setText("" + c);
  76.             }
  77.         });
  78.         myButton.setForeground(Color.BLACK);
  79.         myButton.setBackground(Color.GREEN);
  80.         myButton.setBounds(216, 88, 35, 29);
  81.         contentPane.add(myButton);
  82.        
  83.         result = new JTextField();
  84.         result.setBounds(263, 85, 57, 33);
  85.         contentPane.add(result);
  86.         result.setColumns(10);
  87.        
  88.         myButton_1 = new JButton("+");
  89.         myButton_1.addActionListener(new ActionListener() {
  90.             public void actionPerformed(ActionEvent arg0) {
  91.                 String x = opA.getText();
  92.                 String y = opB.getText();
  93.                 int a = Integer.parseInt(x);
  94.                 int b = Integer.parseInt(y);
  95.                 int c = a + b;
  96.                 if (b==0)
  97.                     result.setText("undef");
  98.                 else
  99.                 result.setText("" + c);
  100.             }
  101.         });
  102.         myButton_1.setForeground(Color.BLACK);
  103.         myButton_1.setBackground(Color.GREEN);
  104.         myButton_1.setBounds(98, 88, 35, 29);
  105.         contentPane.add(myButton_1);
  106.        
  107.         myButton_2 = new JButton("-");
  108.         myButton_2.addActionListener(new ActionListener() {
  109.             public void actionPerformed(ActionEvent arg0) {
  110.                 String x = opA.getText();
  111.                 String y = opB.getText();
  112.                 int a = Integer.parseInt(x);
  113.                 int b = Integer.parseInt(y);
  114.                 int c = a - b;
  115.                 result.setText("" + c);
  116.             }
  117.         });
  118.         myButton_2.setForeground(Color.BLACK);
  119.         myButton_2.setBackground(Color.GREEN);
  120.         myButton_2.setBounds(98, 129, 35, 29);
  121.         contentPane.add(myButton_2);
  122.        
  123.         myButton_3 = new JButton("*");
  124.         myButton_3.addActionListener(new ActionListener() {
  125.             public void actionPerformed(ActionEvent arg0) {
  126.                 String x = opA.getText();
  127.                 String y = opB.getText();
  128.                 int a = Integer.parseInt(x);
  129.                 int b = Integer.parseInt(y);
  130.                 int c = a * b;
  131.                 result.setText("" + c);
  132.             }
  133.         });
  134.         myButton_3.setForeground(Color.BLACK);
  135.         myButton_3.setBackground(Color.GREEN);
  136.         myButton_3.setBounds(98, 170, 35, 29);
  137.         contentPane.add(myButton_3);
  138.        
  139.         myButton_4 = new JButton("/");
  140.         myButton_4.addActionListener(new ActionListener() {
  141.             public void actionPerformed(ActionEvent arg0) {
  142.                 String x = opA.getText();
  143.                 String y = opB.getText();
  144.                 if (y=="0")
  145.                     result.setText("undef");
  146.                 else {
  147.                 int a = Integer.parseInt(x);
  148.                 int b = Integer.parseInt(y);
  149.                 int c = a / b;
  150.                 result.setText("" + c);}
  151.             }
  152.         });
  153.         myButton_4.setForeground(Color.BLACK);
  154.         myButton_4.setBackground(Color.GREEN);
  155.         myButton_4.setBounds(98, 211, 35, 29);
  156.         contentPane.add(myButton_4);
  157.     }
  158. }
  159.  
  160.  
  161. Exercise 2
  162. ___________________________ ___________________________ ___________________________ _________________________
  163. public class Main {
  164.     public static void main(String args[])
  165.     {
  166.         ThreadA thd1 = new ThreadA();
  167.         ThreadB thd2 = new ThreadB();
  168.         ThreadC thd3 = new ThreadC();
  169.         thd1.start();
  170.         thd2.start();
  171.         thd3.start();
  172.         System.out.println("I am the Main thread");
  173.     }
  174. }
  175. /*
  176. I am Tyheir
  177. I am ThreadB
  178. I am the Main thread
  179. I am ThreadA
  180.  
  181. I am ThreadA
  182. I am Tyheir
  183. I am ThreadB
  184. I am the Main thread
  185.  
  186. I am ThreadB
  187. I am Tyheir
  188. I am ThreadA
  189. I am the Main thread
  190.  
  191.  
  192. */
  193.  
  194.  
  195. public class ThreadA extends Thread {
  196.     public void run()
  197.     {
  198.         System.out.println("I am ThreadA");
  199.     }
  200. }
  201.  
  202.  
  203. public class ThreadB extends Thread{
  204.     public void run()
  205.     {
  206.         System.out.println("I am ThreadB ");
  207.     }
  208. }
  209.  
  210.  
  211. public class ThreadC extends Thread{
  212.     public void run()
  213.     {
  214.         System.out.println("I am Tyheir");
  215.     }
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement