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