Coder Social home page Coder Social logo

03-inheritance's Introduction

This is an assignment to the class Programmieren 3 at the University of Applied Sciences Rosenheim.

Assignment 3: Inheritance revisited

In this assignment we'll be looking at

  • a classical refactoring situation: you have code that works, but with bad implementation
  • abstract classes
  • final classes
  • when to use abstract base classes

Given is a class Animal which represents an animal with a lifecycle. The class is borrowed from a zoo game as you see when you inspect the given methods. As (game) time progresses, the main game engine calls .tick() on every animal to simulate the progress of time. In the following you can see the lifecycle of the animal modeled as state machine.

  1. it starts in a sleeping state
  2. after a certain time it awakes and is hungry
  3. if you feed it in time it starts to digest otherwise it's dying
  4. again after a certain time it has finished digesting and is getting in a playful mood (now you could collect() money)
  5. once again after a certain time it's getting tired and goes back to sleep

state machine

Setup

  1. Create a fork of this repository (button in the right upper corner)
  2. Clone the project (you're getting the link by clicking the green Clone or download button)
  3. Import the project to your IDE (remember the guide of the first assignment)

Your Assignment

First, understand the given implementation of the Animal.tick() method which is based on a switch-case; see the AnimalTest class for example state transitions. Your job is to replace the existing switch-case by a clever polymorphism construct (which might be difficult to understand in contrast to the relatively simple state machine). To get you an idea how this is possible, consider the UML diagram below. It models an abstract base class State which implements (!) the tick() method and has an abstract method successor(). This method is needed to get the successor state if enough time passed (e.g. the animal has completed digesting and is getting in playful mood).

Note that the State.tick() method should be marked final; why is that?

Hint: there's still one special case: when the animal was fed it's switching to the digesting state immediately no matter how long it was hungry before.

The test suite to ensure that the behavior is still the same is given, too. That's a classical refactoring situation. You want to replace an existing implementation by a better one but the behavior shouldn't change. Now you might get an idea why test cases are important beside the fact that they ensure that you're implementation is working correctly when you do it the first time.

classes

03-inheritance's People

Contributors

prskr avatar sikoried avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.