Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Lab Manual - Lab 14-Abstract Class & Interface

The document outlines a lab exercise for CSE 215L at North South University focused on abstract classes and interfaces in programming. It includes the implementation of a Shape class and its Rectangle subclass, as well as a Discountable interface with two implementations: PercentageDiscount and ThresholdDiscount. The document also provides example code demonstrating the functionality of these classes and interfaces.

Uploaded by

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

Lab Manual - Lab 14-Abstract Class & Interface

The document outlines a lab exercise for CSE 215L at North South University focused on abstract classes and interfaces in programming. It includes the implementation of a Shape class and its Rectangle subclass, as well as a Discountable interface with two implementations: PercentageDiscount and ThresholdDiscount. The document also provides example code demonstrating the functionality of these classes and interfaces.

Uploaded by

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

North South University

Department of Electrical and Computer Engineering


CSE 215L: Programming Language II Lab
Lab – 14: Abstract Class & Interface
Objective:
● To understand abstract class
● To understand interface

Task:
1. Implement the following classes:

Shape Rectangle extends Shape

- name: String - length: double


- width: double
+ Shape(name: String)
+ /* accessor-mutator */ + Rectangle(name: String, length: double, width:
+ area(): double double)
/* accessor-mutator */
+ perimeter(): double
+ area(): double
+ perimeter(): double

2. Implement the following classes and invoke discountedPrice() for object of each class.
<<interface>>
Discountable

discountedPrice(price: double): double

PercentageDiscount ThresholdDiscount

- percentage: double - threshold: double


- discount: double
+ PercentageDiscount(percentage: double)
+ ThresholdDiscount(threshold: double, discount:
+ getPercentage(): double double)
+ setPercentage(percentage: double): void + getThreshold(): double
+ discountedPrice(price: double): double + getDiscount(): double
+ setThreshold(threshold: double): void
+ setDiscount(discount: double): void
+ discountedPrice(price: double): double
abstract class Shape {
protected String name;

public Shape(String name) {


this.name = name;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public abstract double area();


public abstract double perimeter();
}

class Rectangle extends Shape {


private double length;
private double width;

public Rectangle(String name, double length, double width) {


super(name);
this.length = length;
this.width = width;
}

public double getLength() {


return length;
}

public void setLength(double length) {


this.length = length;
}

public double getWidth() {


return width;
}

public void setWidth(double width) {


this.width = width;
}

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

@Override
public double perimeter() {
return 2 * (length + width);
}
}

interface Discountable {
double discountedPrice(double price);
}

class PercentageDiscount implements Discountable {


private double percentage;

public PercentageDiscount(double percentage) {


this.percentage = percentage;
}
public double getPercentage() {
return percentage;
}

public void setPercentage(double percentage) {


this.percentage = percentage;
}

@Override
public double discountedPrice(double price) {
return price - (price * (percentage / 100.0));
}
}

class ThresholdDiscount implements Discountable {


private double threshold;
private double discount;

public ThresholdDiscount(double threshold, double discount) {


this.threshold = threshold;
this.discount = discount;
}

public double getThreshold() {


return threshold;
}

public void setThreshold(double threshold) {


this.threshold = threshold;
}

public double getDiscount() {


return discount;
}

public void setDiscount(double discount) {


this.discount = discount;
}

@Override
public double discountedPrice(double price) {
if (price > threshold) {
return price - discount;
} else {
return price;
}
}
}

public class Main {


public static void main(String[] args) {
// Test Shape and Rectangle
Rectangle rect = new Rectangle("Rectangle", 5.0, 3.0);
System.out.println("Area of " + rect.getName() + ": " + rect.area());
System.out.println("Perimeter of " + rect.getName() + ": " + rect.perimeter());

// Test Discountable interface implementations


Discountable percentageDiscount = new PercentageDiscount(10);
System.out.println("Price after percentage discount: " + percentageDiscount.discountedPrice(100.0));

Discountable thresholdDiscount = new ThresholdDiscount(50, 10);


System.out.println("Price after threshold discount: " + thresholdDiscount.discountedPrice(60.0));
}
}

You might also like