Pascal's Triangle - s3.amazonaws.com

advertisement
CSC202 Midterm Part I
CSC202 Midterm Exam Part I Data Structures Program
Pascal’s Triangle
In the tenth century, mathematicians studied a triangular integer pattern which
became known as Pascal’s Triangle, named after the 17th century mathematician
Blaise Pascal. A sample is shown below in the traditional style of a triangle:
1
1
1
1
1
1
2
3
4
1
3
6
1
4
1
. . .
Pascal’s Triangle has many interesting properties:
1. Each row begins and ends with a 1.
2. Each interior entry is the sum of the two entries immediately above it. For
example the entry 6 is the sum of 3 + 3 from the row above it and thte entry
2 is the sum of 1 + 1.
In addition, if the rows and entries are numbered in each row beginning with 0, the
entry in position k of row n is denoted as C(n,k). For example, the number 6 is in
row 4 (remember to start from 0) and position 2 (starting from 0) and can be
written C(4,2). This turns out to be the number of ways you can select k items out
of n items. Thus C(4,2) is the number of ways you can select 2 items out of 4.
Consider the example of four items A, B, C, and D. The number of ways to select
two out of the four items is 6:
AB, AC, AD, BC, BD, CD
The order is unimportant. For example, AB is the same as BA.
CSC202 Midterm Exam Part I Page 1 CSC202 Midterm Part I
Instructions
1. Design and implement the class PascalTriangle.
1.1.
Represent each row in a triangle as a list and the entire triangle as a
list of these lists. Example: triangle = new
ArrayList<ArrayList<Integer>>();
1.2.
Use the class ArrayList for these lists of Integers.
1.3.
Use a default size of 12.
1.4.
Give your class constructors and at least the method getChoices(n,k)
which returns the integer value of C(n,k)
1.5.
Other possible methods you may want to consider include getNextRow,
getRow, getChoices, getEntry, …
2. Design and implement the class DemoTriangle which demonstrates the class
PascalTriangle.
2.1.
2.2.
Creates a new Pascal’s Triangle
Displays the triangle and a message indicating the size of the triangle
Submission
Submit your Midterm Part I on our website by 10/11/2014. Your submission should
be a zip file named FirstnameLastnameMidterm.zip that includes:
1. Your documented .java programs. Use comments to explain what your
program is doing as well as the normal Javadoc comments.
2. Pseudocode for your programs.
3. Class diagram for your application.
Resources on ArrayList
Oracle. Class ArrayList<E>. (2013) Retrieved from
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Java Code Geeks. Arraylist in Java Example – How to use arraylist. Retrieved from
http://examples.javacodegeeks.com/core-java/util/arraylist/arraylist-injavaexample-how-to-use-arraylist/
CSC202 Midterm Exam Part I
Page 2
Download