0% found this document useful (0 votes)
9 views

ALL NOTES BEFORE MIDTERM

Uploaded by

romanhasansourab
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 views

ALL NOTES BEFORE MIDTERM

Uploaded by

romanhasansourab
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
You are on page 1/ 17

BASIC JAVA PROGRAM

import java.util.Scanner;

public class DataTypesDemo {


public static void main(String[] args) {
// Creating a Scanner object for input
Scanner scanner = new Scanner(System.in);

// Prompting and taking input for an integer


System.out.print("Enter an integer: ");
int intValue = scanner.nextInt();

// Prompting and taking input for a float


System.out.print("Enter a float value: ");
float floatValue = scanner.nextFloat();

// Prompting and taking input for a double


System.out.print("Enter a double value: ");
double doubleValue = scanner.nextDouble();

// Prompting and taking input for a character


System.out.print("Enter a character: ");
char charValue = scanner.next().charAt(0);

// Clearing scanner buffer


scanner.nextLine(); // To consume the leftover newline

// Prompting and taking input for a string


System.out.print("Enter a string: ");
String stringValue = scanner.nextLine();

// Displaying the inputs back to the user


System.out.println("\nYou entered:");
System.out.println("Integer: " + intValue);
System.out.println("Float: " + floatValue);
System.out.println("Double: " + doubleValue);
System.out.println("Character: " + charValue);
System.out.println("String: " + stringValue);

// Closing the scanner


scanner.close();
}
}

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!

BASIC CODE TO FIND THE ASCII VALUE OF A CHARACTER :

import java.util.Scanner;

public class ASCIIValue {


public static void main(String[] args) {
// Creating a Scanner object for input
Scanner scanner = new Scanner(System.in);

// Prompting the user to enter a character


System.out.print("Enter a character: ");
char character = scanner.next().charAt(0);

// Finding the ASCII value of the character


int asciiValue = (int) character;

// Displaying the ASCII value


System.out.println("The ASCII value of '" + character + "' is: " + asciiValue);

}
}
JAVA CODE OF ARRAY PROMPT FROM USER AND SHOWING OUTPUT

import java.util.Scanner;

public class ArrayInputOutput {


public static void main(String[] args) {
// Creating a Scanner object for input
Scanner scanner = new Scanner(System.in);

// Prompting the user to enter the size of the array


System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();

// Declaring an array of the given size


int[] numbers = new int[size];

// Taking input for the array elements


System.out.println("Enter " + size + " numbers:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
}

// Displaying the array elements


System.out.println("The elements of the array are:");
for (int i = 0; i < size; i++) {
System.out.println("Element " + (i + 1) + ": " + numbers[i]);
}

}
}

LEFT ROTATE ARRAY

import java.util.Scanner;

public class LeftRotateArray {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Taking array size as input


System.out.println("Enter the size of the array: ");
int size = scanner.nextInt();

// Declaring the array


int[] array = new int[size];

// Taking array elements as input


System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

// Taking number of rotations as input


System.out.print("Enter the number of rotations: ");
int rotations = scanner.nextInt();

// Perform left rotations


for (int r = 0; r < rotations; r++) {
// Store the first element temporarily
int firstElement = array[0];

// Shift elements to the left using array[i] = array[i - 1] logic


for (int i = 1; i < size; i++) {
array[i - 1] = array[i];
}

// Place the first element at the end


array[size - 1] = firstElement;
}

// Displaying the rotated array


System.out.println("Array after " + rotations + " left rotations:");
for (int num : array) {
System.out.print(num + " ");
}

// Closing the scanner


scanner.close();
}
}

STACK

INFIX TO POSTFIX CONVERSION

Precedence Operator Type Associativity

15 () Parentheses Left to Right

[] Array subscript

· Member selection

14 ++ Unary post-increment Right to left

-- Unary post-decrement
13 ++ Unary pre-increment Right to left

-- Unary pre-decrement

+ Unary plus

- Unary minus

! Unary logical negation

~ Unary bitwise complement

(type) Unary type cast

12 * Multiplication Left to right

/ Division

% Modulus

11 + Addition Left to right

- Subtraction

10 << Bitwise left shift Left to right

