CS303 Chapter 3 Homework

advertisement
Note for Doing CS303 Homework
1. Your programs should be error-free and well-documented.
2. For the class with the main method, you have to add the program title comments as follows:
/**
*
*
*
*
*
*/
CS303 Lab #: Title of the program
Name: FirstName LastName
Date:
Purpose of the program:
How to play the game (for game programs only):
3. For other programs in the same homework, you must add comments to fields, constructors, methods,
parameters and key statements.
CS303 Appendix A Homework (Review of Java)
Due: 1/31/2013 Name: ___________________ Signature: ___________________
1. Click the DrJava icon to open DrJava window. Key in the Java program in Figure A-1
(the Hello program) on page 520 to the Interactions Pane.
1.1 Change “Hello” in line 2 to “A1hello”.
1.2 Change “Hello, world!” in line 6 to “Hello, your name!”, such as “Hello, Barack
Obama!”.
1.3 Save this program as “A1hello.java” in your own memory stick.
1.4 Click the “Compile” icon on the top menu bar to compile this program.
1.5 Click the “Run” icon on the top menu bar to execute this program.
1.6 Run this program in the Interactions Pane again as below.
> java A1hello
1.7 Hand in screenshot of this program and its execution results.
1.8 What does “public” mean in line 5 of the program?
2. Key in the Java program in Figure A-2 on page 522 to the Definitions Pane.
2.1 Key in “public class A2hello {“ and a “}” as the first and last lines of the program.
2.2 Change “Bob” in line 4 to “your name”.
2.3 Save this program as “A2hello.java” in your own memory stick.
2.4 Click the “Compile” icon on the top menu bar to compile this program.
2.5 Click the “Run” icon on the top menu bar to execute this program.
2.6 Run this program in the Interactions Pane again as below.
> java A2hello
2.7 Hand in screenshot of this program and its execution results.
2.8 What “static” means in this program?
2.9 What does “void” mean in this program?
3. Key in the Java program in Figure A-5 on page 524 to the Definitions Pane.
3.1 Key in “public class A5hello {“ and a “}” as the first and last lines of the program.
1
3.2 Save this program as “A5hello.java” in your own memory stick.
3.3 Click the “Compile” icon on the top menu bar to compile this program.
3.4 Run this program in the Interactions Pane again as below.
> java A5hello Alice Billy Cathy David Erica Frank Grace Henry
3.5 Hand in screenshot of this program and its execution results.
3.6 What does “String[] args” mean in this program?
4. Replace the “while” loop in A5hello.java with the “for” loop in Figure A-8 on page 525.
4.1 Save the program as “A8hello.java” and compile this program.
4.2 Run this program in the Interactions Pane again as below.
> java A8hello Irene Jimmy Kenny Lenny Marie Nancy Obama
4.3 Hand in screenshot of this program and its execution results.
4.4 What’s the value of “args.length” after you key in the “> java A8hello ….” statement in
step 4.2? Why?
5. Replace the “for” loop in A8hello.java with the “for” loop in Figure A-9 on page 525.
5.1 Save the program as “A9hello.java” and compile this program.
5.2 Run this program in the Interactions Pane again as below.
> java A9hello Peter Queen Robert Steve Tammy Udora
5.3 Hand in screenshot of this program and its execution results.
5.4 What does “String name : args” mean in the “for” loop in this program?
6. Replace the “for” loop in A9hello.java with the “if ... do …” loop in Figure A-10 on page 527.
6.1 Save the program as “A10hello.java” and compile this program.
6.2 Run this program in the Interactions Pane again as below.
> java A10hello Victor Wendy Xerox Yvonne Zebra
6.3 Hand in screenshot of this program and its execution results.
6.4 What are the advantages or disadvantages of this program over programs A-5 and A-8?
7. Key in the Java program in Figure A-11 (the Guess program) on page 528.
7.1 Save this program as “A11guess.java” in your own memory stick.
7.2 Compile and run this program.
7.3 Hand in screenshot of this program and its execution results.
7.4 What did you learn in Java programming from this program?
8. Replace the “if … else if … else…” statements in Figure A-11 with the “switch … case …”
statement in Figure A-13 on page 529.
8.1 Save this program as “A13guess.java” in your own memory stick.
8.2 Compile and run this program.
8.3 Hand in screenshot of this program and its execution results.
8.4 What are the advantages of this program over Figure A-11 if any?
9. Key in the Java program in Figure A-14 (the Guess program) on page 530.
9.1 Change “return min;” on line 6 to “return (min + max)/2;” as in Figure A-15.
9.2 Save this program as “A14guess.java” in your own memory stick.
9.3 Compile and run this program.
9.4 Hand in screenshot of this program and its execution results.
9.5 What are the advantages of this program over Figure A-13 if any?
9.6 What did you learn in Java programming from this program?
10. Key in the Java program in Figure A-17 (the Circle program) on page 533.
2
10.1 Save this program as “A17circle.java” in your own memory stick.
10.2 Compile and run this program.
10.3 Hand in screenshot of this program and its execution results.
10.4 What does “final” means on line 5 in this program?
10.5 What did you learn in Java programming from this program?
CS303 Chapter 1 Homework (Encapsulation)
Due: 2/7/2013 Name: ________________ Signature: ________________ Score: _____
1. Study the Beetle Game programs from page 10 to page 30 in Chapter 1 completely.
2. Then key in and/or modify all necessary classes and methods from the book.
3. Add your own methods or classes if necessary.
4. Refer to the UML class diagram for the entire Beetle program in Figure 1-30.
5. Compile, run and debug each class and method.
6. The execution partial results of your program will similar to Figure 1-29.
7. Email me your complete and error-free programs.
8. Hand in the screenshot of the complete execution results of your program.
9. Add more comments to describe the key concept and purpose in each class and method.
10. Explain why and where the Beetle programs implement the “encapsulation” feature.
CS303 Chapter 2 Homework (Polymorphism)
Due: 2/14/2013 Name: _______________ Signature: ________________ Score: _____
1. Study the Domineering Game programs in Chapter 2 (from pp 47-53) completely.
2. Then key in and/or modify all necessary classes and methods from the book.
3. Add your own methods or classes if necessary.
4. Add more comments to describe the key concept and purpose in each class and method.
5. Refer to the UML class diagram for the entire Beetle program in Figure 2-14.
6. Compile, run and debug each class and method.
7. The execution partial results of your program will similar to Figure 2-21.
8. Email me your complete and error-free programs.
9. Hand in the screenshot of the complete execution results of your program.
10. Explain why and where the Domineering programs implement the “polymorphism” feature.
CS303 Chapter 3 Homework (Inheritance)
Due: 2/21/2013 Name: _______________ Signature: ________________ Score: _____
1. The Game of Cram
a. Key in, compile and run the programs on Figure 3-1 (p68) in Chapter 3.
b. Modify the game such that no overlap # signs are allowed to put on the board.
For example: if the first player selects 0,0 horizontally, then the second player can’t selects 0,0
vertically again. If overlap, then show the “IllegalMoveException” message and ask the player
try other location (different row and column numbers).
c. Hand in the complete and error-free program.
d. Hand in the screenshot of the complete execution results of your program.
3
e. Explain why and where the Cram programs implement the “inheritance” feature.
2. A Color Light Bulbs
a. Study Fig. 3-3, Fig. 3-4, Fig. 3-6 and fig. 3-8 in Chapter 3.
b. Key in, compile and run these programs.
c. Debug and modify the program to make it executable if necessary.
d. Email me your complete and error-free programs.
e. Hand in the screenshot of the complete execution results of your program.
f. Explain why and where the Color Light Bulbs programs implement the “inheritance” feature.
CS303 Chapter 4 Homework (Stacks and Queues)
Due: 2/28/2013 Name: _______________ Signature: ________________ Score: _____
Part 1:
1. The Idiot’s Delight Game
1.1 Key in, compile and run the programs on Figures 4.7-4.12 in textbook.
1.2 If necessary, debug and modify these programs to make it executable.
1.3 Email me your complete and error-free programs.
1.4 Hand in the screenshot of the first 20 lines and last 20 lines of the execution
results of your program.
1.5 Explain why and where the Idiot’s Delight programs implement the
stacks and/or queues feature.
2. The War Game
2.1 Study Figures 4.33 - 4.37 in Chapter 4.
2.1 Key in, compile and run these programs.
2.3 Debug and modify the program to make it executable if necessary.
2.4 Email me your complete and error-free programs.
2.5 Hand in the screenshot of first 20 lines and last 20 lines of the execution results of
your program.
2.6 Explain why and where the War Game programs implement the
stacks and/or queues feature.
Part 2:
Do the following problems on page 115:
● Exercises: 4.15 (Hint: to implement a queue)
● Exercise: 4.16 (Hint: to implement a stack)
CS303 Chapter 5 Homework (Array-Based Structures)
Due: 3/7/2013 Name: _______________ Signature: ________________ Score: _____
1. The Go Fish Game
1.1 Key in, compile and run the programs on Figures 5.39-5.47 in textbook.
1.2 If necessary, modify and correct these programs to make it executable.
1.3 Email me your complete and error-free .java programs.
4
1.4 Hand in the screenshot of the first 20 lines and last 20 lines of the execution
Results, similar to Figure 5-48, of your program.
1.5 Explain why and where the Go Fish programs implement the Array and/or
ArrayList feature.
CS303 Chapter 6 Homework (Linked Structures)
Due: 3/21/2013 Name: _______________ Signature: ________________ Score: _____
Purpose: To implement and operate a Linked List
1. Study the LinkedList Class (not LinkedStack or LinkedQueue) data structure and its methods in textbook
Chapter 6, pages 157-159 and pages 168-175. Hint: The LinkedList implements the List interface from
Chapter 5 (Section 5.3). Also refer to Figure 6-18.
2. Each node in the linked list has a pointer field called “next” and a data field called “item” as in the
textbook. Assume the item type is integer.
3. Implement the following methods to operate your LinkedList:
add(Integer target) (see Figure 6-22)
get(int index)
set(int index, Integer target)
contains(Integer target)
remove(int index)
remove(Integer target)
size()
toString() (see Figure 6-19)
4. Then call the above methods in your main method in the following sequence and show the execution
results:
Add(Integer 10)
Add(Integer 20)
Add(Integer 30)
Add(Integer 40)
Add(Integer 50)
toString()
The linked list will look like as below:
Head
10
20
30
40
Then call the following methods in sequence:
(Note: Using System.out.println to show each execution result)
size()
5
50
null
get(int 4)
set(int 3, Integer 35)
toString()
contains(Integer 33)
get(int 3)
remove(int 1)
toString()
set(int 2, Integer 55)
remove(Integer 35)
contains(Integer 20)
remove(Integer 25)
size()
toString()
5. Email me your complete and error-free programs.
6. Hand in the screenshot of the execution results of your program.
CS303 Homework 7 Part 1 (Chapter 7)
Due: 4/4/2013 Name: _______________ Signature: ________________ Score: _____
Part1:
1. Run the program once in Figure 7-1 on page 184 in textbook.
2. Run the same program in Figure 7-2 on page 185 for three times. Are the three execution
times of ArrayList same or not? Are the three execution times of LinkedList same or not?
If not, explain?
3. Change TEST_RUNS to 100,000,000 in line 2 in Figure 7-2 and run it again.
4. Change list.get(5) to list.get(99) in line 14 in Figure 7-2 and run it again.
5. If necessary, modify and correct these programs to make it executable.
6. Email me your complete and error-free programs.
7. Hand in the screenshot of the execution results, similar to pages 184-185, of each program in
steps 1 to 4 above.
8. What did you learn in “time complexity analysis” from these programs?
Part2:
1. Using the Counting Steps approach as in Figures 7-11 to 7-14 (pages 194-195) to count the execution steps
for each statement in Figure 5-8 (p124) and Figure 7-2 (p185) respectively.
2. Then use the θ() notation to represent the time complexity for Fig 5-8 and 7-2 respectively.
CS303 Homework 8 (Chapter 8)
Due: 4/16/2013 Name: _______________ Signature: ________________ Score: _____
1. Modifiy the program in Figure 8-1 on page 206 to search a number in the following unsorted array:
6
5
8
11
6
2
13
3
4
7
10
9
12
1
Note: You have to create the above array in the same program first. Then call
linearSearch (Fig. 8-1) to search the following integers in the array interactively
as below:
Enter the number you want to search or -1 to quit: 14
NOT FOUND!
Enter the number you want to search or -1 to quit: 9
FOUND! It is in the array index #10.
Enter the number you want to search or -1 to quit: 0
NOT FOUND!
Enter the number you want to search or -1 to quit: 11
FOUND! It is in the array index #2.
Enter the number you want to search or -1 to quit: -1
Quit!
2. Run the program in Figure 8-3 to search a number in the following sorted array:
1
2
3
4
5
6
7
8
9
10
11
12
13
Note: You have to create the above array in the same program first. Then call
binarySearch (Fig. 8-3) to search the following integers in the array interactively
as below:
Enter the number you want to search or -1 to quit: 14
NOT FOUND!
Enter the number you want to search or -1 to quit: 9
FOUND! It is in the array index #8.
Enter the number you want to search or -1 to quit: 0
NOT FOUND!
Enter the number you want to search or -1 to quit: 11
FOUND! It is in the array index #10.
Enter the number you want to search or -1 to quit: -1
Quit!
3. Run the program in Figure 8-8 to sort the following array elements in ascending order,
5
8
11
6
2
13
3
4
7
10
9
12
and print out the sorted numbers in the array as below.
The sorted numbers in the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13.
4. If necessary, modify and correct these programs to make it executable.
5. Email me your complete and error-free .java programs.
6. Hand in the screenshot of the execution results of each program.
7
1
CS303 Chapter 9 Homework (Recursion)
Due: 4/23/2013 Name: _______________ Signature: ________________ Score: _____
1. Study the Tower of Hanoi methods in Chapter 9.
1.1 Assume there are 5 disks in the source peg initially.
1.2 Run the “Hanoi” method to generate all steps to move these 5 disks from the source
peg to the destination peg as on page 225.
1.3 Email me your complete and error-free .java programs.
1.4 Hand in your error-free and well-documented program and screen-shot of outputs.
2. Study the Merge Sort methods in Chapter 9.
2.1 Write a method to create an unsorted array with the following 8 integers
(see Fig. 9-18): 3, 8, 6, 1, 7, 2, 5, 4
2.2 Then call the Merge Sort methods to sort the array elements in ascending order
(see Fig. 9-21): 1, 2, 3, 4, 5, 6, 7, 8
2.3 Email me your complete and error-free .java programs.
2.4 Hand in the screen-shot of inputs and outputs of your programs.
3. Study the Quick Sort method in Chapter 9.
3.1 Create the same array as in Fig. 9-24 with the following 8 integers:
3, 8, 6, 1, 7, 2, 5, 4
3.2 Then call the Quick Sort methods to sort the array elements in ascending order
1, 2, 3, 4, 5, 6, 7, 8
3.3 Email me your complete and error-free .java programs.
3.4 Hand in the screen-shot of inputs and outputs of your programs.
4. Study the Fibonacci methods in Chapter 9 (pp246-248).
4.1 Implement the Fibonacci methods to generate the first 16 Fibonacci numbers as below:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987.
4.2 Email me your complete and error-free .java programs.
4.3 Hand in the screen-shot of inputs and outputs of your programs.
4.4 Hint: your input should be 15. You have to modify Fig. 9-35 to generate the above 16 numbers in
sequence.
CS303 Chapter 10 Homework (Trees)
Due: 4/30/2013 Name: _______________ Signature: ________________ Score: _____
1. Study the binary tree implementing methods in Figure 10-8, Chapter 10.
1.1 Create the binary tree as in Figure 10-4.
1.2 Using the above binary tree as input to run the toStringPreorder() method in Figure
10-15 to generate the tree traversal output (AGEKLHBFICJD) as in Figure 10-14.
1.3 Using the above binary tree as input to run the toStringInorder() method in Figure
10-16 to generate the tree traversal output (GKELAFBHCJID) as in Figure 10-14.
1.4 Using the above binary tree as input to run the toStringPostorder() method in Figure
10-17 to generate the tree traversal output (KLEGFBJCDIHA) as in Figure 10-14.
Bonus: Using the above binary tree as input to run the toStringLevelorder() method in
Figure10-19 to generate the tree traversal output (AGHEBIKLFCDJ) as in Figure 10-14.
1.5 Email me your complete and error-free .java programs.
1.6 Hand in the screen-shot of inputs and outputs of your programs.
8
CS303 Chapter 11 Homework (Sets, bonus)
Due: 5/16/2013 Name: _______________ Signature: ________________ Score: _____
1. Study the binary search tree implementing methods from Figure 11-16 to Figure 11-28.
1.1 Create the binary search tree as in Figure 11-14. Hint: study Figures 11-20 to 11-23.
1.2 Using the above binary search tree as input to call the toStringInorder() method as in Figure 10-16 to
generate the tree traversal output. The answer is (2 5 8 11 13 14 16 19 20 22 26).
1.3 Using the above binary search tree as input and call the size(BinaryNode<E> node) method as in Figure
11-16 to return the tree size. The answer is 11.
1.4 Using the above binary search tree as input to call the contains(E target) method as in Figure 11-18 to
check if 20 is in the tree or not. The answer is true.
1.5 Using the above binary search tree as input to call remove(E target) method to delete integers 2 and 13
(the root) from the tree. Hint: study Figures 11-26 to 11-28.
1.6 Using the final binary search tree (after removed 2 and 13) as input to call the toStringInorder() method
as in Figure 10-16 to generate the tree traversal output. The answer is (5 8 11 14 16 19 20 22 26).
1.7 Email me your complete and error-free .java programs.
1.8 Hand in the screen-shot of inputs and outputs of your programs.
2. Study the hash table implementing methods from Figure 11-36 to 11-39.
2.1 Assume the array size is 10 (index from 0 to 9). Create a hash table with the following
integers: {480, 731, 526, 938, 568, 208, 129, 345}. Note: read each integer in this order.
2.2 Print out the contents of the above hash table with the index number of each integer.
2.3 Then insert 618 into the hash table. Hint: pay attention the collision situation if any.
2.4 Print out the contents of the hash table (after inserted 618) with the index number of
each integer.
2.5 Delete 568 from the hash table.
2.6 Print out the contents of the hash table (after deleted 568) with the index number of
each integer.
CS303 Chapter 14 Homework (Advanced Trees)
Due:
Name: _______________ Signature: ________________ Score: _____
1. Study the heap and priority queue implementing methods from Figure 14-3 to Figure 14-7.
1.1 Create the heap as in the bottom tree of Figure 14-4.
1.2 Using the above heap as input to call the toStringLevelOrder() method as in Figure 10-19 to generate the
tree traversal output. The answer is (1 4 3 12 15 6 19 14 18 17 20 9).
1.3 Add element 2 to the above heap and use pre toStringLevelOrder() method to output the tree again. The
answer is (1 4 2 12 15 3 19 14 18 17 20 9 6).
1.4 Add element 5 to the above heap and use toStringLevelOrder() method to output the tree again. The
answer is (1 4 2 12 15 3 5 14 18 17 20 9 6 19).
1.5 Remove element 1 from the above heap and use toStringLevelOrder() method to output the tree again.
The answer is (2 4 3 12 15 6 5 14 18 17 20 9 19).
1.6 Remove element 2 from the above heap and use toStringLevelOrder() method to output the tree again.
The answer is (3 4 5 12 15 6 19 14 18 17 20 9).
1.7 Email me and hand in your error-free and well-documented Java programs and screen-shot of outputs.
2. (Bonus 50%) Study the heapsort implementing method in Figure 14-8.
9
2.1 Use heapsort to sort the elements in the heap in Step 1.6 (after removed element 2 from the heap) and
output the sorted result. The answer is (3 4 5 6 9 12 14 15 17 18 19 20).
2.2 Email me and hand in your error-free and well-documented Java programs and screen-shot of outputs.
CS303 Chapter 15 Homework (Graphs)
Due:
Name: _______________ Signature: ________________ Score: _____
1. Study the Graph and cheapest (Dijkstras Algorithm) implementing methods in Figure 15-11 and Figure
15-32. Use the Dijkstra’s Single-Source Algorithm to find the shortest paths from node A to each of other
nodes (That is from A to B, node A to B, and so on) in the following graph. The answers are:
Shortest path from node A to B is: A  B (distance is 7)
Shortest path from node A to C is: A  C (distance is 8)
Shortest path from node A to D is: A  D (distance is 6)
Shortest path from node A to E is: A  B  E (distance is 11)
Shortest path from node A to F is: A  D  F (distance is 7)
7
B
8
6
3
C
D
4
E
A
5
2
1
F
2. Use the Kruskal’s algorithm as in Figure 15-39 to find the minimum spanning tree of the above graph.
The tree is same as Figure 15-38.
3. Email me and hand in your error-free and well-documented Java programs and screen-shot of outputs in
questions 1 and 2.
10
Download