Question 1    This question explores the String and Random...

Question

# Question 1    This question explores the String and Random...

Question 1

This question explores the String and Random classes and allows you to practice using switch statements (and if you choose, also the conditional operator). You should be able to write the code for this question once you have completed Section 6.5 in B&K, although you will need to read 6.11 in B&K in order to document it using Javadoc.

As you answer the question parts you should provide multiline comments for your class, constructor, and methods. Make sure that your comments explain the purpose of the code and identify which part of the TMA question you are answering in each case.

For example, if you were answering part (d)(ii) of a question, and this involved writing a setter for a field onLoan, a suitable comment would be

/**
* (d)(ii) Set the onLoan field to the received value
*/

Scenario

You will explore a classic problem in probability.

In a meeting of a given size, what is the probability that two people will share the same birthday?

In a simulation, you will create many meetings of a given size and assign random birthdays to the attendees. You will then determine the probability that two attendees have the same birthday in a meeting of that size.

In particular, you are asked to find the size of the meeting where, on average, the probability of two shared birthdays first exceeds 0.5 (50%).

a.  Launch BlueJ, open the project TMA02Q1 in the TMA02Download folder you unzipped earlier and then immediately save the project as TMA02Q1SolXX, where XX should be replaced by your own initials, in the TMA02Download folder.

Begin by adding a new class called BirthdaySimulation to the provided project.

Retain the class comment but edit it to include your own name, the date and a description of the class BirthdaySimulation. Remove the sample code.

Import the java.util.ArrayList and the java.util.Random classes.

b. Declare the field bdays, which will reference an ArrayList of String objects representing the birthdays of the attendees at a meeting.

For testing purposes, make the field public.

Now add a zero-parameter constructor. The constructor should initialise bdays to a new empty instance of ArrayList.

c. Write a method with the header:

public String ranMonth()

This method should return a string that is the name of a randomly generated month. The method should generate a random integer between 0 and 11 (inclusive) and use this to return the name of the corresponding month, e.g., "January" if the integer is 0, "February" if the integer is 1, etc.

d.Write a method with the header:

public int ranDateInMonth(String aMonth)

This method should return a random day in aMonth. As 29 February occurs 4 times less frequently than any other day in that month, if the argument is "February" the method should generate a random number between 0 and 3 (inclusive) and return a random number between 1 and 29 if the random number is 0, and between 1 and 28 otherwise.

e.Write a method with the header:

public void createMeeting(int numAttendees)

This method should first invoke clear on bdays and then add to bdays the number of random birthdays given by numAttendees. Each random birthday is a string in the form of a day and a month, e.g., "4 July", where the month is generated using the ranMonth method and the day is generated using the ranDateInMonth method.

f. Write a method with the header:

public boolean foundMatch()

The method returns true if there is at least one repeated birthday in bdays or false otherwise.

Hint: One way of checking this is to use an outer loop to get each birthday from the bdays list in turn and an inner loop that then looks through the rest of the list to see if there is a match.

g.Write a method with the header:

public double runSimulation(int numAttendees)

The method requires an int local variable called count to count how many meetings with numAttendees people include at least one pair of people with a matching birthday.

The method should first initialise bdays to an empty ArrayList.

Then it should do the following 10,000 times:

• create a meeting with numAttendees attendees,
• if the meeting includes at least one matching birthday, increment the count variable,
• reinitialise bdays to an empty ArrayList.
• (count / 10000.0) * 100

H.Create an object of class BirthdaySimulation in BlueJ on the Object Bench.

Open the Terminal window and make sure that 'Clear screen at method call' is unticked in the Options menu.

Starting with your best guess, use the object you created to call the runSimulation method using different numbers of attendees until you discover the minimum number of attendees for there to be a 50% chance or more of having at least one pair of matching birthdays.

Take a screenshot of the Terminal window showing your tests and their results and paste it into your Solution Document.

Hint: The number you are looking for must be between 2 and 367 (if there are fewer than 2 people, there cannot be a matching birthday and if there are 367 people there is guaranteed to be a match). So, start with your best guess and then raise it if it gives a probability less than 50% and lower it if it gives a probability of more than 50%, and continue until you narrow it down to a single number of attendees.

i. Go back and check that you have provided Javadoc comments for your class, constructors and methods, including the @author, @version, @param and @return annotations where appropriate. Be sure that your comments explain the purpose of the code and identify which part of the TMA question you are answering in each case.

Also check that you have used a consistent style across the whole class. See the M250 Code Conventions and Design document for more advice on coding style and check that you reused methods in this question where possible rather than duplicating code.

j. Follow the link below to test your code. Take a screenshot of the Got column and ticks from the online testing system and paste it into your Solution Document. (Or copy all the text and paste it into your document as a table.)

Include a screenshot even if your code did not compile, or if it compiled and ran but did not produce correct results.

If you had errors in your code and were unable to fix them, explain briefly to your tutor how you tried to solve the problem. This will help them provide the most appropriate feedback.

Solved by verified expert

sec

sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus

sect

10,000 step-by-step explanations

## Step-by-step explanation

sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis.

sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec

sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.