Project 6 is due on Thursday, May
15
The Final Exam (includes all topics, textbook sections, and programs listed
in the syllabus) is on May 15 at 6:15pm
CS 152 - Computer Science II
Spring-2008
Classes: TR 5:15 pm - 6:30 pm, Maria Sanford Hall 221
Instructor: Dr. Zdravko Markov, 30307 Maria Sanford Hall, (860)-832-2711,
http://www.cs.ccsu.edu/~markov/, e-mail: markovz at ccsu dot edu
Office hours: TR 9:30am - 12:00pm, or by appointment
Catalog description: Computer Science II [c] Prereq.: CS 151
and MATH 152. Further topics in object-oriented programming: enhancing
classes (inheritance, polymorphism) and using Application Programmer Interface.
Event-driven programming. Elementary searching and sorting techniques.
Introduction to software engineering issues. Recursion.
Prerequisites: CS 151 and MATH 152
Course objectives: Upon successful completion of the course the
student will be able to:
-
Understand well the basic principles of object-oriented programming, and
apply them in definite application settings.
-
Write Java programs using appropriate control structures (iteration, recursion,
selection).
-
Write Java applets and GUI applications.
-
Organize, write, debug, and modifying Java code with sufficient degree
of proficiency.
Required textbook: Lewis & Loftus, Java Software Solutions:
Foundations of Program Design, 5-th Edition, Addison-Wesley, 2007.
Required software: BlueJ - an interactive Java programming environment
(www.bluej.org)
Class Participation: Active participation in class is expected
of all students. Regular attendance is expected. If you must miss a test,
try to inform the instructor of this in advance.
Assignments and tests: Reading and problem assignments are listed
below. Some of the problems will be worked in class. There will be 2
tests and a final exam. They will include material from the
textbook, the lectures, and the programming projects.
Programming projects: There will be 6 programming projects
requiring the use of BlueJ to write and run Java programs. The projects
with their due dates are listed below (the project descriptions are given
on separate pages). All projects must be submitted through the Vista course
management system (CentralPipeline
> Student > Vista Courses > CS 152 - 01).
Grading: The final grade will be based on projects (60%),
tests
(20%), and the final exam (20%), and will be affected by classroom
participation, conduct and attendance. The letter grades will be calculated
according to the following table:
| A |
A- |
B+ |
B |
B- |
C+ |
C |
C- |
D+ |
D |
D- |
F |
| 95-100 |
90-94 |
87-89 |
84-86 |
80-83 |
77-79 |
74-76 |
70-73 |
67-69 |
64-66 |
60-63 |
0-59 |
Unexcused late submission policy: Projects submitted more than
two days after the due date will be graded one letter grade down. Projects
submitted more than a week late will receive two letter grades down. No
submissions will be accepted more than two weeks after the due date.
Honesty policy: It is expected that all students will conduct
themselves in an honest manner (see the CCSU Student handbook), and NEVER
claim work which is not their own. Violating this policy will result in
a substantial grade penalty, and may lead to expulsion from the University.
However, students are allowed to discuss projects with others and receive
debugging help from others.
Tentative schedule of classes, assignments, projects and tests (by week)
Note: Dates will be placed for all projects due days and tests.
Check the schedule regularly for updates!
-
Object Oriented Design and Arrays
-
Topics:
-
Review of Chapter 6: Static variables and methods, Class relationships,
The this reference, Interfaces, Method design
-
Arrays, Dynamic arrays, ArrayList class
-
Introduction to BlueJ
-
Slides: slides06.ppt,
slides07.ppt
-
Programs/Exercises:
-
Static variables and methods: SloganCounter.java,
Slogan.java
(use a this reference in the constructor, change variable count
and method getCount() to static/instance)
-
Sorting arrays: array.java
(create a method to print the array and use it to trace sorting, use local
variables and parameters or instance variables and no parameters)
-
Arrays: ReverseOrder.java
-
Arrays: LetterCount.java
-
Arrays of objects: index.java
-
Dynamic arrays of objects: Students.java,
Student.java
(use scan.next().equals("n") to stop input, add toString() method to Student
and print Student objects directly)
-
Assignments:
-
Read Chapters 6, 7
-
Download, install and get familiar with BlueJ
-
February 14: Submit Programming
Project 1 (10%)
-
February 12-14: Inheritance
-
Topics:
-
Creating subclasses and class hierarchies
-
Overriding methods.
-
Interfaces. The Comparable Interface
-
Abstract methods and classes
-
Slides: slides08.ppt
-
Programs/Exercises:
-
Words.java,
Dictionary.java,
Book.java.
Create a new book class, e.g. Encyclopedia.java with constructor Encyclopedia(pages,
articles).
-
Staff.java,
Executive.java,
Worker.java.
Add toString() to print the type of worker or use super(name + " (executive)").
-
library.zip
(more elaborated book hierarchy).
-
In library.java create objects from each class and print them.
-
Add constructor to class Book to set up the title and pages (need to add/modify
constructors to other classes).
-
Add more programming books.
-
Assignments:
-
Read Chapter 8 (without sections 8.7 and 8.8)
-
February 19-21: Polymorphism and sorting
-
Topics:
-
Polymorphism (via inheritance and via interfaces)
-
The sorting problem: selection, insertion and bubble sorts
-
Sorting arrays of objects
-
Slides: slides09.ppt
-
Programs/Exercises:
-
Assignment:
-
February 26-28: Searching
-
Topics:
-
Linear search
-
Binary search
-
Slides: slides09.ppt
-
Programs/Exercises:
-
searching-students.zip
(search by name or by grade, evaluate complexity)
-
Use Searching.java and Sorting.java to search a list of 30 random numbers.
Define an object Numb implementing the Comparable interface and use a list
of objects of this type.
-
Assignments:
-
March 4-6: Introduction to Graphics
-
Topics:
-
Introduction to graphics
-
Writing simple applets
-
Adding buttons and text fields to applets
-
Writing GUI applications: containers, frames, panels
-
Graphical objects
-
Slides: slides02.ppt,
slides03.ppt,
slides04.ppt
-
Programs/Exercises:
-
Assignment: Read Sections 2,7-2.9, 3.9, 4.5 4.8.
-
March 6: Test #1 (Inheritance, Polymorphism
and Sorting, Chapters 7, 8, and 9).
-
March 25-27: More Graphics
-
Topics:
-
Drawing with loops and conditionals
-
Determining event sources
-
More GUI objects: dialog boxes, check boxes, and radio buttons
-
Programs/Exercises:
-
Assignments:
-
Read Sections 5.9 5.12.
-
April 1-3: GUI Design
-
Topics:
-
GUI design
-
Layout managers
-
Containment hierarchies
-
Programs/Exercises: layout.zip
-
Assignments:
-
April 3: Polygons and polylines
-
April 8-10: Mouse and Key Events
-
Topics: Mouse events and key events.
-
Programs/Exercises:
-
Assignment:
-
Read Sections 7.8 7.10.
-
April 15-17: Adapter classes, event processing
-
Topics:
-
Extending adapter classes
-
Event processing and polymorphism
-
Programs/Exercises:
-
Assignment: Read Sections 8.7, 8.8, 9.6 9.10.
-
April 22: Submit Programming Project
4
-
April 17: Test #2 (on applets and GUI
applications).
-
April 22-24: Exceptions
-
April 29, May 1: Recursion
-
Topics:
-
Recursive programming
-
Recursion versus iteration
-
Recursive functions
-
List processing
-
Problem solving
-
Tree/Graph search
-
Programs/Exercises:
-
Functions: Fact1.java,
Fact2.java,
Fibonacci1.java,
Fibonacci2.java
-
Lists: List.java,
Lists.java
(see http://en.wikipedia.org/wiki/Prolog,
Quick
Introduction to Prolog), QuickSort.java
(see http://en.wikipedia.org/wiki/QuickSort).
-
Problem solving: Maze.java,
MazeSearch.java,
Farmer.java,
State.java
-
Tree/Graph Search: Path.java,
AllPaths.java
-
Assignments:
-
Read Sections 11.1 11.3
-
May 6: Recursion in graphics
-
May 15 (6:15-8:15p): Final Exam (includes all topics, textbook
sections, and programs listed in the syllabus)
Programming Project 1
Write a program in Java according to the following specifications:
Description:
The user enters student names (first and last separated by white space)
and grades (integers between 0 and 100) on separate lines. After the and
of the input (indicated by entering Ctrl-Z, prompting the user after each
line, or using an end of input marker) the program prints the names and
grades grouped by grade (excellent: >=90, failure: <60, ok: otherwise)
followed by the total number of students, their grade average, and the
students with highest and lowest grade. For example, if the user inputs
the following data:
John Smith 90
Al Clark 80
Ann Miller 75
Sue Taylor 55
the program prints the following:
Studentts with excellent grades:
John Smith 90
Students with ok grades
Al Clark 80
Ann Miller 75
Students with failure grades:
Sue Taylor 55
Total number of students: 4
Average grade: 75
Highest grade: John Smith 90
Lowest grade: Sue Taylor 55
Restrictions: Use an ArrayList of objects to store the student
records. Define a class Student that represent a student record
(student name and grade). Compile and run the program using BlueJ.
Documentation: Run your program and make sure it is designed
and works exactly as described above. Then add your name in the beginning
of the code as a comment. Add also comments to explain the classes and
methods used. Note that comments and the way you format your program will
be graded too.
Submission: Submit the following files as attachments through
CentralPipeline
> Student > Vista Courses > CS 152 > Project 1:
-
The sources of all classes used.
-
A screen copy (an MS Word file with the image included) of the BlueJ
Programs window showing all classes used with the "uses" relations
between them.
-
A screen copy or a text file with the content of the BlueJ Terminal
Window showing an example run of your program with at least 5 student
records including all three types of grades.
Programming Project 2
Write a program in Java according to the following specifications:
Description:
The program reads a file with student names (first and last separated
by white space) and grades (integers between 0 and 100) on separate lines.
Then the program prints the student names and grades sorted by first
name, by last name or by grade. The type of sorting (by grade,
first name or last name) is specified by the user. For example, if the
input file includes the following data:
John Smith 90
Sarah Barnes 75
Mark Riley 80
Laura Getz 72
Larry Smith 95
Frank Phelps 75
Mario Guzman 60
Marsha Grant 85
the program proceeds as follows (the user answer is "l", that is sort by
last name):
Sort by first name (f), last name (l), or grade (g)? l
Sarah Barnes 75
Laura Getz 72
Marsha Grant 85
Mario Guzman 60
Frank Phelps 75
Mark Riley 80
John Smith 90
Larry Smith 95
If the user answer is "g", then the program prints:
Larry Smith 95
John Smith 90
Marsha Grant 85
Mark Riley 80
Sarah Barnes 75
Frank Phelps 75
Laura Getz 72
Mario Guzman 60
Restrictions and implementation: Use an array of objects
to store the student records. Define a class Student that represents
a student record (student's first name, last name and grade) and implements
the Comparable interface. Use the bubble sort algorithm for
sorting
objects. Compile and run the program using BlueJ.
Hint:
Define a variable in class Student to represent the type of sorting and
pass its value (e.g. int 0,1, or 2) as a parameter to the Student constructor.
Then use it in the compareTo method to compare first names, last
names, or grades respectively.
Documentation: Run your program and make sure it is designed
and works exactly as described above. Then add your name in the beginning
of the code as a comment. Add also comments to explain the classes and
methods used. Note that comments and the way you format your program will
be graded too.
Submission: Submit the following files as attachments through
CentralPipeline
> Student > Vista Courses > CS 152 > Project 2:
-
The source code (.java) of all classes used.
-
The input file with student names and grades that you used to test the
program.
-
A screen copy (an MS Word file with the image included) of the BlueJ
Programs window showing all classes used with the relations between
them.
-
A screen copy or a text file with the content of the BlueJ Terminal
Window showing an example run of your program.
Programming Project 3
Write a program in Java following the specifications of programming
project 5.39 (Lewis&Loftus, page 290). Implement only the word
count statistics and instead of those specified in the project use
the following GUI objects (see CopyTextPanel.java):
-
a text field for the input text (one line of text)
-
labels to output statistics
-
a button that, when pressed, computes the statisctics
-
a button that, when pressed, clears the text field
-
a panel, that includes all GUI objects listed above
Make your application run in a window (JFrame) and use proper backgrounds
and colors to make all objects clearly visible.
Extra credit (maximum 5 points) will be given for additional
statistics (for example, maximum, minimum or average word size, number
of numeric tokens, word frequencies etc.).
Documentation: Run your program and make sure it compiles without
errors and works as described above. Then add your name in the beginning
of the code as a comment. Add also comments to explain the classes and
methods used. Note that comments and the way you format your program will
be graded too.
Submission: Submit the following files as attachments through
CentralPipeline
> Student > Vista Courses > CS 152 > Project 3:
-
The source code (.java) of all classes used.
-
A screen copy (an MS Word file with the image included) of the BlueJ
Programs window showing all classes used with the relations between
them.
-
A screen copy showing an example run of your program.
Programming Project 4
Write a graph drawing program in Java. Use the program discussed in class
(Graph.java,
GraphPanel.java)
and make the following changes and additions:
-
Replace the filled oval at the end of the edges with an arrow (two short
line segments starting at the end point). See Angle.java in trigonometry.zip
-
Add two more buttons - Create and Delete. When the Create button is pressed
new nodes and edges can be added (as in the original program from the lecture
notes). After pressing the Delete button, nodes and edges can be removed
by clicking on them. The third button, "Print adjacency matrix" should
work as before, printing the adjacency matrix of the current graph shown
in drawing panel. For detecting a click over (or close to) an edge see
the programs in trigonometry.zip.
Extra credit (maximum 5 points) will be given for computing the
prestige score of the graph nodes. Use the power iteration algorithm described
in Social
Networks and PageRank. See the example of power iteration PowerIteration.java.
For more information about matrices see http://mathworld.wolfram.com/Matrix.html
and for vectors and norms see http://mathworld.wolfram.com/VectorNorm.html.
You may also use JAMA (Java Matrix Package) from http://math.nist.gov/javanumerics/jama/.
Documentation: Run your program and make sure it compiles without
errors and works as described above. Then add your name in the beginning
of the code as a comment. Add also comments to explain the classes and
methods used. Note that comments and the way you format your program will
be graded too.
Submission: Submit the following files as attachments through
CentralPipeline
> Student > Vista Courses > CS 152 > Project 4:
-
The source code (.java) of all classes used.
-
A screen copy (an MS Word file with the image included) of the BlueJ
Programs window showing all classes used with the relations between
them.
-
A screen copy showing an example run of your program including the graph
panel and the terminal window output with the adjacency matrix or the prestige
score of the nodes.
Programming Project 5
Use the program you created for Project
2 and make the following changes and additions:
-
Make the program throw exceptions as follows:
-
An exception when the user answer to the question "Sort by first name (f),
last name (l), or grade (g)?" is not among the allowed answers ( "f", "l",
or "g"). Define this exception as a new class
derived from class Exception
and throw it in class Student.
-
An exception when the grade is out of the interval [0, 100]. Define this
exception as a new class derived from class Exception and throw
it in class Student.
-
An exception when the number of records read from the file exceeds the
size of the array of Student objects. Use ArrayIndexOutOfBoundsException
class for this purpose.
-
Catch all exceptions in the main method and print proper messages.
-
When no exceptions occur the program should work as described in Project
2.
Documentation: Run your program and make sure it compiles without
errors and works as described above. Then add your name in the beginning
of the code as a comment. Add also comments to explain the classes and
methods used. Note that comments and the way you format your program will
be graded too.
Submission: Submit the following files as attachments through
CentralPipeline
> Student > Vista Courses > CS 152 > Project 5:
-
The source code (.java) of all classes used.
-
A screen copy (an MS Word file with the image included) of the BlueJ Programs
window showing all classes used with the relations between them.
-
Terminal window copies demonstrating each exception as well
as runs without exceptions (prepare and also submit all text inputs
files needed).
Programming Project 6
Extend the graph drawing program you created for Project 4 making the following
changes and additions:
-
Add a button that when pressed prints the path from node X to node Y, where
nodes X and Y are specified by the user with text fields or dialog boxes.
If a path from X to Y does not exist the program should indicate this.
-
Use a recursive method to find the path based on Path.java.
-
The graph may include loops (the graph search method should avoid going
into infinite loops).
Extra credit (maximum 5 points) will be given for extending the
program so that it prints all possible paths from node X to node
Y. Hint: see AllPaths.java
Documentation: Run your program and make sure it compiles without
errors and works as described above. Then add your name in the beginning
of the code as a comment. Add also comments to explain the classes and
methods used. Note that comments and the way you format your program will
be graded too.
Submission: Submit the following files as attachments through
CentralPipeline
> Student > Vista Courses > CS 152 > Project 6:
-
The source code (.java) of all classes used.
-
A screen copy (an MS Word file with the image included) of the BlueJ
Programs window showing all classes used with the relations between
them.
-
Screen copies showing example runs of your program including graph panel
and terminal window outputs.
Test 1 Sample Problems
1) What does the following code do? Assume list is an array of int
values, temp is some previously initialized int value, and c is an int
initialized to 0.
for (int j = 0; j < list.length; j++)
if (list[j] < temp) c++;
a) It finds the smallest value and stores it in temp
b) It finds the largest value and stores it in temp
c) It counts the number of elements equal to the smallest value in
list
d) It counts the number of elements in list that are less than temp
e) It sorts the values in list to be in ascending order
2) An int array stores the following values (in that order): 9,
4, 12, 2, 6, 8, 18
a) Demonstrate how the array is sorted in ascending order by
the Selection sort algorithm. Show the content of the array after each
pass of the outermost loop of the algorithm.
b) Demonstrate how the array is sorted in ascending order by the Insertion
sort algorithm. Show the content of the array after each pass of the outermost
loop of the algorithm.
3) The code below adds elements to an array of objects Numbers and
then prints the array. Add declarations for: Numbers, x, gen, and n, and
define the class Num with the minimum information so that the code below
runs. Hint: gen is an instance of the Random class.
for (int i = 0; i < 10; i++)
{
x = gen.nextInt(100);
Num n = new Num (x);
Numbers.add(n);
}
for (int index = 0; index < Numbers.length;
index++)
System.out.println (Numbers.get(index));
4) The following program generates a list of parts each one defined
with a name and an ID. Then it finds the ID of the part named Part 4
and prints its ID. Define the class Part accordingly. Hint: Part must implement
the Comparable interface (defining properly compareTo) and should also
include a constructor and a method toString().
public class Items
{
public static void main (String[] args)
{
Part [] list = new Part[6];
Part target = new Part ("Part 4",
0);
list[0] = new Part ("Part 1", 123);
list[1] = new Part ("Part 2", 127);
list[2] = new Part ("Part 3", 531);
list[3] = new Part ("Part 4", 131);
list[4] = new Part ("Part 5", 789);
list[5] = new Part ("Part 6", 254);
for (int i = 1; i < list.length;
i++)
if (list[i].compareTo(target)
= 0)
System.out.println(list[i]);
}
}
5) Consider the following partial class definitions:
public class A
{
public int x1;
private int y1;
protected int z1;
}
public class B extends A
{
public int x2;
private int y2;
protected int z2;
}
public class C extends A
{
public int x3;
private int y3;
}
public class D extends B
{
public int x4;
private int y4;
}
a) Draw a diagram showing the class hierarchy.
b) Explain the visibility (accessibility) of all variables.
Test 2 Review Topics
-
Applets
-
GUI applications
-
Objects: frames, panels, dialog boxes, check boxes, and radio buttons
-
Events, event sources: ActionListener, MouseListener, MouseMotionListener
interfaces
-
Event processing using the Adapter class: MouseAdapter, MouseMotionAdapter
(see AnglePanel.java,
Angle.java)
-
Programs: Authority.java,
PiFrame.java,
PiSquare.java,
Guess.java,CopyText.java,
CopyTextPanel.java,
StyleOptions.java,
StyleOptionsPanel.java,
QuoteOptions.java,
QuoteOptionsPanel.java
-
Polygons and polylines