Question

Java question! Ask the user for the number of boxes to use (N). These boxes will...

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……

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

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:

Add a comment
Know the answer?
Add Answer to:
Java question! Ask the user for the number of boxes to use (N). These boxes will...
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
  • CSCI 111 Assignment 4 Aim: To write some programs using loops, arrays and library functions. Procedure:...

    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...

    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...

    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...

  • Java question for two classes: Given the upper limit n as a parameter, the result of...

    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. ****...

    **** 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...

    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...

    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...

    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...

  • Project 4: Month-end Sales Report with array and validation Input dialog box for initial user prompt...

    Project 4: Month-end Sales Report with array and validation Input dialog box for initial user prompt with good data and after invalid data Input OK Cancel Input dialog boxes with sample input for Property 1 Ingut X Input Enter the address for Property 1 Enter the value of Property 1: OK Cancel Input dialog boxes after invalid data entered for Property 1 Error Please enter anmumber greater than D Ester the walue of Peoperty t Console output END SALES REPORT...

  • Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods...

    Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods Program Specification: You are to develop a program to play a variation on a game of chance called single player Poker. Game Description: • There is a Player who starts with 100 chips, each worth $1.00. • There is a deck of 36 cards: – Each card has a number in the range of [1, 9] printed on it - 9 possible values /...

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
ADVERTISEMENT