0% found this document useful (0 votes)
1K views24 pages

Java Project Report Project

- The document discusses the N-queens problem in chess, which aims to place N queens on an N×N chessboard so that no two queens attack each other. It describes the backtracking algorithm used to solve this problem. - It introduces Java programming language and discusses some of its key features like being simple, object-oriented, platform independent, secure, robust, architecture neutral, portable, high performance, and distributed. - The document appears to be describing a student project to solve the N-queens problem using Java. It includes an abstract, acknowledgements, table of contents, and outlines chapters on the project description and code.

Uploaded by

KUMAR GAURAV
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
1K views24 pages

Java Project Report Project

- The document discusses the N-queens problem in chess, which aims to place N queens on an N×N chessboard so that no two queens attack each other. It describes the backtracking algorithm used to solve this problem. - It introduces Java programming language and discusses some of its key features like being simple, object-oriented, platform independent, secure, robust, architecture neutral, portable, high performance, and distributed. - The document appears to be describing a student project to solve the N-queens problem using Java. It includes an abstract, acknowledgements, table of contents, and outlines chapters on the project description and code.

Uploaded by

KUMAR GAURAV
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 24

-

University of Engineering & Management,


Jaipur

Project Title
Department Of Computer Science & Engineering

Subject Name : Object Oriented Programming Using Java


Subject Code :
Semester : 5th
Batch :
Enrollment No :
Name :
Abstract

The N-queens problem originally introduced in 1850 by Carl Gauss. The goal of this
problem is to place N queens on a N  N chessboard, so that no queens can take each other.
Queens can move horizontally, vertically, and diagonally, this means that there can be only
one queen per row and one per column, and that no two queens can find themselves on the
same diagonal . It is a computationally expensive problem - NP-complete, what makes it very
famous problem in computer science.

Backtracking solution for this problem based on the idea is to place queens one by one in
different columns, starting from the leftmost column. When we place a queen in a column,
we check for clashes with already placed queens. In the current column, if we find a row for
which there is no clash, we mark this row and column as part of the solution. If we do not
find such a row due to clashes then we backtrack and return false.

2|Page
Acknowledgement

The completion of any inter-disciplinary project depends upon cooperation, co-ordination


and combined efforts of several sources of knowledge. I am really grateful because I
managed to complete my Java assignment within the time given by my lecturer Prof. Santanu
Basak. This assignment couldn’t have been completed without the effort and co-operation
from our group members. I am amazed to see his even willingness to shower my team with
valuable advice and direction, whenever my team approached him with a problem. I am
thankful to him for providing immense guidance for this project. I am very grateful to have
him as our mentor .He has helped my team in every possible way. I am also thankful to my
team members, without them this project wouldn’t have been possible.Last but not the least ,
I would like to express our gratitude to our friends and respondents for the support and
willingness to spend some time with us to fill in the questionnaires.

3|Page
Table of Contents
SL. NO. Chapter Topic Page No.

1 1 Introduction 6
2
1 Java Technology 6
3 2.1 Features of Java 6
4 1 2.1.1 Simple 6
5 2.1.2 Object- Oriented 7
6 2.1.3 Platform Independent 7
7 2.1.4 Secure 8
8 2.1.5 Robust 8
9 2.1.6 Architecture-Neutral 8
10 2.1.7 Portable 9
11 2.1.8 High-performance 9
12 2.1.9 Distributed 9
13 2.1.10 Multi-threaded 9

14 2 3.Project Usability 9
15 3.1 Dynamic scheduling of Tasks Branching 10
16 3.2 Symmetry of Solutions 10

17 4.Others Project 10

18 3 5.Project Description 11-14


19 5.1 Flow chart
20 4 6 Project Code Description 14
21 6.1 Packages 14
22 6.1.1. Java Swing 14
23 6.1.2 Awt- Java AWT 14
24 6.1.3 Java.util 15
25 6.2 Class NQueens2 15
26 6.2. 1.Methods of Class NQueens2 15-23
27 6.2.1.1. private void
jButton1ActionPerformed(java.awt.event.ActionEvent 21
evt)
28 6.2.1.2. private void
clearActionPerformed(java.awt.event.ActionEvent 22
evt)
29 6.2.1.3. private void

