Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 1 June 2013

JAVA Program: The Nintendo GameCube Game Class [Inheritance]

Posted on 06:42 by Unknown
Good morning, everyone!

It's a warm overcast morning here in Montreal; we're at 24°C, winds 6 km/h to the south, 67% humidity, and pressure is at 101.3 kilopascals.

Anyway, I want to demonstrate to you guys what inheritance in JAVA Programming is all about.

Let's say you have a class that serves a general purpose. For example, a car. Each car would consist of many different parts; the name, the year of manufacturing, the chassis, engine block, wheels, transmission, fuel, and drivetrain. If you would want to design a class that is designed for Formula 1 racing cars, consider their special equipment: front and back wings, tire type and pressure, ride height, suspension, gear ratios, brake balance, steering, differential, and additional aerodynamics.

Instead of designing a class where it carries data fields for both car and Formula 1, why not expand the car class to a class designed for Formula 1? It's a relatively simple process, just as long as you follow the rules of inheritance.

Here's an example. This is the code for a mutable class designed to instantiate and carry information on Nintendo GameCube games. It is an extension of the ComputerGame class, which you can see right here: http://gregorypdesrosiersmontreal.blogspot.ca/2013/05/demonstration-of-java-classes.html

The advantages of inheritance is simply that you can go into much more specifics out of a generalized class that it can handle the type of objects in the real world you want to design the class for. The extended class will carry all the methods and data fields from the original class where it's being expanded by this class.


Take note that the equals method, originally defined in the Object class, is not overridden in this class. It's simply a boolean method where instead of returning whether or not two objects have the same reference address, it checks the contents of the objects and determines whether or not they are equal.

This involves using polymorphism, generic programming, and the instanceof operator, which checks to see if the actual type of the object, at the type of construction, is the specified class on the right side of the operator. It can even reference the class you are currently in.

The key to expanding an original class to more specifics is by using the key word extends.

public class NintendoGamecubeGame extends ComputerGame
{
    private String gameMemorySize;
    private boolean gameIsLicensed;
  
    public static void main(String[] args) // Testing purposes only
    {
        NintendoGamecubeGame game = new NintendoGamecubeGame("The Legend of Zelda: The Wind Waker", "Nintendo EAD",
                "Nintendo of America, Inc.", "Nintendo of America, Inc.", (short)2003, "1.4 GB", true);
      
        System.out.println(game + "\n\nModifying object...\n\n");
      
        game.setName("Super Mario Sunshine");
        game.setYearOfRelease((short)2002);
      
        System.out.println(game + "\n\n" + game.getGameDeveloper() + " " + game.getGamePlatform());
    }
  
    public NintendoGamecubeGame()
    {
        super("", "Nintendo Gamecube", "", "", "Nintendo of America, Inc.", (short)2001);
        gameMemorySize = "1.4 GB";
        gameIsLicensed = true;
    }
  
    public NintendoGamecubeGame(String gameName, String developer, String publisher,
            String distributor, short releaseYear, String memorySize, boolean licensedGame)
    {
        super(gameName, "Nintendo Gamecube", developer, publisher, distributor, releaseYear);
        gameMemorySize = memorySize;
        gameIsLicensed = licensedGame;
    }
  
    public void setGameMemorySize(String gameMemorySize)
    {
        this.gameMemorySize = gameMemorySize;
    }
  
    public void setGameIsLicensedFlag(boolean gameIsLicensed)
    {
        this.gameIsLicensed = gameIsLicensed;
    }
  
    public String getGameMemorySize()
    {
        return gameMemorySize;
    }
  
    public boolean getGameIsLicensedFlag()
    {
        return gameIsLicensed;
    }
  
    public String toString()
    {
        String messageToPrint = super.toString() + "\n\nMemory Size : " + gameMemorySize + "\nIs the game licensed? ";
      
        if (gameIsLicensed)
            messageToPrint += "Yes";
        else
            messageToPrint += "No";
      
        return messageToPrint;
    }
  
    public String superToString()
    {
        return Object.class.toString();
    }
}

Invoking "super" in the constructors will call the superclass' own constructors explicitly with the parameters passed. When a subclass object is constructed, the superclass constructors are called first before the rest of the construction operation can begin, so it is a must to use super as the first statement in a subclass constructor.

You will notice that the method toString is also defined in the ComputerGame class; this is technically called overriding where it is invoked differently than the method defined in the superclass. This means that by constructing an object from this class and calling the toString() method, it will print differently than the toString method defined in the ComputerGame class.

Sadly, we can't explain what overloading means because it does not exist in this code.


Anyway, moving on to something else, by designing a subclass that extends the generalized "superclass," you'll be able to hold more information on the specifics that subclass is designed for.

So what will be your two classes to design? Please let me know; anyway, good luck, and I'll see you later!
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • The Asperger Computer: A Self-Published Guide on Asperger's Syndrome and Multimedia
    The Asperger Computer - A Self-Published Guide on Asperger's Syndrome and Multimedia by Gregory Desrosiers How is Asperger’s syndrome li...
  • The Asperger Computer (e-book) - Sample Paragraph
    This is the sample paragraph for my e-book adaptation of a speech I've delivered at my college, Champlain College Saint-Lambert, on May ...
  • The Asperger Computer - Table des matières et une sample
    Excusez-moi si il ya beaucoup d'erreurs avec l'écriture , j'ai dû utiliser un traducteur pour obtenir ce parce que je su...
  • Arcade Games to check out
    Hello, everyone. How you people doing? I'm okay, though I am feeling mightily frustrated and lazy to try to work on a lot of things befo...
  • JAVA Program: Number Loop (Experimental Program of the Timer class)
    Hello there. Here's another program I want to present to you: it's a self-running experiment that tests the Timer class provided in ...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • JAVA Program: Military Time to Real Time
    Hello, people. How you doing? I'm really busy with my college homework, but I am working so hard to pass my courses and continue in my s...

Blog Archive

  • ▼  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ▼  June (12)
      • "The Asperger Computer" outline and Spreading the ...
      • JAVA Program - Essay Class (From Chapter 11 of Ton...
      • JAVA Program: Circle Cannon Shooter (Shooting Game...
      • Message from my College English teacher
      • How to Play a DOS Game using DOSBox
      • JAVA Program: Finding Smallest Number (Demonstrati...
      • JAVA Program: Entering Non-Duplicate Numbers to a ...
      • Need some help on publicity with my e-book
      • JAVA Program: The PlayStation Class [SonyPlaystati...
      • The Asperger Computer - Second Edition Preview: Th...
      • Am I Happy on My Brother's 21st Birthday?
      • JAVA Program: The Nintendo GameCube Game Class [In...
    • ►  May (12)
    • ►  April (2)
    • ►  January (1)
  • ►  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile