figured i would show a very simple breif on who this can be achieved.
In order to address the problem i have resorted to abstraction, super classes and the use of instance of in java.
Firstly the breif
Store new game information inside the array. Display all the game information. Search for particular game based on a game type. Update information for a particular game detail.
The first part will be addressed using ArrayList found in java.util package, the second part will be handled in the Game class for this simple example. The 3rd part will be determined using instanceof and will not go into depth into searching for particular strings in the game title or descriptions. Finally the fourth part is rather straight forward for this example and will be commented and explained later on.
Ok lets begin, so there are a few classes we will use in this example
- Game Interface - Main program
- Game - abstract class
- FirstPersonShooter - type of game
- RealTimeStrategy - type of game
Now the Game class is abstract and briefly it is depicted as below
abstract class Game { private String game_name, game_description; public Game(String name, String description ) { game_name = name; game_description = description; } public String toString() { return ( "GameTitle :: " + game_name + "\nInformation :: " + game_description +"\n" ); } }
Now this is rather useless to us unless we have classes that use the extends Game convention as we cannot instantiate an abstract class. So whats the point of the toString() method ? Well we call that when we create objects of the type below.
So below we have one of our first classes which extends Game class.
public class FirstPersonShooter extends Game { public FirstPersonShooter(String name, String info) { super( name, info ); } }
Notice calls super in the constructor, what this does is actually uses the Game class to define its storage of variables, hence the variables name & info are not stored here but rather in the Game class. The same will happen in the following class which defines the class RealTimeStrategy.
public class RealTimeStrategy extends Game { public RealTimeStrategy( String name, String info ) { super( name, info ); } }
So now we have the 3 main classes we need. Lets created a simple tester to see how to actually use the classes defined above. This is where our GameInterface class comes in, although its not GUI it serves to demonstrate the use of abstraction to handle objects that are similar or of a super class.
An ArrayList is used to store objects of type Game, which by definition can be RealTimeStrategy and FirstPersonShooter objects as they are naturally of the type Game as they do extend the Game class defining them to inherit all the parameters of the Game class.
Later on in the class below the instanceof comparison is used to determine if a game in the ArrayList is an instanceof FirstPersonShooter. If it is, then the toString() method is called and the information is displayed to the console.
import java.util.ArrayList; public class GameInterface { private ArrayList<Game> gameArray; public GameInterface() { gameArray = new ArrayList<Game>(); // now we don't create games as we cannot instantiate Game class hence we create // // the type of game we need and add it to a Games ArrayList // gameArray.add( new RealTimeStrategy( "Age Of Empires", "All time classic") ); gameArray.add( new RealTimeStrategy( "Red Alert", "Great cold war game" ) ); gameArray.add( new FirstPersonShooter( "CounterStrike", "Still one of the most popular FPS games ")); gameArray.add( new FirstPersonShooter( "Battlefield 2", "Delve into a chopper and shoot down enemies ftw" ) ); gameArray.add( new RealTimeStrategy( "Company of Heros", "I never played this, did not like it " ) ); gameArray.add( new FirstPersonShooter( "Assault Cube", "Free open source fps game based on the cube engine from memory" ) ); // now we will print out only the FPS games in our list System.out.println("FPS Games\n---------------"); int counter = 0; do { if ( gameArray.get(counter) instanceof FirstPersonShooter ) System.out.println( gameArray.get(counter).toString() ); counter++; } while ( counter < gameArray.size() ); } public static void main(String [] args) { new GameInterface(); } }
When the program is run the following output is achieved
FPS Games --------------- GameTitle :: CounterStrike Information :: Still one of the most popular FPS games GameTitle :: Battlefield 2 Information :: Delve into a chopper and shoot down enemies ftw GameTitle :: Assault Cube Information :: Free open source fps game based on the cube engine from memory Process completed.
I hope this little tute has helped
