Gregory Desrosiers Montreal

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

Friday, 7 June 2013

JAVA Program: The PlayStation Class [SonyPlaystationGame]

Posted on 19:26 by Unknown
Hello, everyone! Gregory here! Tonight what I want to present to you guys is the code for three JAVA files where one of them is a full program.

This blog will be a demonstation of designing what is called a class interface, as well as how to use an interface, along with file operation and exception handling.

This program is designed to input and output data on games published for the Sony PlayStation, a 32-bit game console that came out in 1995 to the United States. It was one of the world's remarkable gaming systems of all time, although the graphics were a bit of an issue. But then again, it did succeed in delivering pure entertainment to the players; it has surpassed the expectations of players who favored either the Nintendo 64 or the Sega Saturn.

It's time to turn our heads back to the Sony PlayStation and experience the pure nostalgia... Well, not really! I'm showing you a program, of course! And yes, you can modify it at your own convenience.


First, we need to consider the interface, because we want to show the building process to the full program. So, here's what the interface looks like in pure code:


public interface SonyPlaystationInterface
{
public void setNumberOfMemoryBlocks(byte numberOfBlocks);
public void setNumberOfPlayers(byte numberOfPlayers);
public void setAdditionalPeripherals(String additionalInputDevices);

public byte getNumberOfMemoryBlocks();
public byte getNumberOfPlayers();
public String getAdditionalPeripherals();

public String toString();
}

In a JAVA interface, all you do is declare the interface definition, then type in the method headers; you do not actually type in the method definitions. They are to be written when you actually use the interface.

You cannot instantiate an interface, unless you explicitly define the methods denoted by the braces. When you start to use the interface in a program, you need to implement the methods using the same signature and headers as shown here. Notice on how you need to type in the code yourself; this is what's known as an abstracted class, where the methods are not defined explicitly. You need to type in the code yourself to define those methods.

We are now going to use the interface to design the class to instantiate objects for containing information about PlayStation games. To declare an interface to be used, after the class name or the superclass name, in case you a designing a subclass, type in the keyword implements, then type in the name of the interface.

The class has been designed a long time ago, so here's the code with the interface in place. Notice on how the class actually inherits properties from the ComputerGame class; I've shared with you the code for it a few blogs back. Notice on how the key words in red are related to the interface.

public class SonyPlaystationGame extends ComputerGame implements SonyPlaystationInterface
{
private boolean licensedGame;
private byte memoryCardFileSize;
private byte maxNumberOfPlayers;
private String additionalInputDevices;

public static void main(String[] args) // Testing purposes only
{
SonyPlaystationGame game1 = new SonyPlaystationGame("Final Fantasy VII", "Square Product Development Division 1",
"Sony Computer Entertainment America, Inc.", "Sony Computer Entertainment America, Inc.", (short)1997, 
true, (byte)1, (byte)1, "None");

System.out.println(game1 + "\n\n\n");

game1.setName("Spyro The Dragon");
game1.setGameDeveloper("Insominac Games, Inc.");
game1.setGameDistributor("Universal Interactive Studios");
game1.setYearOfRelease((short)1998);
game1.setAdditionalPeripherals("Analog Controller");

System.out.println(game1);

ComputerGame game2 = new SonyPlaystationGame("Destruction Derby", "Reflections", "Psygnosis, Ltd.", "Sony Computer Entertainment America, Inc.", (short)1995,
true, (byte)1, (byte)2, "Link Cable between 2 PS Consoles");

System.out.println(game2 + "\n\n\n" + game2.getName() + "\n\n" + game2.getGameDeveloper() + "\n" + game2.getGamePublisher() + "\n\n" + game2.getYearOfRelease());
}

public SonyPlaystationGame()
{
super("", "Sony PlayStation", "", "", "", (short)1995);
licensedGame = true;
memoryCardFileSize = 1;
maxNumberOfPlayers = 1;
additionalInputDevices = "None";
}

public SonyPlaystationGame(String gameName, String developer, String publisher,
String distributor, short releaseYear, boolean licensedGame, byte memoryCardFileSize,
byte maxNumberOfPlayers, String additionalInputDevices)
{
super(gameName, "Sony PlayStation", developer, publisher, distributor, releaseYear);
this.licensedGame = licensedGame;
this.memoryCardFileSize = memoryCardFileSize;
this.maxNumberOfPlayers = maxNumberOfPlayers;
this.additionalInputDevices = additionalInputDevices;
}

public void setNumberOfMemoryBlocks(byte numberOfBlocks)
{
memoryCardFileSize = numberOfBlocks;
}

public void setNumberOfPlayers(byte numberOfPlayers)
{
maxNumberOfPlayers = numberOfPlayers;
}

public void setAdditionalPeripherals(String additionalInputDevices)
{
this.additionalInputDevices = additionalInputDevices;
}

public byte getNumberOfMemoryBlocks()
{
return memoryCardFileSize;
}

public byte getNumberOfPlayers()
{
return maxNumberOfPlayers;
}

public String getAdditionalPeripherals()
{
return additionalInputDevices;
}

public boolean getLicensed()
{
return licensedGame;
}

public String toString()
{
String messageToPrint = super.toString() + "\n\nMemory Card File Size : " + memoryCardFileSize + "\n\nMax # of Players : " + maxNumberOfPlayers 
+ "\nAdditional compatible peripherals : " + additionalInputDevices + "\n\nIs the game licensed? ";

if (licensedGame)
messageToPrint += "Yes";
else
messageToPrint += "No";

return messageToPrint;
}

}

