0% found this document useful (0 votes)
4 views

codeinterview

The document is a Java program that creates a graphical user interface (GUI) for a Student Mark Calculation System. It allows users to input marks for three subjects, calculates the total and average marks, and determines if the student has passed or failed based on the average. The GUI includes input fields, labels, and buttons for calculating results and exiting the application.

Uploaded by

bandishivani2002
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

codeinterview

The document is a Java program that creates a graphical user interface (GUI) for a Student Mark Calculation System. It allows users to input marks for three subjects, calculates the total and average marks, and determines if the student has passed or failed based on the average. The GUI includes input fields, labels, and buttons for calculating results and exiting the application.

Uploaded by

bandishivani2002
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

import java.awt.

EventQueue; // Importing the EventQueue class to manage event dispatching for


GUI applications.

import javax.swing.JFrame; // Importing the JFrame class to create the main window for the GUI
application.

import javax.swing.JLabel; // Importing the JLabel class to display text labels.

import java.awt.Font; // Importing the Font class to set font styles and sizes for text.

import javax.swing.JPanel; // Importing the JPanel class to group components in a container.

import javax.swing.border.BevelBorder; // Importing the BevelBorder class to add a beveled border


around components.

import java.awt.Color; // Importing the Color class to set colors.

import javax.swing.JTextField; // Importing the JTextField class to create text input fields.

import javax.swing.JButton; // Importing the JButton class to create buttons for user actions.

import java.awt.event.ActionListener; // Importing the ActionListener interface to respond to button


clicks.

import java.awt.event.ActionEvent; // Importing the ActionEvent class, which represents an action


(like a button click).

