Record Lab
Record Lab
import java.io.*;
import java.util.Scanner;
try {
// Read from file
scanner = new Scanner(new File(inputFile));
String content = "";
while (scanner.hasNextLine()) {
content += scanner.nextLine() + "\n";
}
// Write to file
writer = new FileWriter(outputFile);
writer.write(content);
} catch (FileNotFoundException e) {
System.err.println("Error: Input file not found - " + e.getMessage());
} catch (IOException e) {
System.err.println("Error: IO exception - " + e.getMessage());
} finally {
// Close resources in the finally block
if (scanner != null) {
scanner.close();
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
System.err.println("Error closing FileWriter - " + e.getMessage());
}
}
}}}
OUTPUT
PROGRAM
import java.lang.Thread;
import java.util.Random;
import java.lang.Math;
while(true){
int num = rand.nextInt(300);
if(num%2 == 0){
Even even = new Even(num);
even.start();
}else{
Odd odd = new Odd(num);
odd.start();
}
try{
this.sleep(1000);
}catch(Exception e){
System.out.println("An error occurred");
}
}
}
}
Odd(int num){
this.num = num;
}
Even(int num){
this.num = num;
}
public void run(){
System.out.println(Math.pow(this.num, 2));
}
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
counter.increment();
System.out.println("Count " + counter.getCount());
}
}
}
return nums;
}
int sum = 0;
for(int i=0; i<nums.length; i++) {
System.out.println(nums[i]);
sum += nums[i];
}
}
OUTPUT
PROGRAM
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
//setting up buttons
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(5, 4));
num.addActionListener(this);
buttonsPanel.add(num);
}
rootPanel.add(buttonsPanel);
this.add(rootPanel);
public TrafficLight() {
setTitle("Traffic Light Simulator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 400);
setLayout(new BorderLayout());
redButton.addActionListener(this);
yellowButton.addActionListener(this);
greenButton.addActionListener(this);
add(controlPanel, BorderLayout.NORTH);
add(lightPanel, BorderLayout.CENTER);
setVisible(true);
}
public TrafficLightPanel() {
setPreferredSize(new Dimension(100, 300));
setColors(Color.LIGHT_GRAY, Color.LIGHT_GRAY, Color.LIGHT_GRAY);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color1);
g.fillOval(x, y, diameter, diameter);
g.setColor(color2);
g.fillOval(x, y + diameter + 10, diameter, diameter);
g.setColor(color3);
g.fillOval(x, y + 2 * (diameter + 10), diameter, diameter);
}
}
OUTPUT
PROGRAM
import java.util.*;
class QuickSort {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.print("\nEnter the number of strings: ");
int n = scan.nextInt();
String array[] = new String[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter the string " + (i + 1) + ": ");
array[i] = scan.next();
}
scan.close();
QuickSort quicksort = new QuickSort();
quicksort.qSort(array, 0, (n - 1));
System.out.println("\nSORTED ARRAY");
printArray(array);
}
public int partition(String array[], int low, int high) {
String pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j].compareTo(pivot) < 0) {
i++;
String temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
String temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return i + 1;
}
public void qSort(String array[], int low, int high) {
if (low < high) {
int pi = partition(array, low, high);
qSort(array, low, pi - 1);
qSort(array, pi + 1, high);
}
}
static void printArray(String array[]) {
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
}
}
OUTPUT
PROGRAM
import java.util.*;
class DList {
static LinkedList<Integer> list = new LinkedList<Integer>();
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int element;
while (true) {
System.out.print("\nSelect an operation: \n");
System.out.print(
"\n1.Add element to List" +
"\n2.Delete from front" +
"\n3.Delete from end" +
"\n4.Delete from a position" +
"\n5.Display List" +
"\n6.Exit" +
"\n\nEnter an operation number: "
);
int choice = scan.nextInt();
switch (choice) {
case 1:
// Add element to list
System.out.print("\nEnter element to add: ");
element = scan.nextInt();
list.add(element);
System.out.println("[" + element + " added to list]");
display();
break;
case 2:
// Delete from front
try {
element = list.getFirst();
list.removeFirst();
System.out.println("[" + element + " deleted from list]");
display();
} catch (NoSuchElementException e) {
System.out.println("[List is empty]");
}
break;
case 3:
// Delete from end
try {
element = list.getLast();
list.removeLast();
System.out.println("[" + element + " deleted from list]");
display();
} catch (NoSuchElementException e) {
System.out.println("[List is empty]");
}
break;
case 4:
// Delete from a position
System.out.print("\nEnter position: ");
int position = scan.nextInt();
try {
element = list.get(position - 1);
list.remove(position - 1);
System.out.println("[" + element + " deleted from list]");
display();
} catch (IndexOutOfBoundsException e) {System.out.println("[Invalid
position]");
}
break;
case 5:
// Display list
display();
break;
case 6:
// Exit
scan.close();
System.exit(0);
break;
default:
System.out.println("[Invalid choice. Please try again]");
}
}
}
static void display() {
System.out.print("\nList: ");
Iterator<Integer> itr = list.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
// for (int items : list)
// System.out.print(items + " ");
// System.out.println();
}
}
OUTPUT