473,714 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Java exercise

4 New Member
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)

*
**
***
****
*****
*****
Oct 29 '06 #1
5 3034
Ganon11
3,652 Recognized Expert Specialist
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.
Oct 29 '06 #2
ebrimagillen
4 New Member
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.prin tln(x);
System.out.prin tln("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.prin tln(x);
while (x=x+x)
System.out.prin tln("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.prin tln(x);
}
System.out.prin tln("x is = " + x);
}
}
Oct 30 '06 #3
r035198x
13,262 MVP
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.prin tln(x);
System.out.prin tln("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.prin tln(x);
while (x=x+x)
System.out.prin tln("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.prin tln(x);
}
System.out.prin tln("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,8 54,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. }
Oct 30 '06 #4
r035198x
13,262 MVP
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,8 54,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. }
Oct 30 '06 #5
Ganon11
3,652 Recognized Expert Specialist
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.
Oct 30 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
10843
by: Fu Bo Xia | last post by:
the java.lang.Object.forName method takes a java class name and returns a Class object associated with that class. eg. Class myClass = Object.forName("java.lang.String"); by if i only know the absolute file name of a .class file eg. C:\myJava\myApp.java, then how do i translate this file name to a java class name the Object.forName method would accept has it's parameter? thanks,
54
17410
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I give interviewees seems to trip them up all the time, and I wonder if I'm being too much of a hardass... it seems easy enough to ME, but these guys, when I get them up to the whiteboard, seem to get really confused. The exercise is this:
0
1206
manuleka
by: manuleka | last post by:
hi, got this Java exercise package and don't know where to start! could anyone help... a little explaining will be helpfull... not really familiar with Java Exercise Java Package (ziped): http://skux.cokenut.net/cods/Exercise_RentalShop.zip Exersise Spec: http://skux.cokenut.net/cods/RentalShop_Spec.doc
3
9464
by: Dave128 | last post by:
Hi, I have an exercise I need to do for Java. I am not sure how to start this program. Can somebody help me get started on this problem? Thanks in advance for your help. The problem is as follows: Develop a Java application that will determine whether any of several department-store customers has exceeded the credit limit on a charge account. For each customer, the following facts are available: a) account number b)...
15
2796
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs. For example: f(3) f(3, )
2
3980
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: D:\original\CaseStudy-2-5\CaseStudy\Day02\exercise>asant database Buildfile: build.xml env-user: prop-user: set-user:
1
4818
by: alternative49e | last post by:
Hello. I'm trying to figure out an exercise. I started working on it but I kind of hit a wall. I'm not very good at the programming thing and the book I have is not very helpful. I could use some help or suggestions. I don’t know if what I have started so far is even the correct way to do this. Here is exercise. Create a class called CarRental that contains fields that hold a renter's name, zip code, size of the car rented, daily rental fee,...
1
3705
by: gypsyman58 | last post by:
First I need to create 2 classes. The first will contain an ID number and an array of 5 course titles. I also have to create a get and a set method for populating the ID. I also must create a method to get a course. It does this by taking an integer and returning the course to that position (0 through 4). Then I must create a set method that sets the value of one of the courses by taking two arguements a name and an integer and returning the...
2
7377
by: cioccolatina | last post by:
Hey guys, is there anyone who could help me..? I have file ExpressionBinaryTree.java : /** class ExpressionBinaryTree * uses a binary tree to represent binary expressions * does not implement BinaryTree - all iterators return String */ package foundations;
0
8796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8704
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9009
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.