Skip to content

Commit 017d2f3

Browse files
authored
Add files via upload
0 parents  commit 017d2f3

14 files changed

+601
-0
lines changed

GenPermutations.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
import java.util.stream.*;
3+
public class GenPermutations {
4+
public static void main(String[] args){
5+
Scanner sc = new Scanner(System.in);
6+
String S = sc.next();
7+
8+
char[] arr = S.toCharArray();
9+
findAllPermutations(arr, arr.length, arr.length);
10+
}
11+
static void findAllPermutations(char[] arr, int size, int n){
12+
if(size == 1) {Stream.of(arr).forEach(System.out::print);
13+
System.out.println();
14+
}
15+
for(int i=0;i<size;i++){
16+
findAllPermutations(arr, size-1, n);
17+
18+
if(size % 2 ==1){
19+
char temp = arr[0];
20+
arr[0] = arr[size-1];
21+
arr[size-1] = temp;
22+
}else{
23+
char temp = arr[i];
24+
arr[i] = arr[size-1];
25+
arr[size-1] = temp;
26+
}
27+
}
28+
}
29+
30+
}

Java_Classes.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
interface Animals {
2+
// (1) Every method in the implementing class should be noted here first
3+
//Like this
4+
void makeSound();
5+
}
6+
class Dog implements Animals{
7+
public void makeSound(){
8+
System.out.println("woof");
9+
}
10+
}
11+
class Cat implements Animals{
12+
public void makeSound(){
13+
System.out.println("meow");
14+
}
15+
}
16+
class Java_Classes{
17+
public static void main(String [] ags){
18+
Animals[] anims = new Dogs[2]; // (2) Will not work
19+
Animals[] anims = new Animals[2]; //this will
20+
anims[0] = new Cat();
21+
anims[1] = new Dog();
22+
anims[0].makeSound();
23+
anims[1].makeSound();
24+
25+
}
26+
}

Java_Hard_Lessons.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
3+
Concepts to be learned
4+
5+
1. main args array contains nothing unless some args are provided while running java .class. [args]
6+
2. Exceptions class throwing exception in try catch and finally, inside a method
7+
3. inheritance, extends, implements, abstract extension
8+
4. final static method works
9+
5. SQL statement query result format
10+
6. main class name works
11+
7. If there are two different classes, each with their own main methods, then either one can be run after choosing the class file to run after compilation, but the CLASS with NAME of file should have higher access specifier
12+
8. null comaprator
13+
9. copyOnWrite method
14+
10. System.console
15+
*/
16+
import java.io.Console;
17+
import java.util.*;
18+
import java.util.concurrent.CopyOnWriteArrayList;
19+
import java.util.logging.ConsoleHandler;
20+
import java.util.stream.*;
21+
22+
class Java_Hard_Lessons{
23+
public final static void main(String[] args){
24+
System.out.println("inside jhl \n"+args.length);
25+
int []arr = {1,5,6,4,6,8,3,2,545,8,8};
26+
//Arrays.sort(arr, null); //doesn't work
27+
List<Integer> li = new ArrayList();
28+
li.add(2);
29+
li.add(1);
30+
li.add(3);
31+
//List<Integer> nli = CopyOnWriteArrayList(li);
32+
System.out.println(li);
33+
Console iConsole = System.console(); //read more
34+
System.out.println(iConsole.toString());
35+
}
36+
}
37+
38+
class differentClass{
39+
public static void main(String []args){
40+
System.out.println("inside dc");
41+
}
42+
}
43+
class main extends differentClass{
44+
45+
/*public static void main(String[] args){
46+
System.out.println("inside main class");
47+
}*/
48+
/* On commenting the main method after extending differentClass, the main method of diffClass is called. If not commented, then main method is overwritten
49+
*/
50+
}
51+
52+
abstract class Animals{
53+
void sound(){
54+
System.out.println("Animal sounds");
55+
}
56+
}
57+
interface Fauna{
58+
void makeSound();
59+
}
60+
interface Flora{
61+
void comm();
62+
}
63+
class Dogs extends Animals{}
64+
65+
//class Cats implements Animals{} // Doesn't work, implementation is for interfaces
66+
67+
class Cats implements Fauna, Flora{
68+
public void makeSound(){System.out.println("Meow");}
69+
public void comm(){System.out.println("comm Meow");}
70+
}

