In: Computer Science
In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game.
Now that you have successfully created Trivia objects, you will continue 6B by creating a linked list of trivia objects. Add the linked list code to the Trivia class.
Your linked list code should include the following: a TriviaNode class with the attributes:
1. trivia game - Trivia object
2. next- TriviaNode
3. write the constructor, accessor, mutator and toString methods.
A TriviaLinkedList Class which should include the following attributes:
1. head - TriviaNode
2. number of items – integer
3. write the code for the constructor, accessor and mutator, and toString methods.
4. methods to insert a triviaNode on the list - You may assume inserts always insert as the first node in the list.
5. write a method to delete a node by passing the node id of the game to delete. Take into consideration that the game may not exist in the list. Your method should let the user know that the node was successfully deleted or not.
Write a client to test all aspects - creating trivia objects, inserting the objects as nodes to the list, deleting a node by passing the id of the trivia game. Print out the list every time you make a change such as adding a node and deleting a node. You should create at least 5 objects to be inserted to your list, then delete at least 1. Also, test deleting an object that is not in the list.
6B. In this module, you will combine your knowledge of class objects, inheritance and linked lists. You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game.
Game is the parent class with the following attributes:
Trivia is the subclass of Game with the additional attributes:
1. trivia game id - integer
2. ultimate prize money - double
3. number of questions that must be answered to win - integer.
4. write the accessor, mutator, constructor, and toString methods.
Write a client class to test creating 5 Trivia objects. Once you have successfully created Trivia objects, you will continue 6B by adding a linked list of trivia objects to the Trivia class.
CODE:
import java.math.*;
class Game
{
String description;
Game(String d)
{
description =d;
}
String getDescription()
{
return description;
}
void setDescription(String s)
{
description=s;
}
public String toString()
{
return description;
}
}
class Trivia extends Game
{
int game_id;
double prize_money;
int no_of_questions;
Trivia(String d, int id, double p, int n)
{
super(d);
game_id=id;
prize_money=p;
no_of_questions=n;
}
void setGameId(int n)
{
game_id=n;
}
void setPrizeMoney(double n)
{
prize_money=n;
}
void setNoOfQuestions(int n)
{
no_of_questions=n;
}
int getGameId()
{
return game_id;
}
double getPrizeMoney()
{
return prize_money;
}
int getNoOfQuestions()
{
return no_of_questions;
}
public String toString()
{
return "Id: "+game_id+"\nPrize Money "+prize_money+"\nNo. of
Questions: "+no_of_questions+"\nDesc: "+description;
}
}
class TriviaNode
{
Trivia game;
TriviaNode next;
TriviaNode(Trivia t)
{
game=t;
next=null;
}
Trivia getNode()
{
return game;
}
TriviaNode getNext()
{
return next;
}
void setNext(TriviaNode t)
{
next=t;
}
public String toString()
{
return game.toString();
}
}
class TriviaList
{
TriviaNode head;
int no_of_items;
TriviaList(TriviaNode t)
{
head=t;
no_of_items=1;
}
void addNode(TriviaNode t)
{
t.setNext(head);
head=t;
no_of_items++;
}
void deleteNode(int id)
{
TriviaNode t=head;
boolean f=false;
if(head.getNode().getGameId()==id)
{
head=head.getNext();
no_of_items--;
f=true;
}
else
for(int i=0; i<no_of_items-1; i++)
{
if(t.getNext().getNode().getGameId()==id)
{
t.setNext(t.getNext().getNext());
no_of_items--;
f=true;
break;
}
t=t.getNext();
}
if(f) System.out.println("Delete Successful");
else System.out.println("Item not found");
}
void display()
{
TriviaNode t=head;
for(int i=0; i<no_of_items; i++)
{
System.out.println("\n"+t);
t=t.getNext();
}
}
}
public class Driver
{
public static void main(String s[])
{
TriviaList t = new TriviaList(new TriviaNode(new Trivia("Game
1",1,200,10)));
t.addNode(new TriviaNode(new Trivia("Game 2",2,100,20)));
t.addNode(new TriviaNode(new Trivia("Game 3",3,300,20)));
t.addNode(new TriviaNode(new Trivia("Game 4",4,200,20)));
t.addNode(new TriviaNode(new Trivia("Game 5",5,1100,50)));
t.deleteNode(5);
t.display();
}
}