0% found this document useful (0 votes)
10 views16 pages

Questionset 6

The document contains multiple Java classes demonstrating object-oriented programming principles, including inheritance, interfaces, and adapters. Key examples include appliance management systems, user management, and payment processing, showcasing how to implement functionality using abstract classes and interfaces. Additionally, it features GUI components that respond to key events, illustrating interaction in a graphical user interface.

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

Questionset 6

The document contains multiple Java classes demonstrating object-oriented programming principles, including inheritance, interfaces, and adapters. Key examples include appliance management systems, user management, and payment processing, showcasing how to implement functionality using abstract classes and interfaces. Additionally, it features GUI components that respond to key events, illustrating interaction in a graphical user interface.

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

QUESTION 1

public abstract class Appliance {


private String brand;

public Appliance(String brand) {


this.brand = brand;
}

public abstract double calculateElectricityUsage();

public static void main(String[] args) {


Appliance washingMachine = new WashingMachine("Samsung", 1.5);
Appliance refrigerator = new Refrigerator("LG", 0.2);

System.out.println(washingMachine.brand + " Washing Machine


electricity usage: " +
washingMachine.calculateElectricityUsage() + " kWh");
System.out.println(refrigerator.brand + " Refrigerator electricity
usage: " +
refrigerator.calculateElectricityUsage() + " kWh");
}

public class Refrigerator extends Appliance {


private double powerUsagePerHour;

public Refrigerator(String brand, double powerUsagePerHour) {


super(brand);
this.powerUsagePerHour = powerUsagePerHour;
}

public double calculateElectricityUsage() {


return powerUsagePerHour * 24;
}
}

public class WashingMachine extends Appliance{


private double powerUsagePerHour;
public WashingMachine(String brand, double powerUsagePerHour){
super(brand);
this.powerUsagePerHour=powerUsagePerHour;
}
public double calculateElectricityUsage(){
return powerUsagePerHour*2;
}
}
QUESTION 2
public abstract class Appliance {
String brand;

public Appliance(String brand) {


this.brand = brand;
}

public abstract double calculateElectricityUsage();

public static void main(String[] args) {


SmartAppliance smartWashingMachine = new SmartAppliance("Samsung",
1.5);
smartWashingMachine.addSmartFeature("WiFi Connectivity", 0.2);
smartWashingMachine.addSmartFeature("Energy Monitoring", 0.1);

System.out.println(smartWashingMachine.brand + " Smart Washing


Machine electricity usage: "
+ smartWashingMachine.calculateElectricityUsage() + "
kWh");
System.out.println("Smart Features: " +
smartWashingMachine.getSmartFeatures());
}

import java.util.ArrayList;
import java.util.List;

public class SmartAppliance extends Appliance {

private List<String> smartFeatures;


private double powerUsagePerHour;

public SmartAppliance(String brand, double powerUsagePerHour) {


super(brand);
this.powerUsagePerHour = powerUsagePerHour;
this.smartFeatures = new ArrayList<>();
}

public void addSmartFeature(String x, double additionalUsage) {


smartFeatures.add(x);
powerUsagePerHour = powerUsagePerHour + additionalUsage;
}

@Override
public double calculateElectricityUsage() {
return powerUsagePerHour * 2;
}
public List<String> getSmartFeatures() {
return smartFeatures;
}
}

QUESTION 3
QUESTION 4
public class Configuration {
public static final int MAX_USERS = 100;

public static void main(String[] args) {


UserManagementSystem ums = new UserManagementSystem(95);
for (int i = 0; i < 10; i++) {

ums.addnewuser();
}
}
}

public class UserManagementSystem {


private int usercount;

public UserManagementSystem(int usercount) {


this.usercount = usercount;
}

public void addnewuser() {


if (usercount + 1 <= Configuration.MAX_USERS) {
usercount = usercount + 1;
System.out.println("User added. Total users: " + usercount);
} else
System.out.println("Cannot add more users. Max limit
reached");

}
}

QUESTION 5
import java.util.ArrayList;
import java.util.List;

public class BookManagementSystem {


private List<String> books;

public BookManagementSystem() {
this.books = new ArrayList<>();
}
public void addBook(String title) {

if (books.size() + 1 <= LibraryConfig.MAX_BOOKS) {


books.add(title);
System.out.println(title + " has been added to the library.");
} else
System.out.println("Cannot add more books the maximum limit
has been reached.");
}
}

public class LibraryConfig {


public static final int MAX_BOOKS = 4;

public static void main(String[] args) {


BookManagementSystem bookSystem = new BookManagementSystem();
bookSystem.addBook("The Great Gatsby");
bookSystem.addBook("To Kill a Mockingbird");
bookSystem.addBook("1984");
bookSystem.addBook("Don Quixote");
bookSystem.addBook("Moby Dick");
}
}

QUESTION 6
public class Device {

protected String modelname;

public Device(String modelname){


this.modelname=modelname;
}

public void turnOn() {


System.out.println("device is turned on");
}

public static void main(String[] args) {


Device smartphone = new Smartphone("iphone");
Device laptop = new Laptop("Dell");

smartphone.turnOn();
laptop.turnOn();
}
}

public class Laptop extends Device {


public Laptop(String modelname){
super(modelname);
}

@Override
public void turnOn() {
System.out.println(modelname + " laptop is turning on.");
}
}

public class Smartphone extends Device {

public Smartphone(String modelname){


super(modelname);
}

@Override
public void turnOn() {

System.out.println(modelname + " Smartphone is turning on.");


}
}

QUESTION 7
public class Clothing extends Product {

private String size;

public Clothing(String productName, double price, int quantity, String


size) {
super(productName, price, quantity);
this.size=size;
}

@Override
public void displayProductInfo() {
super.displayProductInfo();
System.out.println("Size: " + size);

}
}

public class Electronics extends Product {

private double warrantyPeriod;

public Electronics(String productName, double price, int quantity,


double warrantyPeriod) {
super(productName, price, quantity);
this.warrantyPeriod = warrantyPeriod;
}
@Override
public void displayProductInfo() {
super.displayProductInfo();
System.out.println("Warranty Period: " + warrantyPeriod);
}
}

public class Product {


private String productName;
private double price;
private int quantity;

public Product(String productName, double price, int quantity) {


this.productName = productName;
this.price = price;
this.quantity = quantity;
}

public void displayProductInfo() {


System.out.println("-----DISPLAYING PRODUCT INFO'S-----\nProduct
Name: " + productName
+ "\n Individual Price: " + price
+ "\n Quantity: " + quantity
+ "\nTOTAL PRICE: " + price*quantity );
}

public static void main(String[] args) {


Product laptop =new Electronics("Laptop", 999.99,10,24);
Product shirt =new Clothing("T-Shirt", 19.99,50,"M");

laptop.displayProductInfo();
shirt.displayProductInfo();
}
}

QUESTION 8
public interface Storable {
public void storeItem();

public static void main(String[] args) {


Storable warehouse = new Warehouse(1000);
Storable store = new Store("Downtown");
warehouse.storeItem();
store.storeItem();
}
}

public class Store implements Storable {


private String location;

public Store(String location) {


this.location = location;
}
@Override
public void storeItem() {
System.out.println("An item is being displayed at " + location);
}
}

public class Warehouse implements Storable {


private int capacity;

public Warehouse(int capacity) {


this.capacity = capacity;
}

@Override
public void storeItem() {
System.out.println(capacity + " amount of items can be stored");
}
}

QUESTION 9
public class CreditCard implements Payable {

private String cardNumber;


private String cardHolder;

public CreditCard(String cardNumber, String cardHolder) {


this.cardNumber = cardNumber;
this.cardHolder = cardHolder;
}

@Override
public void processPayment() {
System.out.println("Processing payment with Credit Card: " +
cardHolder + ", Card Number: " + cardNumber);
}
}

public interface Payable {

public void processPayment();

public static void main(String[] args) {


Payable creditCardPayment = new CreditCard("1234-5678-9876-5432",
"Alice Johnson");
Payable paypalPayment = new PayPal("[email protected]");
creditCardPayment.processPayment();
paypalPayment.processPayment();
}
}
public class PayPal implements Payable {

private String email;

public PayPal(String email) {


this.email = email;
}

@Override
public void processPayment() {
System.out.println("Processing payment through PayPal account: " +
email);
}
}

QUESTION 10

public class EmailNotification implements Notifiable{

private String emailAddress;

public EmailNotification(String emailAddress) {


this.emailAddress = emailAddress;
}

public void sendNotification(){


System.out.println("Sending Email Notification to: " +
emailAddress);
}
}

public interface Notifiable {

public void sendNotification();

public static void main(String[] args) {


Notifiable emailNotification = new
EmailNotification("[email protected]");
Notifiable smsNotification = new SMSNotification("123-456-7890");
emailNotification.sendNotification();
smsNotification.sendNotification();
}
}

public class SMSNotification implements Notifiable {


private String phoneNumber;

public SMSNotification(String phoneNumber) {


this.phoneNumber = phoneNumber;
}

public void sendNotification() {


System.out.println("Sending SMS Notification to: " + phoneNumber);
}

QUESTION 11
public class LegacyPrinter {
private String printerName;

public LegacyPrinter(String printerName) {


this.printerName = printerName;
}

public void printDocument() {


System.out.println("Printing document using " + printerName);
}

public static void main(String[] args) {


LegacyPrinter oldPrinter = new LegacyPrinter("HP LaserJet");
NewPrinter printer = new PrinterAdapter(oldPrinter);

printer.print();
}
}

public interface NewPrinter {


public void print();
}

public class PrinterAdapter implements NewPrinter {


private LegacyPrinter legacyPrinter;

public PrinterAdapter(LegacyPrinter legacyPrinter) {


this.legacyPrinter = legacyPrinter;
}

@Override
public void print() {
legacyPrinter.printDocument();
}
}

QUESTION 12
public class AudioPlayerAdapter implements ModernAudioPlayer {
private LegacyAudioPlayer legacyAudioPlayer;

public AudioPlayerAdapter(LegacyAudioPlayer legacyAudioPlayer) {


this.legacyAudioPlayer = legacyAudioPlayer;
}

@Override
public void play() {
legacyAudioPlayer.playAudio();
}

@Override
public void pause() {

@Override
public void stop() {

}
}

public class LegacyAudioPlayer {


private String audioFileName;

public LegacyAudioPlayer(String audioFileName) {


this.audioFileName = audioFileName;
}

public void playAudio() {


System.out.println(audioFileName + " is playing.");
}

public void stopAudio() {


System.out.println("audio stopped");
}

public void pauseAudio() {


System.out.println("audio paused");
}

public static void main(String[] args) {


LegacyAudioPlayer oldPlayer = new LegacyAudioPlayer("song.mp3");

ModernAudioPlayer modernPlayer = new


AudioPlayerAdapter(oldPlayer);

modernPlayer.play(); // Will call legacy's playAudio()


modernPlayer.stop(); // Does nothing
modernPlayer.pause(); // Does nothing
}
}

public interface ModernAudioPlayer {


public void play();
public void stop();
public void pause();
}

QUESTION 13
public class LegacyPaymentProcessor {
private double paymentAmount;

public LegacyPaymentProcessor(double paymentAmount) {


this.paymentAmount = paymentAmount;
}

public void processPayment() {


System.out.println("Processing payment of $" + paymentAmount + " "
+ getClass().getName());
}

public void refundPayment() {


System.out.println("payment refund");
}

public void cancelPayment() {


System.out.println("payment cancelled");
}

public static void main(String[] args) {


LegacyPaymentProcessor oldProcessor = new
LegacyPaymentProcessor(250.00);

PaymentGateway payment = new PaymentAdapter(oldProcessor);

payment.makePayment(); // Should call processPayment()


payment.refund(); // Does nothing
payment.cancel(); // Does nothing
}
}

public class PaymentAdapter implements PaymentGateway {


private LegacyPaymentProcessor legacyPaymentProcessor;

public PaymentAdapter(LegacyPaymentProcessor legacyPaymentProcessor) {


this.legacyPaymentProcessor = legacyPaymentProcessor;
}

@Override
public void cancel() {

@Override
public void makePayment() {
legacyPaymentProcessor.processPayment();
}

@Override
public void refund() {

}
}
public interface PaymentGateway {
public void makePayment();

public void refund();

public void cancel();


}

QUESTION 14 & 15
public class Audio extends MediaContent{
private int bitrate;

public Audio(String title, double duration, int bitrate) {


super(title, duration);
this.bitrate = bitrate;
}

public String getDetails() {


return super.getDetails() + ", Bitrate: " + bitrate + " kbps";
}
}

public class MediaContent {


private String title;
private double duration;

public MediaContent(String title, double duration) {


this.title = title;
this.duration = duration;
}

public String getDetails() {


return ("Title: " + title + ", Duration: " + duration + "
seconds");
}

public static void main(String[] args) {


MediaContent myVideo = new Video("Nature Documentary", 3600,
"1920x1080");
MediaContent myAudio = new Audio("Classical Symphony", 1800, 320);
System.out.println(myAudio.getDetails());
System.out.println(myVideo.getDetails());

}
}

public class Video extends MediaContent{


private String resolution;
public Video(String title, double duration, String resolution) {
super(title, duration);
this.resolution = resolution;
}
public String getDetails() {
return super.getDetails() + ", Resolution: " + resolution;
}
}

public class ContentManager {


public void displayContentDetails(MediaContent content) {
if (content instanceof Video) {
System.out.println(content + " is a type Video");
} else if (content instanceof Audio) {
System.out.println(content + " is a type Audio");
} else {
System.out.println(content + " is a type unknown");
}
}
}

QUESTION 16
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Adapotör implements KeyListener {


private JLabel label;

public Adapotör(JLabel label) {


this.label = label;
}

@Override
public void keyReleased(KeyEvent e) {

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
label.setText("you pressed enter");
} else {
label.setText("you pressed '" + e.getKeyChar() + "'");
}
}

@Override
public void keyTyped(KeyEvent e) {

}
}
import javax.swing.*;
import java.awt.*;
public class Mai extends JFrame {
private JLabel label;
private JTextField textField;

public Mai() {
setTitle("deneme");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

label = new JLabel();


textField = new JTextField();

add(label);
add(textField);

textField.addKeyListener(new Adapotör(label));
}

public static void main(String[] args) {


Mai example = new Mai();
example.setVisible(true);
}
}

QUESTION 17
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Adapotör implements KeyListener {


private JLabel label;

public Adapotör(JLabel label) {


this.label = label;
}

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == 'A' || e.getKeyChar() =='a'){
label.setText("A");
}else {label.setText("X");}
}

@Override
public void keyReleased(KeyEvent e) {

@Override
public void keyTyped(KeyEvent e) {
}
}

import javax.swing.*;

public class Mai extends JFrame {

public static void main(String[] args) {


JFrame frame = new JFrame("Key Press Display");
JLabel label = new JLabel("Press a key", SwingConstants.CENTER);
frame.add(label);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.addKeyListener(new Adapotör(label));
}
}

QUESTION 20

public class Task {


private String title;
private String description;
private boolean isCompleted;

public Task(String title, String description) {


this.title = title;
this.description = description;
this.isCompleted = false;
}
public boolean isCompleted() {
return isCompleted;
}
public void completeTask() {
isCompleted = true;
}

public String displayTask() {


return "title= " + title + '\n' +
"description= " + description + '\n' +
"is completed?= " + isCompleted;
}
}
/*
The Task class should include attributes for title, description, and a
boolean isCompleted to
track the task's status. I
*/
import java.util.ArrayList;
import java.util.List;

public class TaskManager {


private List<Task> tasks;

public TaskManager() {
this.tasks = new ArrayList<>();
}

public void addTask(Task task) {


tasks.add(task);
}

public void displayAllTasks() {


for (int i = 0; i < tasks.size(); i++) {
System.out.println(tasks.get(i).displayTask());
}
}
public void displayIncompleteTasks(){
for (int i = 0; i < tasks.size(); i++) {
if (!tasks.get(i).isCompleted()){
System.out.println(tasks.get(i).displayTask());
}
}
}

public static void main(String[] args) {


TaskManager taskManager = new TaskManager();
Task task1 = new Task("Finish homework", "Complete math and
science assignments.");
Task task2 = new Task("Grocery shopping", "Buy vegetables and
fruits.");
taskManager.addTask(task1);
taskManager.addTask(task2);
taskManager.displayAllTasks();
task1.completeTask();
taskManager.displayIncompleteTasks();
}
}

You might also like