Question

You will need to first create an object class encapsulating a Trivia Game which INHERITS from...

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:

  1. description - which is a string
  2. write the constructor, accessor, mutator and toString methods.

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.

Lab Assignment 6B

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Done. Have a look and incase of any doubt please leave a comment.

CODE:
//////////////////////////////////////////////Game.java/////////////////////////////////////////////////////////////////////

public class Game {
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Game [description=" + description + "]";
}
public Game(String description) {
this.description = description;
}
}
/////////////////////////////////////////////ENd Game.java//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////TriviaGame.java/////////////////////////////////////////////////////////////////////
public class TriviaGame extends Game {
private int gameId;
private double priceMoney;
private int numOfQuestion;
// Constructor
public TriviaGame(String description, int gameId, double priceMoney, int numOfQuestion) {
super(description);
this.gameId = gameId;
this.priceMoney = priceMoney;
this.numOfQuestion = numOfQuestion;
}
public int getGameId() {
return gameId;
}
public double getPriceMoney() {
return priceMoney;
}
public int getNumOfQuestion() {
return numOfQuestion;
}
public void setGameId(int gameId) {
this.gameId = gameId;
}
public void setPriceMoney(double priceMoney) {
this.priceMoney = priceMoney;
}
public void setNumOfQuestion(int numOfQuestion) {
this.numOfQuestion = numOfQuestion;
}
@Override
public String toString() {
return "Trivia [gameId=" + gameId + ", price Money=" + priceMoney + ", num of Question=" + numOfQuestion
+ ", Description=" + getDescription() + "]";
}
// Override equals to compare to TriviaGame objects. Comparison is done on the
// basis of GameId. This is used in the deletion of object in TriviaLinkedList
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TriviaGame other = (TriviaGame) obj;
if (gameId != other.gameId)
return false;
return true;
}
}
/////////////////////////////////////////////////////End TriviaGame.java/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////TriviaNode.java///////////////////////////////////////////////////////////////////////
public class TriviaNode {
private TriviaGame triviaObject; // TriviaGame object
private TriviaNode nextNode; // Pointer to the next node in the list
// constructor
public TriviaNode(TriviaGame obj) {
triviaObject = obj;
nextNode = null;
}
@Override
public String toString() {
return "TriviaNode [triviaObject=" + triviaObject + ", nextNode=" + nextNode + "]";
}
public TriviaGame getTriviaObject() {
return triviaObject;
}
public TriviaNode getNextNode() {
return nextNode;
}
public void setTriviaObject(TriviaGame triviaObject) {
this.triviaObject = triviaObject;
}
public void setNextNode(TriviaNode nextNode) {
this.nextNode = nextNode;
}
}
////////////////////////////////////////////////End TriviaNode.java////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////TriviaLinkedList.java/////////////////////////////////////////////////////////////
public class TriviaLinkedList {
private TriviaNode head;
private int numberOfItems;
// counter to maintain number of items added in the linkedList
private int counter = 1;
public TriviaLinkedList(TriviaNode head, int numberOfItems) {
super();
this.head = head;
this.numberOfItems = numberOfItems;
}
public void insertNode(TriviaNode newNode) {
// Add new element only if the linkedList has not reached the size defined by
// NumberOfItems
if (!(counter >= numberOfItems)) {
newNode.setNextNode(head);
head = newNode;
counter++;
} else {
System.out.println("Can not add more items. Reacher max Number of items");
return;
}
}
public void deleteNode(int id) {
// Store head node
TriviaNode tempNode = head, prev = null;
TriviaGame gameNode = new TriviaGame("", id, 0, 0);
// If head node itself holds the key to be deleted
if (tempNode != null && tempNode.getTriviaObject().equals(gameNode)) {
head = tempNode.getNextNode(); // Changed head
System.out.println("Node deleted successfully");
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change temp.next
while (tempNode != null && !(tempNode.getTriviaObject().equals(gameNode))) {
prev = tempNode;
tempNode = tempNode.getNextNode();
}
// If key was not present in linked list
if (tempNode == null) {
System.out.println("Cant find the node with the gameid=" + id);
return;
}
// Unlink the node from linked list
prev.setNextNode(tempNode.getNextNode());
}
public TriviaNode getHead() {
return head;
}
public int getNumberOfItems() {
return numberOfItems;
}
public void setHead(TriviaNode head) {
this.head = head;
}
public void setNumberOfItems(int numberOfItems) {
this.numberOfItems = numberOfItems;
}
@Override
public String toString() {
return "TriviaLinkedList [head=" + head + ", numberOfItems=" + numberOfItems + "]";
}
public void printList() {
TriviaNode tnode = head;
while (tnode != null) {
System.out.println(tnode.getTriviaObject() + " ");
tnode = tnode.getNextNode();
}
}
}
/////////////////////////////////////////////////End TriviaLinkedList.java/////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////TriviaGameDriver.java//////////////////////////////////////////////////////////
public class TriviaGameDriver {
public static void main(String args[]) {
TriviaGame triviaObject1 = new TriviaGame("Math Quiz", 12, 250.0, 4);
TriviaGame triviaObject2 = new TriviaGame("Science Quiz", 13, 350.0, 5);
TriviaGame triviaObject3 = new TriviaGame("English Quiz", 14, 150.0, 3);
TriviaGame triviaObject4 = new TriviaGame("French Quiz", 15, 150.0, 3);
TriviaNode node1 = new TriviaNode(triviaObject1);
TriviaNode node2 = new TriviaNode(triviaObject2);
TriviaNode node3 = new TriviaNode(triviaObject3);
TriviaNode node4 = new TriviaNode(triviaObject4);

OUTPUT:

#Could you please leave me THUMBS UP for my work...

Add a comment
Know the answer?
Add Answer to:
You will need to first create an object class encapsulating a Trivia Game which INHERITS from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now...

    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...

  • In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and...

    In JAVA, please 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: 1. description - which is a string 2. write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money...

  • Write a class encapsulating a music store, which inherits from Store. A music store has the...

    Write a class encapsulating a music store, which inherits from Store. A music store has the following additional attributes: the number of titles it offers and its address. Code the constructor and the toString method of the new class. You also need to include a client class (with the main method) to test your code.

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • Write a class Store which includes the attributes: store name, city. Write another class encapsulating an...

    Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method returning the...

  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...

    In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...

  • java In this project you will implement a trivia game. It will ask random trivia questions,...

    java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow for managing the question bank. Question bank management will include adding new questions, deleting questions and displaying all of the questions, answers and point values. 2. The project can be a GUI or a menu based command line program. 3. Project details 1. Create a class to...

  • Create the Python code for a program adhering to the following specifications. Write an Employee class...

    Create the Python code for a program adhering to the following specifications. Write an Employee class that keeps data attributes for the following pieces of information: - Employee Name (a string) - Employee Number (a string) Make sure to create all the accessor, mutator, and __str__ methods for the object. Next, write a class named ProductionWorker that is a subclass of the Employee class. The ProductionWorker class should keep data attributes for the following information: - Shift number (an integer,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT