Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java, modify the existing code. I t i s three classes. 1 . Replace the ArrayList in your assignment with a LinkedList from the

Using Java, modify the existing code. Itis three classes.
1. Replace the ArrayList in your assignment with a LinkedList from the Java collections. Do NOT reimplement the linked list like you did in CodeStepByStep, just use the one provided. The algorithm that you implement for your program, will be exactly the same, only the list implementation will change.
2. Compare the timing for using an ArrayList with the same timing for using a LinkedList. Try to explain what you see and make any recommendations. Also, consider your proposed algorithm from Unit 1 that rearranges Contestants in the same list. Based on your timing estimates, explain whether you think it would take more or less time using a LinkedList rather than an ArrayList.
Contestant Class:
import java.util.ArrayList;
import java.util.List;
public class Contestant extends Person {
private int currentPosition;
private List positionHistory;
// A list of integers that records the history of all positions the contestant has been in.
public Contestant(String name){
super(name);
this.positionHistory = new ArrayList<>();
}
public int getCurrentPosition(){
return currentPosition;
}
//Returns the current position of the contestant.
public void setCurrentPosition(int position){
this.currentPosition = position;
this.positionHistory.add(position);
}
// Sets the current position to the given position and adds this position to the positionHistory list.
public double getAveragePosition(){
//Calculates and returns the average of all positions stored in
int sum =0;
for (int pos : positionHistory){
sum += pos;
}
return (double) sum / positionHistory.size();
// It sums up all the positions and divides the sum by the number of positions in the list.
}
}
ContestDriver Class:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ContestSimulation {
private static final Random random = new Random();
public static void main(String[] args){
int numberOfContestants =10;
int numberOfRounds =12;
//Initializes a list of contestants.
List contestants = new ArrayList<>();
for (int i =0; i < numberOfContestants; i++){
contestants.add(new Contestant("Contestant "+(char)('A'+ i)));
}
for (int i =0; i < contestants.size(); i++){
contestants.get(i).setCurrentPosition(i);
}
//Runs a specified number of rounds.
for (int round =0; round < numberOfRounds; round++){
simulateRound(contestants);
}
printAveragePositions(contestants);
}
//Each round simulates contestants either answering correctly or incorrectly and updates their positions accordingly.
public static void simulateRound(List contestants){
for (int i =0; i < contestants.size(); i++){
Contestant contestant = contestants.get(i);
boolean answeredCorrectly = random.nextBoolean();
if (answeredCorrectly){
contestants.remove(i);
contestants.add(0, contestant);
} else {
contestants.remove(i);
contestants.add(contestant);
}
}
for (int i =0; i < contestants.size(); i++){
contestants.get(i).setCurrentPosition(i);
}
//After all rounds, it prints the average position of each contestant.
printContestants(contestants);
}
public static void printContestants(List contestants){
System.out.println("Current positions:");
for (Contestant contestant : contestants){
System.out.println(contestant.getName()+" at position "+ contestant.getCurrentPosition());
}
System.out.println();
}
public static void printAveragePositions(List contestants){
System.out.println("Average positions:");
for (Contestant contestant : contestants){
System.out.println(contestant.getName()+" average position "+ contestant.getAveragePosition());
}
}
}
Person Class:
public class Person {
private String name;
public Person(String name){
this.name = name;
}
// Creates a Person object with a specific name.
public String getName(){
return name;
}
// Allows external code to access the value of the private name field. It returns the current value of name.
public void setName(String name){
this.name = name;
//Allows external code to modify the value of the private name field. It takes a String argument and assigns it to the name field.
}
}

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

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions