0% found this document useful (0 votes)
6 views

SDA LabSession3

Uploaded by

Ayesha Asad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SDA LabSession3

Uploaded by

Ayesha Asad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Session 3: Understanding SOLID Principles

Exercise
1. Write a class representing a user profile. Ensure that it adheres to the SRP by having
only one responsibility. Separate concerns such as data storage, authentication, and
profile information display into different classes.

CODE:

class UserProfile {
String name;
String address;
String contact;
String NIC;
String emailAddress;
String qualifications;

public UserProfile() {
this.name = name;
this.contact= contact;
this.NIC = NIC;
this.emailAddress = emailAddress;
this.qualifications = qualifications;
this.address = address;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public String getContact() {


return contact;
}

public void setContact(String contact) {


this.contact = contact;
}

public String getNIC() {


return NIC;
}

public void setNIC(String NIC) {


this.NIC = NIC;
}

public String getEmailAddress() {


return emailAddress;
}

public void setEmailAddress(String emailAddress) {


this.emailAddress = emailAddress;
}

public String getQualifications() {


return qualifications;
}

public void setQualifications(String qualifications) {


this.qualifications = qualifications;
}

public class UserDatabase {


// Methods for saving, retrieving, and managing user records
public void saveUser(UserProfile user) {
// Implementation details for saving to a database or file
}

public UserProfile getUserByUsername(String username) {


// Implementation details for retrieving user by username
return null;
}
}

// AuthenticationManager.java
public class AuthenticationManager {
public boolean authenticateUser(String username, String password) {
// Implementation details for user authentication
return false;
}
}

// ProfileDisplay.java
public class ProfileDisplay {
public void displayUserProfile(UserProfile user) {
// Implementation details for displaying user profile information
}
}

public static void main(String[] args) {


// Create an Employee object
UserProfile up = new UserProfile();
up.setAddress("abc");
System.out.println(up.getAddress());

}
}

2. Implement a basic shape hierarchy (e.g., Circle, Square, and Triangle). Extend this
hierarchy to support the calculation of area for each shape without modifying the
existing shape classes.
CODE:
abstract class Shape {
public abstract double calculateArea();
}
class Rectangle extends Shape {
protected double width;
protected double height;

public void setWidth(double width) {


this.width = width;
}
public void setHeight(double height) {
this.height = height;
}

@Override
public double calculateArea() {
return width * height;
}
}

class Circle extends Shape {


private double radius;

public void setRadius(double radius) {


this.radius = radius;
}

@Override
public double calculateArea() {
return 3.14 * radius * radius;
}
}
class Square extends Shape {
private double side;

public void setSide(double side) {


this.side = side;
}

@Override
public double calculateArea() {
return side * side;
}
}
class ShapeHierarchy{
public static void main(String[] args){
Square obj1= new Square();
Rectangle obj2=new Rectangle();
Circle obj3=new Circle();
obj1.setSide(5);
obj2.setWidth(4);
obj2.setHeight(3);
obj3.setRadius(3);
System.out.println("Square Area: "+obj1.calculateArea());
System.out.println("Rectangle Area: "+obj2.calculateArea());
System.out.println("Circle Area: "+obj3.calculateArea());
}
}
3. Create an interface representing a shape and two classes implementing this interface:
Rectangle and Square. Ensure that the Square class can be substituted for the Rectangle
class without altering the behavior of the program.

CODE:

// Define the Shape interface


interface Shape {
double area();
}

// Implement the Rectangle class


class Rectangle implements Shape {
private double width;
private double height;

public Rectangle(double width, double height) {


this.width = width;
this.height = height;
}

@Override
public double area() {
return width * height;
}
}

// Implement the Square class (which extends Rectangle)


class Square extends Rectangle {
public Square(double sideLength) {
super(sideLength, sideLength);
}

public static void main(String[] args) {


Rectangle rectangle = new Rectangle(5, 3);
Square square = new Square(4);

System.out.println("Rectangle area: " + rectangle.area());


System.out.println("Square area: " + square.area());
}
}

4. Define an interface for a document printer. Implement classes for different types of
printers (e.g., InkjetPrinter, LaserPrinter). Ensure that each printer class implements only
the methods relevant to its type.

CODE:

// Define an interface for a document printer


interface DocumentPrinter {
void print(String document);
}

// Implement the InkjetPrinter class


class InkjetPrinter implements DocumentPrinter {
@Override
public void print(String document) {
// Implementation specific to InkjetPrinter
System.out.println("Printing with an Inkjet Printer: " + document);
}

// Other methods relevant to InkjetPrinter (if needed)


// ...
}

// Implement the LaserPrinter class


class LaserPrinter implements DocumentPrinter {
@Override
public void print(String document) {
// Implementation specific to LaserPrinter
System.out.println("Printing with a Laser Printer: " + document);
}

// Other methods relevant to LaserPrinter (if needed)


// ...
}

// Example usage
public class PrinterDemo {
public static void main(String[] args) {
DocumentPrinter inkjet = new InkjetPrinter();
DocumentPrinter laser = new LaserPrinter();

String myDocument = "Hello, world!";

inkjet.print(myDocument);
laser.print(myDocument);
}
}

5. Implement a simple messaging system where a NotificationService class sends messages


to recipients. Apply DIP by introducing an abstraction (e.g., IMessageSender interface)
and modifying NotificationService to depend on the abstraction rather than concrete
implementations.

CODE:

interface IMessageSender {
void sendMessage(String recipient, String message);
}

class EmailSender implements IMessageSender {


@Override
public void sendMessage(String recipient, String message) {
System.out.println("Sending email to " + recipient + ": " + message);
}
}

class SmsSender implements IMessageSender {


@Override
public void sendMessage(String recipient, String message) {
System.out.println("Sending SMS to " + recipient + ": " + message);
}
}

class NotificationService {
private final IMessageSender messageSender;

public NotificationService(IMessageSender messageSender) {


this.messageSender = messageSender;
}
public void sendNotification(String recipient, String message) {
messageSender.sendMessage(recipient, message);
}

public static void main(String[] args) {


IMessageSender emailSender = new EmailSender();
NotificationService emailNotificationService = new
NotificationService(emailSender);
emailNotificationService.sendNotification("[email protected]", "Important
update!");

IMessageSender smsSender = new SmsSender();


NotificationService smsNotificationService = new NotificationService(smsSender);
smsNotificationService.sendNotification("+1234567890", "Reminder: Meeting at 2
PM.");
}
}

You might also like