This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| * */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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); | |
| } |