4|Page
jButton3ActionPerformed(java.awt.event.ActionEvent 22
evt)
30 6.2.1.4. private void
jButton2ActionPerformed(java.awt.event.ActionEvent 22
evt)
31 6.2.1.5. void start(int index) 22
32 6.2.1.6. void set Panel() 22
33 6.2.1.7. void design(int n) 22
34 6.2.1.8. public boolean match(int[] q, int n) 22
35 6.2.1.9. public void print Queens(int[] q) 22
36 6.2.1.10. public void check(int n) 23
37 6.2.1.11. public void check(int[] q, int k) 23
38 6.2.1.12. private int sizeof(ArrayList<String[][]> arL) 23

39 References Book References 24


40 Web References 24

5|Page
Chapter 1
1. Introduction:
Here we implement our project N-QUEEN on Net Beans. Net Beans is a software
development platform written in Java. The NetBeans Platform allows applications to
be developed from a set of modular software components called modules.

2. Java Technology: Java is a programming language and a platform. Java is a high


level, robust, secured and object-oriented programming language.

Platform: Any hardware or software environment in which a program runs is known


as a platform. Since Java has its own runtime environment (JRE) and API, it is called

2.1 . Features of Java:


The following are some of the features of Java:

Fig: 2.1

2.1.1 Simple

According to Sun, Java language is simple because:


a)Syntax is based on C++ (so easier for programmers to learn it after C++).
b) Removed many confusing and/or rarely-used features e.g., explicit pointers, operator
overloading etc.
c) No need to remove unreferenced objects because there is Automatic Garbage
Collection in java.

6|Page
2.1.2 Object-oriented

Object-oriented means we organize our software as a combination of different types of


objects that incorporates both data and behavior.
Object-oriented programming (OOPs) is a methodology that simplifies software
development and maintenance by providing some rules.
Basic concepts of OOPs are:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation

2.1.3 Platform Independent

Fig: 2.2

A platform is the hardware or software environment in which a program runs.


7|Page
There are two types of platforms software-based and hardware-based. Java provides
software-based platform.

The Java platform differs from most other platforms in the sense that it is a software-based
platform that runs on the top of other hardware-based platforms. It has two components:
1. Runtime Environment
2. API(Application Programming Interface)

Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris, and Mac/OS
etc. Java code is compiled by the compiler and converted into byte code. This byte code is a
platform-independent code because it can be run on multiple platforms i.e. Write Once and
Run Anywhere (WORA).

2.1.4 Secured

Java is secured because:

o No explicit pointer
o Java Programs run inside virtual machine sandbox
o Class loader: adds security by separating the package for the classes of the local file
system from those that are imported from network sources.
o Byte code Verifier: checks the code fragments for illegal code that can violate access
right to objects.
o Security Manager: determines what resources a class can access such as reading and
writing to the local disk.

These securities are provided by java language. Some security can also be provided by
application developer through SSL, JAAS, Cryptography etc.

2.1.5 Robust

Robust simply means strong. Java uses strong memory management. There is lack of pointers
that avoids security problem. There is automatic garbage collection in java. There is
exception handling and type checking mechanism in java. All these points make java robust.

2.1.6. Architecture-neutral

There is no implementation dependent feature e.g. size of primitive types is fixed. In C


programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of
memory for 64-bit architecture. But in java, it occupies 4 bytes of memory for both 32 and 64
bit architectures.

2.1.7 Portable

8|Page
We may carry the java byte code to any platform.

2.1.8 High-performance

Java is faster than traditional interpretation since byte code is "close" to native code still
somewhat slower than a compiled language (e.g., C++)

2.1.9 Distributed

We can create distributed applications in java. RMI and EJB are used for creating distributed
applications. We may access files by calling the methods from any machine on the internet.

2.1.10 Multi-threaded

A thread is like a separate program, executing concurrently. We can write Java programs that
deal with many tasks at once by defining multiple threads. The main advantage of multi-
threading is that it doesn't occupy memory for each thread. It shares a common memory area.
Threads are important for multi-media, Web applications etc.

9|Page
Chapter 2
3. Project Usability :
3.1 Dynamic scheduling of Tasks

In this project we started with initial configurations that are were of depth 1. The initial
configurations contained information had queens only on the first row resulting in n
combinations. The most important characteristic of the N Queen problem is it is a
representative of the Constraint satisfaction problem. In these problems the general methods
is to form of a tree of different combinations of input values from domain. Applying
constraints to nodes leads to pruning of sub-trees to nodes. This leads to a tree that has
uneven depth with certain branches being too long and other branches too short. In our case
we had the same problem with the rows n/2 generally having a lot of more combinations than
the others. This leads to some processors doing work while majority of processors are idle.
The ideal situation will be one where we have all processors equally working on the tasks. To
resolve in our case in a simple way we had created initial configurations till level 2 and used
that to start the SPUs. In this way the n/2 column selected in first row combination will divide
into many combinations to form more initial configurations with n/2 as the starting point and
this will divided too many SPUs thereby splitting work in a better way.