>> Bitwise right shift with sign extension

>>> Bitwise right shift with zero extension

9 < Relational less than Left to right

<= Relational less than or equal

> Relational greater than

>= Relational greater than or equal

instanceof Type comparison (objects only)

8 == Relational is equal to Left to right

!= Relational is not equal to

7 & Bitwise AND Left to right

6 ^ Bitwise exclusive OR Left to right

5 | Bitwise inclusive OR Left to right

4 && Logical AND Left to right


3 || Logical OR Left to right

2 ?: Ternary conditional Right to left

1 = Assignment Right to left

+= 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/+

INFIX EXPRESSION : 8+(3*(4+2)-7)/(6-4)*9

POSTFIX EXPRESSION : 8342+*7-64-/9*+

1. ANSWER FORMAT

Conversion Process in Table Format:


Step Symbol Stack Postfix Expression Action

1 3 3 Operand, add to postfix

2 + + 3 Operator, push + onto the stack

3 4 + 3 4 Operand, add to postfix

4 * + * 3 4 Operator, push * onto the stack

5 ( + * ( 3 4 Left parenthesis, push ( onto the stack

6 2 + * ( 3 4 2 Operand, add to postfix

7 - + * ( 3 4 2 Operator, push - onto the stack


-
8 1 + * ( 3 4 2 1 Operand, add to postfix
-

9 ) + * 3 4 2 1 - Right parenthesis, pop -, add to postfix

10 * + * 3 4 2 1 - * Operator *, pop it and add to postfix

11 / + / 3 4 2 1 - * 5 Operator /, push / onto the stack

12 5 + / 3 4 2 1 - * 5 Operand, add to postfix

13 End 3 4 2 1 - * 5 / Pop remaining operators + and / and add to postfix


+

Final Postfix Expression:

3 4 2 1 - * 5 / +

TASK 02

Infix Expression:

8 + (3 * (4 + 2) - 7) / (6 - 4) * 9

Step-by-Step Conversion (Table Format):

Step Symbol Stack Postfix Action


Expression

1 8 8 Operand, add to postfix

2 + + 8 Operator +, push to stack

3 ( + ( 8 Left parenthesis (, push to


stack

4 3 + ( 8 3 Operand 3, add to postfix


5 * + ( * 8 3 Operator *, push to stack

6 ( + ( * 8 3 Left parenthesis (, push to


( stack

7 4 + ( * 8 3 4 Operand 4, add to postfix


(

8 + + ( * 8 3 4 Operator +, push to stack


( +

9 2 + ( * 8 3 4 2 Operand 2, add to postfix


( +

10 ) + ( * 8 3 4 2 + Right parenthesis ), pop +,


add to postfix

11 * + ( * 8 3 4 2 + * Operator *, add * to postfix


(no pop needed)

12 - + ( - 8 3 4 2 + * Operator -, pop * and add it


to postfix, then push -

13 7 + ( - 8 3 4 2 + * 7 Operand 7, add to postfix

14 ) + 8 3 4 2 + * 7 - Right parenthesis ), pop -,


add to postfix

15 / + / 8 3 4 2 + * 7 - Operator /, push / onto stack


16 ( + / ( 8 3 4 2 + * 7 - Left parenthesis (, push to
stack

17 6 + / ( 8 3 4 2 + * 7 - Operand 6, add to postfix


6

18 - + / ( 8 3 4 2 + * 7 - Operator -, push - onto stack


- 6

19 4 + / ( 8 3 4 2 + * 7 - Operand 4, add to postfix


- 6 4

20 ) + / 8 3 4 2 + * 7 - Right parenthesis ), pop -,


6 4 - add to postfix

21 * + * 8 3 4 2 + * 7 - Operator *, push * onto stack


6 4 - /

22 9 + * 8 3 4 2 + * 7 - Operand 9, add to postfix


6 4 - / 9

23 End 8 3 4 2 + * 7 - Pop all remaining operators


6 4 - / 9 * + (*, /, +) and add to postfix

Final Postfix Expression:

8 3 4 2 + * 7 - 6 4 - / 9 * +
LINKED LIST :

import java.util.LinkedList;

public class LinkedListOperations {

public static void main(String[] args) {

LinkedList<Integer> list = new LinkedList<>();

1. Add elements to the linked list

list.add(10);

list.add(20);

list.add(30);

list.add(40);

System.out.println("Original Linked List:");

for (Integer element : list) {

System.out.print(element + " -> ");

System.out.println("None");

if (list.contains(20)) {

list.remove(Integer.valueOf(20));

} else {

System.out.println("Element 20 not found in the list.");


}

System.out.println("\nAfter deleting element 20:");

for (Integer element : list) {

System.out.print(element + " -> ");

System.out.println("None");

int indexX = list.indexOf(10);

int indexY = list.indexOf(40);

if (indexX != -1 && indexY != -1) {

list.set(indexX, 40);

list.set(indexY, 10);

} else {

System.out.println("One or both values not found.");

System.out.println("\nAfter swapping elements 10 and 40:");

for (Integer element : list) {

System.out.print(element + " -> ");

System.out.println("None");
int value = 30;

boolean found = list.contains(value);

System.out.println("\nIs " + value + " in the list? " + (found ? "Yes"


: "No"));

BUBBLE SORT EXAMPLE

public class BubbleSortExample {

// Method to perform Bubble Sort

public static void bubbleSort(int[] arr) {

int n = arr.length;

for (int i = 0; i < n - 1; i++) {

boolean swapped = false; // Track if any swaps occur

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

// Swap arr[j] and arr[j + 1]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

swapped = true;

// If no two elements were swapped, the array is sorted

if (!swapped) {

break;

}
}

// Main method to test the sorting

public static void main(String[] args) {

int[] arr = {64, 34, 25, 12, 22, 11, 90};

System.out.println("Original array:");

for (int num : arr) {

System.out.print(num + " ");

bubbleSort(arr);

System.out.println("\n\nSorted array:");

for (int num : arr) {

System.out.print(num + " ");

}
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;

public class LinkedListTest {

public static void main(String[] args) {

// Create a new linked list of Strings

LinkedList<String> list = new LinkedList<>();

// Add nodes with values "Alice", "Bob", and "Charlie"

list.add("Alice");

list.add("Bob");

list.add("Charlie");
// Print the initial list

System.out.println("Initial list:");

printList(list);

// Replace "Bob" with "David"

replaceNode(list, "Bob", "David");

System.out.println("After replacing 'Bob' with 'David':");

printList(list);

// Rename the first node to "Alicia"

renameNode(list, 0, "Alicia");

System.out.println("After renaming the first node to 'Alicia':");

printList(list);

// Swap nodes containing "Alicia" and "Charlie"

swapNodes(list, 0, 2); // Swapping nodes at indices 0 and 2

System.out.println("After swapping nodes containing 'Alicia' and


'Charlie':");

printList(list);

// Remove the node containing "David"

removeNode(list, "David");

System.out.println("After removing node containing 'David':");

printList(list);

// Method to print the list


public static void printList(LinkedList<String> list) {

for (String node : list) {

System.out.print(node + " -> ");

System.out.println("null");

// Replace method: Replace a node's value

public static void replaceNode(LinkedList<String> list, String oldValue,


String newValue) {

int index = list.indexOf(oldValue);

if (index != -1) {

list.set(index, newValue);

} else {

System.out.println("Node with value " + oldValue + " not found.");

// Rename method: Rename a node (change value to a new string) by index

public static void renameNode(LinkedList<String> list, int index, String


newValue) {

if (index >= 0 && index < list.size()) {

list.set(index, newValue);

} else {

System.out.println("Node at index " + index + " not found.");

}
// Swap method: Swap two nodes by their indices

public static void swapNodes(LinkedList<String> list, int index1, int


index2) {

if (index1 >= 0 && index1 < list.size() && index2 >= 0 && index2 <
list.size()) {

String temp = list.get(index1);

list.set(index1, list.get(index2));

list.set(index2, temp);

} else {

System.out.println("Invalid indices for swapping.");

// Remove method: Remove a node by its value

public static void removeNode(LinkedList<String> list, String value) {

if (list.contains(value)) {

list.remove(value);

} else {

System.out.println("Node with value " + value + " not found.");

You might also like