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

  • Create the following program in java please Write a class Store which includes the attributes: store...

    Create the following program in java please Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a...

  • 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 a class encapsulating the concept of a vendor, if a vendor has the following attributes:...

    Write a class encapsulating the concept of a vendor, if a vendor has the following attributes: company name, id, array of quarterly purchase order totals (4 double elements). Include a constructor, accessor, mutator, and toString methods. Also code the following methods: one returning the total purchase orders (total all array elements) and a method to modify an array element (change the value of an array element). Write a client class to create 2 vendor objects and test all your methods.

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

  • Need help extending and constructing this problem in JAVA using the given guidelines. public class gatorade...

    Need help extending and constructing this problem in JAVA using the given guidelines. public class gatorade { public void nutrition() { System.out.println("nutrition facts method"); System.out.println("90 Calories"); System.out.println("0 Grams Fat"); System.out.println("20 MG Sodium"); System.out.println("18 Grams Sugar"); System.out.println("0 Grams Protein"); } } //inheritance public class blueberry extends gatorade{ System.out.println("Color is blue"); public class orange extends gatorade{ System.out.println("Color is orange"); public class lemonlime extends gatorade{ System.out.println("Color is yellow"); public class fruitpunch extends gatorade{ System.out.println("Color is red"); ________________________________________________________ Write one application program by using...

  • Using Java Write a class encapsulating the concept of student grades (50 elements) on a test,...

    Using Java Write a class encapsulating the concept of student grades (50 elements) on a test, assuming student grades are composed of a list of integers between 0 and 100. Write the following methods: A constructor with just one parameter, the number of students; all grades can be randomly generated Accessor, mutator, toString, and equals methods A method returning the highest grade A method returning the average grade A method returning the lowest grade Write a client class to test...

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