Question

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

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

//=====================java game class================================

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:

code:

public class Game {

      

       private String description;

      

       public Game()

       {

             this.description = null;

       }

      

       public Game(String description)

       {

             this.description = description;

       }

      

       public String getDescription()

       {

             return description;

       }

      

       public void setDescription(String desc)

       {

             description = desc;

       }

      

       public String toString()

       {

             return ("Description : "+description);

       }

}

//end of Game class

// Java Trivia class

public class Trivia extends Game{

      

       private int game_id;

       private double prize_money;

       private int questions_to_win;

      

       public Trivia()

       {

             super();

             game_id = 0;

             prize_money = 0;

             questions_to_win=0;

       }

      

       public Trivia(String description,int game_id,double prize_money, int questions_to_win)

       {

             super(description);

             this.game_id = game_id;

             this.prize_money = prize_money;

             this.questions_to_win = questions_to_win;

       }

      

       public int getGameId()

       {

             return game_id;

       }

      

       public double getPrizeMoney()

       {

             return prize_money;

       }

      

       public int getNumQuestionsToWin()

       {

             return questions_to_win;

       }

      

       public void setGameId(int game_id)

       {

             this.game_id = game_id;

       }

      

       public void setPrizeMoney(double prize_money)

       {

             this.prize_money = prize_money;

       }

      

       public void setNumQuestionsToWin(int questions_to_win)

       {

             this.questions_to_win = questions_to_win;

       }

      

       public String toString()

       {

             return(super.toString()+" Game id :"+game_id+" Prize Money : "+prize_money+" Number of questions to win : "+questions_to_win);

       }

}

//end of Trivia class

// Java TriviaLinkedList class

public class TriviaLinkedList {

      

       // TriviaNode class

       class TriviaNode{

             private Trivia game;

             private TriviaNode next;

            

             public TriviaNode(Trivia game)

             {

                    this.game = game;

                    next = null;

             }

            

             public TriviaNode(Trivia game,TriviaNode next)

             {

                    this.game = game;

                    this.next = next;

             }

            

             public Trivia getGame()

             {

                    return game;

             }

            

             public TriviaNode getNext()

             {

                    return next;

             }

            

             public void setGame(Trivia game)

             {

                    this.game = game;

             }

            

             public void setNext(TriviaNode next)

             {

                    this.next = next;

             }

            

             public String toString()

             {

                    return game.toString();

             }

       } // end of TriviaNode class

      

       private TriviaNode head;

       private int num_of_items;

      

       public TriviaLinkedList()

       {

             head = null;

             num_of_items = 0;

       }

      

       public TriviaNode getHead()

       {

             return head;

       }

      

       public int getNumberOfItems()

       {

             return num_of_items;

       }

      

       public void setHead(TriviaNode head)

       {

             this.head = head;

       }

      

       // method to insert a game in the list

       public void insert(Trivia game)

       {

             if(head == null)

             {

                    head = new TriviaNode(game);

             }

             else

             {

                    TriviaNode newNode = new TriviaNode(game);

                    newNode.setNext(head);

                    head = newNode;

             }

            

             num_of_items++;

       }

      

       // method to delete the game in the list

       public void delete(int game_id)

       {

             if(head == null)

             {

                    System.out.println("\n Empty list ");

                    return;

             }

            

             if(head.getGame().getGameId() == game_id)

             {

                    head = head.getNext();

                    num_of_items--;

                    System.out.println("\n Deleted game with id : "+game_id);

             }else

             {

                    TriviaNode temp = head;

                    while(temp.getNext() != null)

                    {

                           if(temp.getNext().getGame().getGameId() == game_id )

                           {

                                 temp.setNext(temp.getNext().getNext());

                                 System.out.println("\n Deleted game with id : "+game_id);

                                 num_of_items--;

                                 return;

                           }

                          

                           temp = temp.getNext();

                    }

                   

                    System.out.println("\n Game id : "+game_id+" not present in the list");

             }

       }

      

       // method to display the list

       public void display()

       {

             if(head == null)

                    System.out.println("\n Empty list");

             else {

                    System.out.println("\n List contents : ");

                    TriviaNode temp= head;

                    while(temp!=null)

                    {

                           System.out.println(temp);

                           temp = temp.getNext();

                    }

             }

       }

}

// end of TriviaLinkedList class

// Java program to create Trivia object and insert them in linked list

public class TriviaLinkedListClient {

       public static void main(String[] args) {

            

             TriviaLinkedList list = new TriviaLinkedList(); // create the linked list

             // insert elements

             list.insert(new Trivia("Game 1",1,200,2));

             list.insert(new Trivia("Game 2",2,300,1));

             list.insert(new Trivia("Game 3",3,1000,5));

             list.insert(new Trivia("Game 4",4,5000,10));

             list.insert(new Trivia("Game 5",3,10000,20));

             list.display();

       }

}

==========================================================

output:

list contents "

Description : Game 5 Game id : 3 Prize Money : 10000.0 Number of questions to win : 20

Description : Game 4 Game id : 4 Prize Money : 5000.0 Number of questions to win : 10

Description : Game 5 Game id : 3 Prize Money : 1000.0 Number of questions to win : 5

Description : Game 5 Game id : 2 Prize Money : 300.0 Number of questions to win : 1

Description : Game 5 Game id : 1Prize Money : 200.0 Number of questions to win : 2

Add a comment
Know the answer?
Add Answer to:
In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and...
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 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 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...

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

  • Write ONE application program by using the following requirements: Using JAVA File input and outp...

    Write ONE application program by using the following requirements: Using JAVA File input and output Exception handling Inheritance At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods At least one interface: this interface needs to have at least two abstract methods At least one method overloading At least one method overriding At least...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

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

  • Can someone please help with this in JAVA? Write one application program by using the following...

    Can someone please help with this in JAVA? Write one application program by using the following requirements: 1) Exception handling 2) Inheritance a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods c) At least one interface: this interface needs to have at least two abstract methods d) At least one method...

  • Please give the code without errors Define a class named Doctor whose objects are records for...

    Please give the code without errors Define a class named Doctor whose objects are records for a clinic's doctors. Derive this class from the class Person given in Listing 8.1. It's also posted here in this module. A Doctor record has the doctor's name-defined in the class Person,-a specialty as a String (Pediatrician, Obstetrician, General Practitioner, and so on--these are just examples. The type is String.), and an office-visit fee (use the type double). Give your class a constructor and...

  • Java HW Define a class called Counter whose objects count things. An object of this class...

    Java HW Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Be sure that no method allows the value of the counter to become negative. Include an accessor method that returns the current count value and a method that outputs the count to the screen. There...

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