0% found this document useful (0 votes)
10 views6 pages

Final Oop Project

OOP in Java

Uploaded by

merajgohar403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Final Oop Project

OOP in Java

Uploaded by

merajgohar403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

FINAL OOP PROJECT

Malik Faisal Wadd


MTN-SP-000177
BSSE-2nd
OOP in Java
A Cricket Match
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// Main class
public class CricketMatchGUI {
public static void main(String[] args) {
// Create an instance of CricketMatch and display the
GUI
CricketMatch match = new CricketMatch();
match.display();
}
}

// CricketMatch class
class CricketMatch {
private JFrame frame;
private JTextField team1Field, team2Field,
team1ScoreField, team2ScoreField;
private JLabel resultLabel;
// Constructor to initialize GUI components
public CricketMatch() {
// Create the main frame
frame = new JFrame("Cricket Match Score");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new GridLayout(5, 2, 10, 10));

// Labels
JLabel team1Label = new JLabel("Team 1 Name:");
JLabel team2Label = new JLabel("Team 2 Name:");
JLabel team1ScoreLabel = new JLabel("Team 1 Score:");
JLabel team2ScoreLabel = new JLabel("Team 2 Score:");
resultLabel = new JLabel("Result: ",
SwingConstants.CENTER);

// Text Fields
team1Field = new JTextField();
team2Field = new JTextField();
team1ScoreField = new JTextField();
team2ScoreField = new JTextField();
// Button to calculate the result
JButton calculateButton = new JButton("Calculate
Result");
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculateResult();
}
});

// Adding components to the frame


frame.add(team1Label);
frame.add(team1Field);
frame.add(team2Label);
frame.add(team2Field);
frame.add(team1ScoreLabel);
frame.add(team1ScoreField);
frame.add(team2ScoreLabel);
frame.add(team2ScoreField);
frame.add(calculateButton);
frame.add(resultLabel);
}
// Method to display the GUI
public void display() {
frame.setVisible(true);
}

// Method to calculate the result


private void calculateResult() {
String team1 = team1Field.getText().trim();
String team2 = team2Field.getText().trim();

if (team1.isEmpty() || team2.isEmpty()) {
resultLabel.setText("Error: Enter team names!");
return;
}

try {
int team1Score =
Integer.parseInt(team1ScoreField.getText().trim());
int team2Score =
Integer.parseInt(team2ScoreField.getText().trim());
if (team1Score > team2Score) {
resultLabel.setText("Result: " + team1 + " Wins!");
} else if (team2Score > team1Score) {
resultLabel.setText("Result: " + team2 + " Wins!");
} else {
resultLabel.setText("Result: It's a Draw!");
}
} catch (NumberFormatException e) {
resultLabel.setText("Error: Enter valid numeric
scores!");
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy