diff --git a/vishal_karanjekar _source_codes_java(1-4) b/vishal_karanjekar _source_codes_java(1-4) new file mode 100644 index 0000000..e5a376d --- /dev/null +++ b/vishal_karanjekar _source_codes_java(1-4) @@ -0,0 +1,1070 @@ +// lecture 1 source code (vishal_karanjekar) +//hello world code + +class HelloWorld { + public static void main(String[] args) { + + System.out.println("Hello, World!");// for printing output + + } +} + + + +//lecture 2 source code (vishal_karanjekar) +//various datatypes in java + +public class DataType { + public static void main(String[] args) { + int i=1; + System.out.println(i); + short a=32767; + System.out.println("a= " + a); + String a_1="My name is Vishal \b"; + System.out.println(a_1); + String z="NULL \b"; + System.out.println(z); + float pi=3.14_15F; + System.out.println("pi= " + pi); + + + } + +} + +//lecture 3 source code (vishal_karanjekar) +// nested while loop concept + +public class Solution{ + +public static void main(String[] args) { + int i=1,j=1; //initialization + while(i<=10) { + while(j<=5) { + System.out.print("*"); //loop 1 + j++; + + } + System.out.println(""); //loop 2 + i++; + j=1; + + } + // TODO Auto-generated method stub + } + + +} + + +//lecture 4 source code(vishal_karanjekar) +//oops concepts & strings + +//abstraction + +package Abstraction_code; + +abstract class AbstractClass { + public void Mymethod(){ + System.out.println("Hi,I'm Vishal"); + } + abstract public void anotherMethod(); + +} + +//abtraction in inherited class +package Abstract; + +public class Demo extends AbstractClass { + + public void anotherMethod() { + System.out.println("Hi, this is Ramesh"); + } + + public static void main(String[] args) { + + AbstractClass obj=new Demo(); + obj.anotherMethod(); //abstract method + obj.Mymethod(); //normal method + + } +} + +//strings functions + +public class StringFunctions { + + public static void main(String[] args) { + + char a='V'; + String b="Ramesh"; + String c="vishal"; + System.out.println(b.length()); + + System.out.println(c.charAt(0)); + + System.out.println(c.compareTo(b)); //compare two strings based on their ASCII values + + System.out.println(b.strip()); //to remove the spaces at the start and at the end of a string + + System.out.println(c.toUpperCase()); //convert in to upper case + + System.out.println(c.toLowerCase()); //convert in to lower case + + System.out.println(c.substring(1,3)); //return substring from position 1 to position 3-1 + System.out.println(c.substring(3)); + + + System.out.println(c.isBlank()); //tell us whether the string is empty or not + + // TODO Auto-generated method stub + + } + +} + + +//lecture 5 source code(vishal_karanjekar) +//creating class and objects + +class Demo{ + String name; + int age; + int rollno; + + public void info() + { + System.out.println("Name = " + name); + System.out.println("Age = " + age); + System.out.println("Roll No = " + rollno); + } + + + + public static void main (String[] args) + { + Demo student=new Demo(); + student.name="Vishal"; + student.age=21; + student.rollno=272; + student.info(); + } + + } + + +//searching in an array + +class DemoArray +{ + public static void main (String[] args) + { + int[] arrayInt = {32,87,3,589,1076,12,2000,8,622,127}; //declaration of an array + + int key = 2000; + int i; + boolean flag = false; + for(i=0;i 1 +$1 ==> 1 + +jshell> 5 +$2 ==> 5 + + +jshell> 2+3 +$3 ==> 5 + + +jshell> 3*4 +$4 ==> 12 + +jshell> + String print(String s){ + ...> return s+s; + ...> } +| created method print(String) + + +jshell> print("Vishal") +$6 ==> "vishalvishal" + +jshell> int x=5 +x ==> 5 + +jshell> + int sum(int x,int y){ + ...> return x+y; + ...> } +| created method sum(int,int) + +jshell> sum(12,13) +$9 ==> 25 + + + +//lecture 12 source code(vishal_karanjekar) +//jlambda + + +import java.util.ArrayList; + + class A +{ + public static void main(String[] args) { + ArrayList numbers = new ArrayList(); + numbers.add(1); + numbers.add(2); + numbers.add(3); + numbers.add(4); + numbers.add(5); + numbers.forEach( (i) -> {System.out.println(i); }); + } + } + + + +//lecture 1 data structures (vishal_karanjekar) +//Exception Handling + +public class ExceptionTest{ + + public static void main(String[] args) + { + //String age="33";//error (exception handling) + int age = 33; + int maxheartrate = 220 - age ; + System.out.println("Max heart rate is = " + maxheartrate); + } +} + + +//Exception Handling Example + +import java.util.Scanner; + + class ExceptionTest{ + + public static void main(String[] args) + { + System.out.println("Welcome to Max Heart Rate Calculator"); + Scanner sc=new Scanner(System.in); + + System.out.println("Please enter your age : "); + int age =sc.nextInt(); + int maxheartrate = 220 - age ; + System.out.println("Max heart rate is = " + maxheartrate); + } +} + +output: +Welcome to Max Heart Rate Calculator +Please enter your age : Twenty One +Exception in thread "main" java.util.InputMismatchException +at java.base/java.util.Scanner.throwFor(Scanner.java:939) +at java.base/java.util.Scanner.next(Scanner.java:1594) + at java.base/java.util.Scanner.nextInt(Scanner.java:2258) + at java.base/java.util.Scanner.nextInt(Scanner.java:2212) +at ExceptionTest.main(ExceptionTest.java:14) + + +//Exception Handling + +import java.util.InputMismatchException; +import java.util.Scanner; + + class ExceptionTest{ + + public static void main(String[] args) + { + //exception handling + try{ + System.out.println("Welcome to Max Heart Rate Calculator"); + Scanner sc=new Scanner(System.in); + + System.out.println("Please enter your age : "); + int age =sc.nextInt(); + int maxheartrate = 220 - age ; + System.out.println("Max heart rate is = " + maxheartrate); + }catch(InputMismatchException e){ + System.out.println("Please enter integer only"); + } + } +} + +output: Welcome to Max Heart Rate Calculator +Please enter your age : Twenty One +Please enter integer only + + + +//lecture 2 DSA souce code(vishal_karanjekar) +//Advanced Exception Handling + +import java.util.InputMismatchException; +import java.util.Scanner; + + class ExceptionTest{ + + public static void main(String[] args) + { + //exception handling + try{ + System.out.println("Welcome to Max Heart Rate Calculator"); + Scanner sc=new Scanner(System.in); + + System.out.println("Please enter your age : "); + int age =sc.nextInt(); + int maxheartrate = 220 - age ; + System.out.println("Max heart rate is = " + maxheartrate); + }catch(Exception e){ + System.out.println("Please enter integer only"); + System.out.println(e.getLocalizedMessage()); + System.out.println(e.getMessage()); + + } + } +} +output: +Welcome to Max Heart Rate Calculator +Please enter your age : +twenty one +Please enter integer only +null +null + + +//lecture 2 DSA souce code(vishal_karanjekar) +//Advanced Exception Handling +//try catch and finally blocks + +import java.util.InputMismatchException; +import java.util.Scanner; + + class TryCatchFinallyTest{ + + public static void main(String[] args) + { + try{ + Scanner sc=new Scanner(System.in); + System.out.println("Enter no 1: "); + int a =sc.nextInt(); + System.out.println("Enter no 2: "); + int b =sc.nextInt(); + int d=add(a,b); + System.out.println("Sum is equal to= "+d); + }catch(InputMismatchException e){ + System.out.println("please enter integers only "); + }finally{ + System.out.println("Integers not found"); + } + } + private static int add(int a, int b){ + int c=0; + try{ + c=a+b; + }catch(Exception e) { + System.out.println("Please enter integers only"); + }finally { + System.out.println("Fianlly executed "); + return c; + } + + } + } + + +//throwing an exception + +import java.util.Scanner; + + public class ThrowExceptionTest{ + + public static void main(String[] args) + { + Scanner sc=new Scanner(System.in); + System.out.println("Please enter your age: "); + int age =sc.nextInt(); + if (age<=0){ + throw new NumberFormatException(); + } + else if(age>=120){ + throw new NumberFormatException(); + } + System.out.println("Max Heart Rate is" + (220 - age)); + + } + } + + + //chained exception + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class ChainedException { + + public static void main(String[] args) + { + try + { + Scanner sc=new Scanner(System.in); + System.out.println("Please enter your height in cm "); + float height =sc.nextInt(); + + double idealWeight=50+(0.91*(height-152.4)); //Standard ideal weight + + System.out.println("your ideal weight is : "+idealWeight); + } + catch(InputMismatchException e) + { + throw new NumberFormatException(); + } + + } +} + + + +//lecture 3 (DSA) source code(vishal_karanjekar) +//Lists interface and arraylist in java + +import java.util.ArrayList; + +public class MyArrayListTest{ + + public static void main(String[] args){ + ArrayList myArrayList= new ArrayList(); + myArrayList.add(1); + myArrayList.add(2); + myArrayList.add(3); + myArrayList.add(4); + myArrayList.add(5); + System.out.println("printing list 1: "); + System.out.println(myArrayList.get(1)); + System.out.println(myArrayList.get(0)); + System.out.println(myArrayList.get(4)); + + + System.out.println("updating elements"); + myArrayList.add(1,6); + System.out.println(myArrayList.get(1)); + + ArrayList List2= new ArrayList(); + List2.addAll(myArrayList); + + System.out.println("printing list 2: "); + System.out.println(List2.get(1)); + System.out.println(List2.get(0)); + System.out.println(List2.get(4)); + + List2.clear(); + try{ + System.out.println("printing list 2: "); + System.out.println(List2.get(1)); + System.out.println(List2.get(0)); + System.out.println(List2.get(4)); + }catch(IndexOutOfBoundsException e){ + System.out.println("list is cleared"); + } + + ArrayList myArrayListClone = (ArrayList) myArrayList.clone(); + System.out.println("printing from my arraylist clone"); + System.out.println(myArrayListClone.get(1)); + System.out.println(myArrayListClone.get(0)); + System.out.println(myArrayListClone.get(4)); + + System.out.println("check if my arraylist clone contains 6"); + boolean result=myArrayListClone.contains(6); + if(result){ + System.out.println("item exists"); + } + else{ + System.out.println("item does not exist"); + } + } +} + + + +//lecture 4 (DSA) source code (vishal_karanjekar) +//stacks in java + +import java.util.Stack; + +public class MyStack{ + public static void main(String[] args){ + Stack intStack = new Stack(); + System.out.println("Is stack empty: " + intStack.empty()); + intStack.push(10); + intStack.push(20); + intStack.push(30); + System.out.println(" Get the top element: " + intStack.peek()); + System.out.println("Is stack contains 20: "+ intStack.search(20)); + intStack.pop(); + System.out.println("Top element after pop: "+ intStack.peek()); + } +} + + + +//ArrayDeque + +import java.util.ArrayDeque; +import java.util.Iterator; + +public class StackUsingDeque{ + public static void main(String[] args) + { + ArrayDeque myDeque = new ArrayDeque(); + + myDeque.add(" Vishal "); + myDeque.addFirst(" Rishabh "); + myDeque.addLast(" New Person "); + + myDeque.offerFirst(" Ram "); //at the first + myDeque.offer(" Lakhan "); //at the last + + System.out.println(" First Element: " + myDeque.getFirst()); + System.out.println(" Last Element: " + myDeque.getLast()); + System.out.println(" Deque contains Rishabh: " + myDeque.contains(" Rishabh ")); + + System.out.println("Iterating all the elements in descending order"); + + Iterator myIterator = myDeque.descendingIterator(); + + while(myIterator.hasNext()) { + System.out.println(myIterator.next()); + } + } +}