Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a. Launch BlueJ, open the project TMAO2Q1 in the TMAO2Download folder you unzipped earlier and then immediately save the project as TMAO2Q1SolXX, where XX should

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

a. Launch BlueJ, open the project TMAO2Q1 in the TMAO2Download folder you unzipped earlier and then immediately save the project as TMAO2Q1SolXX, where XX should be replaced by your own initials, in the TMAO2Download 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. (5 marks) 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 ne empty instance of ArrayList. 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. (5 marks) 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. (10 marks) 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 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. 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. The final value of count should tell how many of the 10,000 meetings had at least one matching birthday. Now use the expression: (count/10000.0)100 to estimate the percentage of the time that a meeting of the given number of attendees will have at least one matching pair of birthdays. This value should be returned by the method. . 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. (5 marks) 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

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

Step: 3

blur-text-image

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

Oracle 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions