Giter Club home page Giter Club logo

javaintro4debrief's Introduction

Polymorphism Exercise

  1. Create an abstract Person class with attributes String name and int health. Create a constructor with parameter name and set health = 100;. Create getter methods for name and health and setter method for health.
  2. Create a Warrior class that extends Person with an attribute Weapon weapon;. Create a constructor with parameter name. Call the super constructor with the name. Create a setter method setWeapon(Weapon weapon);.
  3. Create a Weapon interface with one abstract method strike(Warrior opponent); an no attributes.
  4. To the Warrior class. Add a decHealth method with a single parameter int amt. Set the health value to Math.max(0, health – amt); which will keep the health attribute from going negative. Then call the opponent’s decHealth method. The getName, getHealth, and setHealth from Person class will be used.
  5. To the Warrior class add an attack method that takes a Warrior opponent parameter. If this warrior’s health is positive and the opponent’s health is positive and this warrior’s weapon is not null, print this warrior’s name is attacking the opponent’s name. Finally, print the opponent’s name and health.
  6. Use the provided Weapons and Main classes to validate your Warrior and Weapon classes.
  7. Output should be:
    • Xena is attacking Star
    • Mesmerizing Star
    • Star health = 97
    • Star is attacking Xena
    • Slashing Xena
    • Xena health = 95
    • Xena is attacking Star
    • Snapping Star
    • Star health = 93

javaintro4debrief's People

Contributors

roesnera avatar

Stargazers

 avatar  avatar

Watchers

 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.