Question

Mailboxes Java Peter the postman was bored one evening and he decided to experiment with the...

Mailboxes Java

Peter the postman was bored one evening and he decided to experiment with the mailboxes in the mailroom. The mailboxes were numbered from zero to 200 (there were 201 total mailboxes). Initially all boxes were closed. Starting with mailbox #2, he opened the door of every even numbered mailbox. Next, beginning with mailbox #3 and going to every third mailbox, he opened its door if it was closed and closed it if it was open. Then he repeated this procedure with every fourth mailbox starting with mailbox #4; and then every fifth mailbox starting with mailbox #5; continuing in this manner all the way up to the changing every 200th mailbox starting with mailbox #200 (which of course just changes mailbox #200).

Your job is to write a program to determine which mailboxes were closed at the end of the experiment.The program should ask the user for how many mailboxes are in the mailroom, and then perform the desired experiment for that many mailboxes. This will allow us to run the experiment for a small mailroom with only 201 mailboxes, or a large mailroom with thousands of mailboxes.

Notes:

  1. Simply create an array of integers of the desired size. Each mailbox will be represented by the array element whose index matches the mailbox number; i.e., mailbox #0 will be represented by the array element at index 0. After creating an array of the needed size, be sure to initialize the array so that all mailboxes start (or are filled) in the "closed" position.
  2. Let the integer value 0 represent a closed mailbox and the integer value 1 represent an open mailbox. Alternatively, you could use an array of booleans and let the values true and false represent open and closed.
  3. You will need a double-nested loop (a loop inside a loop) to conduct the experiment.
  4. Print the numbers of the boxes that are in the closed position, starting at mailbox #0. Print out the distance between the closed mailboxes. See the example execution below for the format of the output.
  5. You should write separate methods to tackle the different sections of this problem a) create and initialize the array, b) the actual experiment, and c) printing of the results.

Example execution (user input is 5)

This is an example execution to show you how Mailboxes.java should behave, it is run for only 5 mailboxes

How many mailbox are in the mailroom? 5

When done, here are all the mailboxes that are closed:

Box #0; distance from previous box: 0

Box #1; distance from previous box: 1

Box #4; distance from previous box: 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Mailboxes.java

import java.util.Arrays;
import java.util.Scanner;

public class Mailboxes {
  
private static final int OPEN = 1;
private static final int CLOSED = 0;
  
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.print("How many mailbox are in the mailroom? ");
int nMailbox = Integer.parseInt(sc.nextLine().trim());
int mailboxes[] = new int[nMailbox];
Arrays.fill(mailboxes, CLOSED);
  
// open every even numbered mailbox starting from mailbox 2
for(int i = 2; i <= nMailbox; i++)
{
if(i % 2 == 0)
mailboxes[i] = OPEN;
}
  
// start from 3rd mailbox and change every 3rd box
// repeat the above process for all the boxes
for(int i = 3; i <= nMailbox; i++)
{
int j = i;
while(j < nMailbox)
{
if(mailboxes[j] == OPEN)
{
mailboxes[j] = CLOSED;
}
else if(mailboxes[j] == CLOSED)
{
mailboxes[j] = OPEN;
}
j += i;
}
}
  
int previous = 0;
for(int i = 0; i < nMailbox; i++)
{
if(mailboxes[i] == CLOSED)
{
System.out.println("Box #" + i + "; distance from previous box: " + (i - previous));
previous = i;
}
}
}
}

****************************************************************** SCREENSHOT *********************************************************

Add a comment
Know the answer?
Add Answer to:
Mailboxes Java Peter the postman was bored one evening and he decided to experiment with the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Program 7 Arrays: building and sorting (100 points) Due: Friday, October 30 by 11:59 PM Overview...

    Program 7 Arrays: building and sorting (100 points) Due: Friday, October 30 by 11:59 PM Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

  • 1.2 Recruitment is one of the crucial functions of HRM. Based on the information provided below,...

    1.2 Recruitment is one of the crucial functions of HRM. Based on the information provided below, how would you describe THE COMPANY’s approach to recruitment, before and after the implementation of the Brand Ambassador Program? How did the use of social media lead to the revision of the whole approach regarding recruitment? How ‘THE COMPANY’ Developed a Brand Ambassador Program At ‘THE COMPANY’ we usually categorize Employment Brand at ‘THE COMPANY’ into four big ‘buckets’: candidate experience, brand ambassador programs,...

  • Assignment 2 In this assignment, you will write two short programs to solve problems using recursion....

    Assignment 2 In this assignment, you will write two short programs to solve problems using recursion. 1. Initial Setup Log in to Unix. Run the setup script for Assignment 2 by typing: setup 2 2. Towers of Hanoi Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter T...

    I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter Two, “Keys to Successful IT Governance,” from Roger Kroft and Guy Scalzi’s book entitled, IT Governance in Hospitals and Health Systems, please refer to the following assignment instructions below. This chapter consists of interviews with executives identifying mistakes that are made when governing healthcare information technology (IT). The chapter is broken down into subheadings listing areas of importance to understand...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT