quicksort
1. **quickSort(arr, left, right):**
- If `left` is less than `right`:
- Calculate the partition index using the partition method.
- Recursively call `quickSort` for the subarrays to the left and right of the partition index.
2. **partition(arr, left, right):**
- Choose `arr[right]` as the pivot.
- Initialize `i` to `left - 1`.
- Iterate `j` from `left` to `right - 1`:
- If `arr[j]` is less than the pivot, increment `i` and swap `arr[i]` with `arr[j]`.
- Swap `arr[i + 1]` with `arr[right]`.
- Return `i + 1` as the partition index.
3. **main method:**
- Initialize an array of names.
- Call `quickSort` to sort the array.
- Print the sorted array.
.
token
1. Start
2. Prompt the user to enter a line of integers separated by spaces.
3. Read the input line from the user.
4. Create a StringTokenizer object with the input line.
5. Initialize a variable `sum` to 0 to store the sum of integers.
6. Print "Individual Integers:" to indicate the start of individual integers display.
7. While there are more tokens in the StringTokenizer:
1. Get the next token using the `nextToken()` method.
2. Convert the token to an integer using `[Link]()`.
3. Print the integer.
4. Add the integer to the `sum`.
8. Print "Sum of Integers: " followed by the value of `sum`.
9. End.
inheritance and polymorphism:
1. Define a base class `Animal` with a method `sound`.
2. Define derived class `Dog` that extends `Animal`.
3. Override the `sound` method in the `Dog` class to print "Woof".
4. Define derived class `Cat` that extends `Animal`.
5. Override the `sound` method in the `Cat` class to print "Meow".
6. In the `Main` class:
- Create an object of type `Dog` and store it in a variable of type `Animal`.
- Create an object of type `Cat` and store it in another variable of type `Animal`.
- Call the `sound` method on each animal object.
7. End.
DLL
1. Define a `Node` class with fields `data`, `prev`, and `next`.
2. Define a `DoublyLinkedList` class with fields `head` and `tail`.
3. Implement a `insert(int data)` method in the `DoublyLinkedList` class to insert a new node at the
end of the list.
4. Implement a `delete(int key)` method in the `DoublyLinkedList` class to delete a node with the
given key from the list.
5. Implement a `display()` method in the `DoublyLinkedList` class to display the contents of the list.
6. In the `Main` class:
- Create a new `DoublyLinkedList` instance.
- Insert elements into the list.
- Display the original list.
- Delete a specific element from the list.
- Display the list after deletion.
7. End.
traffic light:
1. Create a `TrafficLightSimulator` class that extends `JFrame` and implements the `ActionListener`
interface.
2. Inside the `TrafficLightSimulator` class:
- Declare variables for radio buttons representing red, yellow, and green lights, and a panel for
displaying the selected light.
- Create a constructor for initializing the GUI components.
- Implement the `actionPerformed` method to handle button clicks:
- Remove any existing light from the light panel.
- Based on the clicked button, add the corresponding light to the light panel.
- Repaint the light panel to reflect changes.
3. Create `Light`, `RedLight`, `YellowLight`, and `GreenLight` classes:
- `Light` class defines a base panel for displaying the lights.
- `RedLight`, `YellowLight`, and `GreenLight` classes extend `Light` and set the color of the
respective lights.
4. In the `main` method:
- Create an instance of `TrafficLightSimulator`.
- Set the visibility of the simulator frame to true.
5. End.
file
1. Define input and output file paths.
2. Open a try-catch block to handle file-related exceptions.
3. Inside the try block:
- Create FileReader and BufferedReader objects to read from the input file.
- Create FileWriter and BufferedWriter objects to write to the output file.
- Read each line from the input file using BufferedReader's `readLine()` method.
- Write each line to the output file using BufferedWriter's `write()` method and add a new line using
`newLine()`.
4. Close the BufferedReader and BufferedWriter objects using their `close()` method to release
resources.
5. Catch FileNotFoundException if the input file is not found, and IOException for other file-related
errors.
6. Print appropriate error messages if any exceptions occur during file operations.
7. End.
calculator
1. Create a class `Calculator` that extends `JFrame` and implements the `ActionListener` interface.
2. Inside the `Calculator` class:
- Declare instance variables for a JTextField to display the result, arrays of JButtons for digits and
operations, and variables to store current input, result, and operation.
- Implement a constructor to initialize the GUI components and set up the layout.
- Implement the `actionPerformed` method to handle button clicks:
- If a digit button is clicked, append the digit to the current input and update the display field.
- If an operation button is clicked:
- Evaluate the current input and perform the corresponding operation if it's an arithmetic
operation.
- Handle exceptions like NumberFormatException and ArithmeticException for invalid input or
division by zero.
- Update the display field with the result.
- Implement the `main` method to create an instance of the `Calculator` class and make it visible.
3. End.
Try catch finally
1. Define a class `FileReadExample`.
2. Inside the `FileReadExample` class:
- Define a method `readFile(String fileName)` that takes a file name as input and reads the contents
of the file.
- Open the file using a BufferedReader in a try block.
- Read each line from the file and print it to the console.
- Close the BufferedReader in the finally block to ensure proper cleanup, even if an exception
occurs during file reading.
- Define the `main` method.
- Call the `readFile` method with the file name "[Link]".
- Catch any IOException that may occur during file reading and print an error message.
- The finally block is always executed regardless of whether an exception occurred or not, and it
prints a message indicating its execution.
3. End.