Question

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

Code implemented in JAVA

Filename: TriviaNode.java

public class TriviaNode<E> {
public Trivia element;
public TriviaNode<E> next;

public TriviaNode(Trivia element) {
this.element = element;
}

public Trivia getElement() {
return element;
}

public TriviaNode<E> getNext() {
return next;
}

public void setNext(TriviaNode<E> next) {
this.next = next;
}

@Override
public String toString() {
return "TriviaNode{" +
"element=" + element +
", next=" + next +
'}';
}
}

Filename: TriviaLinkedList.java

public class TriviaLinkedList {
TriviaNode head, tail;
private int size = 0;

public TriviaLinkedList() {
}

public void addFirst(Trivia e){
TriviaNode newNode = new TriviaNode(e);
newNode.next = head;
head = newNode;
size++;

if (tail == null){
tail = head;
}
}

public void addLast(Trivia e){
TriviaNode newNode = new TriviaNode(e);

if (tail == null){
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}

size++;
}

public void insert(Trivia e){
insert(0, e);
}

public void insert(int index, Trivia e){
if (index == 0){
addFirst(e);
} else if (index >= size){
addLast(e);
} else {
TriviaNode cur = head;
for (int i = 1; i < index; i++) {
cur = cur.next;
}
TriviaNode tmp = cur.next;
cur.next = new TriviaNode(e);
(cur.next).next = tmp;
size++;
}
}

public Trivia removeFirst(){
if (size == 0)
return null;
else {
TriviaNode temp = head;
head = head.next;
size--;
if(head == null){
tail = null;
}
return temp.element;
}
}

public Trivia removeLast(){
if (size == 0){
return null;
} else if (size == 1){
TriviaNode tmp = head;
head = null;
tail = null;
size = 0;
return tmp.element;
} else {
TriviaNode cur = head;

for (int i = 0; i < size - 2; i++) {
cur = cur.next;
}

TriviaNode tmp = tail;
tail = cur;
tail.next = null;
size--;
return tmp.element;

}
}

public Trivia remove(int index){
if (index < 0 || index >= size){
return null;
} else if (index == 0){
return removeFirst();
} else if (index == size - 1){
return removeLast();
} else {
TriviaNode pre = head;

for (int i = 1; i < index; i++) {
pre = pre.next;
}

TriviaNode cur = pre.next;
pre.next = cur.next;
size--;

return cur.element;
}
}

public Trivia removeByID(int id){
TriviaNode cur = head;
int index = 0;
while(cur != null){
if (id == cur.element.getTriviaGameID())
return remove(index);
else
cur = cur.next;
index++;
}

return null;
}

@Override
public String toString() {
return "TriviaLinkedList{" +
"head=" + head +
", tail=" + tail +
", size=" + size +
'}';
}
}

Filename: Trivia.java

public class Trivia extends Game {
int triviaGameID;
double ultimatePrizeMoney;
int numberOfQuestionsThatMustBeAnsweredToWin;

public Trivia(String description, int triviaGameID, double ultimatePrizeMoney, int numberOfQuestionsThatMustBeAnsweredToWin) {
super(description);
this.triviaGameID = triviaGameID;
this.ultimatePrizeMoney = ultimatePrizeMoney;
this.numberOfQuestionsThatMustBeAnsweredToWin = numberOfQuestionsThatMustBeAnsweredToWin;
}

public int getTriviaGameID() {
return triviaGameID;
}

public void setTriviaGameID(int triviaGameID) {
this.triviaGameID = triviaGameID;
}

public double getUltimatePrizeMoney() {
return ultimatePrizeMoney;
}

public void setUltimatePrizeMoney(double ultimatePrizeMoney) {
this.ultimatePrizeMoney = ultimatePrizeMoney;
}

public int getNumberOfQuestionsThatMustBeAnsweredToWin() {
return numberOfQuestionsThatMustBeAnsweredToWin;
}

public void setNumberOfQuestionsThatMustBeAnsweredToWin(int numberOfQuestionsThatMustBeAnsweredToWin) {
this.numberOfQuestionsThatMustBeAnsweredToWin = numberOfQuestionsThatMustBeAnsweredToWin;
}

@Override
public String toString() {
return "Trivia{" +
"triviaGameID=" + triviaGameID +
", ultimatePrizeMoney=" + ultimatePrizeMoney +
", numberOfQuestionsThatMustBeAnsweredToWin=" + numberOfQuestionsThatMustBeAnsweredToWin +
", description='" + description + '\'' +
'}';
}
}

Filename: Main.java

public class Main {

public static void main(String[] args) {
Trivia t1 = new Trivia("Trivia Game 1", 1, 1.0, 2);
Trivia t2 = new Trivia("Trivia Game 2", 2, 1.0, 2);
Trivia t3 = new Trivia("Trivia Game 3", 3, 1.0, 2);
Trivia t4 = new Trivia("Trivia Game 4", 4, 1.0, 2);
Trivia t5 = new Trivia("Trivia Game 5", 5, 1.0, 2);

System.out.println("Create Blank Linked List");
TriviaLinkedList ll = new TriviaLinkedList();
System.out.println(ll);
printLinkedList(ll);

System.out.println("Insert 5 Trivia objects");
addAndPrint(ll, t5);
addAndPrint(ll, t4);
addAndPrint(ll, t3);
addAndPrint(ll, t2);
addAndPrint(ll, t1);

System.out.println("Remove node by triviaGameID");
ll.removeByID(3);
printLinkedList(ll);

System.out.println("Remove node that doesn't exist");

Trivia removed = ll.removeByID(23);
if (removed == null) {
System.out.println("Node Does Not Exist");
}
printLinkedList(ll);
}

public static void printLinkedList(TriviaLinkedList ll){
TriviaNode current = ll.head;

System.out.print("\n{\n");
while (current != null){
System.out.println("\t" + current.element);
current = current.next;
}

System.out.print("}\n\n");

}

public static void addAndPrint(TriviaLinkedList ll, Trivia t){
ll.insert(t);
System.out.println(ll);
printLinkedList(ll);
}
}

Filename: Game.java

public class Game {
String description;

public Game() {
this("This Game has no description");
}

public Game(String description) {
this.description = description;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Override
public String toString() {
return "Game{" +
"description='" + description + '\'' +
'}';
}
}

Working Code Output Screenshot:

If you like my answer, hit thumbs up. Thank you

Add a comment
Know the answer?
Add Answer to:
In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now...
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
  • 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: description - which is a string 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,...

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

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

  • *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented...

    *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented programming using classes to create objects. PROBLEM DEFINITION: Write a class named Employee that holds the following data about an employee in attributes: name, IDnumber, department, jobTitle The class should have 8 methods as follows:  For each attribute, there should be a method that takes a parameter to be assigned to the attribute. This is known as a mutator method.  For each...

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

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

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

  • 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