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 / ArrayDemo2.java
Created March 18, 2021 17:14
Array Example: To store 3 integer values
/**
* Example to learn about array of integer
* */
public class ArrayDemo2 {
public static void main(String[] args) {
//Declaration of array of integer
int[] arr = new int[3];
//initialize elements of an array
arr[0]=10;
arr[1]=20;
@BT-ICD
BT-ICD / ComparableDemo.java
Created March 15, 2021 10:05
Example to understand Comparable interface
/**
* Example: To understand Comparable interface
* Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
* To sort array of products based on rate, in ascending order.
* */
package ComparableExample;
import java.util.Arrays;
public class ComparableDemo {
public static void main(String[] args) {
@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();
@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 / 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 / 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 / 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 / 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 / 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 / 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);
}