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 find compound interest annually | |
* Learning Reference: https://www.easycalculation.com/compound-interest.php | |
* */ | |
import java.util.Scanner; | |
public class CompoundInterestAnnually { | |
public static void main(String[] args) { | |
double p, r, amount; | |
int n; |
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
package APTest2017Demo; | |
public class APArrayDemo1 { | |
public static void main(String[] args) { | |
int[][] arr = {{15,5,9,10},{12,16,11,6},{14,8,13,7}}; | |
int num =80; | |
Position findPos = Successors.findPosition(num,arr); | |
if(findPos!=null){ | |
System.out.println("Found at position : " + findPos); | |
} |
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 find HCF - Highest Common Factor (also called Greatest Common Divisor) | |
* 60 and 40 is 20, i.e, HCF(60,40) = 20 | |
* 100 and 150 is 50, i.e, HCF(150,50) = 50 | |
* 144 and 24 is 24, i.e, HCF(144,24) = 24 | |
* 17 and 89 is 1, i.e, HCF(17,89) = 1 | |
* LCM of (72,120) = 360 | |
* LCM of (15,20) = 60 | |
* Learning Reference: | |
* https://byjus.com/maths/lcm-of-two-numbers/ |
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 access data from MySQL database. | |
* References: | |
* https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-statements.html | |
* */ | |
import java.sql.*; | |
public class JdbcDemo1 { | |
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: Fibonacci Sequence | |
* In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. | |
* Reference: https://en.wikipedia.org/wiki/Fibonacci_number | |
* Sample Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 | |
* */ | |
public class FibonacciNumbersDemo { | |
public static void main(String[] args) { | |
int f0, f1, fn; | |
int num = 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
package GenericDemo; | |
public class GenericDemoArrayIssue { | |
public static void main(String[] args) { | |
ColoredPoint[] cptArray = new ColoredPoint[1]; | |
// cptArray[0]= new Point(); //This is illegal because Point instances have fewer members (only x and y) than ColoredPoint instances (x, y and color). Attempting to access a Point instance's nonexistent color field from its entry in the ColoredPoint array would result in a memory violation (because not memory has been assigned to color) and ultimately crash the JVM. | |
Point[] ptArray = cptArray; //This is legal because of covariance (an array of supertype references is a supertype of an array of subtype references). In this case, an array of point references is a supertype of an array of ColoredPoint references. Covariance is dangerous when abused. For Example line (ptArray[0] = new Point();) results in ArrayStoredException at runtime because a Point instance is not a ColoredPoint instance. Without this exception, an attempt to access the |
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: Generic Method with upper bound | |
* The type parameter section specifies that T extends Comparable< T >only objects of classes that implement interface Comparable< T > can be used with this method. | |
* In this case, Comparable is known as the upper bound of the type parameter. | |
* By default, Object is the upper bound. | |
* */ | |
package GenericDemo; | |
public class GenericMethodUpperBoundDemo1 { | |
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: Array variables contain references to arrays. When you make an assignment to an array variable, it simply copies the reference. But it doesn’t copy the array itself. | |
* To create a new copy of array java.util.Arrays provides a method named copyOf. | |
* Reference: | |
* https://books.trinket.io/thinkjava2/chapter7.html | |
* | |
* */ | |
import java.util.Arrays; | |
public class ArraysByRefDemo { |
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: Conversion from binary to decimal | |
* Reference: https://www.rapidtables.com/convert/number/binary-to-decimal.html, https://byjus.com/maths/binary-to-decimal-conversion/ | |
* Sample Data: | |
* 11 - 3 | |
* 111 - 7 | |
* 101 - 5 | |
* 1111 - 15 | |
* 1001 - 9 | |
* |
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: Conversion from decimal to octal | |
* Reference: https://www.rapidtables.com/convert/number/decimal-to-binary.html, https://byjus.com/maths/convert-decimal-to-octal/ | |
* Sample Data: | |
* 11 - 13 | |
* 21 - 25 | |
* 57 - 71 | |
* 17 - 21 | |
* | |
* Example 1: Convert (127)10 to Octal. |
NewerOlder