Please follow me here!

Sunday, May 26, 2013

Demonstration of JAVA Classes

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 void setGamePlatform(String platformOfGame)
       {
              gamePlatform = platformOfGame;
       }

      

       public void setGameDeveloper(String developer)
       {
              gameDeveloper = developer;
       }

       public void setGamePublisher(String publisher)
       {
              gamePublisher = publisher;
       }
      
       public void setGameDistributor(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!

Google+ Followers

Popular Posts by Gregory