---
title: "Marina class diagram implementation code"
subject: "Object Oriented Analysis and Design"
chapter: 4
key: "semester_6/object_oriented_analysis_and_design:marina-class-diagram-implementation-code"
source: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/marina-class-diagram-implementation-code
site: BCT Engineering
---

# Marina class diagram implementation code

Object Oriented Analysis and Design · Chapter 4: Implementation

- Asked in: 2073 Bhadra, 8 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/marina-class-diagram-implementation-code
- Whole chapter: https://www.bctengineer.com/md/semester_6/object_oriented_analysis_and_design/chapter-4

## Question

For the above class diagram created (XYZ Marina), apply object oriented techniques to convert such class diagram into implementation code in any of your favorite object oriented programming language. Your implementation code should clearly show class definition, Attributes with their proper visibility and method signatures with required parameter.

## Short answer

Map class diagram to code using private attributes, public methods, ‘extends’ for inheritance, and reference collections for associations. Define abstract Boat with SailBoat and PowerBoat subclasses. Lease computes daily/yearly charges plus length surcharge, then creates a Bill.

## Answer

The class diagram has `Customer`, `Boat` (abstract) with `SailBoat` and `PowerBoat`, `Slip`, `Lease` and `Bill`. Mapping rules: attributes are `private`, methods are `public`, generalization is `extends`, associations are reference fields or collections.

```java
import java.time.LocalDate;
import java.util.*;

// ---------- Boat hierarchy ----------
abstract class Boat {
    private String registrationNo;
    private int manufactureYear;
    private double length;

    public Boat(String regNo, int year, double length) {
        this.registrationNo = regNo;
        this.manufactureYear = year;
        this.length = length;
    }
    public String getRegistrationNo() { return registrationNo; }
    public double getLength() { return length; }
    public abstract String getType();
}

class SailBoat extends Boat {
    private double sailArea;
    public SailBoat(String r, int y, double l, double sailArea) {
        super(r, y, l); this.sailArea = sailArea;
    }
    public String getType() { return "Sail boat"; }
}

class PowerBoat extends Boat {
    private int enginePower;
    public PowerBoat(String r, int y, double l, int power) {
        super(r, y, l); this.enginePower = power;
    }
    public String getType() { return "Power boat"; }
}

// ---------- Slip ----------
class Slip {
    private int slipNo;
    private double size;
    private boolean vacant = true;

    public Slip(int slipNo, double size) { this.slipNo = slipNo; this.size = size; }
    public boolean isVacant() { return vacant; }
    public void assign()  { vacant = false; }
    public void release() { vacant = true; }
    public int getSlipNo() { return slipNo; }
}

// ---------- Customer ----------
class Customer {
    private int customerId;
    private String name, address;
    private List<Boat> boats = new ArrayList<>();      // 1 - *
    private List<Lease> leases = new ArrayList<>();    // 1 - *

    public Customer(int id, String name, String address) {
        this.customerId = id; this.name = name; this.address = address;
    }
    public void addBoat(Boat b) { boats.add(b); }
    public void addLease(Lease l) { leases.add(l); }
    public List<Lease> getLeases() { return leases; }
}

// ---------- Lease ----------
class Lease {
    public enum Type { DAILY, YEARLY }
    private int leaseId;
    private Type type;
    private LocalDate startDate, endDate;
    private Customer customer;
    private Slip slip;
    private Boat boat;
    private Bill bill;

    public Lease(int id, Type type, LocalDate start, LocalDate end, Customer c) {
        this.leaseId = id; this.type = type;
        this.startDate = start; this.endDate = end; this.customer = c;
        c.addLease(this);
    }
    public void assignBoat(Boat b, Slip s) {       // assign boat to slip
        if (!s.isVacant()) throw new IllegalStateException("Slip not vacant");
        this.boat = b; this.slip = s; s.assign();
    }
    public double computeAmount() {                // assumed rates
        long days = java.time.temporal.ChronoUnit.DAYS.between(startDate, endDate) + 1;
        double rate = (type == Type.DAILY) ? 500.0 * days : 100000.0;
        return rate + boat.getLength() * 10;       // length-based surcharge
    }
    public Bill generateBill() {
        bill = new Bill(leaseId, computeAmount());
        return bill;
    }
    public Customer getCustomer() { return customer; }
    public Slip getSlip() { return slip; }
}

// ---------- Bill ----------
class Bill {
    private int billNo;
    private double amount;
    private boolean paid;
    public Bill(int no, double amt) { billNo = no; amount = amt; }
    public void pay() { paid = true; }
    public double getAmount() { return amount; }
    public boolean isPaid() { return paid; }
}

// ---------- Marina (facade) ----------
class Marina {
    private List<Slip> slips = new ArrayList<>();
    private List<Customer> customers = new ArrayList<>();

    public void addSlip(Slip s) { slips.add(s); }
    public void addCustomer(Customer c) { customers.add(c); }

    public List<Slip> searchVacantSlips() {
        List<Slip> result = new ArrayList<>();
        for (Slip s : slips) if (s.isVacant()) result.add(s);
        return result;
    }
    public List<Slip> slipsLeasedTo(Customer c) {
        List<Slip> result = new ArrayList<>();
        for (Lease l : c.getLeases()) result.add(l.getSlip());
        return result;
    }
}
```

