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 / QuadrantDemo.java
Created January 28, 2021 16:18
Example Conditional Execution - To determine cartesian point lies in which quadrant
/**
* Initialize cartesian pair of point and determine point lies in which quadrant.
* */
public class QuadrantDemo {
public static void main(String[] args) {
int x,y;
x=1;
y=-2;
if(x>0 && y>0)
System.out.println("Point ( " + x + ", " + y + ")" + " lies in the first quadrant" );
@BT-ICD
BT-ICD / InitializerDemo.java
Created February 3, 2021 13:46
JAVA: Ways to initialize data members: Field Initializer Constructor Initializer block
/**
* Three ways to establish initialize data member
* 1. Field initializer
* a. Specify field’s initial value as part of the field’s declaration
* b. Example: long result = 24;
* 2. Constructor
* 3. Initialization blocks
* a. Share code across all constructors
* b. Initialization block cannot receive any parameters
* c. A class can have zero or more initialization block
@BT-ICD
BT-ICD / PrintAToZCharacters.java
Created February 5, 2021 02:11
Exercise for loop: To display/print A to Z
/**
* Exercise for loop: To display/print A to Z
* */
public class PrintAToZCharacters {
public static void main(String[] args) {
char ch;
for (int i = 65; i <=90 ; i++) {
ch = (char)i;
System.out.println(ch);
}
@BT-ICD
BT-ICD / ForLoopBreakAndContinueDemo.java
Created February 10, 2021 15:31
Example: To understand use of break and continue with for loop
/**
* To understand use of break and continue in for loop
* */
public class ForLoopBreakAndContinueDemo {
public static void main(String[] args) {
int i;
//Example of break - will print 1 to 5 moves outside loop when value of i>5
for ( i = 1; i <=10 ; i++) {
System.out.print("Loop-1 --> ");
if(i>5)
@BT-ICD
BT-ICD / WhileLoopDemo1.java
Created February 10, 2021 15:39
Example of while loop: Print 1 to 5 using while loop
/**
* To print 1 to 5 using while loop
* Example of while loop
* */
public class WhileLoopDemo1 {
public static void main(String[] args) {
int i=1;
while(i<=5){
System.out.println(i);
i++;
@BT-ICD
BT-ICD / WhileLoopDemo2.java
Created February 10, 2021 15:47
Example of while loop: Sum of positive values input by the user.
/**
* While loop demo
* Allow user to input values as long as user input positive values.
* Stop getting values from the user once user input negative value.
* Display number of positive values as well as sum of positive values input by the user.
* */
import java.util.Scanner;
public class WhileLoopDemo2 {
public static void main(String[] args) {
@BT-ICD
BT-ICD / CelsiusToFahrenheitDemo.java
Created February 14, 2021 06:48
Example: Basic Math Operators - Program to convert temperature in celsius to temperature in fahrenheit
import java.util.Scanner;
/**
* Program to convert temperature in celsius to temperature in fahrenheit
* 0 degrees Celsius is equal to 32 degrees Fahrenheit:
* F = C × 9/5 + 32
* Sample Data
* Celsius =0 Fahrenheit = 32
* Celsius =11 Fahrenheit = 51.8
* */
@BT-ICD
BT-ICD / DoWhileLoopDemo1.java
Created February 14, 2021 06:59
Example Do while loop - to print 1 to 5
/**
* Example of Do While Loop
* Print 1 to 5
* */
public class DoWhileLoopDemo1 {
public static void main(String[] args) {
int i=1;
do{
System.out.println("I is " + i);
i++;
@BT-ICD
BT-ICD / PatternDemo1.java
Created February 18, 2021 15:55
Example of nested loop and System.out.format
/**
* Example of nested loop
* */
public class PatternDemo1 {
public static void main(String[] args) {
int i,j;
for ( i = 1; i <= 4; i++) {
for(j=1;j<=4;j++){
// System.out.print(j);
System.out.format("%3d",j);
@BT-ICD
BT-ICD / StringClassDemo.java
Created March 10, 2021 15:47
Example to learn methods of String class
/**
* Example to learn methods of String class
* Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
* */
public class StringClassDemo {
public static void main(String[] args) {
String data = "We will do it";
//Convert to upper case and lover case
String s1 = data.toUpperCase();