SADP
SADP
Sr.No Program
1 Write a JAVA Program to implement built-in support (java.util.Observable) Weather station with
members temperature, humidity, pressure and methods mesurmentsChanged(),
setMesurment(),getTemperature(), getHumidity(), getPressure()
2 Write a Java Program to implement I/O Decorator for converting uppercase letters to lower case
letters.
3 Write a Java Program to implement Factory method for Pizza Store with createPizza(),
orederPizza(), prepare(), Bake(), cut(), box(). Use this to create variety of pizza’s like
NyStyleCheesePizza, ChicagoStyleCheesePizza etc
8 Write a Java Program to implement Iterator Pattern for Designing Menu like Breakfast, Lunch or
Dinner Menu.
Q1) Write a JAVA Program to implement built-in support
(java.util.Observable) Weather station with members temperature,
humidity, pressure and methods mesurmentsChanged(),
setMesurment(), getTemperature(), getHumidity(), getPressure()
import java.util.*;
display();
}
display();
}
CurrentConditionsDisplay currentDisplay =
new CurrentConditionsDisplay(weatherData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
Output
import java.io.*;
import java.util.*;
import java.util.ArrayList;
Step 2: Create Concrete Pizza classes which extends abstract Pizza class
- CheesePizza, ClamPizza, VeggiePizza, and PepperoniPizza class:
if (type.equals("cheese")) {
pizza = new CheesePizza();
} else if (type.equals("pepperoni")) {
pizza = new PepperoniPizza();
} else if (type.equals("clam")) {
pizza = new ClamPizza();
} else if (type.equals("veggie")) {
pizza = new VeggiePizza();
}
return pizza;
}
}
package com.ramesh.gof.factory.pizzas;
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
pizza = store.orderPizza("veggie");
System.out.println("We ordered a " + pizza.getName() + "\n");
}
}
Output :
Preparing Cheese Pizza
Baking Cheese Pizza
Cutting Cheese Pizza
Boxing Cheese Pizza
We ordered a Cheese Pizza
package in.bench.resources.singleton.design.pattern;
private SingletonDesignPatternInMultiThreadedEnvironment()
{}
public static SingletonDesignPatternInMultiThreadedEnvironment
getInstance()
{
synchronized
(SingletonDesignPatternInMultiThreadedEnvironment.class)
{
if(null == INSTANCE)
{
INSTANCE =new SingletonDesignPatternInMultiThreadedEnvironment();
}
return INSTANCE;
}
}
}
Output
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 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 SimpleRemoteControl
{
Command slot;
public SimpleRemoteControl()
{
}
class RemoteControlTest
{
public static void main(String[] args)
{
SimpleRemoteControl remote =new SimpleRemoteControl();
remote.setCommand(newLightOnCommand(light));
remote.buttonWasPressed();
remote.setCommand(newStereoOnWithCDCommand(stereo));
remote.buttonWasPressed();
remote.setCommand(newStereoOffCommand(stereo));
remote.buttonWasPressed();
}
}
Output
Light is on
Stereo is on
Stereo is set for CD input
Stereo volume set to 11
Stereo is off
Q6. Write a Java Program to implement undo command to test Ceiling
fan.
interface Command
{
public void execute();
}
class CeilingFan
{
public void on()
{
System.out.println("Ceiling Fan is on");
}
public void off()
{
System.out.println("Ceiling Fan is off");
}
}
public CeilingFanOnCommand(CeilingFan l)
{
this.c = l;
}
public CeilingFanOffCommand(CeilingFan l)
{
this.c = l;
}
public void execute()
{
c.off();
}
}
class SimpleRemoteControl
{
Command slot;
public SimpleRemoteControl() {}
}
public class Main
{
public static void main(String[] args)
{
SimpleRemoteControl remote = new SimpleRemoteControl();
remote.setCommand(ceilingFanOn);
remote.buttonWasPressed();
remote.setCommand(ceilingFanOff);
remote.buttonWasPressed();
}
}
Output
Ceiling Fan is on
Ceiling Fan is off
Q7. Write a Java Program to implement Adapter pattern for
Enumeration iterator
import java.util.*;
String name;
return name;
String name;
String description;
boolean vegetarian;
double price;
double price)
this.name = name;
this.description = description;
this.vegetarian = vegetarian;
this.price = price;
return name;
return description;
return price;
return vegetarian;
}
ArrayList<MenuItem> menuItems;
public PancakeHouseMenu()
name = "BREAKFAST";
true,
2.99);
false,
2.99);
addItem("Blueberry Pancakes",
"Pancakes made with fresh blueberries, and blueberry syrup",
true,
3.49);
addItem("Waffles",
true,
3.59);
menuItems.add(menuItem);
return menuItems;
}
public Iterator<MenuItem> createIterator()
return menuItems.iterator();
import java.util.Iterator;
public DinerMenu()
{
name = "LUNCH";
menuItems = new MenuItem[MAX_ITEMS];
addItem("Vegetarian BLT",
"(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);
addItem("BLT",
"Bacon with lettuce & tomato on whole wheat", false, 2.99);
addItem("Soup of the day",
"Soup of the day, with a side of potato salad", false, 3.29);
addItem("Hotdog",
"A hot dog, with saurkraut, relish, onions, topped with cheese",
false, 3.05);
addItem("Steamed Veggies and Brown Rice",
"Steamed vegetables over brown rice", true, 3.99);
addItem("Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true, 3.89);
}
public
// other menu methods here
}
import java.util.Iterator;
System.out.print(MENU\n----\n);
while(menuIterator.hasNext()) {
Menu menu = (Menu)menuIterator.next();
System.out.print("\n" + menu.getName() + "\n");
printMenu(menu.createIterator());
}
}
}
}
The output printing the menus is:
$ java MenuTestDrive
MENU
----
BREAKFAST
K&B's Pancake Breakfast, 2.99 -- Pancakes with scrambled eggs, and toast
Regular Pancake Breakfast, 2.99 -- Pancakes with fried eggs, sausage
Blueberry Pancakes, 3.49 -- Pancakes made with fresh blueberries, and blueberry
syrup
Waffles, 3.59 -- Waffles, with your choice of blueberries or strawberries
LUNCH
Vegetarian BLT, 2.99 -- (Fakin') Bacon with lettuce & tomato on whole wheat
BLT, 2.99 -- Bacon with lettuce & tomato on whole wheat
Soup of the day, 3.29 -- Soup of the day, with a side of potato salad
Hotdog, 3.05 -- A hot dog, with saurkraut, relish, onions, topped with cheese
Steamed Veggies and Brown Rice, 3.99 -- Steamed vegetables over brown rice
Pasta, 3.89 -- Spaghetti with Marinara Sauce, and a slice of sourdough bread