**Notes:** attributes carry private visibility; the abstract `Boat` and its subclasses use inheritance; one-to-many associations use `List`; `Lease` implements the tasks "creating lease", "computing lease amount" and "assigning boats", and `Marina` supports "search for vacant slips" and "slips leased to specific customer"; `Bill` supports the billing system. Rates are assumed.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What visibility is given to attributes and methods in the mapping?
   A. Attributes private, methods public
   B. Attributes public, methods private
   C. Both protected
   D. Both package-private
   Answer: A. The mapping rules state that attributes are private and methods are public. (Key point: Visibility rule)

2. How is the Boat generalisation implemented?
   A. Boat is an abstract class; SailBoat and PowerBoat extend it and implement getType()
   B. Boat is a final class
   C. SailBoat contains PowerBoat
   D. Boat is an enum
   Answer: A. Generalisation is extends; the abstract Boat declares abstract getType() overridden by each subclass. (Key point: Abstract Boat hierarchy)

3. What happens in Lease.assignBoat when the slip is not vacant?
   A. The boat is assigned anyway
   B. An IllegalStateException is thrown
   C. The slip is released
   D. A new Slip is created
   Answer: B. assignBoat checks s.isVacant() and throws IllegalStateException; otherwise it assigns the boat and slip. (Key point: Assigning boat to slip)

4. How are the Customer-Boat and Customer-Lease one-to-many associations coded?
   A. Single fields
   B. List<Boat> and List<Lease> fields
   C. Static variables
   D. Inheritance
   Answer: B. One-to-many associations use List collections in Customer. (Key point: Customer associations)

## Related in Object Oriented Analysis and Design

- [XYZ Marina class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram) — Object Oriented Analysis and Design, chapter 2
- [Health care center class diagram to code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Ahealth-care-class-diagram-code) — Object Oriented Analysis and Design, chapter 4
- [Singleton class and visibility types](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Asingleton-and-visibility-types) — Object Oriented Analysis and Design, chapter 3
- [Visibility symbols +, -, # in classes](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Avisibility-signs-class-diagram) — Object Oriented Analysis and Design, chapter 3
- [Object oriented system and core concepts](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aoo-system-concepts-explained) — Object Oriented Analysis and Design, chapter 1
- [Algorithmic vs object-oriented decomposition](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aalgorithmic-vs-oo-decomposition) — Object Oriented Analysis and Design, chapter 1

## Related in other subjects

- [Compare procedural and object oriented programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprocedural-vs-oop-compare) — Object Oriented Programming, chapter 1
- [Project attributes; implementation phase activities](https://www.bctengineer.com/question.md?key=semester_7%2Fproject_engineering%3Aproject-attributes-implementation-activities) — Project Engineering, chapter 1
- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1
