Green University of Bangladesh
Department of Computer Science and Engineering (CSE)
Faculty of Sciences and Engineering
Semester: (Fall, Year: 2023), B.Sc. in CSE (Day)
Lab Report NO # 3
Course Title: Object
Oriented Programming
Lab
Course Code: CSE 202 Section: 222 D10
Lab Experiment Name: Make a Project of random number guessing game.
Student Details
Name ID
1. Labib Tahmid 221002269
Lab Date : 16 October 2023
Submission Date : 30 October 2023
Course Teacher’s Name: Md. Parvez Hossain
Lab Report Status
Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
Make A Guess Game using Java Swing:
Code:
package com.mycompany.mavenproject1;
import java.util.Random;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Guess Game");
jLabel2.setText("Enter Number (1-9) :");
jButton1.setText("Guess");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel3.setText("Result :");
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(161, 161, 161))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A
lignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(161, 161, 161)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jLabel2)
.addGap(71, 71, 71)
.addComponent(jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE, 88,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(135, 135, 135)
.addComponent(jLabel3,
javax.swing.GroupLayout.PREFERRED_SIZE, 155,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(90, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jLabel1)
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.A
lignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(46, 46, 46)
.addComponent(jButton1)
.addGap(40, 40, 40)
.addComponent(jLabel3)
.addContainerGap(64, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int x , r ;
String input = jTextField1.getText();
x = Integer.parseInt(input);
Random ran = new Random();
r = ran.nextInt(10);
if (x==r) {
jLabel3.setText("Guessed Right!!!");
} else {
jLabel3.setText("Guessed Wrong!!!Try Again");
}
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) ">
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
Output:
Discussion:
1. I used netbeans to run the program.
2. I used jframe to build this program.I used design window to design the
working window and source to write the codes.
3.For design window , I used Label and text fields.
For the code , I used Random Class to generate random variables.getText &
setText helped to scan the user input & print the result.
4. I ran into some problem while doing this lab project but finally overcomed
it after trying some times.