Posts

Showing posts from March, 2021

Java Thread Methods

  package com.company ; class MyThr extends Thread{ public void run (){ System. out .println( "Thank you " + this .getName()) ; } } class MyThr2 extends Thread{ public void run (){ try { Thread. sleep ( 455 ) ; } catch (InterruptedException e) { e.printStackTrace() ; } System. out .println( "Thank you " + this .getName()) ; } } public class tuts { public static void main (String[] args) { MyThr t1 = new MyThr() ; MyThr t2 = new MyThr() ; t1.start() ; try { t1.join() ; } catch (Exception e){ System. out .println(e) ; } t2.start() ; } }

Java Thread Priorities

Priority In Java:   java.lang.Thread.MIN_PRIORITY = 1 java.lang.Thread.NORM_PRIORITY = 5 java.lang.Thread.MAX_PRIORITY = 10 package com.company ; class MyThr extends Thread{ public MyThr (String name){ super (name) ; } public void run (){ System. out .println( "Thank you " + this .getName()) ; } } public class tuts { public static void main (String[] args) { MyThr t1 = new MyThr( "Aryan1" ) ; MyThr t2 = new MyThr( "Aryan2" ) ; MyThr t3 = new MyThr( "Aryan3" ) ; MyThr t4 = new MyThr( "Aryan4" ) ; MyThr t5 = new MyThr( "Aryan5 (Most Important)" ) ; t5.setPriority(Thread. MAX_PRIORITY ) ; t1.setPriority(Thread. MIN_PRIORITY ) ; t2.setPriority(Thread. MIN_PRIORITY ) ; t3.setPriority(Thread. MIN_PRIORITY ) ; t4.setPriority(Thread. MIN_PRIORITY ) ; t1.start() ; t2.start() ; t3.star...

Constructors from Thread class in Java

  package com.company ; class MyThr extends Thread{ public MyThr (String name){ super (name) ; } public void run (){ // while(true){ // System.out.println("I am a thread"); // } } } public class tuts { public static void main (String[] args) { MyThr t = new MyThr( "Aryan" ) ; t.start() ; System. out .println( "The id of thread t is " + t.getId()) ; System. out .println( "The name of thread t is " + t.getName()) ; } }

Creating a Java Thread Using Runnable Interface

  package com.company; class MyRunnableClass1 implements Runnable{     public void run(){         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");         System.out.println("I am a thread1 not a threat");        ...

Creating a Thread by Extending Thread class

   package  com.company ; class MyThread1 extends Thread{ @Override public void run (){ int i = 0 ; while (i< 40000 ){ System. out .println( "My thread is runing" ) ; System. out .println( "I am happy" ) ; } } } class MyThread2 extends Thread{ @Override public void run (){ int i = 0 ; while (i< 40000 ){ System. out .println( "Thread2 is good" ) ; System. out .println( "I am very happy" ) ; i++ ; } } } public class tuts { public static void main (String[] args) { MyThread1 t1 = new MyThread1() ; MyThread2 t2 = new MyThread2() ; t1.start() ; t2.start() ; } }

Access Modifiers in Java

 package com.company; class C1{     public int x = 5;     protected int y = 45;     int z = 6;     private int a = 4;     public void meth1(){         System.out.println(x);         System.out.println(y);         System.out.println(z);         System.out.println(a);     } } public class tuts {     public static void main(String[] args) {     C1 c = new C1();     c.meth1();     } }

Packages In Java

  package com.company ; import java.util.Scanner ; // Just imports the specified Class. import java.util.* ; // Imports all the classes in util. public class tuts { public static void main (String[] args) { java.util.Scanner sc = new java.util.Scanner(System. in ) ; // Write java.util. to use directly. System. out .println( "This is my scanner" ) ; } }

Polymorphism in Interfaces In Java

  package com.company ; interface Camera{ void takeSnap () ; void recordVideo () ; default void record4KVideo (){ // We can override this default method in the class but then this method will not work // instead the method in class will work. System. out .println( "Recording In 4k ..." ) ; } } interface Wifi{ String [] getNetwork () ; void connectToNetwork (String network) ; } class CellPhone{ void callNumber ( int phoneNumber){ System. out .println( "Calling" + phoneNumber) ; } void pickCall (){ System. out .println( "Connecting ..." ) ; } } class SmartPhone extends CellPhone implements Wifi , Camera{ public void takeSnap (){ System. out .println( "Taking Snap" ) ; } public void recordVideo (){ System. out .println( "Taking Videp" ) ; } public String[] getNetwork (){ System. out .println( "Getting the list of n...

Inheritance In Interfaces

  package com.company ; interface sampleInterface{ void meth1 () ; void meth2 () ; } interface childSampleInterface extends sampleInterface{ void meth3 () ; void meth4 () ; } class SampleClass implements childSampleInterface{ public void meth1 (){ System. out .println( "meth1" ) ; } public void meth2 (){ System. out .println( "meth2" ) ; } public void meth3 (){ System. out .println( "meth3" ) ; } public void meth4 (){ System. out .println( "meth4" ) ; } } public class tuts { public static void main (String[] args) { SampleClass sc = new SampleClass() ; sc.meth1() ; sc.meth2() ; sc.meth3() ; sc.meth4() ; } }

Default Methods In Java

  package com.company ; interface Camera{ void takeSnap () ; void recordVideo () ; default void record4KVideo (){ // We can override this default method in the class but then this method will not work // instead the method in class will work. System. out .println( "Recording In 4k ..." ) ; } } interface Wifi{ String [] getNetwork () ; void connectToNetwork (String network) ; } class CellPhone{ void callNumber ( int phoneNumber){ System. out .println( "Calling" + phoneNumber) ; } void pickCall (){ System. out .println( "Connecting ..." ) ; } } class SmartPhone extends CellPhone implements Wifi , Camera{ public void takeSnap (){ System. out .println( "Taking Snap" ) ; } public void recordVideo (){ System. out .println( "Taking Videp" ) ; } public String[] getNetwork (){ System. out .println( "Getting the list of n...

Interfaces in Java

  package com.company ; interface Bicycle{ int a = 45 ; void applyBrake ( int decrement) ; void speedUp ( int inrement) ; } class AvonCycle implements Bicycle{ public void applyBrake ( int decrement){ System. out .println( "Applying Brake" ) ; } public void speedUp ( int inrement){ System. out .println( "Applying SpeedUp" ) ; } } public class tuts { public static void main (String[] args) { AvonCycle cycle = new AvonCycle() ; cycle.applyBrake( 1 ) ; // You can create properties in Interfaces but, you cannot change them because they are final. System. out .println(cycle. a ) ; } }

Java Exercise : Online Library

package com.company ; import java.util.Scanner ; class Library { // Add Book public void addBook() { Scanner sc = new Scanner(System.in) ; System.out.println( "Please type the Book name you want to add :" ) ; String addBookto = sc.next() ; System.out.println( "Please Enter your name: " ) ; String addname = sc.next() ; System.out.println( "Book Added 👍 " ) ; } public void issueBook() { Scanner sc = new Scanner(System.in) ; System.out.println( "Please Enter the book name to be issued :" ) ; String issuebook = sc.next() ; System.out.println( "Please Enter the person name on whose name it should to be issued :" ) ; String issuername = sc.next() ; System.out.println( "Book Issued 👍 " ) ; } public void returnBook() { Scanner sc = new Scanner(System.in) ; System.out.println( "Enter the book na...

Dynamic Method Dispatch In Java

 package com.company; class Phone{     public void greet(){         System.out.println("Good Morning");     }     public void name(){         System.out.println("My name is Java");     } } class Smartphone extends Phone{     public void swagat(){         System.out.println("Aapka swagat hai");     }     public void name(){         System.out.println("My name is Java in class Two");     } } public class tuts {     public static void main(String[] args) { //        Phone obj = new Phone(); //        Smartphone smobj = new Smartphone(); //        obj.name(); //        smobj.name();         Phone obj = new Smartphone();         obj.name();      } }

Method Overriding in Java

  package com.company ; class A { public int a ; public int aryan () { return 3 ; } public void meth2 () { System. out .println( "I am method 2 of class A" ) ; } } class B extends A{ @Override public void meth2 () { System. out .println( "I am method 32 of class B" ) ; } public void meth3 () { System. out .println( "I am method 3 of class B" ) ; } } public class tuts { public static void main (String[] args) { A a = new A() ; a.meth2() ; B b = new B() ; b.meth2() ; } }

This and Super Keywords In Java

  package com.company ; class Ekclass{ int a ; public int getA () { return a ; } Ekclass ( int v){ this . a = v ; } public int returnone (){ return 1 ; } } class DoClass extends Ekclass{ DoClass ( int c){ super (c) ; System. out .println( "I am a constructor" ) ; } } public class tuts { public static void main (String[] args) { Ekclass e = new Ekclass( 65 ) ; DoClass d = new DoClass( 5 ) ; System. out .println(e.getA()) ; } }

Constructors in Inheritance in Java

 package com.company; class Base1{     Base1(){         System.out.println("I am a constructor");     }     Base1(int x){         System.out.println("I am an overloaded constructor with value of x as: " + x);     } } class Derived1 extends Base1{     Derived1(){         //super(0);         System.out.println("I am a derived class constructor");     }     Derived1(int x, int y){         super(x);         System.out.println("I am an overloaded constructor of Derived with value of y as: " + y);     } } class ChildOfDerived extends  Derived1{     ChildOfDerived(){         System.out.println("I am a child of derived constructor");     }     ChildOfDerived(int x, int y, int z){         super(x, y);     ...

Inheritance in Java

 package com.company; class Base{     int x;     public void setX(int x) {         System.out.println("I am in base setting X now...");         this.x = x;     }     public int getX() {         return x;     }     public void printMe(){         System.out.println("I am a constructor");     } } class Derived extends Base{     int y;     public int getY() {         return y;     }     public void setY(int y) {         this.y = y;     } } public class tuts {     public static void main(String[] args) {         Base b = new Base();         b.setX(3);         System.out.println(b.getX());         Derived d = new Derived();         d.setX(44); ...

Guess The Number Game

 package com.company; import java.util.Random; import java.util.Scanner; class Game{     public int number;     public int inputNumber;     public int noOfGuesses = 0;     public int getNoOfGuesses() {         return noOfGuesses;     }     public void setNoOfGuesses(int noOfGuesses) {         this.noOfGuesses = noOfGuesses;     }     Game(){         Random rand = new Random();         this.number = rand.nextInt(100);     }     void takeUserInput(){         System.out.println("Guess the number");         Scanner sc = new Scanner(System.in);         inputNumber = sc.nextInt();     }     boolean isCorrectNumber(){         noOfGuesses++;         if (inputNumber==number){      ...

Quick Quiz on Constructors Overloading

package com.company ; class QuickQuiz{ private int salary ; public QuickQuiz (){ salary = 0 ; } public QuickQuiz ( int salary1){ salary = salary1 ; } public void setsalary ( int s) { this . salary = s ; } public int getSalary () { return salary ; } } public class tuts { public static void main (String[] args) { QuickQuiz myquiz = new QuickQuiz( 10000 ) ; System. out .println(myquiz.getSalary()) ; } }

Constructors in Java

  package com.company ; class MyMainEmployee{ private int id ; private String name ; public MyMainEmployee (String myname , int myid){ id = myid ; name = myname ; } public String getName () { return name ; } public int getid () { return id ; } public void setName (String n) { this . name = n ; } public void setId ( int i) { this . id = i ; } } public class tuts { public static void main (String[] args) { MyMainEmployee aryan = new MyMainEmployee( "Aryan0001" , 13 ) ; // aryan.setName("Aryan"); System. out .println(aryan.getName()) ; System. out .println(aryan.getid()) ; } }

Class in Java

 package com.company; class Employee{     int id;     int videos;     String name;     public void printdDetails(){         System.out.println("My id is "+id);         System.out.println("My name is "+name);     }     public int getVideos(){         return videos;     } } public class tuts {     public static void main(String[] args) {         Employee aryan = new Employee();         Employee vidhan = new Employee(); //        Setting Attributes         aryan.id = 03;         aryan.videos = 36;         aryan.name = "Aryan Sonone";                  vidhan.id = 12;         vidhan.videos = 46;         vidhan.name = "Vidhan Laddha";     ...

Variable Arguments (VarArgs) in Java

 package com.company; //import java.util.Scanner; public class tuts {     static int sum(int...arr){         int result = 0;         for (int a: arr){             result += a;         }         return result;     }     public static void main(String[] args) {         System.out.println("The Sum of 1+2+3+4+5+6+7+8+9+10 is: "+ sum(1,2,3,4,5,6,7,8,9,10) );         System.out.println("The sum of nothing is: " + sum());         } }

Overloading In Java

  package com.company ; import java.util.Scanner ; public class tuts { static void foo (){ System. out .println( "Hello!" ) ; } static void foo ( int a){ System. out .println( "Good Morning " +a+ " bro!" ) ; } public static void main (String[] args) { foo () ; foo ( 3000 ) ; // Arguments are actual ! } }

Methods In Java

  package com.company ; import java.util.Scanner ; public class tuts { static int logic ( int x , int y){ int z ; if (x>y){ z = x+y ; } else { z = (x+ y) * 5 ; } return z ; } public static void main (String[] args) { int a = 2 ; int b = 4 ; int c ; c = logic (a , b) ; System. out .println(c) ; } }

For Loop For Each Loop For Printing Arrays

  package com.company ; import java.util.Scanner ; public class tuts { public static void main (String[] args) { int marks[] = { 1 , 2 , 3 , 4 , 5 } ; System. out .println(marks[ 0 ]) ; System. out .println(marks. length ) ; // float marks[] = {9.5f, 8f, 9f, 8.5f, 9f}; // System.out.println(marks.length); // System.out.println(marks[4]); String names[] = { "Aryan" , "Vidhan" , "Harry" } ; System. out .println(names. length ) ; System. out .println(names[ 2 ]) ; // Displaying The elements of an Array with for loop // for (int i=0;i<marks.length;i++){ // System.out.println(marks[i]); // } // Quick Quiz // Printing array in reverse with for loop for ( int i= marks. length - 1 ; i>- 1 ; i--){ System. out .println(marks[i]) ; } // Printing with for-each loop for ( int element: marks){ ...

Introduction to Arrays

 package com.company; import java.util.Scanner; public class tuts {     public static void main(String[] args) { //        Three ways to create an Array //        1. //        int [] marks = new int[5]; //        2. //        int marks[]; //        marks = new int[5]; //        3.         int marks[] = {100,96,95,98,88}; //        marks[0] = 100; //        marks[1] = 90; //        marks[2] = 80; //        marks[3] = 850; //        marks[4] = 96; //        marks[4] = 98;         System.out.println(marks[4]);     } }

Break And Continue In Java

package com.company; import java.util.Scanner; public class tuts {     public static void main(String[] args) { //      for (int i=1; i<=10;i++){ //            System.out.println(i); //        } //        Quick Quiz //        int a = 2; //        for (int o = 0; o<=30; o++){ //            System.out.println(o*2+1); //        } //        Quick Quiz //        for (int i = 100; i>0; i--){ //            System.out.println(i); //        } //            Break will end the whole loop and ends/exits the loop. //            Continue means to finish the iteration for the particular value and continue like if continue is for 2 so //        ...

For Loop And Decrementing For Loop

package com.company; import java.util.Scanner; public class tuts {     public static void main(String[] args) { //      for (int i=1; i<=10;i++){ //            System.out.println(i); //        } //        Quick Quiz //        int a = 2; //        for (int o = 0; o<=30; o++){ //            System.out.println(o*2+1); //        } //        Quick Quiz         for (int i = 100; i>0; i--){             System.out.println(i);         }     } }

Do-While Loop

 package com.company; import java.util.Scanner; public class tuts {     public static void main(String[] args) { //        int a = 10; //        do { //            System.out.println(a); //            a++; //        }while (a<5);         int c = 1;         do {             System.out.println(c);             c++;         }while(c<=45);     } }

While Loop In Java

package com.company; import java.util.Scanner; public class tuts {     public static void main(String[] args) {         int i = 100;         while (i<=200){             System.out.println(i);             i++;         }         System.out.println("Printed Natural Numbers from 100-200");     } }  

Switch Case In Java

 package com.company; import java.util.Scanner; public class tuts {     public static void main(String[] args) {         int age;         Scanner sc = new Scanner(System.in);         System.out.println("Please Enter Year:");         age = sc.nextInt();         switch (age){             case 2006:                 System.out.println("You were born");                 break;             case 2017:                 System.out.println("You were in class 6");                 break;             case 2018:                 System.out.println("You were in class 7");           ...

If ,Else If and Else In Java

 package com.company; import java.util.Scanner; public class tuts {     public static void main(String[] args) {         int age;         Scanner sc = new Scanner(System.in);         System.out.println("Please Enter Your Age:");         age = sc.nextInt();         if (age>=18){             System.out.println("Yes you can drive");         }         else if (age==14){             System.out.println("Wait for more 4 years to drive!");         }         else{             System.out.println("No you can drive yet.");         }     } }
Image
  length() – Returns the length of String name. (5 in this case) toLowerCase() – Returns a new String which has all the lowercase characters from the String name. toUpperCase() – Returns a new String which has all the uppercase characters from the String name. trim() – Returns a new String after removing all the leading and trailing spaces from the original string. substring(int start) – Returns a substring from start to the end. Substring(3) returns “ry”. [ Note  that index starts from 0] substring(int start, int end) – Returns a substring from the start index to the end index. The start index is included and the end is excluded. replace(‘r’, ‘p’) – Returns a new string after replacing r with p. Happy is returned in this case. (This method takes char as argument) startsWith(“Ha”) – Returns true if name starts with string “Ha”. (True in this case) endsWith(“ry”) – Returns true if name ends with string “ry”. (True in this case) charAt(2) – Returns the character at a given inde...