Crack Program
JAVA PROGRAMS
Uppercase to lowercase (1-20)
// Java program to Convert characters
// of a string to opposite case
class Test
{
// Method to convert characters
// of a string to opposite case
static void convertOpposite(StringBuffer str)
{
int ln = str.length();
// Conversion using predefined methods
for (int i=0; i<ln; i++)
{
Character c = str.charAt(i);
if (Character.isLowerCase(c))
str.replace(i, i+1, Character.toUpperCase(c)+"");
else
str.replace(i, i+1, Character.toLowerCase(c)+"");
}
}
public static void main(String[] args)
{
StringBuffer str = new StringBuffer("GeEkSfOrGeEkS");
// Calling the Method
convertOpposite(str);
System.out.println(str);
}
}
Weather Station (3-18-28)
import java.util.ArrayList;
class CurrentConditionsDisplay implements Observer, DisplayElement
{
private float temperature;
private float humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData)
{
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temperature, float humidity, float pressure)
{
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display()
{
System.out.println("Current conditions: " + temperature+ "F degrees and " + humidity + "% humidity");
}
}
interface DisplayElement
{
public void display();
}
class ForecastDisplay implements Observer, DisplayElement
{
private float currentPressure = 29.92f;
private float lastPressure;
private WeatherData weatherData;
public ForecastDisplay(WeatherData weatherData)
{
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temp, float humidity, float pressure)
{
lastPressure = currentPressure;
currentPressure = pressure;
display();
}
public void display()
{
System.out.print("Forecast: ");
if (currentPressure > lastPressure)
{
System.out.println("Improving weather on the way!");
}
else if (currentPressure == lastPressure)
{
System.out.println("More of the same");
}
else if (currentPressure < lastPressure)
{
System.out.println("Watch out for cooler, rainy weather");
}
}
}
interface Observer
{
public void update(float temp, float humidity, float pressure);
}
class StatisticsDisplay implements Observer, DisplayElement
{
private float maxTemp = 0.0f;
private float minTemp = 200;
private float tempSum = 0.0f;
private int numReadings;
private WeatherData weatherData;
public StatisticsDisplay(WeatherData weatherData)
{
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temp, float humidity, float pressure)
{
tempSum += temp;
numReadings++;
if (temp > maxTemp)
{
maxTemp = temp;
}
if (temp < minTemp)
{
minTemp = temp;
}
display();
}
public void display()
{
System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)+ "/" + maxTemp + "/" + minTemp);
}
}
interface Subject
{
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
class WeatherData implements Subject
{
private ArrayList<Observer> observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData()
{
observers = new ArrayList<>();
}
public void registerObserver(Observer o)
{
observers.add(o);
}
public void removeObserver(Observer o)
{
int i = observers.indexOf(o);
if (i >= 0)
{
observers.remove(i);
}
}
public void notifyObservers()
{
for (int i = 0; i < observers.size(); i++)
{
Observer observer = (Observer) observers.get(i);
observer.update(temperature, humidity, pressure);
}
}
public void measurementsChanged()
{
notifyObservers();
}
public void setMeasurements(float temperature, float humidity, float pressure)
{
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
public float getTemperature()
{
return temperature;
}
public float getHumidity()
{
return humidity;
}
public float getPressure()
{
return pressure;
}
}
public class WeatherStation
{
public static void main(String[] args)
{
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 29.2f);
}
}
RemoteControlTest. (6-21)
interface Command
{
public void execute();
}
class Light
{
public void on()
{
System.out.println("Light is on");
}
public void off()
{
System.out.println("Light is off");
}
}
class LightOnCommand implements Command
{
Light light;
public LightOnCommand(Light light)
{
this.light = light;
}
public void execute()
{
light.on();
}
}
class LightOffCommand implements Command
{
Light light;
public LightOffCommand(Light light)
{
this.light = light;
}
public void execute()
{
light.off();
}
}
class Stereo
{
public void on()
{
System.out.println("Stereo is on");
}
public void off()
{
System.out.println("Stereo is off");
}
public void setCD()
{
System.out.println("Stereo is set " + "for CD input");
}
public void setDVD()
{
System.out.println("Stereo is set" + " for DVD input");
}
public void setRadio()
{
System.out.println("Stereo is set" + " for Radio");
}
public void setVolume(int volume)
{
System.out.println("Stereo volume set" + " to " + volume);
}
}
class StereoOffCommand implements Command
{
Stereo stereo;
public StereoOffCommand(Stereo stereo)
{
this.stereo = stereo;
}
public void execute()
{
stereo.off();
}
}
class StereoOnWithCDCommand implements Command
{
Stereo stereo;
public StereoOnWithCDCommand(Stereo stereo)
{
this.stereo = stereo;
}
public void execute()
{
stereo.on();
stereo.setCD();
stereo.setVolume(11);
}
}
class SimpleRemoteControl
{
Command slot;
public SimpleRemoteControl()
{}
public void setCommand(Command command)
{
slot = command;
}
public void buttonWasPressed()
{
slot.execute();
}
}
class RemoteControlTest
{
public static void main(String[] args)
{
SimpleRemoteControl remote = new SimpleRemoteControl();
Light light = new Light();
Stereo stereo = new Stereo();
remote.setCommand(new LightOnCommand(light));
remote.buttonWasPressed();
remote.setCommand(new StereoOnWithCDCommand(stereo));
remote.buttonWasPressed();
remote.setCommand(new StereoOffCommand(stereo));
remote.buttonWasPressed();
}
}
SingletonPatter. (2-25)
class Singleton
{
private static Singleton uniqueInstance;
Singleton()
{
}
public static synchronized Singleton getInstance()
{
if(uniqueInstance==null)
{
uniqueInstance=new Singleton();
}
System.out.println(uniqueInstance);
return uniqueInstance;
}
}
public class SingletonPatter
{
public static void main(String[] args)
{
Singleton s=new Singleton();
s.getInstance();
}
}
PizzaTestDrive (4-19-30)
import java.util.ArrayList;
class ChicagoStyleCheesePizza extends Pizza
{
public ChicagoStyleCheesePizza()
{
name = "Chicago Style Deep Dish Cheese Pizza";
dough = "Extra Thick Crust Dough";
sauce = "Plum Tomato Sauce";
toppings.add("Shredded Mozzarella Cheese");
}
void cut()
{
System.out.println("Cutting the pizza into square slices");
}
}
class NYStyleCheesePizza extends Pizza
{
public NYStyleCheesePizza()
{
name = "NY Style Sauce and Cheese Pizza";
dough = "Thin Crust Dough";
sauce = "Marinara Sauce";
toppings.add("Grated Reggiano Cheese");
}
}
abstract class Pizza
{
String name;
String dough;
String sauce;
ArrayList toppings = new ArrayList();
void prepare()
{
System.out.println("Preparing " + name);
System.out.println("Tossing dough...");
System.out.println("Adding sauce...");
System.out.println("Adding toppings : ");
for (int i = 0; i < toppings.size(); i++)
{
System.out.println(" " + toppings.get(i));
}
}
void bake()
{
System.out.println("Bake for 25 minutes at 350");
}
void cut()
{
System.out.println("Cutting the pizza into diagonal slices");
}
void box()
{
System.out.println("Place pizza in official PizzaStore box");
}
public String getName()
{
return name;
}
}
abstract class PizzaStore
{
public Pizza orderPizza(String type)
{
Pizza pizza;
pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract Pizza createPizza(String type);
}
class ChicagoPizzaStore extends PizzaStore
{
public Pizza createPizza(String item)
{
if (item.equals("cheese"))
{
return new ChicagoStyleCheesePizza();
}
else
{
return null;
}
}
}
class NYPizzaStore extends PizzaStore
{
public Pizza createPizza(String item)
{
if (item.equals("cheese"))
{
return new NYStyleCheesePizza();
}
else
{
return null;
}
}
}
public class PizzaTestDrive
{
public static void main(String[] args)
{
PizzaStore nyStore = new NYPizzaStore();
Pizza pizza = nyStore.orderPizza("cheese");
System.out.println("Sai ordered a " + pizza.getName() + "\n");
PizzaStore chicagoStore = new ChicagoPizzaStore();
pizza = chicagoStore.orderPizza("cheese");
System.out.println("Jyoti ordered a " + pizza.getName() + "\n");
}
}
DuckTestDrive (10-26)
interface Duck
{
public void quack();
public void fly();
}
class MallardDuck implements Duck {
public void quack() {
System.out.println("Quack");
}
public void fly() {
System.out.println("I'm flying");
}
}
interface Turkey {
public void gobble();
public void fly();
}
class WildTurkey implements Turkey {
public void gobble() {
System.out.println("Gobble gobble");
}
public void fly() {
System.out.println("I'm flying a short distance");
}
}
class TurkeyAdapter implements Duck {
Turkey turkey;
public TurkeyAdapter(Turkey turkey) {
this.turkey = turkey;
}
public void quack() {
turkey.gobble();
}
public void fly() {
for(int i=0; i < 5; i++) {
turkey.fly();
}
}
}
public class DuckTestDrive
{
public static void main(String[] args) {
MallardDuck duck = new MallardDuck();
WildTurkey turkey = new WildTurkey();
Duck turkeyAdapter = new TurkeyAdapter(turkey);
System.out.println("The Turkey says...");
turkey.gobble();
turkey.fly();
System.out.println("\nThe Duck says...");
testDuck(duck);
System.out.println("\nThe TurkeyAdapter says...");
testDuck(turkeyAdapter);
}
static void testDuck(Duck duck) {
duck.quack();
duck.fly();
}
}
GumballMachineTestDrive (8-23-29)
class GumballMachine {
final static int SOLD_OUT = 0;
final static int NO_QUARTER = 1;
final static int HAS_QUARTER = 2;
final static int SOLD = 3;
int state = SOLD_OUT;
int count = 0;
public GumballMachine(int count) {
this.count = count;
if (count > 0) {
state = NO_QUARTER;
}
}
public void insertQuarter() {
if (state == HAS_QUARTER) {
System.out.println("You can't insert another quarter");
} else if (state == NO_QUARTER) {
state = HAS_QUARTER;
System.out.println("You inserted a quarter");
} else if (state == SOLD_OUT) {
System.out.println("You can't insert a quarter, the machine is sold out");
} else if (state == SOLD) {
System.out.println("Please wait, we're already giving you a gumball");
}
}
public void ejectQuarter() {
if (state == HAS_QUARTER) {
System.out.println("Quarter returned");
state = NO_QUARTER;
} else if (state == NO_QUARTER) {
System.out.println("You haven't inserted a quarter");
} else if (state == SOLD) {
System.out.println("Sorry, you already turned the crank");
} else if (state == SOLD_OUT) {
System.out.println("You can't eject, you haven't inserted a quarter yet");
}
}
public void turnCrank() {
if (state == SOLD) {
System.out.println("Turning twice doesn't get you another gumball!");
} else if (state == NO_QUARTER) {
System.out.println("You turned but there's no quarter");
} else if (state == SOLD_OUT) {
System.out.println("You turned, but there are no gumballs");
} else if (state == HAS_QUARTER) {
System.out.println("You turned...");
state = SOLD;
dispense();
}
}
public void dispense() {
if (state == SOLD) {
System.out.println("A gumball comes rolling out the slot");
count = count - 1;
if (count == 0) {
System.out.println("Oops, out of gumballs!");
state = SOLD_OUT;
} else {
state = NO_QUARTER;
}
} else if (state == NO_QUARTER) {
System.out.println("You need to pay first");
} else if (state == SOLD_OUT) {
System.out.println("No gumball dispensed");
} else if (state == HAS_QUARTER) {
System.out.println("No gumball dispensed");
}
}
// other methods here like toString() and refill()
}
public class GumballMachineTestDrive
{
public static void main(String[] args)
{
GumballMachine gumballMachine = new GumballMachine(5);
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.ejectQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.ejectQuarter();
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine);
}
}
