Posts

Jarvis 0.0.1

  package com.company ; import java.time.LocalDateTime ; import java.time.format.DateTimeFormatter ; import java.util.Date ; import java.util.Scanner ; class Jarvis{ public String name ; public void date(){ LocalDateTime dt = LocalDateTime.now() ; // This is date DateTimeFormatter df = DateTimeFormatter.ofPattern( "dd/MM/yyyy" ) ; // This is date format String myDate = dt.format(df) ; //Creating date string using date and format System.out.println( "Today's date is " + myDate) ; } public void time(){ LocalDateTime dt = LocalDateTime.now() ; // This is date DateTimeFormatter df = DateTimeFormatter.ofPattern( "H:M a" ) ; // This is date format String myDate = dt.format(df) ; //Creating date string using date and format System.out.println( "It's " +myDate) ; } public void day(){ LocalDateTime dt = LocalDateTime.now() ; // This is date Date...

Using Api In Java

  package com.company ; import java.io.IOException ; import java.net.URI ; import java.net.http.HttpClient ; import java.net.http.HttpRequest ; import java.net.http.HttpResponse ; public class Main { public static void main (String[] args) throws IOException , InterruptedException { var url = "f0574529f9324dc3b291eb6a569fcbed" ; // httprequest var request = HttpRequest. newBuilder ().GET().uri(URI. create (url)).build() ; var client = HttpClient. newBuilder ().build() ; var response = client.send(request , HttpResponse.BodyHandlers. ofString ()) ; System. out .println(response.statusCode()) ; System. out .println(response.body()) ; } }

File Handling In Java

  package com.company ; import java.io.File ; import java.io.FileNotFoundException ; import java.io.FileWriter ; import java.io.IOException ; import java.util.Scanner ; public class tuts { public static void main (String[] args) { /* // Creating new File File myFile = new File("aryan111.txt"); try { myFile.createNewFile(); } catch (IOException e) { System.out.println("Unable to create this file"); } // Code to write to a file try { FileWriter fileWriter = new FileWriter("aryan111.txt"); fileWriter.write("This is our first file from this Java Course\nOk now Bye"); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } // Reading A File File myFile = new File("aryan111.txt"); try { Scanner sc = new Scanner(myFile); while(sc.h...

Creating JavaDoc With Method Tags

  package com.company ; /** * This class is to demonstrate what javadoc is and how it is used in the java industry * This is <i> italic </i> word <p> this is a new paragraph </p> * @author Aryan * @version 0.1 * @since 2002 * @see <a href="https://docs.oracle.com/en/java/javase/14/docs/api/index.html" target="_blank"> Java Docs </a> */ public class tuts { /** * @param args These are arguments supplied to the command line */ public static void main (String[] args) { System. out .println( "I am main method !!" ) ; } /** * * @param i This is the first number to add * @param j This is the second number to add * @return Sum of two numbers as integers * @exception Exception if i or j is more than 2147483647 */ public int add ( int i , int j) throws Exception { if (i== 2147483647 ) { throw new Exception() ; } if (j== 2...

Creating JavaDoc With Tags

package com.company ; /** * This class is to demonstrate what javadoc is and how it is used in the java industry * This is <i> italic </i> word <p> this is a new paragraph </p> * @author Aryan * @version 0.1 * @since 2002 * @see <a href="https://docs.oracle.com/en/java/javase/14/docs/api/index.html" target="_blank"> Java Docs </a> */ public class tuts { public void add ( int a , int b){ System. out .println( "The sum is: " + a+b) ; } public static void main (String[] args) { System. out .println( "This is my main method" ) ; } }

DateTimeFormatter in Java

Image
package com.company; import java.time.DateTimeException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class tuts {      public static void main(String[] args) {           LocalDateTime dt = LocalDateTime.now(); // This is date           System.out.println(dt);           DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MM/yyyy -- E H:M a"); // This is date format           String myDate = dt.format(df); //Creating date string using date and format           System.out.println(myDate);      } } Link For Java Doc : https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html  

java.time API - Classes & Methods

  package com.company ; import java.time.LocalDate ; import java.time.LocalTime ; public class tuts { public static void main (String[] args) { LocalDate d = LocalDate. now () ; System. out .println(d) ; LocalTime t = LocalTime. now () ; System. out .println(t) ; } }

GregorianCalendar class & TimeZone in java

  package com.company ; import java.util.Calendar ; import java.util.GregorianCalendar ; import java.util.TimeZone ; public class tuts { public static void main (String[] args) { // Calendar c = Calendar.getInstance(); // System.out.println(c.getCalendarType()); // System.out.println(c.getTimeZone()); // Calendar c = Calendar. getInstance () ; System. out .println(c.getCalendarType()) ; System. out .println(c.getTimeZone().getID()) ; System. out .println(c.get(Calendar. AM_PM )) ; System. out .println(c.get(Calendar. DATE )) ; System. out .println(c.get(Calendar. HOUR )) ; System. out .println(c.get(Calendar. HOUR_OF_DAY ) + ":" + c.get(Calendar. MINUTE ) + ":" + c.get(Calendar. SECOND )) ; GregorianCalendar cal = new GregorianCalendar() ; System. out .println(cal.isLeapYear( 2020 )) ; System. out .println(cal.isLeapYear( 2019 )) ; ...

Calendar Class In Java

package com.company ; import java.util.Calendar ; import java.util.TimeZone ; public class tuts { public static void main (String[] args) { // Calendar c = Calendar.getInstance(); // System.out.println(c.getCalendarType()); // System.out.println(c.getTimeZone()); // Calendar c = Calendar. getInstance () ; System. out .println(c.getCalendarType()) ; System. out .println(c.getTimeZone().getID()) ; System. out .println(c.get(Calendar. AM_PM )) ; System. out .println(c.get(Calendar. DATE )) ; System. out .println(c.get(Calendar. HOUR )) ; System. out .println(c.get(Calendar. HOUR_OF_DAY ) + ":" + c.get(Calendar. MINUTE ) + ":" + c.get(Calendar. SECOND )) ; } }

Date Class In Java

  package com.company ; import java.util.Date ; public class tuts { public static void main (String[] args) { // System.out.println(Long.MAX_VALUE); // System.out.println(Integer.MAX_VALUE); // System.out.println(System.currentTimeMillis()); Date d = new Date() ; System. out .println(d) ; System. out .println() ; } }

Date and Time in Java

  package com.company ; public class tuts { public static void main (String[] args) { System. out .println( "Years Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()/ 1000 / 3600 / 24 / 365 ) ; System. out .println( "Days Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()/ 1000 / 3600 / 24 ) ; System. out .println( "Hours Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()/ 1000 / 3600 ) ; System. out .println( "Seconds Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()/ 1000 ) ; System. out .println( "Milliseconds Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()) ; } }

Arraylist in Java

package com.company ; import java.util.ArrayList ; public class tuts { public static void main (String[] args) { ArrayList<Integer> l1 = new ArrayList<>() ; l1.add( 6 ) ; l1.add( 7 ) ; l1.add( 4 ) ; l1.add( 6 ) ; l1.add( 5 ) ; for ( int i= 0 ; i<l1.size() ; i++){ System. out .println(l1.get(i)) ; } // l1.clear(); System. out .println(l1) ; System. out .println(l1.contains( 4 )) ; System. out .println(l1.contains( 44 )) ; System. out .println(l1.contains( 6 )) ; System. out .println(l1.indexOf( 948 )) ; } }

Finally Block in Java & Why is it needed!

 package com.company; public class cwh_85_finally {     public static int greet(){         try{             int a = 50;             int b = 10;             int c = a/b;             return c;         }         catch(Exception e){             System.out.println(e);         }         finally {             System.out.println("Cleaning up resources...This is the end of this function");         }         return -1;     }     public static void main(String[] args) {         int k = greet();         System.out.println(k);         int a = 7;         int b = 9;   ...

Throw And Throws In Java

 package com.company; class NegativeRadiusException extends Exception{     @Override     public String toString() {         return "Radius cannot be negative!";     }     @Override     public String getMessage() {         return "Radius cannot be negative!";     } } public class cwh_84_throw_throws {     public static double area(int r) throws NegativeRadiusException{         if (r<0){             throw new NegativeRadiusException();         }         double result = Math.PI * r * r;         return result;     }     public static int divide(int a, int b) throws ArithmeticException{         // Made By Harry         int result = a/b;         return result;     }     ...

Nested Try Catch In Java

 package com.company; import java.util.Scanner; public class cwh_82_nested_try_catch {     public static void main(String[] args) {         int [] marks = new int[3];         marks[0] = 7;         marks[1] = 56;         marks[2] = 6;         Scanner sc = new Scanner(System.in);         boolean flag = true;         while(flag) {             System.out.println("Enter the value of index");             int ind = sc.nextInt();             try {                 System.out.println("Welcome to video no 82");                 try {                     System.out.println(marks[ind]);             ...

Try - Catch Blocks In Java

  package com.company ; public class tuts { public static void main (String[] args) { int a = 6000 ; int b = 0 ; try { int c = a/b ; System. out .println(c) ; } catch (Exception e){ System. out .println( "We can perform the division and the reason is: " ) ; System. out .println(e) ; } } }

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();      } }