0% found this document useful (0 votes)
9 views32 pages

Advanced Java Program Examples and Outputs

GB ha sc ZZ fa gd BB xx se na z yha de s th gd se h sb hd ch gf gf st hnn NCC x tk NCC de rn kr HC rs hnn hd sh hd sh hd hnn hd

Uploaded by

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

Advanced Java Program Examples and Outputs

GB ha sc ZZ fa gd BB xx se na z yha de s th gd se h sb hd ch gf gf st hnn NCC x tk NCC de rn kr HC rs hnn hd sh hd sh hd hnn hd

Uploaded by

chantsdenfer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Advance Java Program …

PDF 120.46KB

Write all aim program and output of the following questions

I'll provide the aim, program, and sample output for each question from the
Advanced Java Program List. Due to the length, I'll group them logically and provide
a few examples here. If you need the full set for all 31 questions, I can break them
into multiple responses.

1. Sort array using [Link]() and perform binary search


using [Link]()

Aim:
To demonstrate sorting and binary search on an array using Arrays class methods.

Program:

java

import [Link];

public class ArraySortSearch {


public static void main(String[] args) {
int[] arr = {5, 3, 9, 1, 6};
[Link]("Original Array: " + [Link](arr));

[Link](arr);
[Link]("Sorted Array: " + [Link](arr));

int key = 6;
int index = [Link](arr, key);
[Link]("Index of " + key + ": " + index);
}
}

Output:
text

Original Array: [5, 3, 9, 1, 6]


Sorted Array: [1, 3, 5, 6, 9]
Index of 6: 3

2. Demonstrate [Link]() and [Link]()

Aim:
To show how to copy parts or entire arrays using [Link]() and
[Link]() .

Program:

java

import [Link];

public class ArrayCopyDemo {


public static void main(String[] args) {
int[] original = {1, 2, 3, 4, 5};

int[] copyFull = [Link](original, [Link]);


[Link]("Full Copy: " + [Link](copyFull));

int[] copyRange = [Link](original, 1, 4);


[Link]("Range Copy (1 to 4): " + [Link](copyRang
e));
}
}

Output:

text

Full Copy: [1, 2, 3, 4, 5]


Range Copy (1 to 4): [2, 3, 4]
3. Fill an array using [Link]()

Aim:
To fill an array with a specific value using [Link]() .

Program:

java

import [Link];

public class ArrayFillDemo {


public static void main(String[] args) {
int[] arr = new int[5];
[Link](arr, 7);
[Link]("Filled Array: " + [Link](arr));
}
}

Output:

text

Filled Array: [7, 7, 7, 7, 7]

4. Compare two arrays using [Link]()

Aim:
To compare two arrays for equality using [Link]() .

Program:

java

import [Link];

public class ArrayEqualsDemo {


public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
int[] arr3 = {1, 2, 4};

[Link]("arr1 equals arr2: " + [Link](arr1, arr2));


[Link]("arr1 equals arr3: " + [Link](arr1, arr3));
}
}

Output:

text

arr1 equals arr2: true


arr1 equals arr3: false

5. Weather changes observed by two display classes (Observer


Pattern)

Aim:
To demonstrate the Observer pattern where two displays observe temperature
changes.

Program:

java

import [Link].*;

interface Observer {
void update(int temperature);
}

class WeatherStation {
private List<Observer> observers = new ArrayList<>();
private int temperature;

public void addObserver(Observer o) {


[Link](o);
}

public void setTemperature(int temp) {


[Link] = temp;
notifyObservers();
}

private void notifyObservers() {


for (Observer o : observers) {
[Link](temperature);
}
}
}

class Display1 implements Observer {


public void update(int temperature) {
[Link]("Display1: Temperature updated to " + temperatur
e);
}
}

class Display2 implements Observer {


public void update(int temperature) {
[Link]("Display2: Temperature is now " + temperature);
}
}

