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