Java question!
Ask the user for the number of boxes to use (N). These boxes will be represented in our program as an array. Each box has only two states – open or closed. So make this an array of Boolean, true = closed, false = open. Start with all boxes closed. Begin with box #2 (remember this is index 1 in the list) and open it and every 2nd box. Now starting with box 3, and continuing every 3rd box we will open it if it is closed and close it if it is opened. Go to fourth and reverse the status of every fourth box the same way. Continue opening and closing boxes every 5th, every 6th, etc… until we get to where we are starting at box N. Report all the boxes that are closed after we finish our procedure, do not report the open ones. You should see a recognizable pattern with the few that remain closed.
The printed sequence should be: 1 4 9 16 25 36 49……
The executable code for the given program is:
import java.util.Scanner;
class sequence
{
public static void main(String args[])
{
System.out.print("Enter
number of boxes:");/*entering no of boxes*/
int N,i,j,k;
Scanner sc=new
Scanner(System.in);/*creating object for Scanner class for taking
input from user*/
N=sc.nextInt();/*entering no of boxes*/
boolean arr[];
/*declearing array as boolean to store true(box is closed) and
false (box is opened)values */
arr = new
boolean[N];
for(i=0;i<N;i++)
{
arr[i]=true;/*initializing array with all true values.means,all
boxes are closed initially*/
}
for(i=0;i<N;i++)
{
k=i+1;/*we need to start from the second block*/
for(j=0;j<=N;j++)
{
if(j==k && k<N)
{
/*swapping values.if true is present,we will change it as false.if
false is present,we will change that as true*/
if(arr[k]==true)
{
arr[k]=false;
}
else
{
arr[k]=true;
}
k=k+i+2;/*this step is the crutial step.if i = 1 we have to
increment 2 to the previous value.if i=2 we have to increment 3 to
the previous values
and so on.*/
}
}
}
System.out.println("The
Closed boxes are:");
for(i=0;i<N;i++)
{
if(arr[i]==true)
{
System.out.println(i+1);/*printing the indices of the boxes that
are closed.Here i+1 represents blocks are starting with 1 not
0.*/
}
}
}
}
program alignment in screenshots :

continution.

OUTPUT for the given CODE is:

Java question! Ask the user for the number of boxes to use (N). These boxes will...
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...
CSCI 111 Assignment 4 Aim: To write some programs using loops, arrays and library functions. Procedure: Part A: Consider an apartment block which has 40 units. The letterboxes for the 40 units are lined up in sequence outside the building, and are numbered 1 to 40. We're going to play with the boxes. Start with all the boxes closed. Next, starting with box 2, open every alternate box (that is, 2, 4, 6, ...). Next, starting with box 3, change...
Create a Java Program that calculates payroll for N number of workers in a company Using Arrays and Methods Pass Elements and Pass arrays Array length = size of the array. Input :- First Name, MI, Last Name, Id, Hours Worked, Rate(1st method ) Constants :- State Tax, Federal Tax, Union Fees(2nd Method) Calculate The following using different methods Gross income(3rd Method) State Tax(4th Method) Federal Tax(5th Method) Union Fees(6th Method) Net Income(7th Method) Out Put (Formatted)(8th Method) (FirstName[i]...
In this problem, you will complete several classes involving boxes, locks, keys and secrets. Follow the specifications in the starter files for the classes. You can run the javadoc program to generate the API (see Tutorial 2) for the classes. A box has a secret inside it (and maybe a key!) and can be locked. In order to open a box, you need the right key to unlock the lock. Secrets.java will be a program that takes an array of...
USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...
Java question for two classes: Given the upper limit n as a parameter, the result of both methods is a boolean[] array of n elements that reveals the answers to that problem for all natural numbers below n in one swoop. public static boolean[] sumOfTwoDistinctSquares(int n) Determines which natural numbers can be expressed in the form a 2 + b 2 so that a and b are two distinct positive integers. In the boolean array returned as result, the i...
**** ITS MULTI-PART QUESTION. PLEASE MAKE SURE TO ANSWER THEM ALL. SOLVE IT BY JAVA. **** *** ALSO MAKE SURE IT PASS THE TESTER FILE PLEASE*** Often when we are running a program, it will have a number of configuration options which tweak its behavior while it's running. Allow text completion? Collect anonymous usage data? These are all options our programs may use. We can store these options in an array and pass it to the program. In its simplest...
Problem B: Clean up your potty mouth: blogging software Online blogs and other written content generation are very popular. However, as we try different ways to allow anybody on the internet to create and share written content, we run into a problem. A lot of people on the internet have foul potty-mouths and write lots of bad words. The common solution to this is to perform some form of automatic content filtering to either remove or obscure inappropriate text on...
Assignment 5 will be way different. It will be more like what
you will receive in a programming shop. By that I mean that some
things are built for you and others you will need to create or
finish. P.S. part of your grade will include: Did you add in the
required files? Did you rename your project? does your linear
searches work? Did you put your code in the correct modules? did
you change modules that you weren't supposed...
Use the example Graph.javaPreview the document class and
Edge.javaPreview the document class as a starting point for this
assignment, you'll need to make your own main class to create an
instance of the Graph class.
For this assignment we are going to create a map of a city
subway system using a Graph data structure. The Metro Station Map
that you'll use for this assignment is here: Assignment 9 - Metro
Station Map.PNG
Enter all the information from the Metro...