public class WeatherDemo {


public static void main(String[] args) {
WeatherStation station = new WeatherStation();
[Link](new Display1());
[Link](new Display2());

[Link](25);
[Link](30);
}
}

Output:

text

Display1: Temperature updated to 25


Display2: Temperature is now 25
Display1: Temperature updated to 30
Display2: Temperature is now 30
6. Display current date and time using LocalDate , LocalTime ,
LocalDateTime

Aim:
To display current date, time, and date-time using Java 8 Date-Time API.

Program:

java

import [Link];
import [Link];
import [Link];

public class DateTimeDemo {


public static void main(String[] args) {
[Link]("Current Date: " + [Link]());
[Link]("Current Time: " + [Link]());
[Link]("Current Date-Time: " + [Link]());
}
}

Output:

text

Current Date: 2025-01-20


Current Time: 14:35:10.123
Current Date-Time: 2025-01-20T14:35:10.123

Would you like me to continue with the remaining programs in the same format? I
can provide all 31 in batches.

Continue remaining

I'll continue with the remaining programs in the same format:


7. Format date and time using DateTimeFormatter

Aim:
To demonstrate formatting of date and time using DateTimeFormatter .

Program:

java

import [Link];
import [Link];

public class DateTimeFormatterDemo {


public static void main(String[] args) {
LocalDateTime now = [Link]();

DateTimeFormatter formatter1 = [Link]("dd-MM-yyy


y HH:mm:ss");
DateTimeFormatter formatter2 = [Link]("yyyy/MM/d
d");
DateTimeFormatter formatter3 = [Link]("EEEE, MMM
M dd, yyyy");

[Link]("Format 1: " + [Link](formatter1));


[Link]("Format 2: " + [Link](formatter2));
[Link]("Format 3: " + [Link](formatter3));
}
}

Output:

text

Format 1: 20-01-2025 14:35:10


Format 2: 2025/01/20
Format 3: Monday, January 20, 2025

8. Calculate difference between two dates using Period and


Duration
Aim:
To calculate differences between dates using Period and Duration .

Program:

java

import [Link];
import [Link];
import [Link];
import [Link];

public class DateDifferenceDemo {


public static void main(String[] args) {
// Period example (for dates)
LocalDate date1 = [Link](2020, 1, 15);
LocalDate date2 = [Link]();
Period period = [Link](date1, date2);

[Link]("Date Difference:");
[Link]("Years: " + [Link]());
[Link]("Months: " + [Link]());
[Link]("Days: " + [Link]());

// Duration example (for times)


LocalTime time1 = [Link](10, 30);
LocalTime time2 = [Link]();
Duration duration = [Link](time1, time2);

[Link]("\nTime Difference:");
[Link]("Hours: " + [Link]());
[Link]("Minutes: " + [Link]());
}
}

Output:

text

Date Difference:
Years: 5
Months: 0
Days: 5
Time Difference:
Hours: 4
Minutes: 245

9. Check valid email using regex

Aim:
To validate an email address using regular expressions.

Program:

java

import [Link];
import [Link];

public class EmailValidator {


public static void main(String[] args) {
String email = "test@[Link]";
String regex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";

Pattern pattern = [Link](regex);


Matcher matcher = [Link](email);

if ([Link]()) {
[Link](email + " is a valid email address.");
} else {
[Link](email + " is an invalid email address.");
}
}
}

Output:

text

test@[Link] is a valid email address.


10. Check Indian mobile number format using regex

Aim:
To validate Indian mobile numbers using regular expressions.

Program:

java

import [Link];
import [Link];

public class MobileNumberValidator {


public static void main(String[] args) {
String[] numbers = {
"9123456789",
"81234567890",
"7123456789",
"9876543210",
"1234567890"
};

String regex = "^[6-9]\\d{9}$";


Pattern pattern = [Link](regex);

for (String number : numbers) {


Matcher matcher = [Link](number);
[Link](number + " : " + ([Link]() ? "Valid"
: "Invalid"));
}
}
}

Output:

text

9123456789 : Valid
81234567890 : Invalid
7123456789 : Valid
9876543210 : Valid
1234567890 : Invalid
11. Write string to file using FileWriter

Aim:
To write text to a file using FileWriter .

Program:

java

import [Link];
import [Link];

public class FileWriterDemo {


public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("[Link]");
[Link]("Hello, World!\n");
[Link]("This is a file writing demo.\n");
[Link]("Using FileWriter in Java.");
[Link]();
[Link]("Successfully wrote to the file.");
} catch (IOException e) {
[Link]("An error occurred.");
[Link]();
}
}
}

Output:

text

Successfully wrote to the file.

File Content ([Link]):

text

Hello, World!
This is a file writing demo.
Using FileWriter in Java.

12. Read data from file using FileReader

Aim:
To read text from a file using FileReader .

Program:

java

import [Link];
import [Link];

public class FileReaderDemo {


public static void main(String[] args) {
try {
FileReader reader = new FileReader("[Link]");
int character;
[Link]("File Content:");
while ((character = [Link]()) != -1) {
[Link]((char) character);
}
[Link]();
} catch (IOException e) {
[Link]("An error occurred.");
[Link]();
}
}
}

Output:

text

File Content:
Hello, World!
This is a file writing demo.
Using FileWriter in Java.
13. Copy file content using byte streams

Aim:
To copy content from one file to another using byte streams.

Program:

java

import [Link];
import [Link];
import [Link];

public class FileCopyDemo {


public static void main(String[] args) {
try {
FileInputStream in = new FileInputStream("[Link]");
FileOutputStream out = new FileOutputStream("[Link]");

int byteData;
while ((byteData = [Link]()) != -1) {
[Link](byteData);
}

[Link]();
[Link]();
[Link]("File copied successfully!");
} catch (IOException e) {
[Link]("An error occurred.");
[Link]();
}
}
}

Output:

text

File copied successfully!


14. Display file properties using File class

Aim:
To display file properties like name, size, path, and existence.

Program:

java

import [Link];

public class FilePropertiesDemo {


public static void main(String[] args) {
File file = new File("[Link]");

[Link]("File Name: " + [Link]());


[Link]("File Path: " + [Link]());
[Link]("File Size: " + [Link]() + " bytes");
[Link]("File Exists: " + [Link]());
[Link]("Is Directory: " + [Link]());
[Link]("Is File: " + [Link]());
[Link]("Can Read: " + [Link]());
[Link]("Can Write: " + [Link]());
}
}

Output:

text

File Name: [Link]


File Path: C:\JavaProjects\[Link]
File Size: 65 bytes
File Exists: true
Is Directory: false
Is File: true
Can Read: true
Can Write: true
15. Demonstrate ByteArrayInputStream and
ByteArrayOutputStream

Aim:
To demonstrate the use of ByteArrayInputStream and ByteArrayOutputStream .

Program:

java

import [Link];
import [Link];
import [Link];

public class ByteArrayStreamDemo {


public static void main(String[] args) {
try {
// ByteArrayOutputStream demo
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String data = "Hello Byte Array Streams!";
[Link]([Link]());

[Link]("Data in ByteArrayOutputStream: " + [Link]


ing());
byte[] byteArray = [Link]();

// ByteArrayInputStream demo
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
[Link]("Data read from ByteArrayInputStream: ");

int character;
while ((character = [Link]()) != -1) {
[Link]((char) character);
}

[Link]();
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
Output:

text

Data in ByteArrayOutputStream: Hello Byte Array Streams!


Data read from ByteArrayInputStream: Hello Byte Array Streams!

16. Write text to file using [Link]()

Aim:
To write text to a file using FileChannel .

Program:

java

import [Link];
import [Link];
import [Link];

public class FileChannelWriteDemo {


public static void main(String[] args) {
try {
RandomAccessFile file = new RandomAccessFile("channel_output.tx
t", "rw");
FileChannel channel = [Link]();

String data = "Writing using FileChannel!\nThis is NIO demo.";


ByteBuffer buffer = [Link](1024);
[Link]([Link]());
[Link]();

[Link](buffer);
[Link]();
[Link]();

[Link]("Data written using FileChannel successfull


y!");
} catch (Exception e) {
[Link]();
}
}
}

Output:

text

Data written using FileChannel successfully!

17. Read file contents using [Link]()

Aim:
To read file contents using FileChannel .

Program:

java

import [Link];
import [Link];
import [Link];

public class FileChannelReadDemo {


public static void main(String[] args) {
try {
RandomAccessFile file = new RandomAccessFile("channel_output.tx
t", "r");
FileChannel channel = [Link]();
ByteBuffer buffer = [Link](1024);

int bytesRead = [Link](buffer);


[Link]();

[Link]("File content using FileChannel:");


while ([Link]()) {
[Link]((char) [Link]());
}

[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
}

Output:

text

File content using FileChannel:


Writing using FileChannel!
This is NIO demo.

18. Serialize a Student object using ObjectOutputStream

Aim:
To serialize a Student object to a file.

Program:

java

import [Link].*;

class Student implements Serializable {


private static final long serialVersionUID = 1L;
private int id;
private String name;
private transient String password; // transient field won't be serialized

public Student(int id, String name, String password) {


[Link] = id;
[Link] = name;
[Link] = password;
}

@Override
public String toString() {
return "Student{id=" + id + ", name='" + name + "', password='" + pas
sword + "'}";
}
}

public class SerializationDemo {


public static void main(String[] args) {
Student student = new Student(101, "John Doe", "secret123");

try {
FileOutputStream fileOut = new FileOutputStream("[Link]");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
[Link](student);
[Link]();
[Link]();
[Link]("Student object serialized successfully!");
} catch (IOException e) {
[Link]();
}
}
}

Output:

text

Student object serialized successfully!

19. Deserialize object using ObjectInputStream

Aim:
To deserialize a Student object from a file.

Program:

java

import [Link].*;

public class DeserializationDemo {


public static void main(String[] args) {
try {
FileInputStream fileIn = new FileInputStream("[Link]");
ObjectInputStream in = new ObjectInputStream(fileIn);
Student student = (Student) [Link]();
[Link]();
[Link]();

[Link]("Student object deserialized successfully!");


[Link]("Deserialized Student: " + student);
} catch (IOException | ClassNotFoundException e) {
[Link]();
}
}
}

Output:

text

Student object deserialized successfully!


Deserialized Student: Student{id=101, name='John Doe', password='null'}

20. Create simple window frame with a button

Aim:
To create a basic window frame with a button using Swing.

Program:

java

import [Link].*;

public class SimpleWindow {


public static void main(String[] args) {
JFrame frame = new JFrame("Simple Window");
JButton button = new JButton("Click Me!");

[Link](100, 100, 100, 40);


[Link](button);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

Output:
A window appears with a "Click Me!" button.

21. Display three buttons using FlowLayout

Aim:
To display three buttons using FlowLayout manager.

Program:

java

import [Link].*;
import [Link].*;

public class FlowLayoutDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout Demo");
[Link](new FlowLayout());

JButton btn1 = new JButton("Button 1");


JButton btn2 = new JButton("Button 2");
JButton btn3 = new JButton("Button 3");

[Link](btn1);
[Link](btn2);
[Link](btn3);

[Link](300, 200);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

Output:
A window with three buttons arranged in a flow layout.

22. Create GUI with JLabel , JTextField , JButton

Aim:
To create a GUI with label, text field, and button.

Program:

java

import [Link].*;
import [Link].*;

public class BasicGUI {


public static void main(String[] args) {
JFrame frame = new JFrame("Basic GUI");
[Link](new FlowLayout());

JLabel label = new JLabel("Enter your name:");


JTextField textField = new JTextField(15);
JButton button = new JButton("Submit");

[Link](label);
[Link](textField);
[Link](button);

[Link](300, 150);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

Output:
A window with label, text field, and submit button.
23. Use JPanel as container inside JFrame

Aim:
To demonstrate using JPanel as a container inside JFrame .

Program:

java

import [Link].*;
import [Link].*;

public class JPanelDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("JPanel Demo");
[Link](new BorderLayout());

JPanel panel = new JPanel();


[Link](Color.LIGHT_GRAY);
[Link](new Dimension(300, 200));

JButton btn1 = new JButton("Button 1");


JButton btn2 = new JButton("Button 2");

[Link](btn1);
[Link](btn2);

[Link](panel, [Link]);
[Link]();
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

Output:
A window with a gray panel containing two buttons.

24. Demonstrate FlowLayout , BorderLayout , GridLayout


Aim:
To demonstrate different layout managers.

Program:

java

import [Link].*;
import [Link].*;

public class LayoutManagersDemo {


public static void main(String[] args) {
// FlowLayout Demo
JFrame frame1 = new JFrame("FlowLayout");
[Link](new FlowLayout());
for (int i = 1; i <= 5; i++) {
[Link](new JButton("Button " + i));
}
[Link](300, 200);
[Link](true);
[Link](JFrame.DISPOSE_ON_CLOSE);

// BorderLayout Demo
JFrame frame2 = new JFrame("BorderLayout");
[Link](new BorderLayout());
[Link](new JButton("North"), [Link]);
[Link](new JButton("South"), [Link]);
[Link](new JButton("East"), [Link]);
[Link](new JButton("West"), [Link]);
[Link](new JButton("Center"), [Link]);
[Link](300, 200);
[Link](true);
[Link](JFrame.DISPOSE_ON_CLOSE);

// GridLayout Demo
JFrame frame3 = new JFrame("GridLayout");
[Link](new GridLayout(2, 3));
for (int i = 1; i <= 6; i++) {
[Link](new JButton("Btn " + i));
}
[Link](300, 200);
[Link](true);
[Link](JFrame.DISPOSE_ON_CLOSE);
}
}

Output:
Three windows showing different layout managers with buttons arranged
accordingly.

25. Login form GUI using GridLayout

Aim:
To create a login form using GridLayout .

Program:

java

import [Link].*;
import [Link].*;

public class LoginForm {


public static void main(String[] args) {
JFrame frame = new JFrame("Login Form");
[Link](new GridLayout(3, 2, 5, 5));

JLabel userLabel = new JLabel("Username:");


JTextField userField = new JTextField();
JLabel passLabel = new JLabel("Password:");
JPasswordField passField = new JPasswordField();
JButton loginBtn = new JButton("Login");
JButton cancelBtn = new JButton("Cancel");

[Link](userLabel);
[Link](userField);
[Link](passLabel);
[Link](passField);
[Link](loginBtn);
[Link](cancelBtn);

[Link](300, 150);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

Output:
A login form with username, password fields, and login/cancel buttons arranged in a
grid.

26. Show AWT components

Aim:
To demonstrate basic AWT components.

Program:

java

import [Link].*;

public class AWTComponents extends Frame {


public AWTComponents() {
setLayout(new FlowLayout());

Button btn = new Button("Click Me");


Label label = new Label("This is a label");
TextField textField = new TextField(20);
Checkbox checkbox = new Checkbox("Check me");

add(btn);
add(label);
add(textField);
add(checkbox);

setTitle("AWT Components");
setSize(300, 200);
setVisible(true);
}

public static void main(String[] args) {


new AWTComponents();
}
}

Output:
A window with AWT button, label, text field, and checkbox.

27. Handle button click event using AWT

Aim:
To handle button click events in AWT.

Program:

java

import [Link].*;
import [Link].*;

public class AWTEventHandling extends Frame {


private Label messageLabel;

public AWTEventHandling() {
setLayout(new FlowLayout());

Button btn = new Button("Click Me");


messageLabel = new Label("Click the button");

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Button was clicked!");
}
});

add(btn);
add(messageLabel);

setTitle("AWT Event Handling");


setSize(300, 200);
setVisible(true);

// Close window
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}

public static void main(String[] args) {


new AWTEventHandling();
}
}

Output:
A window with a button and label. When button is clicked, label text changes.

28. Create MenuBar with menus and menu items

Aim:
To create a menu bar with File menu containing Open, Save, Exit.

Program:

java

import [Link].*;

public class MenuBarDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Menu Bar Demo");

// Create MenuBar
JMenuBar menuBar = new JMenuBar();

// Create File menu


JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem exitItem = new JMenuItem("Exit");

[Link](openItem);
[Link](saveItem);
[Link]();
[Link](exitItem);

[Link](fileMenu);
[Link](menuBar);

[Link](400, 300);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

Output:
A window with File menu containing Open, Save, and Exit menu items.

29. Close window when "Exit" menu item is clicked

Aim:
To close the window when Exit menu item is clicked.

Program:

java

import [Link].*;
import [Link].*;

public class MenuExitDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Menu Exit Demo");

JMenuBar menuBar = new JMenuBar();


JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");

// Add action listener to exit item


[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](0);
}
});
[Link](exitItem);
[Link](fileMenu);
[Link](menuBar);

[Link](400, 300);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

Output:
Window closes when Exit menu item is clicked.

30. Use Swing components: JComboBox , JList , JTable

Aim:
To demonstrate Swing components: combo box, list, and table.

Program:

java

import [Link].*;
import [Link].*;

public class SwingComponentsDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Swing Components");
[Link](new FlowLayout());

// JComboBox
String[] countries = {"USA", "India", "UK", "Japan", "Germany"};
JComboBox<String> comboBox = new JComboBox<>(countries);

// JList
String[] colors = {"Red", "Green", "Blue", "Yellow", "Purple"};
JList<String> list = new JList<>(colors);
[Link](3);
JScrollPane listScrollPane = new JScrollPane(list);
// JTable
String[][] data = {
{"101", "John", "IT"},
{"102", "Alice", "HR"},
{"103", "Bob", "Finance"}
};
String[] columns = {"ID", "Name", "Department"};
JTable table = new JTable(data, columns);
JScrollPane tableScrollPane = new JScrollPane(table);

[Link](new JLabel("ComboBox:"));
[Link](comboBox);
[Link](new JLabel("List:"));
[Link](listScrollPane);
[Link](new JLabel("Table:"));
[Link](tableScrollPane);

[Link](500, 400);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

Output:
A window with combo box, list, and table components.

31. Display popup message using JOptionPane

Aim:
To display various types of popup messages using JOptionPane .

Program:

java

import [Link].*;

public class JOptionPaneDemo {


public static void main(String[] args) {
// Information message
[Link](null,
"This is an information message",
"Information",
JOptionPane.INFORMATION_MESSAGE);

// Warning message
[Link](null,
"This is a warning message",
"Warning",
JOptionPane.WARNING_MESSAGE);

// Error message
[Link](null,
"This is an error message",
"Error",
JOptionPane.ERROR_MESSAGE);

// Input dialog
String name = [Link]("Enter your name:");
if (name != null) {
[Link](null,
"Hello, " + name + "!",
"Greeting",
JOptionPane.PLAIN_MESSAGE);
}

// Confirmation dialog
int choice = [Link](null,
"Do you want to continue?",
"Confirmation",
JOptionPane.YES_NO_OPTION);

if (choice == JOptionPane.YES_OPTION) {
[Link](null, "You chose YES!");
} else {
[Link](null, "You chose NO!");
}
}
}

Output:
Series of popup dialogs showing different message types and user interactions.

You might also like