Java Archives | Programming Guide

Java Factory pattern

When do we use it? Design patterns are used when you have a super class with multiple sub classes depending on output you need to return one of the sub classes. Usage Method 1: Apply a Singleton pattern Usage Method 2: make the factory method static advantage Provide approach to code for interface rather than implementation Changes of […]

try-catch block

a try catch block prevents to crash the application to occur and intercept the Exceptions while runtime . Syntax:  try{ // code catch([Exceptionclass ex]){ // error handling } But what is a Exception?Exception is an event , which collides  serious unforseen problem.Exception class is a subclass of Throwable. Exception Hierarchy A typical IndexOutofBoundExceptions occurs when you access the […]

How to use reflections in java

Reflections provide you access to other classes,methods and constructors from a loaded class.Manipulating methods,Fields,Constructors is possible through invoking different operations. Reflections are often used in RPC(Remote Procedure Call) providing remote access to Constructors and Fields. The main disadvantages using reflections are: No compile Time checking Performing complex access low Perfomance Advantages: knowing compile time is not needed modify […]

Palindrome

A Palindrome is a text, number or sentence which can be read the same phrase or value backwards and forwards. For instance: Anna(forward)    annA (backwards)  We implement our code ignoring the lower and uppercase words because our word has the same meaning. This may be prevented by adding to  palindrom.toLowerCase() to our  String. Source Code: DOWNLOAD

Quicksort

Quicksort is an efficient sorting algorithm, developed by British computer scientist Tony Hoare in 1959. Algorithm:  find Pivot element (it may be the first, last or random value in array) put elements in order. The elements on the Left side are lesser than the pivot element. The elements on the Right side are greater than the pivot element […]

Recursion

Recursion Recursion is a function that always repeats a specific number of executions over time until abort criteria(s) of execution(s) has(have) reached. 1. Abort critera is a condition when you reached your objective. example: if(n== 1) { //Do nothing } 2.Recursive function: void Dosomething(int n){ if(n== 1) { //Do nothing } Dosomething(n-1); } Exercise: Recursive Decimal to Dual Converter […]

Finding derivative of a function

How do we find a derivative of a function? There are many different ways to solve the derivative. Let’s use Newton difference quotient and evaluate derivative of \[ f(x)= x^2 \] Formula: $f^\prime(x)$ = $\lim_{h = 0}$ = \(\frac{f(x_0+h)-f(x)}{h}\) (1.0) h:range[slope] between 2 points x0 and x+h are data points at f(x0) and f(x+h). The red line is […]

Variables and Datatypes

Datatype Table Datatype Size Range Int 4 Bytes -2147483648 … +2147483647 Boolean 1 bit true or false Byte 1 Byte(8 bit) -127…+127 short 2 bytes -32.768 – 32.767 long 8 Bytes -9,223..*10E18….+9,223*10E18 float 4 Bytes -+3.410E38 double 8 Bytes -+ 1.7e+038 char 2 Bytes 0 … 65,536 Example int number = 10; float num2 = 10f; // dont […]