ALL NOTES BEFORE MIDTERM
ALL NOTES BEFORE MIDTERM
import java.util.Scanner;
EXPLATION: This Java program demonstrates the usage of various data types, including int, float, double,
char, and String. The program begins by importing the Scanner class, which is essential for taking user
inputs. Inside the main method, a Scanner object is created to facilitate reading data from the console. The
program then prompts the user to enter values for each data type in sequence.
An int data type is used to store whole numbers like 42. Next, a float variable is used for storing
single-precision decimal numbers, such as 3.14. The double data type, which allows for higher precision,
stores more accurate decimal values like 2.71828. The program then reads a single character using the
char data type, capturing a single letter or symbol, such as A. Finally, it uses the String data type to store a
sequence of characters, which could represent text input like "Hello, Java!".
After gathering all inputs, the program displays them back to the user, highlighting the values entered for
each data type. The scanner.nextLine() method is used strategically to clear the input buffer, ensuring the
program functions correctly when transitioning from numeric inputs to string inputs. Finally, the
scanner.close() method is called to release the resources used by the Scanner. This program is a
foundational example of handling different data types in Java, providing a clear understanding of how to
interact with various inputs and outputs effectively.
INPUT :
Enter an integer: 42
Enter a float value: 3.14
Enter a double value: 2.71828
Enter a character: A
Enter a string: Hello, Java!
OUTPUT :
You entered:
Integer: 42
Float: 3.14
Double: 2.71828
Character: A
String: Hello, Java!
import java.util.Scanner;
}
}
JAVA CODE OF ARRAY PROMPT FROM USER AND SHOWING OUTPUT
import java.util.Scanner;
}
}
import java.util.Scanner;
STACK
[] Array subscript
· Member selection
-- Unary post-decrement
13 ++ Unary pre-increment Right to left
-- Unary pre-decrement
+ Unary plus
- Unary minus
/ Division
% Modulus
- Subtraction
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
REF
https://www.web4college.com/converters/infix-to-postfix-prefix.php
INFIX : 3+4*(2-1)/5
POSTFIX : 3421-*5/+
1. ANSWER FORMAT
3 4 2 1 - * 5 / +
TASK 02
Infix Expression:
8 + (3 * (4 + 2) - 7) / (6 - 4) * 9
8 3 4 2 + * 7 - 6 4 - / 9 * +
LINKED LIST :
import java.util.LinkedList;
list.add(10);
list.add(20);
list.add(30);
list.add(40);
System.out.println("None");
if (list.contains(20)) {
list.remove(Integer.valueOf(20));
} else {
System.out.println("None");
list.set(indexX, 40);
list.set(indexY, 10);
} else {
System.out.println("None");
int value = 30;
int n = arr.length;
arr[j + 1] = temp;
swapped = true;
if (!swapped) {
break;
}
}
System.out.println("Original array:");
bubbleSort(arr);
System.out.println("\n\nSorted array:");
}
LINKED LIST FOR STRING DATATYPES
write down the code also by simulating a simple linked list that can perform
the following operations: add a node, replace a node's value, rename a node
(change the value to a new string), swap two nodes, and remove a node.
Implement a singly linked list class in Java with the following
functionalities:
Add: Insert a new node at the end of the list. Add nodes with values "Alice",
"Bob", and "Charlie".
Replace: Change the value of a specified node. Replace "Bob" with "David".
Rename: Change the value of a node identified by its index to a new string.
Rename the first node to "Alicia".
Swap: Exchange the values of two nodes by their indices. Swap the nodes
containing "Alicia" and "Charlie".
Remove: Delete a node from the list by its value. Remove the node containing
"David".
import java.util.LinkedList;
list.add("Alice");
list.add("Bob");
list.add("Charlie");
// Print the initial list
System.out.println("Initial list:");
printList(list);
printList(list);
renameNode(list, 0, "Alicia");
printList(list);
printList(list);
removeNode(list, "David");
printList(list);
System.out.println("null");
if (index != -1) {
list.set(index, newValue);
} else {
list.set(index, newValue);
} else {
}
// Swap method: Swap two nodes by their indices
if (index1 >= 0 && index1 < list.size() && index2 >= 0 && index2 <
list.size()) {
list.set(index1, list.get(index2));
list.set(index2, temp);
} else {
if (list.contains(value)) {
list.remove(value);
} else {