AP Computer Science I

advertisement
AP Computer Science I
Java Keyword Lab Assignment # 13A
The "Sieve of Eratosthenes" Program
80 & 100 Point Versions
Assignment Purpose:
The purpose of this assignment is to practice declaring one-dimensional array
objects and manipulating the elements of the array.
Write a program that computes prime numbers using the “Sieve of Eratosthenes” method. The
Sieve prime number generator uses an ingenious method, which does not involve any type of
division, by using the following steps:
[1] Initialize all numbers in the array, starting with 2, as prime numbers. Ignore number 1.
[2] Check the first number, 2, to see if it is prime. Since it is designated prime, change all the
multiples of 2 to Not Prime.
[3] Check the next number, 3, to see if it is prime. Since it is designated prime, change all the
multiple of 3 to Not Prime.
[4] Continue this process, until the upper limit is reached.
Imagine that a small upper limit of 21 is requested. The “Sieve” will work with Pr (Prime) and NP
(Not Prime) as follows:
STEP 01
xx
1
Pr
2
Initialize all elements to Prime
Pr
3
STEP 02
xx
1
Pr
2
Pr
4
Pr
5
Pr
6
Pr
7
Pr
8
Pr
9
Pr
10
Pr
11
Pr
12
Pr
13
Pr
14
Pr
15
Pr
16
Pr
17
Pr
18
Pr
19
Pr
20
Pr
21
Change all multiples of 2 to Not Prime
Pr
3
NP
4
Pr
5
NP
6
Pr
7
NP
8
Pr
9
NP Pr NP Pr NP Pr NP Pr NP Pr NP Pr
10 11 12 13 14 15 16 17 18 19 20 21
STEP 03 Change all multiples of 3 to Not Prime
xx
1
Pr
2
Pr
3
NP Pr
4
5
NP Pr
6
7
NP NP NP Pr
8
9
10 11
NP Pr
12 13
NP NP NP Pr
14 15 16 17
STEP 04 Repeat this process until the upper limit is reached
Exposure Java Chapter XIII Lab Assignments Page 1 04-28-03
NP Pr
18 19
NP NP
20 21
xx
1
Pr
2
Pr
3
NP
4
Pr
5
NP
6
Pr
7
NP NP NP Pr NP Pr NP NP NP Pr NP Pr NP NP
8
9 10 11 12 13 14 15 16 17 18 19 20 21
Prime Numbers left are: 2, 3, 5 ,7 , 11, 13, 17, 19
80 Point Version
The 80-point version displays all the prime numbers between 1 and 100. Create separate methods
for ComputePrimes and DisplayPrimes inside the Lab13a8 class. There is only a single execution
and there is no program user input at all.
Lab13a 80 Point Version
Required main Method
public static void main(String args[])
{
System.out.println("\nLAB13A 80 POINT VERSION\n");
final int MAX = 100;
boolean primes[];
primes = new boolean[MAX];
computePrimes(primes);
displayPrimes(primes);
}
Lab13a 80 Point Version
One Required Output
LAB13A 80 POINT VERSION
COMPUTING PRIME NUMBERS
PRIMES BETWEEN 1 AND 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
100 Point Version
Exposure Java Chapter XIII Lab Assignments Page 2 04-28-03
The 100-point version requires interactive input in a text window. Additionally, the 100-point version
needs to format program output so that all prime numbers are displayed as three digit numbers with
leading zeroes where necessary using "000" decimal format. Execute the program twice.
Lab13a 100 Point Version
Required main Method
public static void main(String args[]) throws IOException
{
System.out.println("\nLAB13A 100 POINT VERSION\n");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the primes upper bound ===>> ");
final int MAX = Integer.parseInt(input.readLine());
boolean primes[] = new boolean[MAX];
computePrimes(primes);
displayPrimes(primes);
}
Lab13a 100 Point Version
Two Separate Required Outputs
LAB13A 100 POINT VERSION
Enter the primes upper bound
===>>
100
COMPUTING PRIME NUMBERS
PRIMES BETWEEN 1 AND 100
002
053
003
059
005
061
007
067
011
071
013
073
017
079
019
083
023
089
029
097
031
037
041
043
047
023
089
167
251
347
433
029
097
173
257
349
439
031
101
179
263
353
443
037
103
181
269
359
449
041
107
191
271
367
457
043
109
193
277
373
461
047
113
197
281
379
463
LAB13A 100 POINT VERSION
Enter the primes upper bound
===>>
500
COMPUTING PRIME NUMBERS
PRIMES BETWEEN 1 AND 500
002
053
127
199
283
383
467
003
059
131
211
293
389
479
005
061
137
223
307
397
487
007
067
139
227
311
401
491
011
071
149
229
313
409
499
013
073
151
233
317
419
017
079
157
239
331
421
019
083
163
241
337
431
AP Computer Science I
Java Keyword Lab Assignment # 13B
Exposure Java Chapter XIII Lab Assignments Page 3 04-28-03
The "Random Matrix" Program
80 & 100 Point Versions
Assignment Purpose:
The purpose of this lab exercise is to practice defining two-dimensional arrays
objects and also to practice basic input/output operations on two-dimensional arrays.
Write a program that creates a matrix of random numbers. The two-dimensional array object needs
to be constructed with five rows and four columns. Assign random integers to each array element in
the [1..999] range. Display the array in matrix format. This lab assignment does not require separate
methods for separate tasks in the program. Everything may be processed in the main method.
80 Point Version
The 80-point version assigns the random integers and displays the numbers. The numbers need to
be displayed in three-digit format with leading zeroes where appropriate. There is only one program
execution and there is no program input.
100 Point Version
The 100-point version needs to add up all the numbers in the matrix and compute the average value
or mean of the numbers. The array elements need to be displayed in "000" decimal format and the
mean needs to be displayed in "000.000" decimal format.
Lab13b 80 Point Version
One Required Output
LAB13B 80 POINT VERSION
251
055
501
390
787
080
084
389
806
303
241
375
517
012
532
828
802
942
384
175
Lab13b 100 Point Version
One Required Output
LAB13B 100 POINT VERSION
Exposure Java Chapter XIII Lab Assignments Page 4 04-28-03
251
055
501
390
787
080
084
389
806
303
241
375
517
012
532
828
802
942
384
175
Average Random Integer:
422.700
AP Computer Science I
Java Unit Lab Assignment # 13C
Exposure Java Chapter XIII Lab Assignments Page 5 04-28-03
The "Magic Square" Program
80 & 100 Point Versions
Assignment Purpose:
It is time to do some serious two-dimensional array manipulation. The purpose of this program is
to manipulate two-dimensional arrays and to translate an “English” step-by-step algorithm into a
workable program.
Write a program that creates odd-sized Magic Squares. A magic square is a square matrix of
consecutive numbers, such that the numbers in the rows, the numbers in the columns, and the
numbers in the diagonals add up to the same sum. For this program you will only be concerned with
“odd-sized” magic squares. Odd refers to the size of the matrix, such as an 3 X 3, or an 5 X 5, or
an 9 X 9 sized matrix.
Examples of a 3 X 3 magic square and a 5 X 5 magic square are shown below:
8
3
4
1
5
9
6
7
2
Every row, column and diagonal in the 3 X 3 matrix adds up to 15.
17
23
4
10
11
24
5
6
12
18
1
7
13
19
25
8
14
20
21
2
14
16
22
3
9
Every row, column and diagonal in the 5 X 5 matrix adds up to 65.
The magic square program is quite challenging, and comes in two different levels of difficulty. It is
very important that you first are able to create magic squares, of any odd size, yourself on paper. On
the next page is a five-step algorithm to create an odd magic square. Study these steps carefully
and use them to create magic squares on paper. Only after you are confident that you understand
the magic square algorithm, are you ready to proceed to write a program.
Magic Square Algorithm:
The creation of a magic square involves using the following algorithm. It will be your assignment to
translate this algorithm into a program.
Exposure Java Chapter XIII Lab Assignments Page 6 04-28-03
[1] Start number 1 in the top row, middle column.
[2] Place consecutive integers in a diagonally-up-to-the-right pattern.
[3] Any number that goes outside the matrix above row 1 is moved to the bottom row.
[4] Any number that goes outside the matrix past the right column is moved to the left column.
[5] Any number, which follows the multiple of the matrix size, is moved down 1 row.
80 Point Version
The 80-point version enters the magic square size in a text window prompt. This should be a number
in the [3..11] integer range. It is not necessary to protect against erroneous input. Two methods are
called from the main method. First, complete CreateSquare, which places consecutive integers in
the proper matrix locations. Second, DisplaySquare, which displays the values of the matrix. The
80-point version does not require number formatting.
Important hint: Place each consecutive number, starting at 1, in the right matrix location, not the
other way around. Think what you do when you create a magic square on paper. You start by
placing number 1 in the right place, then number 2, number 3 and so on.
100 Point Version
The 100-point version adds two features not found in the 80-point version. You will note that the
matrix display of the 80-point version is not very attractive. The output numbers do not use any
formatting. Numbers for this version need to use the three digits "000" decimal format. A second
feature requires an additional method, CheckSquare, which determines if in fact the magic square is
a "magic" square. You need to traverse every row, every column and every column and display the
sum of the integers, which should all be the same.
Magic Square Sum Rule
The sum of the rows, columns and diagonals in an odd magic square will always be
the size of the square times the median number in the square.
Exposure Java Chapter XIII Lab Assignments Page 7 04-28-03
In a 3 X 3 that means 3 * 5 = 15.
In a 5 X 5 that means 5 * 13 = 65.
Lab13cst.java File
Student Provided Program
// Lab13cst.java
// This the student file for the Lab13c assignment.
import java.text.DecimalFormat;
import java.io.*;
public class Lab13cst
{
//
public static void main (String args[]) throws IOException
{
System.out.println("\nLAB13C ??? POINT VERSION\n");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter magic square size [3,5,7,9,11] ===>> ");
int size = Integer.parseInt(input.readLine());
int square[][] = new int[size][size];
createSquare(square);
displaySquare(square);
checkSquare(square); // remove comments for 100 point version
System.out.println();
}
public static void createSquare(int[][] square)
{
}
public static void displaySquare(int[][] square)
{
System.out.println("\nDISPLAY MAGIC SQUARE");
DecimalFormat two = new DecimalFormat("000");
int size = square.length;
for (int r = 0; r < size; r++)
{
for (int c = 0; c < size; c++)
System.out.print(two.format(square[r][c]) + " ");
System.out.println();
}
}
public static void checkSquare(int[][] square)
{
}
}
Lab13c 80 Point Version
Two Separate Required Outputs
LAB13C 80 POINT VERSION
Enter magic square size [3,5,7,9,11]
===>>
5
CREATE MAGIC SQUARE
DISPLAY MAGIC SQUARE
Exposure Java Chapter XIII Lab Assignments Page 8 04-28-03
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
LAB13C 80 POINT VERSION
Enter magic square size [3,5,7,9,11]
===>>
7
CREATE MAGIC SQUARE
DISPLAY MAGIC SQUARE
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
Lab13c 100 Point Version
Two Separate Required Outputs
LAB13C 100 POINT VERSION
Enter magic square size [3,5,7,9,11]
===>>
5
CREATE MAGIC SQUARE
DISPLAY MAGIC SQUARE
Exposure Java Chapter XIII Lab Assignments Page 9 04-28-03
017
023
004
010
011
024
005
006
012
018
001
007
013
019
025
008
014
020
021
002
015
016
022
003
009
CHECKING FOR VALID MAGIC SQUARE
65 65 65 65 65
65 65 65 65 65
65 65
LAB13C 100 POINT VERSION
Enter magic square size [3,5,7,9,11]
===>>
7
CREATE MAGIC SQUARE
DISPLAY MAGIC SQUARE
030 039 048 001 010
038 047 007 009 018
046 006 008 017 026
005 014 016 025 034
013 015 024 033 042
021 023 032 041 043
022 031 040 049 002
019
027
035
036
044
003
011
028
029
037
045
004
012
020
CHECKING FOR VALID MAGIC SQUARE
175 175 175 175 175 175 175
175 175 175 175 175 175 175
175 175
Exposure Java Chapter XIII Lab Assignments Page 10 04-28-03
Download