You are on page 1of 11

Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project

Instructions
 In this project, you will solve problem(s) based on what you have learnt in this
course.
 There is 1 question in this project assignment.
 Email/paper/other modes of submissions will not be accepted.
 Upload a word version of this document.

Due Date: 10 pm, June 03, 2022.

Submitting this Assignment

You will submit (upload) this project assignment in MS Teams. Name this document as
Project_AJPEVEN2022_John_Doe.doc in case your name is John Doe. Paste your code and
snapshot of output after the question, save and upload the document.

Grading: 10 points (out of your total 100 points in the course). Out of the 10 points, 5
points are for correct code as desired with screenshot of your output. Rest 5 points will be
awarded based on your performance in the project Viva Voce.

1. Question 1: Write a program using JCF (single java file based project with java file name
as linlist) to achieve the following:

Write a program that randomly stores 10 integer numbers (to start with) in a LinkedList
object. Using this LinkedList object, implement stack and queue operations using GUI as
follows: Add 2 Radio Buttons for choosing options (a) Stack and (b) Queue, and 4 JButtons
(Push, Pop for Stack, and Add, Delete for Queue).

Once you run the program, it should display a LinkedList of 10 random integers in a text
field. You should then be able to choose a radio button of either Stack or Queue, followed
by one of its operations (Push or Pop Buttons for Stack ONLY, and Add or Delete Buttons for
Queue ONLY (Invalid choices like choosing Stack followed by pressing ADD button should
not work). The program should then display the modified list ( as per the operation
undertaken) in the text field.

Note: In case you think you may not be able to implement the GUI, then use
JOptionPane to enter your commands (as shown below), and show the resultant Linked
List in the console. The commands to be entered in showInputDialogue will be as follows:
Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project

Push
Pop
Add
Delete

Note that Stack is a LIFO (Last in first out) structure, and Queue is a FIFO (First in first out)
structure.

The maximum points (for all correct output) that you may get if you do not
implement the GUI and choose to use JOptionPane instead is 6 points out of the total
of 10 points.

Also, above your code, mention in 2-3 lines of comments, what exactly you have achieved,
in case you could not complete the desired output, and whether you used GUI or chose to
use JOptionPane.

Solution:-

package sourabh;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.awt.event.ActionEvent;
public class Demo extends JFrame {

private JPanel contentPane;


private JTextField resultjava;
private JTextField stack;
Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project

private JTextField queue;

public static void main(String[] args) {


EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Demo frame = new Demo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public Demo() {
LinkedList<Integer> listjava = new LinkedList<Integer>();

listjava.add(0, 0);
listjava.add(1, 1);
listjava.add(2, 2);
listjava.add(3, 3);
listjava.add(4, 4);
listjava.add(5, 5);
listjava.add(6, 6);
listjava.add(7, 7);
listjava.add(8, 8);
listjava.add(9, 9);
JFrame frame = new JFrame("Java Project");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project

resultjava = new JTextField();


resultjava.setText("");
resultjava.setBounds(10, 11, 564, 36);
contentPane.add(resultjava);
resultjava.setColumns(10);

String a = listjava.toString();
resultjava.setText(a);

JRadioButton radiostack = new JRadioButton("STACK");


radiostack.setBounds(59, 102, 109, 23);
contentPane.add(radiostack);

JRadioButton radioqueue = new JRadioButton("QUEUE");


radioqueue.setBounds(430, 102, 109, 23);
contentPane.add(radioqueue);

ButtonGroup option = new ButtonGroup();


option.add(radioqueue);
option.add(radiostack);

JButton btnNewButton = new JButton("PUSH");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (stack.getText().toString().isBlank() != true) {
if (radiostack.isSelected() && listjava.size() < 10) {
listjava.addLast(Integer.valueOf(stack.getText()));
String a = listjava.toString();
resultjava.setText(a);
}
}
}
});
btnNewButton.setBounds(56, 172, 89, 23);
contentPane.add(btnNewButton);

JButton btnNewButton_1 = new JButton("POP");


Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project

btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (radiostack.isSelected() && listjava.size() > 0) {
listjava.removeLast();
String a = listjava.toString();
resultjava.setText(a);
}
}
});
btnNewButton_1.setBounds(56, 206, 89, 23);
contentPane.add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("ADD");


btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (queue.getText().toString().isBlank() != true) {
if (radioqueue.isSelected() && listjava.size() < 10) {
listjava.addLast(Integer.valueOf(queue.getText()));
String a = listjava.toString();
resultjava.setText(a);
}
}
}
});
btnNewButton_2.setBounds(427, 172, 89, 23);
contentPane.add(btnNewButton_2);

JButton btnNewButton_3 = new JButton("DELETE");


btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (radioqueue.isSelected() && listjava.size() > 0) {
listjava.pop();
String a = listjava.toString();
resultjava.setText(a);
}
}
});
Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project

btnNewButton_3.setBounds(427, 206, 89, 23);


contentPane.add(btnNewButton_3);

stack = new JTextField();


stack.setBounds(59, 142, 86, 20);
contentPane.add(stack);
stack.setColumns(10);

queue = new JTextField();


queue.setBounds(430, 141, 86, 20);
contentPane.add(queue);
queue.setColumns(10);
}
}
Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project

Output:-
Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project
Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project
Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project
Project – CAF612 Advanced Java Programming, Even Sem, 2022

Your Name: Ravi Raj SAP ID: 1000016095 Major: java project

You might also like