Now, we can use this class in a file processor. Unfortunately, it's not that fancy when I coded it last winter, but at least it can demonstrate file actions and exception handling. The biggest mistake is the input; you have to follow a certain format in order for the program to process it properly; otherwise, you will get an error.

Here's the code anyway.

import java.io.*;
import javax.swing.JOptionPane;

public class PlaystationGameFileListProcessor
{
public static void main(String[] args) throws IOException
{
try
{
BufferedReader fReader = new BufferedReader(new FileReader(new File("PlayStationGames.txt")));

for (byte x = 0; x < 4; x++)
{
fReader.readLine();
}

String input = fReader.readLine();
short numberOfGames = 0;
byte numberOfLines = 0;

while (input != null)
{
if (!input.equals(""))
numberOfLines++;

if (numberOfLines == 9)
{
numberOfLines = 0;
numberOfGames++;
}

input = fReader.readLine();
}

fReader.close();
fReader = new BufferedReader(new FileReader(new File("PlayStationGames.txt")));

input = fReader.readLine();


for (byte x = 0; x < 4; x++)
{
fReader.readLine();
}

SonyPlaystationGame[] gameArray = new SonyPlaystationGame[numberOfGames];

String title, gameDeveloper, gamePublisher, gameDistributor, additionalPeripherals;
short releaseYear, currentIndex = 0;
boolean licensedGame;
byte memoryCardFileSize, maxNumberOfPlayers;

while (input != null)
{

title = fReader.readLine();
gameDeveloper = fReader.readLine();
gamePublisher = fReader.readLine();
gameDistributor = fReader.readLine();
releaseYear = Short.parseShort(fReader.readLine());
licensedGame = Boolean.parseBoolean(fReader.readLine());
memoryCardFileSize = Byte.parseByte(fReader.readLine());
maxNumberOfPlayers = Byte.parseByte(fReader.readLine());
additionalPeripherals = fReader.readLine();

gameArray[currentIndex] = new SonyPlaystationGame(title, gameDeveloper, gamePublisher, gameDistributor, 
releaseYear, licensedGame, memoryCardFileSize, maxNumberOfPlayers, additionalPeripherals);

currentIndex++;
input = fReader.readLine();
}

fReader.close();

String outputFileName = JOptionPane.showInputDialog("What is the name of the file you want to output the game list?\nDo not include file extension.");

PrintWriter fWriter = new PrintWriter(outputFileName + ".txt");

for (short index = 0; index < gameArray.length; index++)
{

fWriter.println("Name of Game : " + gameArray[index].getName());
fWriter.println("Platform : " + gameArray[index].getGamePlatform());
fWriter.println();
fWriter.println("Developer : " + gameArray[index].getGameDeveloper());
fWriter.println("Publisher : " + gameArray[index].getGamePublisher());
fWriter.println();
fWriter.println("Distributor : " + gameArray[index].getGameDistributor());
fWriter.println();
fWriter.println("Release Year : " + gameArray[index].getYearOfRelease());
fWriter.println();
fWriter.println("Memory Card File Size : " + gameArray[index].getNumberOfMemoryBlocks());
fWriter.println();
fWriter.println("Max # of Players : " + gameArray[index].getNumberOfPlayers());
fWriter.println("Additional compatible peripherals : " + gameArray[index].getAdditionalPeripherals());
fWriter.println();
fWriter.print("Is the game licensed? ");

if(gameArray[index].getLicensed())
fWriter.println("Yes");
else
fWriter.println("No");

for (byte x = 1; x <= 3; x++)
{
if (index != (gameArray.length - 1))
fWriter.println();
}

}

fWriter.close();

}
catch (EOFException endOfFileError)
{
JOptionPane.showMessageDialog(null, "Uh-oh! You did not enter enough details to have the program accept\nor are trying to add a new game but did not put in any details.", "End of File Reached", JOptionPane.ERROR_MESSAGE);
}
catch (NumberFormatException fileWritingError)
{
JOptionPane.showMessageDialog(null, "Shucks! You have not written the input text in the text file properly.\nPlease follow the writing format.", "Input Error", JOptionPane.ERROR_MESSAGE);
}
catch (IOException fileOperationError)
{
JOptionPane.showMessageDialog(null, "Oh dear!\nSomething went wrong with the IO system. Please run program again.", "File Operation Error", JOptionPane.ERROR_MESSAGE);
}
}

}

Well, that does it for this blog. Hope to see you again soon!
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)
      • "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