Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

Computer Science Courses

Related sites

Introductory Programming In Java

HW 2

Homework 2

Big numbers
2D Arrays, classes and API documentation

The problem

This problem was a part of the year 2007 final examination paper.

The bunch of non-negative integer numbers written in a shapeof the triangle:

is called the Euler–Bernoulli triangle and can be constructedusing the following algorithm:

  1. the triangle is constructed row by row from the top down; the rows are indexed starting from zero (like elements in an array)
  2. the zeroth (top-most) row consists of the only element 1
  3. the first row comprises 0 and 1
  4. the second row begins from the right — put 0 and move to the left; at every step, we take the sum of the preceding number in the same row and the preceding number in the row above, ie, the sum of the nearest right and upper right numbers
  5. the third row starts at the left — put 0 and move to the right; at every step, we take the sum of the nearest left and upper left numbers
  6. all rows are constructed in the same way — every even indexed row begins on the right with 0 and is built right-to-left, and every odd indexed row begins on the left with 0 and is built left-to-right

The importance of this construction lies in the fact, that all non-zero numbers on the left side of the triangle represent the so-called Euler numbers, while all non-zero numbers on the right side are the so-called Bernoulli numbers. Both are important sequences in mathematics, and the described triangle gives the simplest algorithm to calculate them.

Your task is to write a program which will calculate the first n rows of the Euler–Bernoulli triangle. You may use a two-dimensional integer array, and your program will assign its elements values according to the above algorithm. Do not write code which will print the triangle as it's done above (there are formatting problems when the numbers become multi-digit). However, using the constructed array, make your code to print all Euler and Bernoulli numbers contained in the calculated triangle...

Perhaps, this problem was a bit too complex for the exam (so I will nottry something equally daunting this time), but this is not what you haveto do in this Homework. The thing is that I had not been careful and had givenone student a code with my solution. So you can all have it,.

Download it and study. Compile and run it. It should be done as follows:

> java EulerBernoulliTriangle 21

where the command line argument, 21 in the above example, must be an odd positivenumber greater than 2. The output will look as follows:

> java EulerBernoulliTriangle 21 The height of the triangle is 21The Euler numbers:115611385505212702765199360981193915121452404879675441That's 10 Euler numbersThe Bernoulli numbers:112162727936353792223682561903757312209865342976That's 10 Bernoulli numbers

But attempts to calculate larger Euler and Bernoulli (EB) numbers soon runinto a problem: if you use the value of command line argument 27 and higher,you will see that the EB stop growing and even become negative (which shouldn'thappen — check the algorithm for their calculation). This is theconsequence of the finite range of long primitive type which I usedin my program.

Your task is to modify the above program so it can calculate the EB numbersof arbitrary order. You should replace the long type onto a differentone (which?!) and make appropriate changes in other places of the code.

Test the correctness of your output by comparing it with the Euler numbersfrom the picture:

Hint: look up the Java API docs, the package java.math.

Assessment

You will get up to two marks, if you present a solution to the Homeworkexercise during the next week lab.

HW 2

Updated:  Sun 12 Jun 2016 17:27:37 AEST Responsible Officer:   JavaScript must be enabled to display this email address. Page Contact:  

+61 2 6125 5111
The Australian National University, Canberra
CRICOS Provider : 00120C       ABN : 52 234 063 906