Commit f79547b1 authored by Lokesh Singh's avatar Lokesh Singh

cloned and updated code

parent b6509158
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JpaBuddyIdeaProjectConfig">
<option name="renamerInitialized" value="true" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="ProjectType">
<option name="id" value="jpab" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Hello-Java.iml" filepath="$PROJECT_DIR$/.idea/Hello-Java.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
# Array
In java arrays are obkects.
In java arrays are objects.
1. Arrays in Java work differently than they do in C/C++.
2. A Java array variable can also be declared like other variables with [] after the data type.
......
import java.util.Scanner;
// first method using if-else
public class checkNoisOddorEven {
static public void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number:");
int val = input.nextInt();
if (val % 2 == 0) {
// if((val | 1) > val)
// if (val & 1 == 1)
System.out.println("No. is even");
}
else {
System.out.println("No. is odd");
}
}
}
import java.util.Scanner;
// first method using if-else
public class checkNoisOddorEven {
static public void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number:");
int val = input.nextInt();
if (val % 2 == 0) {
// if((val | 1) > val)
// if (val & 1 == 1)
System.out.println("No. is even");
}
else {
System.out.println("No. is odd");
}
}
}
public class findFactorial {
static public int printFactorial(int n) {
if(n==0)
return 1;
return n * printFactorial(n-1);
}
static public int printFactorialIterative(int n) {
int res = 1;
for(int i = 2; i <= n; i++ ) {
res = res*i;
}
return res;
}
public static void main(String[] args) {
int factVal = 6;
int x = printFactorial(factVal);
System.out.println("factorial of "+ factVal +" is "+ x);
System.out.println(printFactorialIterative(factVal));
}
}
public class findFactorial {
static public int printFactorial(int n) {
if(n==0)
return 1;
return n * printFactorial(n-1);
}
static public int printFactorialIterative(int n) {
int res = 1;
for(int i = 2; i <= n; i++ ) {
res = res*i;
}
return res;
}
public static void main(String[] args) {
int factVal = 6;
int x = printFactorial(factVal);
System.out.println("factorial of "+ factVal +" is "+ x);
System.out.println(printFactorialIterative(factVal));
}
}
public class findLargestAmongThree {
static int largestOfThree(int a, int b, int c) {
// return Math.max(a, Math.max(b, c));
return a>b?(a>c?a:c):(b>c?b:c);
}
public static void main(String[] args) {
int a = 2;
int b = 5;
int c = 4;
int largest = 0;
if (a > b) {
largest = a;
} else if (b > c) {
largest = b;
} else {
largest = c;
}
System.out.println("Largest among three numbers is: " + largest);
System.out.println("Largest among three numbers is: " + largestOfThree(a, b, c));
}
}
public class findLargestAmongThree {
static int largestOfThree(int a, int b, int c) {
// return Math.max(a, Math.max(b, c));
return a>b?(a>c?a:c):(b>c?b:c);
}
public static void main(String[] args) {
int a = 2;
int b = 5;
int c = 4;
int largest = 0;
if (a > b) {
largest = a;
} else if (b > c) {
largest = b;
} else {
largest = c;
}
System.out.println("Largest among three numbers is: " + largest);
System.out.println("Largest among three numbers is: " + largestOfThree(a, b, c));
}
}
public class helloworld {
public static void main(String[] arge) {
System.out.println("Hello, World!");
}
}
public class helloworld {
public static void main(String[] arge) {
System.out.println("Hello, World!");
}
}
public class isLeapYear {
public static void main(String[] args) {
int year = 2020;
if (year % 4 == 0) {
System.out.println("Leap Year");
} else {
System.out.println("Not a Leap Year");
}
}
}
public class isLeapYear {
public static void main(String[] args) {
int year = 2020;
if (year % 4 == 0) {
System.out.println("Leap Year");
} else {
System.out.println("Not a Leap Year");
}
}
}
// java program to multiply two floating nos.
public class multiply {
public static void main(String[] args) {
float a = 2.5f;
float b = 3.5f;
float c = a * b;
System.out.println(c);
}
}
// java program to multiply two floating nos.
public class multiply {
public static void main(String[] args) {
float a = 2.5f;
float b = 3.5f;
float c = a * b;
System.out.println(c);
}
}
import java.util.Scanner;
public class readInput {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number:");
int val = input.nextInt();
System.out.println(val);
}
}
import java.util.Scanner;
public class readInput {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number:");
int val = input.nextInt();
System.out.println(val);
}
}
// public class swapNo {
// public static void main(String[] args) {
// int a = 2;
// int b = 3;
// int temp;
// System.out.println("values of a and b before swaping: ");
// System.out.println("a: "+ a);
// System.out.println("b: "+ b);
// temp = a;
// a = b;
// b = temp;
// System.out.println("values of a and b after swaping: ");
// System.out.println("a:"+ a);
// System.out.println("b:"+ b);
// }
// }
// we will make seprate function for swapping
public class swapNo {
// static void swapValues(int a, int b) {
// int temp;
// temp = a;
// a = b;
// b = temp;
// swapValues(a, b);
// System.out.println("values of a and b after swaping:");
// System.out.println("a: " + a);
// System.out.println("b: " + b);
// }
static void swapWithoutTemp(int a, int b) {
a = a * b;
b = a / b;
a = a / b;
System.out.println("values of a and b after swaping:");
System.out.println("a: " + a);
System.out.println("b: " + b);
}
public static void main(String[] args) {
int a = 3;
int b = 8;
// swapValues(a, b);
swapWithoutTemp(a, b);
}
// public class swapNo {
// public static void main(String[] args) {
// int a = 2;
// int b = 3;
// int temp;
// System.out.println("values of a and b before swaping: ");
// System.out.println("a: "+ a);
// System.out.println("b: "+ b);
// temp = a;
// a = b;
// b = temp;
// System.out.println("values of a and b after swaping: ");
// System.out.println("a:"+ a);
// System.out.println("b:"+ b);
// }
// }
// we will make seprate function for swapping
public class swapNo {
// static void swapValues(int a, int b) {
// int temp;
// temp = a;
// a = b;
// b = temp;
// swapValues(a, b);
// System.out.println("values of a and b after swaping:");
// System.out.println("a: " + a);
// System.out.println("b: " + b);
// }
static void swapWithoutTemp(int a, int b) {
a = a * b;
b = a / b;
a = a / b;
System.out.println("values of a and b after swaping:");
System.out.println("a: " + a);
System.out.println("b: " + b);
}
public static void main(String[] args) {
int a = 3;
int b = 8;
// swapValues(a, b);
swapWithoutTemp(a, b);
}
}
\ No newline at end of file
public class vowelOrConsonent {
public static void vowel_or_consonent(char y) {
if (y == 'a' || y == 'e' || y == 'i' || y =='o' || y == 'u') {
System.out.println("It is vowel");
} else {
System.out.println("It is consonent");
}
}
public static void main(String[] args) {
vowel_or_consonent('b');
}
}
public class vowelOrConsonent {
public static void vowel_or_consonent(char y) {
if (y == 'a' || y == 'e' || y == 'i' || y =='o' || y == 'u') {
System.out.println("It is vowel");
} else {
System.out.println("It is consonent");
}
}
public static void main(String[] args) {
vowel_or_consonent('b');
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="openjdk-18" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="openjdk-18" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
package com.lokesh.arraylist;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class PrintElementsofArrayList {
public static void main(String[] args) {
ArrayList<String> strName = new ArrayList<>();
strName.add("Lokesh");
strName.add("babalu");
strName.add("Nitin");
strName.add("Naveen");
strName.add("Lokesh");
// forEach loop
// for (String s : strName) {
// System.out.println(s);
// }
//access element in particular position
System.out.println(strName.get(2));
//Lambda expression
// strName.forEach(System.out::println);
System.out.println("\nAdd Name at 0th position");
strName.add(0, "milaan");
strName.forEach(System.out::println);
System.out.println("print arraylist\n");
System.out.println(strName);
//Replace element at specific position
System.out.println("\nlist after update the name:");
strName.set(2, "Ramesh");
System.out.println(strName);
//Remove Element from list
System.out.println("\nlist after remove ramesh");
strName.remove(2);
System.out.println(strName);
//Reverse Elements of list
System.out.println("\nlist after reverse the list");
Collections.reverse(strName);
System.out.println(strName);
}
}
package com.lokesh.arraylist;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class PrintElementsofArrayList {
public static void main(String[] args) {
ArrayList<String> strName = new ArrayList<>();
strName.add("Lokesh");
strName.add("babalu");
strName.add("Nitin");
strName.add("Naveen");
strName.add("Lokesh");
// forEach loop
// for (String s : strName) {
// System.out.println(s);
// }
//access element in particular position
System.out.println(strName.get(2));
//Lambda expression
// strName.forEach(System.out::println);
System.out.println("\nAdd Name at 0th position");
strName.add(0, "milaan");
strName.forEach(System.out::println);
System.out.println("print arraylist\n");
System.out.println(strName);
//Replace element at specific position
System.out.println("\nlist after update the name:");
strName.set(2, "Ramesh");
System.out.println(strName);
//Remove Element from list
System.out.println("\nlist after remove ramesh");
strName.remove(2);
System.out.println(strName);
//Reverse Elements of list
System.out.println("\nlist after reverse the list");
Collections.reverse(strName);
System.out.println(strName);
}
}
package com.lokesh.hashset;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class HashSetInJava {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
set.add(2);
set.add(3);
boolean b1 = set.add(2);
set.add(4);
set.add(1);
Set<String> st = new HashSet<>();
st.add("c");
st.add("a");
st.add("b");
st.forEach((n)-> System.out.println(n));
set.forEach(System.out::println);
System.out.println("See hashset also sort the data automatically\n");
System.out.println(set);
System.out.println("b1 = "+b1 + "duplicate value");
System.out.println("\nsize of set" + set.size());
set.remove(1);
System.out.println(set);
System.out.println(set.isEmpty());
TreeSet<Integer> tset = new TreeSet<Integer>(set);
System.out.println(tset+ " This is treeset");
tset.ceiling(2);
}
}
package com.lokesh.hashset;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class HashSetInJava {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
set.add(2);
set.add(3);
boolean b1 = set.add(2);
set.add(4);
set.add(1);
Set<String> st = new HashSet<>();
st.add("c");
st.add("a");
st.add("b");
st.forEach((n)-> System.out.println(n));
set.forEach(System.out::println);
System.out.println("See hashset also sort the data automatically\n");
System.out.println(set);
System.out.println("b1 = "+b1 + "duplicate value");
System.out.println("\nsize of set" + set.size());
set.remove(1);
System.out.println(set);
System.out.println(set.isEmpty());
TreeSet<Integer> tset = new TreeSet<Integer>(set);
System.out.println(tset+ " This is treeset");
tset.ceiling(2);
}
}
package com.lokesh.interfaces;
public class DiamondProblem {
public static void main(String[] args) {
new Toomuch().walk();
}
}
class Toomuch implements Father, Mother {
/*
* When the interfaces that we are implementing has
* the same methods signatures then we must override them.
*/
@Override
public void walk() {
Father.super.walk();
Mother.super.walk();
System.out.println("Walking like myself");
}
}
interface Father {
default void walk() {
System.out.println("Walking like father");
}
}
interface Mother {
default void walk() {
System.out.println("Walking like mother");
}
package com.lokesh.interfaces;
public class DiamondProblem {
public static void main(String[] args) {
new Toomuch().walk();
}
}
class Toomuch implements Father, Mother {
/*
* When the interfaces that we are implementing has
* the same methods signatures then we must override them.
*/
@Override
public void walk() {
Father.super.walk();
Mother.super.walk();
System.out.println("Walking like myself");
}
}
interface Father {
default void walk() {
System.out.println("Walking like father");
}
}
interface Mother {
default void walk() {
System.out.println("Walking like mother");
}
}
\ No newline at end of file
package com.lokesh.interfaces;
public class InterfaceTest implements B{
public static void main(String[] args) {
new InterfaceTest().go();
}
public void go() {
concreteMethod();
A.staticMethod();
B.staticMethod();
}
@Override
public void abstractMethod() {
System.out.println("In class implemented method");
}
@Override
public void concreteMethod() {
B.super.concreteMethod();
}
}
interface A {
void abstractMethod();
default void concreteMethod() {
System.out.println("In interface A concrete method");
}
static void staticMethod() {
System.out.println("in interface A concrete method");
}
}
interface B extends A {
default void concreteMethod() {
System.out.println("In interface B concrete method");
}
static void staticMethod() {
System.out.println("In interface B static method");
}
}
interface C {
default void concreteMethod() {
System.out.println("In interface C concrete method");
}
}
package com.lokesh.interfaces;
public class InterfaceTest implements B{
public static void main(String[] args) {
new InterfaceTest().go();
}
public void go() {
concreteMethod();
A.staticMethod();
B.staticMethod();
}
@Override
public void abstractMethod() {
System.out.println("In class implemented method");
}
@Override
public void concreteMethod() {
B.super.concreteMethod();
}
}
interface A {
void abstractMethod();
default void concreteMethod() {
System.out.println("In interface A concrete method");
}
static void staticMethod() {
System.out.println("in interface A concrete method");
}
}
interface B extends A {
default void concreteMethod() {
System.out.println("In interface B concrete method");
}
static void staticMethod() {
System.out.println("In interface B static method");
}
}
interface C {
default void concreteMethod() {
System.out.println("In interface C concrete method");
}
}
package com.lokesh.lambda;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public abstract class Example_Consumer {
public static void main(String[] args) {
List<String> names = Arrays.asList("Bruce", "Logan", "Peter");
System.out.println("Using anonymous class");
names.forEach(new Consumer<String>() {
@Override
public void accept(String name) {
System.out.println(name);
}
});
System.out.println("\nUsing lambda expression");
names.forEach(name -> System.out.println(name));
System.out.println("\nUsing method reference (will create a class on the fly)");
Consumer<String> myConsumer = System.out::println;
names.forEach(System.out::println);
System.out.println("\nUsing stream to map to another class");
names.stream().map(Hero::new)
.map(Hero::getSecretIdentity)
.forEach(myConsumer);
}
public abstract void accept(String name);
}
class Hero {
private String secretIdentity;
public Hero(String secretIdentity) {
this.secretIdentity = "Mr. " + secretIdentity;
}
public String getSecretIdentity() {
return secretIdentity;
}
}
package com.lokesh.lambda;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public abstract class Example_Consumer {
public static void main(String[] args) {
List<String> names = Arrays.asList("Bruce", "Logan", "Peter");
System.out.println("Using anonymous class");
names.forEach(new Consumer<String>() {
@Override
public void accept(String name) {
System.out.println(name);
}
});
System.out.println("\nUsing lambda expression");
names.forEach(name -> System.out.println(name));
System.out.println("\nUsing method reference (will create a class on the fly)");
Consumer<String> myConsumer = System.out::println;
names.forEach(System.out::println);
System.out.println("\nUsing stream to map to another class");
names.stream().map(Hero::new)
.map(Hero::getSecretIdentity)
.forEach(myConsumer);
}
public abstract void accept(String name);
}
class Hero {
private String secretIdentity;
public Hero(String secretIdentity) {
this.secretIdentity = "Mr. " + secretIdentity;
}
public String getSecretIdentity() {
return secretIdentity;
}
}
package com.lokesh.model;
public class Person {
}
package com.lokesh.model;
public class Person {
}
package com.lokesh.treeset;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetInJava {
public static void main(String[] args) {
/**
* @Author Lokesh singh
* <p>
* TreeSet ts = new TreeSet();
* Set syncSet = Collections.synchronizedSet(ts);
*/
Set<String> tset = new TreeSet<>();
tset.add("Mayukh");
tset.add("Nikhil");
tset.add("Lokesh");
tset.add("Mohit");
tset.add("Ravish");
tset.forEach(System.out::println);
String[] str = tset.toArray(size->new String[size]);// lambda expression
str = tset.toArray(size -> new String[size*10]);
System.out.println(tset.toString());
System.out.println("treeset sort the data ascending order\n because its internal mechanism is sbst");
}
}
package com.lokesh.treeset;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetInJava {
public static void main(String[] args) {
/**
* @Author Lokesh singh
* <p>
* TreeSet ts = new TreeSet();
* Set syncSet = Collections.synchronizedSet(ts);
*/
Set<String> tset = new TreeSet<>();
tset.add("Mayukh");
tset.add("Nikhil");
tset.add("Lokesh");
tset.add("Mohit");
tset.add("Ravish");
tset.forEach(System.out::println);
String[] str = tset.toArray(size->new String[size]);// lambda expression
str = tset.toArray(size -> new String[size*10]);
System.out.println(tset.toString());
System.out.println("treeset sort the data ascending order\n because its internal mechanism is sbst");
}
}
public class exception {
public static void main(String[] args) {
try {
int arr[] = {1,2,3};
System.out.println(arr[1]);
}
catch (Exception e) {
System.out.println("Something went wrong");
} finally {
System.out.println("try catch block finished");
}
}
}
public class exception {
public static void main(String[] args) {
try {
int arr[] = {1,2,3};
System.out.println(arr[1]);
}
catch (Exception e) {
System.out.println("Something went wrong");
} finally {
System.out.println("try catch block finished");
}
}
}
public class thread extends Thread {
public void run() {
System.out.println("Hello! this code is running in a thread");
}
public static void main(String[] args) {
thread m = new thread();
m.start();
System.out.println("Hello! this code is running outside of the main thread");
}
}
public class thread extends Thread {
public void run() {
System.out.println("Hello! this code is running in a thread");
}
public static void main(String[] args) {
thread m = new thread();
m.start();
System.out.println("Hello! this code is running outside of the main thread");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="openjdk-18" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="openjdk-18" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="openjdk-18" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="openjdk-18" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment