Solved

Programming tips

Posted on 2009-07-06
1
304 Views
Last Modified: 2013-11-23
There once was a wise servant who saved the life of a princess. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.

There are 64 squares on a chessboard.

Write a program that shows how many grains were on each square and the total number of grains.

In other words, I want to type

    java Grains

and see

    square 1:  1 grain
    square 2:  2 grains
    square 3:  4 grains
    square 4:  8 grains

etc.

public class Chess {
      public static void main(String[] args) {
            try{
                  int enteredNumber = Integer.parseInt(args[0]);
                  if( enteredNumber < 1 || enteredNumber > 64){
                        System.out.print("invalid numbers");
                        System.exit(0);
                  }
                  int numberOfSquares = (int)Math.pow(2, (enteredNumber - 1));
                  System.out.println(numberOfSquares);
            }catch(NumberFormatException e){
                  e.printStackTrace();
            }
      }
}
0
Comment
Question by:SunScreenCert
1 Comment
 
LVL 20

Accepted Solution

by:
a_b earned 500 total points
ID: 24791766
Run a for loop from 0 to 63 that does the calculation for you
for(int i=0;i<64;i++)
{  
       numberOfSquares = (int) Math.pow(2,i);
       sysout(numberOfSquares);
}

Right now the program is only running for a single number you need to change the dependency on the args[0].
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now