Advanced Object Oriented Programming-AOOP(A)
Course Code-23CS2103A
Name: K. Madhu mitha
ID Number: 2300090088
Skill-Week-3
Question-1:
package Problem1;
interface PaymentProcessor {
void processPayment(double amount);
class PayPalAdapter implements PaymentProcessor {
private PayPalAPI payPalAPI;
public PayPalAdapter() {
this.payPalAPI = new PayPalAPI();
@Override
public void processPayment(double amount) {
payPalAPI.pay(amount);
class StripeAdapter implements PaymentProcessor {
private StripeAPI stripeAPI;
public StripeAdapter() {
this.stripeAPI = new StripeAPI();
@Override
public void processPayment(double amount) {
stripeAPI.charge(amount);
class PayPalAPI {
public void pay(double amount) {
System.out.println("Paid " + amount + " using PayPal");
class StripeAPI {
public void charge(double amount) {
System.out.println("Charged " + amount + " using Stripe");
public class PaymentClient {
public static void main(String[] args) {
PaymentProcessor paypalProcessor = new PayPalAdapter();
paypalProcessor.processPayment(100.0);
PaymentProcessor stripeProcessor = new StripeAdapter();
stripeProcessor.processPayment(200.0);
}
Output:
Question-2:
package Problem2;
abstract class TextProcessor {
public abstract String process(String text);
class BaseTextProcessor extends TextProcessor {
@Override
public String process(String text) {
return text;
abstract class TextProcessorDecorator extends TextProcessor {
protected TextProcessor textProcessor;
public TextProcessorDecorator(TextProcessor textProcessor) {
this.textProcessor = textProcessor;
@Override
public String process(String text) {
return textProcessor.process(text);
class SpellCheckDecorator extends TextProcessorDecorator {
public SpellCheckDecorator(TextProcessor textProcessor) {
super(textProcessor);
@Override
public String process(String text) {
text = super.process(text);
return spellCheck(text);
private String spellCheck(String text) {
return text.replaceAll("teh", "the");
class TextFormatDecorator extends TextProcessorDecorator {
public TextFormatDecorator(TextProcessor textProcessor) {
super(textProcessor);
@Override
public String process(String text) {
text = super.process(text);
return formatText(text);
private String formatText(String text) {
return text.trim().replaceAll("\\s+", " ");
public class TextProcessingClient {
public static void main(String[] args) {
TextProcessor baseProcessor = new BaseTextProcessor();
TextProcessor spellCheckedProcessor = new SpellCheckDecorator(baseProcessor);
TextProcessor formattedProcessor = new TextFormatDecorator(spellCheckedProcessor);
String text = " teh quick brown fox jumps ";
String result = formattedProcessor.process(text);
System.out.println("Original Text: \"" + text + "\"");
System.out.println("Processed Text: \"" + result + "\"");
Output:
Question-3:
package Problem3;
interface VideoFormat {
void play(String fileName);
class MP4Player implements VideoFormat {
@Override
public void play(String fileName) {
System.out.println("Playing MP4 file: " + fileName);
class AVIPlayer implements VideoFormat {
@Override
public void play(String fileName) {
System.out.println("Playing AVI file: " + fileName);
}
}
class MKVPlayer implements VideoFormat {
@Override
public void play(String fileName) {
System.out.println("Playing MKV file: " + fileName);
interface OperatingSystem {
void playVideo(String fileName, VideoFormat videoFormat);
class WindowsOS implements OperatingSystem {
@Override
public void playVideo(String fileName, VideoFormat videoFormat) {
System.out.println("Windows OS:");
videoFormat.play(fileName);
class MacOS implements OperatingSystem {
@Override
public void playVideo(String fileName, VideoFormat videoFormat) {
System.out.println("macOS:");
videoFormat.play(fileName);
class LinuxOS implements OperatingSystem {
@Override
public void playVideo(String fileName, VideoFormat videoFormat) {
System.out.println("Linux OS:");
videoFormat.play(fileName);
public class VideoPlayerClient {
public static void main(String[] args) {
VideoFormat mp4 = new MP4Player();
VideoFormat avi = new AVIPlayer();
VideoFormat mkv = new MKVPlayer();
OperatingSystem windows = new WindowsOS();
OperatingSystem mac = new MacOS();
OperatingSystem linux = new LinuxOS();
windows.playVideo("video1.mp4", mp4);
mac.playVideo("video2.avi", avi);
linux.playVideo("video3.mkv", mkv);
Output:
Question-4:
package Problem4;
interface StockPriceProvider {
double getStockPrice(String stockSymbol);
class StockAPIA {
public double getPrice(String stockSymbol) {
return 150.75;
class StockAPIB {
public String fetchStockPrice(String stockSymbol) {
return "120.50";
}
class StockAPIAAdapter implements StockPriceProvider {
private StockAPIA stockAPIA;
public StockAPIAAdapter(StockAPIA stockAPIA) {
this.stockAPIA = stockAPIA;
@Override
public double getStockPrice(String stockSymbol) {
return stockAPIA.getPrice(stockSymbol);
class StockAPIBAdapter implements StockPriceProvider {
private StockAPIB stockAPIB;
public StockAPIBAdapter(StockAPIB stockAPIB) {
this.stockAPIB = stockAPIB;
@Override
public double getStockPrice(String stockSymbol) {
return Double.parseDouble(stockAPIB.fetchStockPrice(stockSymbol));
class StockPriceViewer {
private StockPriceProvider provider;
public StockPriceViewer(StockPriceProvider provider) {
this.provider = provider;
}
public void displayStockPrice(String stockSymbol) {
System.out.println("Stock Price of " + stockSymbol + ": " + provider.getStockPrice(stockSymbol));
public class Main {
public static void main(String[] args) {
StockAPIA apiA = new StockAPIA();
StockPriceProvider providerA = new StockAPIAAdapter(apiA);
StockPriceViewer viewerA = new StockPriceViewer(providerA);
viewerA.displayStockPrice("AAPL");
StockAPIB apiB = new StockAPIB();
StockPriceProvider providerB = new StockAPIBAdapter(apiB);
StockPriceViewer viewerB = new StockPriceViewer(providerB);
viewerB.displayStockPrice("GOOG");
Output: