Gregory Desrosiers Montreal

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

Sunday, 26 May 2013

Demonstration of JAVA Classes

Posted on 19:41 by Unknown
Hello, everyone. Gregory here; how you people doing? I've completed my fourth semester at my college and now taking the time to find a job and get working for money and earn a better living.

Anyway, there's something I want to share with you guys. This is a demonstration on how to write your own classes in JAVA you can then instantiate and use in your main program for any use that the object is designed for.

What I'm showing you is the code for a class I've written in January 2013 as a practice for me to get back to my knowledge in JAVA for my Data Structures class during the semester. It's called "ComputerGame"; all it does is it lets you create objects destined to hold information about computer games.

*NOTE*: The only thing that's missing in the code is the overridden equals method, originally from the Object class.

public class ComputerGame
{

       private String nameOfGame;
       private String gamePlatform;

       private String gameDeveloper;
       private String gamePublisher;
       private String gameDistributor;

       private short yearOfRelease;

       public static void main(String[] args)  // Testing purposes only
       {

              ComputerGame testObject = new ComputerGame("Super Mario Bros.", "Nintendo Entertainment System", "Nintendo R&D", "Nintendo of America, Inc.", "Nintendo of America, Inc.", (short)1985);

              System.out.println(testObject.toString() + "\n\n" + testObject.superToString() + "\n\nModifying Object...\n\n");

              testObject.setName("The Legend of Zelda: Ocarina of Time");
              testObject.setGamePlatform("Nintendo 64");
              testObject.setGameDeveloper("Nintendo EAD (produced and directed by Shigeru Miyamoto)");
              testObject.setGamePublisher("Nintendo of America, Inc.");
              testObject.setGameDistributor("Nintendo of America, Inc.");

              testObject.setYearOfRelease((short)1998);

              System.out.println(testObject.toString() + "\n\n" + testObject.superToString());

       }
      
       public ComputerGame()
       {
              nameOfGame = "";
              gamePlatform = "";
             
              gameDeveloper = "";
              gamePublisher = "";
              gameDistributor = "";

              yearOfRelease = 1972;

       }
      
       public ComputerGame(String gameName, String platformOfGame, String developer, String publisher, String distributor, short releaseYear)
       {
              nameOfGame = gameName;

              gamePlatform = platformOfGame;
             
              gameDeveloper = developer;
              gamePublisher = publisher;
              gameDistributor = distributor;
             
              yearOfRelease = releaseYear;

       }

       public void setName(String gameName)
       {
              nameOfGame = gameName;
       }

      

       public voidsetGamePlatform(String platformOfGame)
       {
              gamePlatform = platformOfGame;
       }

      

       public voidsetGameDeveloper(String developer)
       {
              gameDeveloper = developer;
       }

       public voidsetGamePublisher(String publisher)
       {
              gamePublisher = publisher;
       }
      
       public voidsetGameDistributor(String distributor)
       {
              gameDistributor = distributor;
       }

       public void setYearOfRelease(short releaseYear)
       {
              yearOfRelease = releaseYear;
       }
      
       public String getName()
       {
              return nameOfGame;
       }

      

       public String getGamePlatform()
       {
              return gamePlatform;
       }

       public String getGameDeveloper()
       {
              return gameDeveloper;
       }
      
       public String getGamePublisher()
       {
              return gamePublisher;
       }
      
       public String getGameDistributor()
       {
              return gameDistributor;
       }
      
       public short getYearOfRelease()
       {
              return yearOfRelease;
       }
      
       public String toString()
       {
              return "Name of Game : "+ nameOfGame + "\nPlatform : " + gamePlatform + "\n\nDeveloper : "+ gameDeveloper + "\nPublisher : " + gamePublisher + "\n\nDistributor : "+ gameDistributor + "\n\nRelease Year : " + yearOfRelease;

       }

       public String superToString()
       {
              return super.toString();
       } 
}
 
The way on how this works is, in the main method, you call the constructor as you would for any object you create. When you use the set methods, pass the required value through the parentheses; upon execution, the field that the set method is designed for will be overwritten with the new value. As for the get method, calling them will return the desired property. The toString method overrides the method defined in the Object class, but a method called superToString will allow you to view the original string from the Object class.
 
Well, that's it for this blog. Have any questions or comments? Please be sure to follow me on Facebook and Twitter!
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...
  • 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...
  • 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: 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)
    • ▼  May (12)
      • JAVA Program: Number Loop (Experimental Program of...
      • JAVA Program: Displaying Unlimited Strings on a GU...
      • JAVA Program: Command-based String Remover in Text...
      • Arcade Games to check out
      • JAVA Program: Random 0s and 1s Table with Refresh ...
      • JAVA Program: Object-Oriented Programming Class, t...
      • Announcement on "The Asperger Computer"
      • JAVA Program : Hello World!
      • Introducing myself (original blog on Autism Suppor...
      • Demonstration of JAVA Classes
      • Non-fiction e-book, The Asperger Computer, reveals...
      • The Asperger Computer - Facebook Edition
    • ►  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