Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Duck, Duck, Goose We are going to play duck, duck, goose. Read the text file of unique first names (players.txt) and place them in a

Duck, Duck, Goose

  1. We are going to play duck, duck, goose. Read the text file of unique first names (players.txt) and place them in a circular linked list. Assume the text file is properly formatted and that there is no bad data. Assume text file is in the current directory.
  2. Print the circular linked list in a serial format to the console. The assumption is that the head of the list will be linked to the tail of the list.
  3. Generate a random number between 1 and 10 inclusive. This number is the player who is the goose. Output this random number to the console.
  4. Iterate over the linked list. Remove the goose (a loser) from the circular linked list. Print the new circular linked list in a serial format. The assumption is that the head of the list will be linked to the tail of the list.
  5. Generate another random number between 1 and 10 inclusive. This is the new goose. Starting at the player AFTER the last loser, find the next goose. Print the new circular linked list in a serial format after this goose is removed.
  6. Continue play until there is only one player left in the listThe WINNER!! Print out the winner.
  7. Name your primary class which contains your main method DuckDuckGoose.java.
  8. Submit all java files needed to build and run your application. Do not use packages, as this makes grading more difficult.
  9. You must use the LinkedList class with an iterator. NO INDEXING MAY BE USED! Any additional instructions given in class must be conformed to.

Here is my code. The problem is that it leaves couple of names. It is supposed to leave one name left as the winner.

There are 19 names.

http://collabedit.com/q248e

import java.io.*; import java.util.*; import java.util.Scanner; import java.util.Iterator; import java.util.LinkedList; import java.util.Random;

public class DuckDuckGoose {

public static void main(String[] args) { LinkedList linkedlist = new LinkedList(); // add try catch to add list try { FileReader fr = new FileReader("C:...players.txt"); Scanner inFile = new Scanner(fr); // while loop to get the next line with add to get the next name while (inFile.hasNextLine()) { linkedlist.add(inFile.nextLine()); } } catch (FileNotFoundException e) { e.printStackTrace(); } Iterator iterator = linkedlist.iterator(); // to get the names of the list in loop while (iterator.hasNext()) { iterator.next(); // random number Goose landed method if (getRandomBoolean()) { iterator.remove(); } } System.out.println(linkedlist); } // gets the random number with a different number .80 // than .5 that is half public static boolean getRandomBoolean() { return Math.random() < .80; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions