Question

In: Computer Science

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.

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:

  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.

Solutions

Expert Solution

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();
}


}


Related Solutions

Write a class encapsulating a board game. A board game has the following additional attributes: the...
Write a class encapsulating a board game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. 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. code in Python
In this python program , you are to build a trivia game. The game should present...
In this python program , you are to build a trivia game. The game should present each question – either in order or randomly – to the player, and display up to four possible answers. The player is to input what they believe to be the correct answer.   The game will tell the player if they got it right or wrong and will display their score. If they got it right, their score will go up. If they got it...
In the previous lab you created a Car class and a Dealership class. Now, in this...
In the previous lab you created a Car class and a Dealership class. Now, in this activity change the design of the Dealership class, in which the list of the cars inside a dealership will be stored in an ArrayList. Then, provide required getter and setter methods to keep the records of all its cars. In your tester class, test your class and also printout the list of all cars of a given dealership object. // Java program import java.util.ArrayList;...
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
In this exercise you will return to your SpecialityCoffee class which you created for the last...
In this exercise you will return to your SpecialityCoffee class which you created for the last coding activity (a sample solution to this exercise will be shown below the entire question if you do not have yours). This time you will override the Coffee method getPrice which returns the price in cents for a given coffee. The SpecialityCoffee method getPrice should return the price given by the Coffee method for that object, plus an extra charge for the flavored syrup....
Python Clean up the code Recall that once a subclass inherits from a parent class, it...
Python Clean up the code Recall that once a subclass inherits from a parent class, it automatically has access to all of the parents' methods. Sometimes, the subclass needs to do extra things within a method which the parent does not do. For example, both UserPlayer and BasicMonster have their specialized __init__ methods which override the one from Player. Discuss the following question with your partner: What are the other methods that are being overridden by a subclass in the...
Suppose jillsBook is a LooseLeaf object that has already been created, but which you cannot see....
Suppose jillsBook is a LooseLeaf object that has already been created, but which you cannot see. Now write one or several statements that double the number of blank pages in jillsBook. Thus if jillsBook starts with 45 blank pages, then after your statements execute jillsBook should hold 90 pages. If her book starts with 80 pages, after your statement executes her book should hold 160 blank pages. Hint: First use the getBlankPages method to extract the number of blank pages...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
Present Value 6a. What is the present value of $1,000,000, due 25 years from now?   ...
Present Value 6a. What is the present value of $1,000,000, due 25 years from now?    b. What is the present value of a $40,000 ordinary annuity for 25 years? c. What is the present value of a $40,000 perpetuity, if the first payment is 1 year from now? d. What is the present value of a $40,000 perpetuity, if the first payment is now? Using formula or Excel function
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT