Skip to content

Instantly share code, notes, and snippets.

View BT-ICD's full-sized avatar

Bhavin BT-ICD

  • ICD
  • Gujarat, India
View GitHub Profile
@BT-ICD
BT-ICD / VariableNumberOfArgumentsDemo.java
Created April 28, 2021 17:52
Example: To learn about variable number of arguments for a function
/**
* Example: To learn about variable number of arguments for a function
* Varargs is a short name for variable arguments.
* In Java, an argument of a method can accept arbitrary number of values.
* This argument that can accept variable number of values is called varargs.
* In order to define vararg, ... (three dots) is used in the formal parameter of a method.
*
* Note:
* A method can have variable length parameters with other parameters too, but one should ensure that there exists only one varargs parameter that should be written last in the parameter list of the method declaration.
* */
@BT-ICD
BT-ICD / DecimalToBinaryDemo.java
Created April 28, 2021 17:39
Conversion from decimal to binary
/**
* Example: Conversion from decimal to binary
* Reference: https://www.rapidtables.com/convert/number/decimal-to-binary.html
* Sample Data:
* 11 - 1011
* 12 - 1100
* 13 - 1101
**/
public class DecimalToBinaryDemo {
public static void main(String[] args) {
@BT-ICD
BT-ICD / FactorialWithRecursion.java
Created April 28, 2021 17:25
Recursion - Function to find factorial using recursion
/**
* Example: To learn about recursion -
* Function to factorial using recursion
* **/
public class FactorialWithRecursion {
public static void main(String[] args) {
int num = 4;
int ans = factorial(num);
System.out.println("Factorial of " + num + " is " + ans);
}
@BT-ICD
BT-ICD / SingletonPatternDemo_SingletonOptionOne.java
Created April 28, 2021 11:09
Example to learn about Singleton Pattern in JAVA
package SingletonPatternDemo;
/*
*Example of Eager Initialization
* */
public class SingletonOptionOne {
private static final SingletonOptionOne INSTANCE = new SingletonOptionOne();
private SingletonOptionOne(){}
private String messageData;
/**
* To get singleton instance of SingletonOptionOne class
@BT-ICD
BT-ICD / ThreadWaitAndNotifyDemo_Message.java
Created April 23, 2021 14:10
Example to learn about wait and notify in Thread
package ThreadWaitAndNotifyDemo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Message {
String mesg;
boolean received =false;
synchronized void readmesg(){
@BT-ICD
BT-ICD / Printing.java
Created April 19, 2021 11:37
Example of Thread Synchronization - Synchronized Method
package ThreadSynchronizing;
/**
* A synchronized method, once having taken up a task, cannot be accessed by another threads until it completes the task.
* When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
* References:
* https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html
* Programming in JAVA2 Dr. K. Somasundaram
* */
class Printing
{
@BT-ICD
BT-ICD / ThreadDemoForWait_FactThread.java
Created April 19, 2021 11:08
Example of Thread.join
package ThreadDemoForWait;
public class FactThread implements Runnable{
int i, fact=1;
@Override
public void run() {
for (int i = 1; i <=5 ; i++) {
fact*=i;
System.out.println("\nFactorial of " + i + " = " + fact);
}
@BT-ICD
BT-ICD / TwoDimArrayDemo2.java
Created April 1, 2021 14:52
Initialize elements of Two Dimension Array during declaration
/**
* Example: 2 - Two Dimension Array
* To initialize elements of an array while declaration
* */
public class TwoDimArrayDemo2 {
public static void main(String[] args) {
int[][] data = {{2,3,4},{10,20,30}, {7,8,9}};
//length to get total number rows of array
System.out.println("Number of rows: " + data.length);
//length - number of columns in 0th row
@BT-ICD
BT-ICD / TwoDimArrayDemo1.java
Created April 1, 2021 14:47
Example of Two Dimension Array
/**
* Example: 1 - Two Dimension Array
* length of array - initialize array and iterate each element of an array
* */
public class TwoDimArrayDemo1 {
public static void main(String[] args) {
//Declaration of two dimension array. Having two rows and three columns
int[][] data = new int[2][3];
//Initialize values of first row
data[0][0]= 10;
@BT-ICD
BT-ICD / QuadraticEquationDemo.java
Created March 22, 2021 17:58
To determine real roots of quadratic equation
/**
* Example to determine real roots of a Quadratic Equation
* The Standard Form of a Quadratic Equation looks like this: ax2 + bx + c =0
* d= b2 - 4ac
* When b2 − 4ac is positive, we get two Real solutions. There are two real roots.
* When it is zero we get just ONE real solution (both answers are the same). There is one real root.
* When it is negative we get a pair of Complex solutions. There are no real roots.
*
* Reference: https://www.mathsisfun.com/quadratic-equation-solver.html
* https://www.mathsisfun.com/algebra/quadratic-equation.html