public class StudentMark { // Declaring the main class named StudentMark.

private JFrame frame; // Declaring a JFrame object to hold the main window of the application.

private JTextField sub1; // Declaring a JTextField for entering marks of Subject 1.

private JTextField sub2; // Declaring a JTextField for entering marks of Subject 2.

private JTextField sub3; // Declaring a JTextField for entering marks of Subject 3.

private JTextField totalmarks; // Declaring a JTextField for displaying the total marks.

private JTextField average; // Declaring a JTextField for displaying the average marks.

private JTextField grade; // Declaring a JTextField for displaying the grade (Pass/Fail).

/**

* Launch the application.

*/

public static void main(String[] args) { // Main method, entry point of the application.

EventQueue.invokeLater(new Runnable() { // Using EventQueue to run the GUI code on a


separate thread.

public void run() { // run method to execute the code within EventQueue.
try { // Starting a try-catch block to handle potential errors.

StudentMark window = new StudentMark(); // Creating an instance of StudentMark.

window.frame.setVisible(true); // Making the main application window visible.

} catch (Exception e) { // Catching any exceptions that may occur.

e.printStackTrace(); // Printing the stack trace for debugging if an error occurs.

});

/**

* Create the application.

*/

public StudentMark() { // Constructor of the StudentMark class.

initialize(); // Calling the initialize method to set up the GUI components.

/**

* Initialize the contents of the frame.

*/

private void initialize() { // Method to initialize and set up the GUI components.

frame = new JFrame(); // Creating a new JFrame instance for the main window.

frame.setBounds(100, 100, 558, 378); // Setting the position and size of the JFrame (x, y, width,
height).

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Ensuring the application exits


when the window is closed.

frame.getContentPane().setLayout(null); // Setting a null layout for absolute positioning of


components.

JPanel panel = new JPanel(); // Creating a JPanel to hold other components.

panel.setBorder(new BevelBorder(BevelBorder.LOWERED, Color.GRAY, Color.LIGHT_GRAY, null,


null)); // Adding a beveled border to the panel.
panel.setBounds(23, 11, 498, 317); // Setting the position and size of the panel within the frame.

frame.getContentPane().add(panel); // Adding the panel to the frame's content pane.

panel.setLayout(null); // Setting a null layout for the panel, allowing absolute positioning within
the panel.

JLabel lblNewLabel = new JLabel("Student Mark Calculation System"); // Creating a JLabel to


display the title.

lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 16)); // Setting the font style
and size of the title.

lblNewLabel.setBounds(105, 11, 257, 26); // Positioning the title label within the panel.

panel.add(lblNewLabel); // Adding the title label to the panel.

// Labels for input fields

JLabel lblNewLabel_1 = new JLabel("Subject 1"); // Creating a label for Subject 1.

lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 12)); // Setting font for the label.

lblNewLabel_1.setBounds(58, 64, 85, 14); // Positioning the label within the panel.

panel.add(lblNewLabel_1); // Adding the label to the panel.

JLabel lblNewLabel_1_1 = new JLabel("Subject 2"); // Creating a label for Subject 2.

lblNewLabel_1_1.setFont(new Font("Tahoma", Font.PLAIN, 12)); // Setting font for the label.

lblNewLabel_1_1.setBounds(58, 101, 85, 14); // Positioning the label within the panel.

panel.add(lblNewLabel_1_1); // Adding the label to the panel.

JLabel lblNewLabel_1_1_1 = new JLabel("Subject 3"); // Creating a label for Subject 3.

lblNewLabel_1_1_1.setFont(new Font("Tahoma", Font.PLAIN, 12)); // Setting font for the label.

lblNewLabel_1_1_1.setBounds(58, 141, 85, 14); // Positioning the label within the panel.

panel.add(lblNewLabel_1_1_1); // Adding the label to the panel.

JLabel lblNewLabel_1_1_1_1 = new JLabel("Total Marks"); // Creating a label for Total Marks.

lblNewLabel_1_1_1_1.setFont(new Font("Tahoma", Font.PLAIN, 12)); // Setting font for the


label.

lblNewLabel_1_1_1_1.setBounds(58, 182, 85, 14); // Positioning the label within the panel.
panel.add(lblNewLabel_1_1_1_1); // Adding the label to the panel.

JLabel lblNewLabel_1_1_1_1_1 = new JLabel("Average"); // Creating a label for Average.

lblNewLabel_1_1_1_1_1.setFont(new Font("Tahoma", Font.PLAIN, 12)); // Setting font for the


label.

lblNewLabel_1_1_1_1_1.setBounds(58, 218, 85, 14); // Positioning the label within the panel.

panel.add(lblNewLabel_1_1_1_1_1); // Adding the label to the panel.

JLabel lblNewLabel_1_1_1_1_1_1 = new JLabel("Grade"); // Creating a label for Grade.

lblNewLabel_1_1_1_1_1_1.setFont(new Font("Tahoma", Font.PLAIN, 12)); // Setting font for the


label.

lblNewLabel_1_1_1_1_1_1.setBounds(58, 257, 85, 14); // Positioning the label within the panel.

panel.add(lblNewLabel_1_1_1_1_1_1); // Adding the label to the panel.

// Input fields for subjects and calculated values

sub1 = new JTextField(); // Creating a text field for Subject 1 input.

sub1.setBounds(164, 62, 110, 20); // Positioning the text field within the panel.

panel.add(sub1); // Adding the text field to the panel.

sub1.setColumns(10); // Setting the number of columns for displaying text.

sub2 = new JTextField(); // Creating a text field for Subject 2 input.

sub2.setBounds(164, 99, 110, 20); // Positioning the text field within the panel.

panel.add(sub2); // Adding the text field to the panel.

sub2.setColumns(10); // Setting the number of columns for displaying text.

sub3 = new JTextField(); // Creating a text field for Subject 3 input.

sub3.setBounds(164, 139, 110, 20); // Positioning the text field within the panel.

panel.add(sub3); // Adding the text field to the panel.

sub3.setColumns(10); // Setting the number of columns for displaying text.

totalmarks = new JTextField(); // Creating a text field for displaying Total Marks.

totalmarks.setBounds(164, 180, 110, 20); // Positioning the text field within the panel.
panel.add(totalmarks); // Adding the text field to the panel.

totalmarks.setColumns(10); // Setting the number of columns for displaying text.

average = new JTextField(); // Creating a text field for displaying Average marks.

average.setBounds(164, 216, 110, 20); // Positioning the text field within the panel.

panel.add(average); // Adding the text field to the panel.

average.setColumns(10); // Setting the number of columns for displaying text.

grade = new JTextField(); // Creating a text field for displaying the Grade.

grade.setBounds(164, 255, 110, 20); // Positioning the text field within the panel.

panel.add(grade); // Adding the text field to the panel.

grade.setColumns(10); // Setting the number of columns for displaying text.

// Button to calculate the results

JButton btnCalculate = new JButton("Calculate"); // Creating a button labeled "Calculate".

btnCalculate.addActionListener(new ActionListener() { // Adding an action listener to respond to


button clicks.

public void actionPerformed(ActionEvent e) { // Method called when the button is clicked.

// Parse the inputs for each subject and calculate the total and average.

int s1 = Integer.parseInt(sub1.getText()); // Converting Subject 1 input from String to integer.

int s2 = Integer.parseInt(sub2.getText()); // Converting Subject 2 input from String to integer.

int s3 = Integer.parseInt(sub3.getText()); // Converting Subject 3 input from String to integer.

int total = s1 + s2 + s3; // Calculating the total marks.

totalmarks.setText(String.valueOf(total)); // Displaying total marks in the respective field.

double avg = total / 3.0; // Calculating average by dividing the total by 3.

average.setText(String.valueOf(avg)); // Displaying the average in the respective field.

// Displaying "Passed" if average is 35 or more; otherwise, "Failed".

grade.setText(avg >= 35 ? "Passed" : "Failed");


}

});

btnCalculate.setFont(new Font("Tahoma", Font.PLAIN, 12)); // Setting font style and size for the
button.

btnCalculate.setBounds(327, 63, 97, 39); // Positioning the Calculate button within the panel.

panel.add(btnCalculate); // Adding the Calculate button to the panel.

// Button to exit the application

JButton btnExit = new JButton("Exit"); // Creating a button labeled "Exit".

btnExit.addActionListener(new ActionListener() { // Adding an action listener for the Exit button.

public void actionPerformed(ActionEvent e) { // Method called when the Exit button is clicked.

System.exit(0); // Exits the application when the button is clicked.

});

btnExit.setFont(new Font("Tahoma", Font.PLAIN, 12)); // Setting font style and size for the
button.

btnExit.setBounds(356, 226, 68, 39); // Positioning the Exit button within the panel.

panel.add(btnExit); // Adding the Exit button to the panel.

You might also like