Java_Synchronized.java

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// A Java program to demonstrate working of
2+
// synchronized.
3+
import java.io.*;
4+
import java.util.*;
5+
6+
// A Class used to send a message
7+
class Sender
8+
{
9+
public void send(String msg)
10+
{
11+
System.out.println("Sending\t" + msg );
12+
try
13+
{
14+
Thread.sleep(1000);
15+
}
16+
catch (Exception e)
17+
{
18+
System.out.println("Thread interrupted.");
19+
}
20+
System.out.println("\n" + msg + "Sent");
21+
}
22+
}
23+
24+
// Class for send a message using Threads
25+
class ThreadedSend extends Thread
26+
{
27+
private String msg;
28+
Sender sender;
29+
30+
// Receives a message object and a string
31+
// message to be sent
32+
ThreadedSend(String m, Sender obj)
33+
{
34+
msg = m;
35+
sender = obj;
36+
}
37+
38+
public void run()
39+
{
40+
// Only one thread can send a message
41+
// at a time.
42+
synchronized(sender)
43+
{
44+
// synchronizing the snd object
45+
sender.send(msg);
46+
}
47+
}
48+
}
49+
50+
// Driver class
51+
class Java_Synchronized
52+
{
53+
public static void main(String args[])
54+
{
55+
Sender snd = new Sender();
56+
ThreadedSend S1 =
57+
new ThreadedSend( " Hi " , snd );
58+
ThreadedSend S2 =
59+
new ThreadedSend( " Bye " , snd );
60+
61+
// Start two threads of ThreadedSend type
62+
S1.start();
63+
S2.start();
64+
65+
// wait for threads to end
66+
try
67+
{
68+
S2.join();
69+
S1.join();
70+
}
71+
catch(Exception e)
72+
{
73+
System.out.println("Interrupted");
74+
}
75+
}
76+
}
77+
78+
/*
79+
// An alternate implementation to demonstrate
80+
// that we can use synchronized with method also.
81+
class Sender
82+
{
83+
public synchronized void send(String msg)
84+
{
85+
System.out.println("Sending\t" + msg );
86+
try
87+
{
88+
Thread.sleep(1000);
89+
}
90+
catch (Exception e)
91+
{
92+
System.out.println("Thread interrupted.");
93+
}
94+
System.out.println("\n" + msg + "Sent");
95+
}
96+
}
97+
*/
98+
99+
/*
100+
// One more alternate implementation to demonstrate
101+
// that synchronized can be used with only a part of
102+
// method
103+
class Sender
104+
{
105+
public void send(String msg)
106+
{
107+
synchronized(this)
108+
{
109+
System.out.println("Sending\t" + msg );
110+
try
111+
{
112+
Thread.sleep(1000);
113+
}
114+
catch (Exception e)
115+
{
116+
System.out.println("Thread interrupted.");
117+
}
118+
System.out.println("\n" + msg + "Sent");
119+
}
120+
}
121+
}
122+
*/

Java_Threads.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.*;
2+
class NewRunnableClass implements Runnable{
3+
int v=0;
4+
NewRunnableClass(int i){
5+
v=i;
6+
}
7+
@Override
8+
public synchronized void run() {
9+
System.out.println("Thread "+v + " "+System.currentTimeMillis());
10+
}
11+
}
12+
class Java_Threads{
13+
public static void main(String [] ags){
14+
15+
Thread[] tarr = new Thread[3];
16+
for(int i=1;i<4;i++){
17+
NewRunnableClass temp = new NewRunnableClass(i);
18+
tarr[i-1] = new Thread(temp);
19+
20+
}
21+
for(int i=0;i<3;i++){
22+
System.out.println((double)System.currentTimeMillis()/1000);
23+
tarr[i].start();
24+
}
25+
26+
27+
}
28+
}
29+
// Thread in array format
30+
// Thread class extension VS Runnable interface impl
31+
// return in try catch finally
32+
// Synchronize method attribute with threads

Java_Try_Catch_Finally.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Java_Try_Catch_Finally{
2+
public static void main(String []args){
3+
int i = Integer.parseInt(args[0]);
4+
5+
System.out.println(method(i));
6+
}
7+
static String method(int i){
8+
try{
9+
System.out.println("Inside the try block");
10+
if(i!=0 && i%2==0){
11+
i += 10;
12+
return "Even";
13+
// System.out.println("Value of i = "+i);
14+
}
15+
16+
else throw new Exception("Exception occured.");
17+
}catch (Exception e){
18+
System.out.println("Inside the catch block");
19+
if(i!=0){
20+
i += 20;
21+
return "Not even";
22+
}
23+
24+
else throw new Exception("Another exception.");
25+
}finally{
26+
System.out.println("Inside the finally block");
27+
return "Zero and value of i = "+i;
28+
}
29+
//System.out.println("After finally");
30+
//return Integer.toBinaryString(55);
31+
}
32+
}

Java_Unchecked_Checked_Exp.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import jdk.tools.jlink.internal.plugins.ExcludePlugin;
2+
3+
public class Java_Unchecked_Checked_Exp {
4+
public static void main(String args[]){
5+
int i=0;
6+
int j=9;/*
7+
try{
8+
System.out.println(j/i);
9+
//}catch (IOException e){
10+
//}catch (ArithmeticException e){
11+
}catch (Exception e){
12+
System.out.print("Exception occured.");
13+
}*/
14+
//System.out.println("After the catch block, no finally clause.");
15+
}
16+
}

Lambdas.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.Collections;
2+
import java.util.List;
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
public class Lambdas {
7+
public static void main(String[] args) {
8+
9+
List<Integer> list = Arrays.asList(4,3,6,5,7,3,5,8,1);
10+
/*
11+
Collections.sort(list, new Comparator<Integer>(){
12+
@Override
13+
public int compare(Integer a, Integer b){
14+
return a-b;
15+
}
16+
});
17+
*/
18+
//Since comparator is a functional interface, we can use it as such
19+
// Functional interfaces have only one abstract method, but can have multiple default methods
20+
21+
22+
Collections.sort(list, (Integer a, Integer b) -> a-b);
23+
System.out.println(list);
24+
25+
Multiplication mul = (int a, int b)->(a*b);
26+
System.out.println(mul.multiply(2,3));
27+
28+
//() -> System.out.println("asasasas");
29+
30+
}
31+
}
32+
//@FunctionalInterface
33+
interface Multiplication{
34+
int multiply(int a, int b); //abstract is automatically added for interface methods(all non static)
35+
36+
static int add(int a, int b){ // but not for static methods, but these are perfectly valid
37+
return a+b;
38+
}
39+
//abstract int div (int a, int b); //this would be valid if this interface is not a functional interface (interface with only 1 abstract method), invalid otherwise
40+
/*int subtr(int a, int b){ // non static (instance) methods are not allowed
41+
return a-b;
42+
}*/
43+
}

0 commit comments

Comments
 (0)