CSC 202 Computer Science II Priority Queue Lab Write a Java class called Game to
ID: 3719991 • Letter: C
Question
CSC 202 Computer Science II
Priority Queue Lab
Write a Java class called Game to represent a video game. The Game object should have three instance variables: two String objects named name and console, and a Date object named date. These instance variables are to have private access modifiers. You will need to write the appropriate getter and setter methods to allow another class or object to access and modify the data values. Game should have a parameterized constructor which takes a String parameter: aName, a Date parameter: aDate, and a String parameter: aConsole (in that order). In accordance with good programming practice, you are to override the toString method inherited from Object. The toString method should return in the following format:
“Name-delim-Date-delim-Console”
When using the date instance variable in the toString method, use Date’s getTime() method to get date’s value.
Write a Java class called Lab5. Lab5 has no instance variables and contains only a single method: sortGames which reads an input file containing text. Use a Scanner object to read the contents of the input file. sortGames takes a File parameter: anInFile, a File parameter: anOutFile, and a SortBy parameter: aSort (in that order). (When importing the Date class, make sure to import java.util.Date)The given text file will contain game information, where each line of the file contains the information for a single game. The properties of each game will be separated by a specific string of characters “-delim-”. For example, a line of text would look like:
Final Fantasy XI: Rise of the Zilart-delim-1073000981498-delim-PS2
Where the first part of the String is the name of the game, the second the date the game was released, and the third the console the game was released for.
The sortGames method should read the contents of anInFile line by line, making each line into Game object. It should then create a PQEntry object where the value is the created Game object and the key is one of the Game objects instance variables. The created PQEntry object should then be placed in a Priority Queue object. To make a line of text into a Game object, utilize the following two methods:
split, which is a method in the String class. split takes a single String parameter and splits the String object from which it was called into a String array, using the given String parameter to determine where to break apart the host String
The Date constructor, which takes a long variable and creates a date which represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
After parsing the entire input file, use a PrintWriter object to write the String value of each Game object in the Priority Queue to anOutFile.
Lastly, SortBy is an enumerated type. An enumerated type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. The SortBy enumerated type contains three values: NAME,DATE,CONSOLE. A variable of type SortBy can only be one of those three values. To define a new enumerated type variable, use:
SortBy toSort = SortBy.NAME;
To compare enumerated types, use the == operator. For example:
if(toSort == SortBy.NAME){}
would evaluate to true. In sortGames, use the given enumerated type to determine which of Games instance variables should be the Key used for PQEntry. If toSort is equal to SortBy.NAME, then when creating a PQEntry object, the key should be the Game objects name instance variable and the value should be the Game object itself.
NOTE: To implement this method, you may find yourself defining a Priority Queue before initializing it. Because you may initialize the defined Priority Queue with different data types depending on the value of the SortBy parameter, you won’t be able to specify the data types of the Priority Queue when you define it. This will cause a compiler warning which we can safely ignore.
NOTE TWO: You will need to create Comparator objects to be used when initializing new priority queues. Use the Comparator classes on Blackboard as templates.
When you have completed your implementation of both the Game and the Lab5 classes, use the provided test cases to test your work. When complete, submit your Game and the Lab5 classes to Blackbaord. I advise implementing and testing your Game class before starting your Lab5 class. The test cases for Lab5 will not pass if you do not have a working Game class. If you create a testing class for your own use, you don’t need to submit that.
Provided classes (Except for the JUnit test, it is exceptionally large, but can be sent upon request):
_____________________
____________________
__________________________
_________________________
___________________________________
___________________________
________________________________
Explanation / Answer
Hello, I have answered the same question before, so I’m referring my own solution here. Implemented everything as per the requirements. Defined following things in this answer.
//Game.java class
import java.util.Date;
public class Game {
private String name;
private String console;
private Date date;
/**
* constructor to initialize all fields
*
* @param name
* - name of game
* @param date
* -date of release
* @param console
* - target console
*/
public Game(String name, Date date, String console) {
this.name = name;
this.date = date;
this.console = console;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getConsole() {
return console;
}
public void setConsole(String console) {
this.console = console;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
// returns a properly formatted string
@Override
public String toString() {
return name + ";" + date.getTime() + ";" + console;
}
}
// Lab5.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Scanner;
public class Lab5 {
/**
* method to sort Games in inFile and save to outFile
* @param inFile -input file
* @param outFile -output file
* @param sortBy - sorting parameter
*/
static void sortGames(File inFile,File outFile,SortBy sortBy){
/**
* Defining a queue
*/
PriorityQueue queue;
/**
* initializing queue with proper values
*/
if(sortBy==SortBy.NAME){
//using a String value, and a StringComparator
queue=new PriorityQueue<String,Game>(new StringComparator());
}else if(sortBy==SortBy.CONSOLE){
queue=new PriorityQueue<String, Game>(new StringComparator());
}else{
//using a Date value, and a DateComparator
queue=new PriorityQueue<Date, Game>(new DateComparator());
}
try {
/**
* Intializing the Scanner to read file
*/
Scanner scanner=new Scanner(inFile);
/**
* looping through all lines
*/
while(scanner.hasNext()){
//reading line
String line=scanner.nextLine();
//splitting line by semicolon (delimeter)
String fields[]=line.split(";");
String name=fields[0];
//parsing long
long timeStamp=Long.parseLong(fields[1]);
//defining date from long
Date date=new Date(timeStamp);
String console=fields[2];
//creating a game object
Game game=new Game(name, date, console);
PQEntry entry = null;
/**
* Defining the PQEntry with proper value field
*/
switch (sortBy) {
case NAME:
entry=new PQEntry<String, Game>(name, game);
break;
case CONSOLE:
entry=new PQEntry<String, Game>(console, game);
break;
case DATE:
entry=new PQEntry<Date, Game>(date, game);
default:
break;
}
/**
* Adding to the queue (safe)
*/
queue.insert(entry);
}
/**
* Defining a print writer to append output
*/
PrintWriter writer=new PrintWriter(outFile);
/**
* loops until the queue is empty
*/
while(!queue.isEmpty()){
/**
* removing the game
*/
Game g=(Game) queue.remove().getValue();
//displaying it
System.out.println(g);
//appending to the file
writer.append(g+" ");
}
//closing the writer
writer.close();
System.out.println(" Output has been written to the output file!");
} catch (FileNotFoundException e) {
System.out.println("Input file not found!");
}catch (Exception e) {
System.out.println("File corrupted");
}
}
public static void main(String[] args) {
/**
* Defining Input and Output files
*/
File inFile=new File("games.txt");
File outFile=new File("output.txt");
/**
* sorting the games list by name
*/
sortGames(inFile, outFile, SortBy.NAME);
}
}
// SortBy.java
public enum SortBy{
NAME,DATE,CONSOLE
}
//games.txt
Final Fantasy XI: Rise of the Zilart;1073000981498;PS2
GTA V;1073009981345;XBox
Far Cry 4;1083089982343;PS2
The Division;1283089912449;XBox
Walking Dead;1063089082340;PS4
//output.txt after running the program
Far Cry 4;1083089982343;PS2
Final Fantasy XI: Rise of the Zilart;1073000981498;PS2
GTA V;1073009981345;XBox
The Division;1283089912449;XBox
Walking Dead;1063089082340;PS4