Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Make a set of Java classes to simulate a line of Contestants. You are given the class Person, and you will make a Contestant class,

Make a set of Java classes to simulate a line of Contestants.

You are given the class Person, and you will make a Contestant class, where Contestant extends Person. Start simple to make sure your code compiles and runs. As you progress through the lab, you will need to add attributes and methods to the Contestant class to complete the assignment. Make n Contestants, each with a different name, and add them all to an ArrayList of Contestants. n can be any value between 5 and 1,000,000, though I suggest you start small with a value like 10. Later, you will increase the size of n, so plan for this now. Figure out how to flip a coin. You want to generate a boolean that is True about 50% of the time and False about 50% of the time, over a large number of coin flips. See java.util.Random or java.util.concurrent.ThreadlocalRandom if you need a place to get started. In a real contest, each Contestant in turn is asked a question, and moves to a new spot in the line depending on whether the answer is correct or not. One simple algorithm is to move to the front of the line on a correct answer, and the back of the line on an incorrect answer. So suppose there is a line of Contestants (a,b,c,d,e) where a and d get an answer wrong, and b, c and e get their answers correct.

a b c d e

Initial position

b c d e a

a answers incorrectly and moves to the end

b c d e a

b answers correctly and moves to the front

c b d e a

c answers correctly and moves to the front

c b e a d

d answers incorrectly and moves to the end

e c b a d

e answers correctly and moves to the front

This represents one round of the competition. At the end of the round, e is at position 0, c is at position 1, ... and d is at position 4. Implement one round using this algorithm. Use your coin flip to determine whether an answer was correct or not. Include this in your final write-up.

Modify your program to run a series of rounds. Each round should process the Contestants starting from position 0. But first, modify the Contestant class so that it can record a Contestant's position in the list and calculate the Contestant's average position. You will have to figure out how to store this data in each Contestant and what calculation to make, and what methods to provide. In the driver program, you should record each Contestant's position at the end of each round. Run your program with a small number of Contestants (8) and a medium number of rounds (12). Use the average position of each contestant to show that this algorithm isn't biased against certain Contestants, that is, no Contestant gets stuck at one end of the line or the other for most of the game. Add code to calculate the time it takes to run your simulation in step 5. Do not include the time to calculate the fairness aspect. Change the number of Contestants to 43 and then run your program 4 more times for 10, 100, 1000, and 10000 rounds. Record the number of rounds and run times in a table. Increase the number of Contestants to 45,000, then repeat step 6. Add this data to your table and include it in your report.

-----------------------------------------------------------------------------------

public class ContestDriver {

public static void main(String[] args) { System.out.println("Starting Simulation");

// Step -1 Compile the program as is to be sure it runs

// Step 0 - Uncomment the two lines below, then add code to the Contestant class to make this compile // This creates a single Contestant and prints its attributes, just to show it can be done // Contestant ac = new Contestant("Billy",22); // System.out.println(ac); // Step 2 // Create multiple Contestants and add them to an ArrayList // Do something simple to get started, but remember, you will need // to generate up to 1,000,000 Contestants // Steps 3 and 4 // Play a round // For each Contestant, flip a coin, then move the Contestant to // one end of the ArrayList or the other. // Steps 5 - 7 // Repeat Steps 3 and 4 many times to gather data. // You will need to modify the Contestant class so each Contestant // will keep track of its individual statistics. Be smart about how you record its // position at the end of each round. System.out.println("Finished"); }

}

-------------------------------------------------------------------------------

/* Contestant.java: * This class extends Person while adding attributes and methods * related to recording performance in the specified contest. * * Author: Name and Student ID * Last updated: */ public class Contestant extends Person { // A Contestant IS-A Person // that also needs needs to keep track of data related to the game // Start simple to get your program running, then add attributes and methods as you need them // Step 1 Add at least one constructor so the driver will compile and run // Step 5 Add attributes and methods to assist with recording game statistics }

--------------------------------------------------------------------------

/* Person.java:

public class Person {

private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; }

public int getAge() { return age; }

public void setAge(int age) { this.age = age; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

@Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Introduction An array is a fundamental data structures used in computer programming to stores a collections of elements of the same data types under a single variables name These elements often referr... 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

Investments An Introduction

Authors: Herbert B Mayo

9th Edition

324561385, 978-0324561388

More Books

Students also viewed these Programming questions