Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 Dates getTime() method to get dates 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 wont 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 dont need to submit that.

Provided classes (Except for the JUnit test, it is exceptionally large, but can be sent upon request):

package chapter9; public interface Entry { K getKey(); V getValue(); }
____________ package chapter9; import java.util.Comparator; public class IntegerComparator implements Comparator { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }
_________________________ package chapter9; public class Job { private int jobNumber; private int requiredExecutionTime; private int givenExecutionTime; private int entryTime; private int priority; public Job(int aJobNumber, int aRequiredExecutionTime, int aPriority) { jobNumber = aJobNumber; requiredExecutionTime = aRequiredExecutionTime; priority = aPriority; givenExecutionTime = 0; entryTime = 0; } public int getJobNumber() { return jobNumber; } public void setJobNumber(int jobNumber) { this.jobNumber = jobNumber; } public int getEntryTime() { return entryTime; } public void setEntryTime(int entryTime) { this.entryTime = entryTime; } public int getPriority() { return priority; } public void setPriority(int priority) { this.priority = priority; } public int getRequiredExecutionTime() { return requiredExecutionTime; } public void setRequiredExecutionTime(int requiredExecutionTime) { this.requiredExecutionTime = requiredExecutionTime; } public int getGivenExecutionTime() { return givenExecutionTime; } public void setGivenExecutionTime(int givenExecutionTime) { this.givenExecutionTime = givenExecutionTime; } public String toString() { return jobNumber +""; } }

_____________________

package chapter9; public class PQEntry implements Entry{ private K key; private V value; public PQEntry(K aKey, V aValue) { key = aKey; value = aValue; } @Override public K getKey() { return key; } @Override public V getValue() { return value; } @Override public String toString() { return "K: " + key.toString() + " V: " + value.toString(); } }

____________________

package chapter9; import java.util.Comparator; public class PriorityQueue { Queue> queue; Comparator comparator; public PriorityQueue(Comparator aComparator) { queue = new Queue>(); comparator = aComparator; } public void insert(PQEntry anEntry) { if(queue.size() == 0) { queue.enqueue(anEntry); } else { boolean isAdded = false; for(int i = queue.size(); i > 0; i--) { PQEntry tempEntry = queue.dequeue(); //If tempEntry key > anEntry key --> evaluates to true if(comparator.compare(tempEntry.getKey(), anEntry.getKey()) > 0 && isAdded == false) { queue.enqueue(anEntry); queue.enqueue(tempEntry); isAdded = true; } else { queue.enqueue(tempEntry); } } if(isAdded == false) { queue.enqueue(anEntry); } } } public PQEntry remove(){ return queue.dequeue(); } public PQEntry min(){ return queue.first(); } public int size() { return queue.size(); } public boolean isEmpty() { return queue.isEmpty(); } }

__________________________

package chapter9; //First In First Out FIFO public class Queue { private SinglyLinkedList linkedList; public Queue() { linkedList = new SinglyLinkedList(); } public int size() { return linkedList.getSize(); } public boolean isEmpty() { return linkedList.isEmpty(); } public E first() { if(isEmpty() == true) { return null; } else { return linkedList.getFirst(); } } public E dequeue() { if(isEmpty() == true) { return null; } else { return linkedList.removeFirst(); } } public void enqueue(E anElement) { linkedList.addLast(anElement); } public String toString() { return linkedList.toString(); } @SuppressWarnings("hiding") public class SinglyLinkedList { //Instance Variables private Node head; private Node tail; private int size; //Default Constructor public SinglyLinkedList() { size = 0; } //Getter methods public int getSize() { return size; } public boolean isEmpty() { boolean toReturn = false; if(size == 0) { toReturn = true; } return toReturn; } public E getFirst() { if(isEmpty() == true) { return null; } else { return head.getElement(); } } public E getLast() { if(isEmpty() == true) { return null; } else { return tail.getElement(); } } public String toString() { String result = ""; Node aNode = head; if(aNode != null) { result = aNode.toString() + " , " + result; while(aNode.getNextNode() != null) { aNode = aNode.getNextNode(); result = aNode.toString() + " , " + result; } } result = "[" + result + "]"; return result; } //Setter methods public void addLast(E anElement) { //Create a new node with the given element. Because this is the last node in the linked list, we set the value of the new nodes next node //to null, as there is no next node Node newNode = new Node(anElement,null); //If the linked list is empty we make the new node the head node because it will be both the head and the tail node. if(isEmpty() == true) { head = newNode; } else { //If the linked list has nodes in it already, we link the new node to the current tail of our linked list tail.setNextNode(newNode); } //We update the tail and size instance variables tail = newNode; size++; } public E removeFirst() { if(isEmpty() == true) { return null; } else { E toReturn = head.getElement(); head = head.getNextNode(); size--; if(size == 0) { tail = head; } return toReturn; } } } public class Node { //Instance Variables private G element; private Node next; //Parameterized Constructor public Node(G anElement, Node nextNode) { element = anElement; next = nextNode; } //Getters and setters public G getElement() { return element; } public void setElement(G anElement) { element = anElement; } public Node getNextNode(){ return next; } public void setNextNode(Node nextNode) { next = nextNode; } public String toString() { return element.toString(); } } }

_________________________

package chapter9; public class Song implements Comparable{ private String title; private String artist; private String album; public Song(String aTitle, String anArtist, String anAlbum) { title = aTitle; artist = anArtist; album = anAlbum; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return title + " - " + artist + " - " + album; } @Override public boolean equals(Object aSong) { if(aSong instanceof Song) { Song asASong = (Song) aSong; if(asASong.getTitle().equals(title) && asASong.getArtist().equals(artist) && asASong.getAlbum().equals(album)) { return true; } else { return false; } } else { return false; } } @Override public int compareTo(Song o) { if(artist.compareTo(o.getArtist()) == 0) { if(album.compareTo(o.getAlbum()) == 0) { return title.compareTo(o.getTitle()); } else { return album.compareTo(o.getAlbum()); } } else { return artist.compareTo(o.getArtist()); } } }

___________________________________

package chapter9; import java.util.Comparator; public class SongComparator implements Comparator { @Override public int compare(Song o1, Song o2) { return o1.compareTo(o2); } }

___________________________

package chapter9; import java.util.Comparator; public class StringComparator implements Comparator { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } } 

________________________________

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxxviii Special Issue On Database And Expert Systems Applications Lncs 11250

Authors: Abdelkader Hameurlain ,Roland Wagner ,Sven Hartmann ,Hui Ma

1st Edition

3662583836, 978-3662583838

More Books

Students also viewed these Databases questions