Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Tyheir Brooks
- Exercise 1
- ___________________________ ___________________________ ___________________________ _________________________
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import javax.swing.JButton;
- import java.awt.Color;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class Main extends JFrame {
- private JPanel contentPane;
- private JTextField opA;
- private JTextField opB;
- private JTextField result;
- private JButton myButton_1;
- private JButton myButton_2;
- private JButton myButton_3;
- private JButton myButton_4;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Main frame = new Main();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Main() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBackground(Color.LIGHT_GRAY);
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- opA = new JTextField();
- opA.setBounds(34, 79, 52, 45);
- contentPane.add(opA);
- opA.setColumns(10);
- opB = new JTextField();
- opB.setBounds(146, 79, 52, 45);
- contentPane.add(opB);
- opB.setColumns(10);
- JButton myButton = new JButton("=");
- myButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String x = opA.getText();
- String y = opB.getText();
- int a = Integer.parseInt(x);
- int b = Integer.parseInt(y);
- int c = a + b;
- result.setText("" + c);
- }
- });
- myButton.setForeground(Color.BLACK);
- myButton.setBackground(Color.GREEN);
- myButton.setBounds(216, 88, 35, 29);
- contentPane.add(myButton);
- result = new JTextField();
- result.setBounds(263, 85, 57, 33);
- contentPane.add(result);
- result.setColumns(10);
- myButton_1 = new JButton("+");
- myButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String x = opA.getText();
- String y = opB.getText();
- int a = Integer.parseInt(x);
- int b = Integer.parseInt(y);
- int c = a + b;
- if (b==0)
- result.setText("undef");
- else
- result.setText("" + c);
- }
- });
- myButton_1.setForeground(Color.BLACK);
- myButton_1.setBackground(Color.GREEN);
- myButton_1.setBounds(98, 88, 35, 29);
- contentPane.add(myButton_1);
- myButton_2 = new JButton("-");
- myButton_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String x = opA.getText();
- String y = opB.getText();
- int a = Integer.parseInt(x);
- int b = Integer.parseInt(y);
- int c = a - b;
- result.setText("" + c);
- }
- });
- myButton_2.setForeground(Color.BLACK);
- myButton_2.setBackground(Color.GREEN);
- myButton_2.setBounds(98, 129, 35, 29);
- contentPane.add(myButton_2);
- myButton_3 = new JButton("*");
- myButton_3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String x = opA.getText();
- String y = opB.getText();
- int a = Integer.parseInt(x);
- int b = Integer.parseInt(y);
- int c = a * b;
- result.setText("" + c);
- }
- });
- myButton_3.setForeground(Color.BLACK);
- myButton_3.setBackground(Color.GREEN);
- myButton_3.setBounds(98, 170, 35, 29);
- contentPane.add(myButton_3);
- myButton_4 = new JButton("/");
- myButton_4.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String x = opA.getText();
- String y = opB.getText();
- if (y=="0")
- result.setText("undef");
- else {
- int a = Integer.parseInt(x);
- int b = Integer.parseInt(y);
- int c = a / b;
- result.setText("" + c);}
- }
- });
- myButton_4.setForeground(Color.BLACK);
- myButton_4.setBackground(Color.GREEN);
- myButton_4.setBounds(98, 211, 35, 29);
- contentPane.add(myButton_4);
- }
- }
- Exercise 2
- ___________________________ ___________________________ ___________________________ _________________________
- public class Main {
- public static void main(String args[])
- {
- ThreadA thd1 = new ThreadA();
- ThreadB thd2 = new ThreadB();
- ThreadC thd3 = new ThreadC();
- thd1.start();
- thd2.start();
- thd3.start();
- System.out.println("I am the Main thread");
- }
- }
- /*
- I am Tyheir
- I am ThreadB
- I am the Main thread
- I am ThreadA
- I am ThreadA
- I am Tyheir
- I am ThreadB
- I am the Main thread
- I am ThreadB
- I am Tyheir
- I am ThreadA
- I am the Main thread
- */
- public class ThreadA extends Thread {
- public void run()
- {
- System.out.println("I am ThreadA");
- }
- }
- public class ThreadB extends Thread{
- public void run()
- {
- System.out.println("I am ThreadB ");
- }
- }
- public class ThreadC extends Thread{
- public void run()
- {
- System.out.println("I am Tyheir");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement