Java Object-Oriented Programming Review: Core Concepts and Final Programming Exercises

The core knowledge system of Java programming is summarized as follows: the characteristics of the language and environment configuration, basic program syntax, object-oriented programming structures, relationships between classes and UML diagrams, prdeefined classes and APIs from the JDK, exception handling mechanisms, GUI programming models, concurrent programming, and application deployment strategies.

Comprehensive Programming Assignment: Data Collection Form

A GUI application is required to collect user information, featuring a form with various input controls. The program must have two primary functions.

  1. Submit: When the user clicks the submit button, all entered data must be printed to the console.
  2. Reset: Clicking the reset button should clear all input fields and restore default selections.
import javax.swing.*;
import java.awt.*;

public class UserInputForm {
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            FormFrame window = new FormFrame();
            window.setVisible(true);
        });
    }
}

class FormFrame extends JFrame {
    private JPanel mainPanel;
    private JTextField nameField;
    private JTextArea addressArea;
    private JComboBox<String> educationBox;
    private JCheckBox reading, sports, music;
    private JRadioButton male, female;
    private ButtonGroup genderGroup;

    public FormFrame() {
        setTitle("Information Entry");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(650, 450);
        mainPanel = new JPanel();
        mainPanel.setLayout(null);
        setupComponents();
        add(mainPanel);
    }

    private void setupComponents() {
        int yPos = 30;
        // Name Field
        addLabel("Name:", 30, yPos);
        nameField = new JTextField();
        nameField.setBounds(100, yPos, 150, 25);
        mainPanel.add(nameField);

        // Address Area
        yPos += 50;
        addLabel("Address:", 30, yPos);
        addressArea = new JTextArea(3, 15);
        addressArea.setLineWrap(true);
        JScrollPane addressScroll = new JScrollPane(addressArea);
        addressScroll.setBounds(100, yPos, 150, 60);
        mainPanel.add(addressScroll);

        // Education Dropdown
        yPos += 90;
        addLabel("Education:", 30, yPos);
        String[] levels = {"Master's", "Bachelor's", "Associate's"};
        educationBox = new JComboBox<>(levels);
        educationBox.setBounds(100, yPos, 150, 25);
        mainPanel.add(educationBox);

        // Hobby Checkboxes
        yPos += 50;
        addLabel("Hobbies:", 30, yPos);
        reading = new JCheckBox("Reading");
        sports = new JCheckBox("Sports");
        music = new JCheckBox("Music");
        reading.setBounds(100, yPos, 80, 25);
        sports.setBounds(180, yPos, 80, 25);
        music.setBounds(260, yPos, 80, 25);
        mainPanel.add(reading); mainPanel.add(sports); mainPanel.add(music);

        // Gender Radio Buttons
        yPos += 50;
        addLabel("Gender:", 30, yPos);
        male = new JRadioButton("Male");
        female = new JRadioButton("Female");
        male.setBounds(100, yPos, 70, 25);
        female.setBounds(180, yPos, 70, 25);
        genderGroup = new ButtonGroup();
        genderGroup.add(male); genderGroup.add(female);
        mainPanel.add(male); mainPanel.add(female);

        // Buttons
        JButton submitBtn = new JButton("Submit");
        submitBtn.setBounds(150, 350, 100, 30);
        submitBtn.addActionListener(e -> handleSubmit());
        mainPanel.add(submitBtn);

        JButton resetBtn = new JButton("Reset");
        resetBtn.setBounds(270, 350, 100, 30);
        resetBtn.addActionListener(e -> handleReset());
        mainPanel.add(resetBtn);
    }

    private void addLabel(String text, int x, int y) {
        JLabel label = new JLabel(text);
        label.setBounds(x, y, 100, 25);
        mainPanel.add(label);
    }

    private void handleSubmit() {
        System.out.println("--- User Information ---");
        System.out.println("Name: " + nameField.getText());
        System.out.println("Address: " + addressArea.getText());
        System.out.println("Education: " + educationBox.getSelectedItem());
        System.out.print("Hobbies: ");
        if (reading.isSelected()) System.out.print("Reading ");
        if (sports.isSelected()) System.out.print("Sports ");
        if (music.isSelected()) System.out.print("Music ");
        System.out.println();
        System.out.print("Gender: ");
        if (male.isSelected()) System.out.println("Male");
        if (female.isSelected()) System.out.println("Female");
        System.out.println();
    }

    private void handleReset() {
        nameField.setText("");
        addressArea.setText("");
        educationBox.setSelectedIndex(0);
        reading.setSelected(false);
        sports.setSelected(false);
        music.setSelected(false);
        genderGroup.clearSelection();
    }
}

Comprehensive Programming Assignment: Citizen Data Analysis

A GUI-based application is developed to process a data file containing citizen information. The system must support several data query and analysis functions, incluidng sorting by name, finding the oldest and youngest individuals, searching for individuals with a specific age, identifying people from the same hometown, and providing a live search filter based on ID number input.

Program Structure Overview The solution consists of three primary Java classes.

  1. CitizenDataAnalyzer: The main GUI JFrame that loads the data file and provides buttons to trigger various analyses. It also includes a live search field that filters results as the user types.
  2. Citizen: A model class representing a single person, with attributes for name, ID, age, gender, and birthplace. It implements the Comparable interface to enable sorting by name.
  3. CitizenAppLauncher: A separate class containing the main method to launch the GUI application in full-screen mode.

Key features of the CitizenDataAnalyzer class include:

  • Data loading from a structured text file into an ArrayList<Citizen>.
  • Using Collections.sort(list) to sort citizens alphabetically by name.
  • Implementing linear search algorithms to find the maximum and minimum age.
  • A findNearestAgeIndex method to locate the citizen whose age is closest to a target value.
  • A javax.swing.TimerTask that runs periodically to update a results panel in real-time based on text entered into a search feild, matching against either ID or birthplace.
  • Event listeners attached to buttons that execute the respective analyses and display results in a text area.

Comprehensive Programming Assignment: Arithmetic Quiz Application

This program is a simple educational tool designed as a GUI application to test arithmetic skills. It generates ten random math problems involving addition, subtraction, multiplication, or division with operands under 100. The user inputs answers, and the program calculates a final score, which is then written to an output file along with the problem list and user answers.

Application Flow The QuizApplication JFrame class manages the interface and logic.

  • Generate Problem: A button click creates a random arithmetic expression, ensuring subtraction and division results are non-negative integers.
  • Submit Answer: The user enters an answer into a field and clicks a button. The program checks it against the pre-calculated correct answer and updates the score.
  • Save Results: After ten problems, another button action writes all problems, the user's answers, and the final score to a text file using a PrintWriter.
  • The application uses Math.random() to select the operation and operands. It employs methods like Math.subtractExact and Math.floorDiv for safe integer calculations.
  • The GUI uses JTextArea components to display the current problem and the user's input, with large, clear fonts for readability.

Tags: java Object-Oriented Programming Swing GUI File I/O Data Structures

Posted on Wed, 13 May 2026 13:05:42 +0000 by ReeceSayer