Skip to content

Instantly share code, notes, and snippets.

@chrisvasqm
Last active April 21, 2024 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvasqm/a6fca6521b61ddfb39da48ccbe156fe8 to your computer and use it in GitHub Desktop.
Save chrisvasqm/a6fca6521b61ddfb39da48ccbe156fe8 to your computer and use it in GitHub Desktop.
Object-Oriented Programming Exercises

Stopwatch Exercise

Design a class that represents a Stopwatch. We want to be able to start and stop it. There's no need to track the time that has passed between a start and stop action.

But we should be able to tell the consumer of this class whenever the Stopwatch has started, is already running, stopped and is already stopped on the console.

You should be able to generate this output on the console:

Stopwatch started
Has already started
Stopwatch stopped
Has already stopped

Time

Spend 20 minutes on this and if you don't succeed, take a break or reach out for help.

Solution

Go here to compare your solution.

StackOverflowPost Exercise

Design a class that represents a Post from Stack Overflow.

We want this class to be able to store information about the Post's

  1. Title
  2. Description
  3. Date it was created
  4. Votes

All of these properties should be read-only, except for the Votes. We would like to be able to up vote and down vote a Post.

Take into consideration that we should allow the users to add a title and description as soon as the Post is created, while the date it was created and votes should be set by default to a value.

Time

Spend 30 minutes on this and if you don't succeed, take a break or reach out for help.

Solution

Go here to compare your solution.

Bonus

  1. If we would like to read and write all the properties, what would you do?
  2. How could we create a Post in two different ways?
  3. How would you add the ability to add Comments to a Posts?

Stack Exercise

Design a class that represents a Stack of items.

We should be able to store any type of item in it, and only take out the last one via these methods:

  1. push
  2. pop

push should add an item to the Stack, while pop should remove the last item and also return it.

Keep in mind, once there are no more items to be popped, we should throw an Exception to prevent someone misusing the class.

Hint

You may want to research about the List<> and Object data types in Java to tackle this exercise.

Time

Spend 1 hour on this and if you don't succeed, take a break or reach out for help.

Solution

Go here to compare your solution.

Inheritance Exercise

Design 2 classes that represent a FirePokemon and a WaterPokemon, both should extend (inherit) from a base/parent Pokemon class that will provide a common set of properties and methods for them to share.

We want to store their:

  1. Name
  2. Level

Both Pokemon sub-types should be able to level up the same way, but each one of them should have their own way of attacking.

Bonus

  1. How could we prevent the Pokemon class from being instantiated?
  2. How could we prevent FirePokemon and WaterPokemon from being inherited?

Time

Spend 1 hour on this and if you don't succeed, take a break or reach out for help.

Solution

Go here to compare your solution.

Polymorfism Exercises

Project 1

Design a few classes that will act as the Download/Export menu from Google Docs. We should be able to create a Document object that will take a File Name at the moment of creation and we should be able to pass it to various classes that should mimic a PDF, Microsoft Word and Text as exporters.

Solution

Go here to compare your solution.

Project 2

Design a few classes for a RPG game, we want to be able to allow the player to choose between 3 types of characters:

  1. Warrior
  2. Archer
  3. Wizzard

All of them should have their own way of attacking and moving. By making use of interfaces, one or more of these behaviors should be able to be added to each character type at a given time. So, maybe one of them can both attack and move, but another one could just attack if we wanted to.

Bonus

  1. Add in a Character class that will be inherited by each character type that will set their HP and MP values right when you initialize them.

Time

Spend 1 hours on this and if you don't succeed, take a break or reach out for help.

Solution

Go here to compare your solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment