8 kyu

OOP: Object Oriented Piracy

7,086 of 21,573By-The-Ocean

Description:

Ahoy matey!

You are a leader of a small pirate crew. And you have a plan. With the help of OOP you wish to make a pretty efficient system to identify ships with heavy booty on board!

Unfortunately for you, people weigh a lot these days, so how do you know if a ship is full of gold and not people?

You begin with writing a generic Ship class / struct:

class Ship {
  constructor(draft, crew) {
    this.draft = draft
    this.crew = crew
  }
}
class Ship:
    def __init__(self, draft, crew):
        self.draft = draft
        self.crew = crew
public class Ship
{
  public int Draft;
  public int Crew;
  
  public Ship(int draft, int crew)
  {
    Draft = draft;
    Crew = crew;
  }
}
struct Ship {
  draft: u32,
  crew: u32,
}
public class Ship {
    private final double draft;
    private final int crew;
    
    public Ship(double draft, int crew) {
        this.draft = draft;
        this.crew = crew;
    }
} 
class Ship
  def initialize(draft,crew)
    @draft=draft
    @crew=crew
  end
end

Every time your spies see a new ship enter the dock, they will create a new ship object based on their observations:

  • draft - an estimate of the ship's weight based on how low it is in the water
  • crew - the count of crew on board
const titanic = new Ship(15, 10);
Titanic = Ship(15, 10)
Ship titanic = new Ship(15, 10);
  let titanic = Ship {
      draft : 15,
      crew : 10,
  };
Ship titanic = new Ship(15, 10);
titanic = Ship.new(15, 10)

Task

You have access to the ship "draft" and "crew". "Draft" is the total ship weight and "crew" is the number of humans on the ship.

Each crew member adds 1.5 units to the ship draft. If after removing the weight of the crew, the draft is still more than 20, then the ship is worth looting. Any ship weighing that much must have a lot of booty!

Add the method

isWorthIt
is_worth_it
is_worth_it(&self)
IsWorthIt
isWorthIt
is_worth_it

to decide if the ship is worthy to loot. For example:

titanic.isWorthIt() // return false
Titanic.is_worth_it()
False
titanic.IsWorthIt() => false
titanic.is_worth_it() -> false
titanic.isWorthIt() == false
titanic.is_worth_it() # false

Good luck and may you find GOOOLD!

Object-oriented Programming
Fundamentals

More By Author:

Check out these other kata created by By-The-Ocean

Stats:

CreatedMar 9, 2015
PublishedMar 9, 2015
Warriors Trained31305
Total Skips1325
Total Code Submissions76810
Total Times Completed21573
JavaScript Completions7086
Python Completions10594
Ruby Completions552
C# Completions1735
Rust Completions407
Java Completions1725
Total Stars338
% of votes with a positive feedback rating88% of 2166
Total "Very Satisfied" Votes1747
Total "Somewhat Satisfied" Votes329
Total "Not Satisfied" Votes90
Ad
Contributors
  • By-The-Ocean Avatar
  • jhoffner Avatar
  • GiacomoSorbi Avatar
  • h3dkandi Avatar
  • anter69 Avatar
  • myjinxin2015 Avatar
  • fxrbfg Avatar
  • Chrono79 Avatar
  • Madjosz Avatar
  • Souzooka Avatar
  • FArekkusu Avatar
  • Awesome A.D. Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • akar-0 Avatar
  • __eloise__ Avatar
  • anusha.j Avatar
  • o2001 Avatar
Ad