Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: Java exercise

Newbie
 
Join Date: Oct 2006
Posts: 4
#1: Oct 29 '06
Hello mates,
Just needed a solution on the exercises below.


Exercise 2

A classic problem in introductory programming is the grains of rice on a chess board problem.
Your program should calculate the number of rice grains on the last square of a 64 square chess board given that there is 1 grain of rice on the first square, 2 on the second square, 4 on the third square and so on.
Use a while loop to double the number of rice grains and output the number of grains on the last square. Additionally output the total number of grains.
Check your answer, is it sensible? If not amend your data types.

Exercise 3

Amend program 2 above so that it is a do – while loop. Test to make sure that the same number of rice grains is on the last square and that the total is the same.

Exercise 4

The Fibonacci series is another classic computing problem.
The series is formed by adding two numbers in the series to generate the third. Note the first two are 0 and 1, so to make the make number we add 0 + 1 to give 1. The fourth number is obtained by adding the second and third numbers, 1 + 1 gives 2. So the series looks like:

0 1 1 2 3 5 8 13 21………

Use a loop to output the first 20 Fibonnaci numbers. Set a counter and output a calculation of number of terms in series.

Exercise 5

Write a program which outputs a series of asterisks (* - stars) to form the following pattern below.
Note this requires you to place one loop inside another loop. An example outline would look like this:

while (test condition1)
{
………;
while (test condition2)
{
……….;
}
……….;
}

The output should look like: (this is a short version, you should have 10 lines of stars)

*
**
***
****
*****
*****

Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Oct 29 '06

re: Re: Java exercise


Have you actually tried solving them yourself?

I'm willing to help you figure these out, but only if you're willing to give it a go first.
Newbie
 
Join Date: Oct 2006
Posts: 4
#3: Oct 30 '06

re: Re: Java exercise


Yes i have been trying something and you can view that below.

*/
public class loopEx2
{
public static void main(String[] args)
{
int x=0;
while (x=x+x)
int xint = EasyIn.getint("Please enter first number");
{
x++;
System.out.println(x);
System.out.println("x is =" + x);
System.exit(0);
{
}
{

}
}
----------------------------------------------------------------------------------------------------------------

*/
public class loopEx3
{
public static void main(String[] args)
{
int x=0;
do

int xint = EasyIn.getint("Please enter number");
{
x++;
System.out.println(x);
while (x=x+x)
System.out.println("Total number is =" + x);
System.exit(0);
{
}
{

}
}
-----------------------------------------------------------------------------------------------------------------

//* Student Name: Ebrima Gillen
public class loopEx4
{

{
public static void main(String[] args)
{

int x = 0;
while (x++ < 20)
{
System.out.println(x);
}
System.out.println("x is = " + x);
}
}
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Oct 30 '06

re: Re: Java exercise


Quote:

Originally Posted by ebrimagillen

Yes i have been trying something and you can view that below.

*/
public class loopEx2
{
public static void main(String[] args)
{
int x=0;
while (x=x+x)
int xint = EasyIn.getint("Please enter first number");
{
x++;
System.out.println(x);
System.out.println("x is =" + x);
System.exit(0);
{
}
{

}
}
----------------------------------------------------------------------------------------------------------------

*/
public class loopEx3
{
public static void main(String[] args)
{
int x=0;
do

int xint = EasyIn.getint("Please enter number");
{
x++;
System.out.println(x);
while (x=x+x)
System.out.println("Total number is =" + x);
System.exit(0);
{
}
{

}
}
-----------------------------------------------------------------------------------------------------------------

//* Student Name: Ebrima Gillen
public class loopEx4
{

{
public static void main(String[] args)
{

int x = 0;
while (x++ < 20)
{
System.out.println(x);
}
System.out.println("x is = " + x);
}
}

One problem at a time will do please. Now for that rice, notice that this is a gp. The value is therefore 2^63 (9,223,372,036,854,775,808). Now the longest long value(forget the int) in java is 9,223,372,036,854,775,807. So you are off by one with the long. You therefore need something else, double, BigInteger, etc. I would recommend BigInteger as follows.
Expand|Select|Wrap|Line Numbers
  1. import java.math.BigInteger;
  2. public class Rice {
  3.     public static void main (String []args) {
  4.         int n = 1;
  5.         BigInteger sum = new BigInteger(""+1);
  6.         while(n < 64) {
  7.             sum = sum.add(sum);
  8.             n++;
  9.         }
  10.         System.out.println(sum);
  11.         System.out.println(Math.pow(2, 63));//As a check 
  12.     }
  13. }
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Oct 30 '06

re: Re: Java exercise


Quote:

Originally Posted by r035198x

One problem at a time will do please. Now for that rice, notice that this is a gp. The value is therefore 2^63 (9,223,372,036,854,775,808). Now the longest long value(forget the int) in java is 9,223,372,036,854,775,807. So you are off by one with the long. You therefore need something else, double, BigInteger, etc. I would recommend BigInteger as follows.

Expand|Select|Wrap|Line Numbers
  1. import java.math.BigInteger;
  2. public class Rice {
  3.     public static void main (String []args) {
  4.         int n = 1;
  5.         BigInteger sum = new BigInteger(""+1);
  6.         while(n < 64) {
  7.             sum = sum.add(sum);
  8.             n++;
  9.         }
  10.         System.out.println(sum);
  11.         System.out.println(Math.pow(2, 63));//As a check 
  12.     }
  13. }

Just realising now that I left out the 64th square.
Expand|Select|Wrap|Line Numbers
  1. import java.math.BigInteger;
  2. public class Rice {
  3.     public static void main (String []args) {
  4.         int n = 1;
  5.         BigInteger sum = new BigInteger(""+1);
  6.         while(n <= 64) {
  7.             sum = sum.add(sum);
  8.             n++;
  9.         }
  10.         System.out.println(sum);
  11.         System.out.println(Math.pow(2, 64));//As a check 
  12.     }
  13. }
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#6: Oct 30 '06

re: Re: Java exercise


Quote:

Originally Posted by ebrimagillen

Exercise 4

The Fibonacci series is another classic computing problem.
The series is formed by adding two numbers in the series to generate the third. Note the first two are 0 and 1, so to make the make number we add 0 + 1 to give 1. The fourth number is obtained by adding the second and third numbers, 1 + 1 gives 2. So the series looks like:

0 1 1 2 3 5 8 13 21………

Use a loop to output the first 20 Fibonnaci numbers. Set a counter and output a calculation of number of terms in series.

Expand|Select|Wrap|Line Numbers
  1. //* Student Name: Ebrima Gillen 
  2. public class loopEx4
  3. {
  4.  
  5. {
  6.     public static void main(String[] args) 
  7.     {
  8.  
  9.         int x = 0;
  10.         while (x++ < 20) 
  11.         {
  12.             System.out.println(x);
  13.         }
  14.         System.out.println("x is = " + x);
  15.     }
  16. }

Try using an array of size 20 to implemet this. Assign 0 to array[0], 1 to array[1], and use a for loop to calculate the remaining values. Finally, print the entire array.
Reply


Similar Java bytes