CS 620 Lab Assignments
CS 620 Lab 1 – Recursion
Part 1: Beginning on slide 27 of the lecture notes, we presented a recursive algorithm to determine if a String was a palindrome. Write a Java program that implements that algorithm. Here is some sample output:
Remarks
1. Your solution must be recursive.
2. You may leave after you demonstrate this program to the instructor.
3. If you leave before demonstrating the program, you will be assigned the grade 0 on this
assignment.
4. If you have not finished the program by the end of class, you will be allowed to work on it at
home. You must, however, be finished by the next class meeting.
Part 2: Write a program that generates the following picture
1
What do we write here?
CS 620 Lab Assignments
Here is an outline for a Java Program
public class Drawing extends Canvas {
public static void main(String[] args) {
JFrame frame = new JFrame(“My Drawing”);
Canvas canvas = new Drawing();
canvas.setSize(600, 600);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
}
public static void sierpinski(Graphics g, int x1, int y1, int x2,
int y2)
{
}
public void paint(Graphics g) {
g.setColor(Color.red);
sierpinski(g, 2, 2, this.getWidth()-2, this.getHeight()-2);
} }
I needed to use the Graphics methods drawLine, setColor, and fillPolygon. You may find them here. https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html
This program is due on January 30. Plan to demonstrate it to the instructor in class on or before that date.
2
CS 620 Lab Assignments
CS 620 Programming Assignment
public class MULinkedList
{
private class Node
{
private E data;
Node
public Node(E data)
{
this.data = data;
this.next=null;
}
}
private Node
public MULinkedList()
{
this.head = null;
}
private String toString(Node
{
if(head==null)
return””;
return head.data.toString() + “\n” + toString(head.next);
}
public String toString()
{
return toString(head);
}
private void add(Node
{
if(head.next == null)
head.next = new Node
else
add(head.next,data);
}
public void add(E data)
{
if(head==null)
head = new Node
else
add(head,data);
}
}
3
This class contains recursive methods for toString and add methods that add new elements to the end of the linked list.
4
CS 620 Lab Assignments
Programming Assignment Linked List
Make the following adjustments to the class MULinkedList shown above.
1. Add the method addAtHead to the class shown above. This method should simply add a new
element to the beginning of the linked list instead of the end.
2. Add the method searchList to the class above. The method should return true if the user
specified object is found and false otherwise. The method must be recursive.
3. Add the remove method described in the lecture notes
Write a class called Student that
1. Has members variables name (String) and gpa (double)
2. Has constructors that
a. Takes name and gpa as inputs
b. The null constructor
3. Implements the toString method (Example: Name: Sue Jones
4. Implements the equals method. Define two students to be equal if they have the same
name
Write a class called MULinkedListTest that conforms to the following
1. Declare a MULinkedList that holds Student objects
2. Implement the following options for the user
1. add a new student to the end 2. add a new student to the head 3. remove a student
4. search for a student
5. print all students (simply use the toString for a MULinkedList) 6. QUIT
GPA 3.11)