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...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • 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...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • 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 ...
  • The Black Glove [Short Poem by me]
    This is a poem written by me back in April 2010. I'm not sure if this was for an assignment, but this is what I got. It may not be good ...
  • 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...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • Princess Peach In India [Story Idea]
    Here's a really fancy story I have made, although it is more of an initial idea and comes from a visual I had more than two years ago. E...
  • Super Mario Script 1 (v 2.3) [Super Mario Galaxy font, with a few photos]
    Hello there. Do you recognize the font of this text? I'll give you two clues. The one below is the a sample sentence of different sizes....

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