Ramirez_RD

Samson_Midterm- OOP

Apr 1st, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. package Samson_MidtermExam;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.time.LocalDate;
  8. import java.time.format.DateTimeFormatter;
  9. import java.time.temporal.ChronoField;
  10.  
  11. public class Samson_MidtermExam extends JFrame {
  12.     JTextField dateInput, resultField;
  13.  
  14.     public Samson_MidtermExam() {
  15.         setTitle("Midterm Exam - Samson");
  16.         setSize(400, 200);
  17.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  18.         //panel
  19.         JPanel panel = new JPanel();
  20.         panel.setLayout(null);
  21.         //bahala ka sa kung anong color gusto mo
  22.         //and kung anong font style ng background and text, pati button
  23.  
  24.         JLabel dateLabel = new JLabel("Enter the date:");
  25.         dateLabel.setBounds(30, 30, 100, 25);
  26.  
  27.         JLabel resultLabel = new JLabel("Day of the Week:");
  28.         resultLabel.setBounds(30, 70, 130, 25);
  29.  
  30.         dateInput = new JTextField();
  31.         dateInput.setBounds(150, 30, 180, 25);
  32.  
  33.         resultField = new JTextField();
  34.         resultField.setBounds(150, 70, 180, 25);
  35.         //buttons
  36.         JButton computeButton = new JButton("compute");
  37.         computeButton.setBounds(40, 120, 90, 30);
  38.  
  39.         JButton resetButton = new JButton("reset");
  40.         resetButton.setBounds(150, 120, 90, 30);
  41.  
  42.         JButton exitButton = new JButton("exit");
  43.         exitButton.setBounds(260, 120, 90, 30);
  44.  
  45.         panel.add(dateLabel);
  46.         panel.add(dateInput);
  47.         panel.add(resultLabel);
  48.         panel.add(resultField);
  49.         panel.add(computeButton);
  50.         panel.add(resetButton);
  51.         panel.add(exitButton);
  52.  
  53.         add(panel);
  54.         //events
  55.         computeButton.addActionListener(new ActionListener() {
  56.             public void actionPerformed(ActionEvent e) {
  57.                 computeDateInfo();
  58.             }
  59.         });
  60.         resetButton.addActionListener(new ActionListener() {
  61.             public void actionPerformed(ActionEvent e) {
  62.                 dateInput.setText("");
  63.                 resultField.setText("");
  64.             }
  65.         });
  66.         exitButton.addActionListener(new ActionListener() {
  67.             public void actionPerformed(ActionEvent e) {
  68.                 System.exit(0);
  69.             }
  70.         });
  71.         setVisible(true);
  72.     }
  73.     //computation
  74.     public void computeDateInfo() {
  75.             String input = dateInput.getText();
  76.             int currentYear = LocalDate.now().getYear();
  77.             String fullDate = currentYear + "-" + input;
  78.  
  79.             DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  80.             LocalDate date = LocalDate.parse(fullDate, formatter);
  81.  
  82.             int dayOfYear = date.get(ChronoField.DAY_OF_YEAR);
  83.             String dayOfWeek = date.getDayOfWeek().toString();
  84.  
  85.             resultField.setText(dayOfWeek + ", Day " + dayOfYear);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment