Solved

Programming tips

Posted on 2009-07-06
1
306 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 56
Cipher Configuration on Apache HTTPD 4 62
fibonacci ten numbers 4 38
String array comparison 4 32
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)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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