public class A {
public static void main(String[] args) {
String temp = "TESTING";
temp = getString(temp);
System.out.println(temp);
}
public static String getString(String asTemp) {
if (asTemp.length() == 0)
return asTemp;
else {
return getString(asTemp.substring(1, asTemp.length()))
+ asTemp.substring(0, 1);
}
}
}
Labels
String reversal using recursion
Floating Point Magic
import java.io.*;
import java.util.*;
public class A {
public static void main(String args[]) {
double a1 = 0.10;
double a2 = 0.80;
double a3 = 4.00;
System.out.println("a1+a2+a3 :" + (a1 + a2 + a3));
System.out.println("a1+a3+a2 :" + (a1 + a3 + a2));
System.out.println("a2+a1+a3 :" + (a2 + a1 + a3));
System.out.println("a2+a3+a1 :" + (a2 + a3 + a1));
System.out.println("a3+a1+a2 :" + (a3 + a1 + a2));
System.out.println("a3+a2+a1 :" + (a3 + a2 + a1));
}
}
Command Design Pattern
The Command Design Patterns main goal is to decouple the invoker from the Receiver.
The invoker is the one which invokes an action on the Receiver, and the receiver is the one on which the command is executed.
// The Receiver
package Command;
public class Movie {
public static void start() {
System.out.println("Start Movie");
}
public static void stop() {
System.out.println("Movie Stopped");
}
}
//Play Command
public class PlayMovieCommand implements CommandIntf {
public void execute() {
Movie.start();
}
}
//Stop Command
public class StopMovieCommand implements CommandIntf {
public void execute() {
Movie.stop();
}
}
//The Invoker
package Command;
public class MoviePlayer {
static CommandIntf play;
static CommandIntf stop;
public static void setPlayCmd(CommandIntf playMovCmd) {
play = playMovCmd;
}
public static void setStopCmd(CommandIntf stopMovCmd) {
stop = stopMovCmd;
}
public static void playMovie() {
play.execute();
}
public static void stopMovie() {
stop.execute();
}
}
Singleton Design Pattern

package singleton; public class Singleton { private static Singleton aSingleton; private Singleton() { } static Singleton getInstance() { synchronized (Singleton.class) { if (Singleton.aSingleton == null) { Singleton.aSingleton = new Singleton(); } } return Singleton.aSingleton; } }
- only one instance of a class across threads
- global point of access
Decorator Design Pattern (Wrapper)
DrinkDecorator
public abstract class DrinkDecorator extends Drink {
public Drink drink;
}
Drink
public abstract class Drink {
String description;
int cost;
abstract int getCost();
String getDesc() {
return this.description;
}
}
Vodka
public class Vodka extends Drink {
String description = " Vodka ";
int cost = 100;
@Override
int getCost() {
return this.cost;
}
}
public class OrangeJuice extends DrinkDecorator {
String description = " fresh orenge juice ";
int cost = 5;
public OrangeJuice(Drink myVodka) {
this.drink = myVodka;
}
@Override
int getCost() {
return this.cost + this.drink.getCost();
}
}
Client Code
//Make Vodka with orange
//make vodka
Drink myVodka = new Vodka();
//mix orenge and vodka
DrinkDecorator orengeVodka= new OrangeJuice(myVodka);
//mix lime juie in the orengeVodka
DrinkDecorator LimeOrengeVodka= new LimeJuice(orengeVodka);
//the cost of the final vodka should be that of lime
//orenge and vodka cost
System.out.println(LimeOrengeVodka.getCost());