5.2 Branching
Branching optimizations (Branch hints, separate registers etc.) can also be used to improve
the performance.

a. Symmetry of Solutions

The N Queen has unique property based on the symmetry of the solution. For example for 8
queen problem there are a total of 92 solutions in which only 12 solutions are unique. So the
time required to find solution goes down by a factor. The algorithm that detects the unique
solutions can be used to improve the performance considerably. The symmetry on the
horizontal, vertical diagonal left, right etc. After identifying the unique solutions the
remaining solutions need to be generated from these solutions. This can be done more
efficiently by using the cell processor because the symmetry solutions will be a fixed kind of
permutation of the unique solution. This can be exploited by using the SPU-permutation
operation to be generated much faster. We expect this to have a considerable impact on the
optimization.

4.Others Project:
We can implement this in a lot of other games like - a maze and Sudoku etc.
Further we can convert our project into a well established logical game.
It can also be converted into a mobile application.

10 | P a g e
Chapter 3
6. Project Description: The aim of our project is to solve N-Queens problem.
6.1 Flow chart: The following is the flowchart of our project.

Step 1: We’ll run the file, shown in fig 6.1.1.

RUN FILE

Fig:6.1.1

Step 2: We’ll have to enter the number of Queens shown in fig 6.1.2.

Fig:6.1.2

11 | P a g e
Step 3: After entering the number of Queens and pressing OK button we get the following
output shown in fig 6.1.3:

Fig: 6.1.3

Step 4: After pressing the next button we get the NEXT output shown in fig 6.1.4.

Fig: 6.1.4.

12 | P a g e
Step 4: After pressing the PREVIOUS button, we get the previous solution shown in fig
6.1.5.

Fig 6.1.5

Step 5: After pressing the RESET button, the entire N*N panel is removed shown in fig
6.1.6.

Fig 6.1.6

13 | P a g e
STEP 4: End shown in fig 6.1.7.

END

Fig 6.1.7

Chapter 4
6.Project Code Description :
Class NQueen2 is the only class used in out project. The basic workings performed by the
class are the logical checking of the queens to be placed, the printing of queens, the creation
of panel containing n*n matrix.
6.1 Packages:

6.1.1. Java Swing is used to create window-based applications. It is built on the top of
AWT (Abstract Windowing Toolkit) API and entirely written in java.

Unlike AWT, Java Swing provides platform-independent and lightweight components.

The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

a.) JFrame- The javax.swing.JFrame class is a type of container which inherits the
java.awt.Frame class. JFrame works like the main window where components like labels,
buttons, and text fields are added to create a GUI.
b.) JLabel- The object of JLabel class is a component for placing text in a container. It is
used to display a single line of read only text. The text can be changed by an application
but a user cannot edit it directly. It inherits JComponent class.
c.) JButton- The JButton class is used to create a labeled button that has platform
independent implementation. The application result in some action when the button is
pushed. It inherits AbstractButton class.
d.)Image Icon- An implementation of the Icon interface that paints Icons from Images.
Image that are created from a URL, filename or byte array are preloaded using
MediaTracker to monitor the loaded

6.1.2 Awt- Java AWT:


(Abstract Window Toolkit) is an API to develop GUI or window - based applications in
java.

Java AWT components are platform-dependent i.e. components are displayed according to
the view of operating system. AWT is heavyweight i.e. its components are using the
resources of OS.

14 | P a g e
The java.awt package provides classes for AWT api such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.

a.) Action Listener-The Java Action Listener is notified whenever you click on the button
or menu item. It is notified against Action Event. The Action Listener interface is found
in java.awt.event package. It has only one method: action Performed ()

b.) Color- Class java.awt.Color. Creates a color with the specified red, green, and blue
values in the range (0 - 255). Creates a color with the specified combined RGB value
consisting of the red component in bits 16-23, the green component in bits 8-15, and the
blue component in bits 0-7.

c.) Grid Layout- Grid Layout. Public Grid Layout (int rows, int cols) creates a grid
layout with the specified number of rows and columns. All components in the layout are
given equal size. One, but not both, of rows and cols can be zero, which means that any
number of objects can be placed in a row or in a column.

