Java Project Report Project
Java Project Report Project
Project Title
Department Of Computer Science & Engineering
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
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
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
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.
Fig: 2.1
2.1.1 Simple
6|Page
2.1.2 Object-oriented
Fig: 2.2
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
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
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.
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.
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
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.
public NQueens2() {
setPanel();
initComponents();
}
@SuppressWarnings("unchecked")
15 | P a g e
RESET = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
txt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtActionPerformed(evt);
}
});
txtSolNo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtSolNoActionPerformed(evt);
}
});
16 | P a g e
}
});
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
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)
);
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)
);
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>
int n = Integer.parseInt(txt.getText());
design(n);
txt.setText(null);
txtSolNo.setText(null);
pnl.removeAll();
pnl.updateUI();
}
19 | P a g e
int index = Integer.parseInt(txtSolNo.getText());
if (index > 1) {
txtSolNo.setText(String.valueOf(index - 1));
start(index);
}
}
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;
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();
pnl.add(btn);
arBtn[i][j] = btn;
}
}
pnl.updateUI();
arL.clear();
check(n);
txtSolNo.setText("1");
start(0);
}
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);
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.
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
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