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!
{
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!
0 comments:
Post a Comment