6.1.3 Java.util: Contains the collections framework, legacy collection classes, event
model, date and time facilities, internationalization, and miscellaneous utility classes (a string
tokenizer, a random-number generator, and a bit array).

a.) Array List: The Array List class extends Abstract List and implements the List
interface.ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are
of a fixed length. After arrays are created, they cannot grow or shrink, which means that you
must know in advance how many elements an array will hold.

6.2. Class NQueens2 :


The following is the source code of class NQueens2:

public class NQueens2 extends javax.swing.JFrame {


JButton bt = new JButton("RESET");

public NQueens2() {
setPanel();
initComponents();

}
@SuppressWarnings("unchecked")

private void initComponents() {

jPanel1 = new javax.swing.JPanel();


jButton1 = new javax.swing.JButton();
txt = new javax.swing.JTextField();
clear = new javax.swing.JButton();
txtSolNo = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
pnl = new javax.swing.JPanel();

15 | P a g e
RESET = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(153, 153, 153));

jButton1.setBackground(new java.awt.Color(255, 153, 0));


jButton1.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

txt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtActionPerformed(evt);
}
});

clear.setBackground(new java.awt.Color(255, 51, 51));


clear.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
clear.setText("RESET");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});

txtSolNo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtSolNoActionPerformed(evt);
}
});

jButton2.setBackground(new java.awt.Color(255, 102, 0));


jButton2.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
jButton2.setText("NEXT");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setBackground(new java.awt.Color(255, 102, 102));


jButton3.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
jButton3.setText("PREVIOUS");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);

16 | P a g e
}
});

javax.swing.GroupLayout jPanel1Layout = new


javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 67,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE,
61, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(clear, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton3)
.addGap(30, 30, 30)
.addComponent(txtSolNo, javax.swing.GroupLayout.PREFERRED_SIZE,
90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE,
81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(1, 1, 1)
.addComponent(txtSolNo))
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE
, false)
.addComponent(txt)
.addComponent(jButton1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(clear)
.addComponent(jButton3)))
.addContainerGap())

17 | P a g e
);

jLabel1.setIcon(new
javax.swing.ImageIcon("C:\\Users\\HPPC\\Desktop\\op.png")); // NOI18N

javax.swing.GroupLayout RESETLayout = new


