Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

slip 29

2
import java.util.LinkedList;
public class LinkedListDemo
{
public static void main(String[] args)
{
// Create a LinkedList of integers
LinkedList<Integer> list = new LinkedList<>();
// Add element at the first position
list.addFirst(10);
list.addFirst(20);
list.addFirst(30);
System.out.println("LinkedList after adding elements at first position: " +list);
// Delete the last element
list.removeLast();
System.out.println("LinkedList after deleting last element: " + list);
// Display the size of LinkedList
System.out.println("Size of LinkedList: " + list.size());
}
}

slip 28
1
public class CurrentThreadName {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("Currently executing thread: " + t.getName());
}
}

slip 23
1
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine().toLowerCase(); // convert input tolowercase

for (int i = 0; i < inputString.length(); i++)


{
char c = inputString.charAt(i);
if (isVowel(c))
{
System.out.print(c + " ");
try
{
Thread.sleep(3000); // pause for 3 seconds
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static boolean isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
}

slip 21
1
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class SubjectNames {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of subjects: ");
int n = sc.nextInt();
LinkedList<String> subjects = new LinkedList<String>();
System.out.println("Enter the subject names:");
for (int i = 0; i < n; i++) {
String subject = sc.next();
subjects.add(subject);
}
System.out.println("Subject names:");
Iterator<String> iterator = subjects.iterator();
while (iterator.hasNext()) {
String subject = iterator.next();
System.out.println(subject);
}
}
}

slip 19
1
import java.util.LinkedList;
import java.util.Scanner;
public class NegativeIntegersInLinkedList {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
LinkedList<Integer> list = new LinkedList<Integer>();
System.out.print("Enter the number of integers: ");
int n = sc.nextInt();
System.out.println("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
list.add(num);
}
System.out.println("Negative Integers:");
for (int i : list) {
if (i < 0) {
System.out.println(i);
}
}
}
}

slip 18
1
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine().toLowerCase(); // convert input tolowercase

for (int i = 0; i < inputString.length(); i++)


{
char c = inputString.charAt(i);
if (isVowel(c))
{
System.out.print(c + " ");
try
{
Thread.sleep(3000); // pause for 3 seconds
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static boolean isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
}
slip 17
1
import java.util.*;
public class SortedIntegers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of integers: ");
int n = scanner.nextInt();
Set<Integer> set = new TreeSet<>();
for (int i = 1; i <= n; i++) {
System.out.print("Enter integer #" + i + ": ");
int num = scanner.nextInt();
set.add(num);
}
System.out.println("The integers in sorted order are: ");
for (int num : set) {
System.out.print(num + " ");
}
}
}

slip 16
1
import java.util.*;
import java.io.*;
public class Collection {
public static void main(String args[]) throws IOException {
Set ts = new TreeSet();
ts.add("Red");
ts.add("Blue");
ts.add("Yellow");
ts.add("Pink");
ts.add("Baby Pink");
System.out.println("TreeSet in ascending order: " + ts);
}
}

slip 15
1
public class main {
public static void main(String[] args) {
// Create a new thread
Thread myThread = new MyThread();

// Set the name and priority of the thread


myThread.setName("MyThread");
myThread.setPriority(Thread.MAX_PRIORITY);

// Start the thread


myThread.start();

// Display the name and priority of the thread


System.out.println("Thread Name: " + myThread.getName());
System.out.println("Thread Priority: " + myThread.getPriority());
}
}

class MyThread extends Thread {


@Override
public void run() {
// Perform thread operations here
System.out.println("MyThread is running");
}
}
slip 8
1
public class A1 extends Thread {
String str;
int n;

A1(String str, int n) {


this.str = str;
this.n = n;
}

public void run() {


try {
for (int i = 0; i < n; i++) {
System.out.println(getName() + " : " + str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
A1 t1 = new A1("COVID19", 10);
A1 t2 = new A1("LOCKDOWN2020", 20);
A1 t3 = new A1("VACCINATED", 30);

t1.start();
t2.start();
t3.start();

}
}

slip 7
1

import java.util.Random;
class Square extends Thread
{
int x;
Square(int n)
{
x = n;
}
public void run()
{
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr );
}
}
class Cube extends Thread
{
int x;
Cube(int n)
{x = n;
}
public void run()
{
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
}
}
class Number extends Thread
{
public void run()
{
Random random = new Random();
for(int i =0; i<5; i++)
{
int randomInteger = random.nextInt(100);
System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
}
public class Thr {
public static void main(String args[])
{
Number n = new Number();
n.start();
}
}
slip 5
1

// Java Program to Demonstrate Getting Keys


// as an Enumeration of Hashtable class

// Importing required classes


import java.io.*;
import java.util.*;

// Main class
class GFG {

// Main driver method


public static void main(String[] args)
{
// Creating an empty hashtable
Hashtable<String, String> ht= new Hashtable<String, String>();

// Inserting key-value pairs into hash table


// using put() method
ht.put("Name", "Rohan");
ht.put("Mpbile_Nos", "8446049402");

// Now creating an Enumeration object


// to store keys
Enumeration<String> e = ht.keys();

// Condition holds true till there is


// single key remaining
while (e.hasMoreElements()) {

// Getting key
String key = e.nextElement();

// Printing key and value corresponding to


// that key
System.out.println(key + ":" + ht.get(key));
}
}
}

slip 2
1

// Java program to sort a HashSet

import java.util.*;

public class GFG {


public static void main(String args[])
{
// Creating a HashSet
HashSet<String> set = new HashSet<String>();

// Adding elements into HashSet using add()


set.add("geeks");
set.add("practice");
set.add("contribute");
set.add("ide");

System.out.println("Original HashSet: "


+ set);

// Sorting HashSet using List


List<String> list = new ArrayList<String>(set);
Collections.sort(list);

// Print the sorted elements of the HashSet


System.out.println("HashSet elements "
+ "in sorted order "
+ "using List: "
+ list);
}
}

You might also like