Java Programs with BlueJ-Style Output (Programs 1-5)
1. Deque Operations (Insertion and Deletion)
// Java program to demonstrate Deque Operations
import [Link].*;
class DequeOperations {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Deque<Integer> deque = new ArrayDeque<>();
[Link]("Enter number of elements to add:");
int n = [Link]();
[Link]("Enter elements to add from right:");
for (int i = 0; i < n; i++) {
[Link]([Link]());
[Link]("Deque after adding elements: " + deque);
[Link]("Enter element to add from left:");
[Link]([Link]());
[Link]("Deque after adding element from left: " + deque);
[Link]("Removing element from right...");
[Link]();
[Link]("Deque after removing element from right: " + deque);
[Link]("Removing element from left...");
[Link]();
[Link]("Deque after removing element from left: " + deque);
BlueJ Console Output:
Enter number of elements to add:
Enter elements to add from right:
5 10 15
Deque after adding elements: [5, 10, 15]
Enter element to add from left:
20
Deque after adding element from left: [20, 5, 10, 15]
Removing element from right...
Deque after removing element from right: [20, 5, 10]
Removing element from left...
Deque after removing element from left: [5, 10]
Variable Description Table:
Variable Name Variable Type Usage
deque Deque<Integer> To store elements in double-ended queue
n int Number of elements to add to deque
sc Scanner To take user inputs
2. Common Words in Two Sentences
// Java program to find common words in two sentences
import [Link].*;
class CommonWords {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the first sentence:");
String sentence1 = [Link]().toLowerCase();
[Link]("Enter the second sentence:");
String sentence2 = [Link]().toLowerCase();
String[] words1 = [Link](" ");
String[] words2 = [Link](" ");
HashSet<String> commonWords = new HashSet<>();
for (String word : words1) {
for (String word2 : words2) {
if ([Link](word2)) {
[Link](word);
}
}
[Link]("The common words are:");
for (String word : commonWords) {
[Link](word);
BlueJ Console Output:
Enter the first sentence:
Today is a holiday
Enter the second sentence:
Today is our last day of school life
The common words are:
today
is
Variable Description Table:
Variable Name Variable Type Usage
sentence1 String To store the first sentence
sentence2 String To store the second sentence
words1 String[] Array of words from the first sentence
words2 String[] Array of words from the second sentence
commonWords HashSet<String> To store the common words
3. Character Sorting in a Sentence
// Java program to sort characters in a sentence
import [Link].*;
class SortCharacters {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence ending with '@', '.', ',', or '!':");
String sentence = [Link]();
char[] characters = [Link]("[@.,!]", "").toCharArray();
[Link](characters);
[Link]("Sorted characters in ascending order:");
[Link](new String(characters));
BlueJ Console Output:
Enter a sentence ending with '@', '.', ',', or '!':
Hello, World!
Sorted characters in ascending order:
HWdellloor
Variable Description Table:
Variable Name Variable Type Usage
sentence String To store the input sentence
characters char[] Array of characters from the sentence