javax.swing.GroupLayout(RESET);
RESET.setLayout(RESETLayout);
RESETLayout.setHorizontalGroup(

RESETLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
RESETLayout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
RESETLayout.setVerticalGroup(

RESETLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

javax.swing.GroupLayout pnlLayout = new javax.swing.GroupLayout(pnl);


pnl.setLayout(pnlLayout);
pnlLayout.setHorizontalGroup(

pnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(RESET, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
);
pnlLayout.setVerticalGroup(

pnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(RESET, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(pnl, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(

18 | P a g e
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pnl, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


jButton1.setForeground(Color.orange);
getContentPane().setBackground(Color.DARK_GRAY);

int n = Integer.parseInt(txt.getText());

design(n);

private void txtActionPerformed(java.awt.event.ActionEvent evt) {

private void clearActionPerformed(java.awt.event.ActionEvent evt) {


RESET.setForeground(Color.orange);

txt.setText(null);
txtSolNo.setText(null);
pnl.removeAll();
pnl.updateUI();
}

private void txtSolNoActionPerformed(java.awt.event.ActionEvent evt) {

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


int index = Integer.parseInt(txtSolNo.getText());

if (index < arL.size()) {


txtSolNo.setText(String.valueOf(index + 1));
start(index);
}
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {


jButton3.setForeground(Color.orange);

19 | P a g e
int index = Integer.parseInt(txtSolNo.getText());
if (index > 1) {
txtSolNo.setText(String.valueOf(index - 1));
start(index);
}
}

public static void main(String args[]) {


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {

new NQueens2().setVisible(true);
}
});
}
private javax.swing.JPanel RESET;
private javax.swing.JButton clear;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel pnl;
private javax.swing.JTextField txt;
private javax.swing.JTextField txtSolNo;

JButton[][] arBtn;

ArrayList<String[][]> arL = new ArrayList<>();


int n = 0;

void start(int index) {

String[][] s = arL.get(index);
pnl.setBackground(Color.YELLOW);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {

arBtn[i][j].setText(s[i][j]);

if (s[i][j].equals("Q")) {
arBtn[i][j].setBackground(Color.white);
arBtn[i][j].setIcon(new
ImageIcon("C:\\Users\\HPPC\\Desktop\\tiara.jpg"));
} else {
arBtn[i][j].setBackground(Color.black);
arBtn[i][j].setIcon(new
ImageIcon("C:\\Users\\HPPC\\Desktop\\c2.jpg"));
}

20 | P a g e
}

jPanel1.updateUI();
}

void setPanel() {
setTitle("N-Queens");

void design(int n) {
this.n = n;
pnl.removeAll();

arBtn = new JButton[n][n];


pnl.setLayout(new GridLayout(n, n));

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {
JButton btn = new JButton("");

pnl.add(btn);
arBtn[i][j] = btn;

}
}
pnl.updateUI();
arL.clear();
check(n);
txtSolNo.setText("1");
start(0);
}

public boolean match(int[] q, int n) {


for (int i = 0; i < n; i++) {
if (q[i] == q[n]) {
return false;
}
if ((q[i] - q[n]) == (n - i)) {
return false;
}
if ((q[n] - q[i]) == (n - i)) {
return false;
}
}
return true;
}

public void printQueens(int[] q) {


int n = q.length;

21 | P a g e
String[][] arSol = new String[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (q[i] == j) {

arSol[i][j] = "Q";

} else {

arSol[i][j] = "X";

}
System.out.println();

arL.add(arSol);

public void check(int n) {


int[] a = new int[n];
check(a, 0);
}
int c = 0;

public void check(int[] q, int k) {


int n = q.length;
if (k == n) {
c++;
printQueens(q);
} else {
for (int i = 0; i < n; i++) {
q[k] = i;
if (match(q, k)) {
check(q, k + 1);
}
}
}
}

private int sizeof(ArrayList<String[][]> arL) {


throw new UnsupportedOperationException("Not supported yet.");
}
}

6.2. 1.Methods of Class NQueens2:


6.2.1.1. private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt):

22 | P a g e
This is the method used to define the functioning of OK button.
6.2.1.2. private void clearActionPerformed(java.awt.event.ActionEvent
evt):
This is the method that defines the functionality of RESET button.
6.2.1.3. private void jButton3ActionPerformed(java.awt.event.ActionEvent
evt):
This method is responsible for the functioning of PREVIOUS button.
6.2.1.4. private void jButton2ActionPerformed(java.awt.event.ActionEvent
evt):
This method defines the functioning of NEXT button.
6.2.1.5. void start(int index):
In this method, the number of Queens to be placed is passed and then the condition of placing
the Queens is checked.
6.2.1.6. void setPanel():
This method is used to name the Panel.
6.2.1.7. void design(int n):
This design() method is used for the designing of the N*N Panel.
6.2.1.8. public boolean match(int[] q, int n):
This method is used to check the places at which Queens cannot be placed.
6.2.1.9. public void printQueens(int[] q):
This is the method, which after checking the logic of placing Queens, places the Queen and
adds it to the array arSol[][] which has the entire solution.
6.2.1.10. public void check(int n):
After taking the value of number of Queens , it calls the method check(int[] q, int k).
6.2.1.11. public void check(int[] q, int k):
It has a counter which takes into account all the numbers of possible solutions. It also calls
printQueens(q) when the total number of columns is traversed and check(q, k + 1) for
checking the logic of placing of Queens.

6.2.1.12. private int sizeof(ArrayList<String[][]> arL):


It throws UnsupportedOperationException("Not supported yet.") exception .

23 | P a g e
References

Book References:
i. Java The Complete Reference Seventh Edition, Herbert Schildt,Tata McGraw
Hill Education
ii. Programming with Java by E Balagurusamy,Tata McGraw Hill Education

iii. JavaBeans: Developing Component software in Java,O’Reilly Media


iv. Java in a Nutshell by Benjamin J.Evans & David Flanagan,O’Reilly Media

Web References:
i. Oracle Java Tutorial , URL : https://docs.oracale.com/javase/tutorial/
ii. JavaPoint ,URL : https://www.javapoint.com/
iii. Wikipedia ,URL : https://www.wikipedia.org/
iv. GeeksforGeeks , URL : https://www.geeksforgeeks.org
v. CodingBat Java , URL : https://codingbat.com
vi. JavaWorld , URL : https://www.javaworld.com
vii. CodeProject , URL : https://www.codeproject.com
viii. StackOverflow , URL : https://stackoverflow.com
ix. Eclipse ,URL : https://www.eclipse.org
x. YouTube , URL : https://www.yotube.com
xi. MKyong , URL : https://www.mkyong.com

24 | P a g e

You might also like