pctechguide.com

  • Home
  • Guides
  • Tutorials
  • Articles
  • Reviews
  • Glossary
  • Contact

Object Oriented Programming 5

To illustrate putting together an object oriented (OO) class hierarchy design, let’s take a look at a simple maze game. We’ll be creating an OO design, but we’re not going to select a programming language at this stage. We’ll see how we can analyse the problem from an OO perspective, and produce a workable OO design solution that can be translated into implementation code.

Our simple game involves a two dimensional maze and two moving objects. The moving objects are the player’s character and a monster that chases the player around while they try to solve the maze. For our OO design, the key here is that, although these two objects have clear differences, initially we’re only interested in what they have in common.

During the game the player’s character and the monster both have to have their screen position stored, and this is best held as a two dimensional coordinate x and y. The point class is a very common class for storing coordinates, and one we’ll use in our design. We also want to know whether or not the characters are alive in the game. To cover these similarities, we can create a class that covers both the game characters, as follows.

We’re using Unified Modelling Language (UML) notation in our class modelling. UML is commonly used in object oriented design, and ArgoUML is a free Java tool you can use to produce your own UML diagrams.

The

Notice that gameCharacter has not been derived from the class point, but has included a data member currentLocation which is of type point. Now we’ll get on to inheritance as we start to think about the differences between the game characters.

Remember that a class doesn’t just have data members, but also methods, and the methods for the game objects concerning movement are very different. The player character is going to be moved in response to player commands, while the monster will need an Artificial Intelligence (AI) game function to make it move in pursuit of the player.

There are also different data requirements. For the player, we want to keep a score, and what level of maze they’ve reached. For the monster, we at least want to have a speed variable, so that we can have it move faster or slower.

A

Note that there are a number of functions that have been ommitted from the class designs, particularly how to get and set the values of the various class data members, assuming they are private class members, but the basics are there. With thought, this class tree can be extended in all sorts of ways. Various game characters might be derived from the gameCharacter class, or from the monster class, whichever makes most sense. You can see that using inheritance each derived class provides greater and greater specialisation, but that the base class provides a foundation of reusable code to build upon.

Next we’ll take a look at times when a class may be designed that has more than one bass class, known as multiple inheritance.

  • Object Oriented Programming 2
  • Object Oriented Programming 3
  • Object Oriented Programming 4
  • Object Oriented Programming 5
  • Object Oriented Programming 6
  • Object Oriented Programming 7
  • Object Oriented Programming 8

Filed Under: Object Oriented Programming

Latest Articles

Protect Your PC From Fake Antivirus Programs

A fake anti-virus program can be the worst and most unrelenting malware that you may encounter on the internet. Malware are programs that phish out your personal information in exchange of “free anti-virus programs” (which are fake antivirus programs). Usually the fake antivirus clients offer a free … [Read More...]

How to Start a Business in Computer Engineering

Did you know that there are 66,200 computer engineers in the United States? This can be a very lucrative career path. However, you may have an easier time starting your own business. For many engineers, creating a computer engineering business is the best way for them to maximize their career … [Read More...]

Bestadblocker virus removal

Yes - Bestadblocker is malware.  By the name of the program you would think it would at least try and pretend to block ads but it provides in-line videos and tons of popups letting you know your computer is infected. It's this kind of adware that gives bundled software programs a bad name and why … [Read More...]

Revolutionize Your Internet Experience with Orbi 960 – The Ultimate WiFi System

In a world where seamless connectivity is essential, slow and unreliable internet connections are a major problem. Whether you are running a business, … [Read More...]

Do You Need a VPN When Trading Cryptocurrency?

There’s no doubt that the biggest global industries in 2023 are tech-driven, while there remains a significant crossover between many of these … [Read More...]

Goodbye Bitcoin: the 3 alternative cryptocurrencies that have great upside potential, according to experts

Bitcoin has been a very lucrative investment for people that got into it early. One report from The Motley Fool pointed out that $10 of bitcoin … [Read More...]

Self-driving cars face their Achilles’ heel and may be targets of hackers

The market for self-driving cars is booming. Customers spent $22.22 billion on these autonomous vehicles in 2021 and they will likely spend more in … [Read More...]

How to avoid scams with bitcoin and other cryptocurrencies

Cryptocurrencies got a bad reputation when scams multiplied like ants on a piece of cake. Even today many people associate bitcoin and other … [Read More...]

WHAT IS CLOUD COMPUTING AND WHAT ARE ITS MAIN BENEFITS?

Users are Increasingly using cloud computing to store their information, which is replacing local storage. The business digitization process goes … [Read More...]

Guides

  • Computer Communications
  • Mobile Computing
  • PC Components
  • PC Data Storage
  • PC Input-Output
  • PC Multimedia
  • Processors (CPUs)

Recent Posts

GPS Technology

The Global Positioning System (GPS), is a constellation of 24 satellites - three of which are backup satellites - which orbit the Earth twice a day at … [Read More...]

Kaspersky Anti-Virus 2015 Review

Pros : Easy to use interface. Scores high in independent labs in the area of detection and anti-virus protection. Comes with a rescue disk feature … [Read More...]

Colombia is Becoming a Major LATAM Software Producing Country

Over a two-day period in December, Cali hosted Softic 2019, an international software business conference organized by Fedesoft, Procolombia and the … [Read More...]

[footer_backtotop]

Copyright © 2023 About | Privacy | Contact Information | Wrtie For Us | Disclaimer | Copyright License | Authors