Docsity

Quiz 1 - Data Structures II: Sorting Algorithms, Recursion, and Collections in Java, Quizzes of Data Structures and Algorithms

Toggle sidebar
bg1
NAME___________________________________________________
Wentworth Institute of Technology
Division of Professional and Continuing Studies
COMP385 Section 71 – Data Structures II - Fall, 2005
Quiz 1 – Open Book
1. List the names of 5 types of sorting algorithms
a.________________________________________________________
b.________________________________________________________
c.________________________________________________________
d.________________________________________________________
e.________________________________________________________
2. What will this recursive program print?
public static void main (String[] args)
{
mysteryPrint("Reverse");
MyInput.pause();
}
static void mysteryPrint(String inString)
{
int n = inString.length()-1;
if (inString.length()!=0)mysteryPrint(inString.substring(0,n));
for(int j=n; j>=0; j--)
System.out.print(inString.charAt(j));
System.out.println();
}
PROBLEMS 3,4,5 ALL USE THE FOLLOWING CLASS, CALLED Rectangle
public class Rectangle
{
protected int side1; //one side of the rectangle
protected int side2; //the other side of the rectangle
public Rectangle() {
super();
}
public Rectangle(int side1, int side2)
{
super();
this.side1 = side1; this.side2 = side2;
}
public int area() { return side1*side2;}
public int perimeter() { return 2*(side1+side2); }
public String toString()
{
return "[s1: " + side1 + " s2: " + side2 + "]";
}
}
3. Write the method compareTo for this class(the definition of compareTo is in your text, pg. 153). One rectangle is
bigger than another if it’s area is bigger.
/app/work/qkd1ni-375833-2765060-quiz1comp385fa05-doc.doc 11/27/2020 1
pf3

Partial preview of the text

Download Quiz 1 - Data Structures II: Sorting Algorithms, Recursion, and Collections in Java and more Quizzes Data Structures and Algorithms in PDF only on Docsity!

NAME___________________________________________________

Wentworth Institute of Technology

Division of Professional and Continuing Studies

COMP385 Section 71 – Data Structures II - Fall, 2005

Quiz 1 – Open Book

1. List the names of 5 types of sorting algorithms

a.________________________________________________________

b.________________________________________________________

c.________________________________________________________

d.________________________________________________________

e.________________________________________________________

2. What will this recursive program print?

public static void main (String[] args) { mysteryPrint("Reverse"); MyInput.pause(); } static void mysteryPrint(String inString) { int n = inString.length()-1; if (inString.length()!=0)mysteryPrint(inString.substring(0,n)); for(int j=n; j>=0; j--) System.out.print(inString.charAt(j)); System.out.println(); }

PROBLEMS 3,4,5 ALL USE THE FOLLOWING CLASS, CALLED Rectangle

public class Rectangle { protected int side1; //one side of the rectangle protected int side2; //the other side of the rectangle public Rectangle() { super(); } public Rectangle(int side1, int side2) { super(); this.side1 = side1; this.side2 = side2; } public int area() { return side1side2;} public int perimeter() { return 2(side1+side2); } public String toString() { return "[s1: " + side1 + " s2: " + side2 + "]"; } }

3. Write the method compareTo for this class(the definition of compareTo is in your text, pg. 153). One rectangle is

bigger than another if it’s area is bigger.

4. Write the declaration to declare a Bag of Rectangles. Note that Bag is a generic class and you need to use the proper

syntax for using a generic class. Name the Collection myBagOfRects

5. Write an enhanced for loop that prints the entire contents of the Bag collection.

(a)Declare an enumerated class called “Emotion”. Have the items in this class be “happy”, “sad”, “angry”.

(b) Write an enhanced for loop that prints out the values of Emotion

7. Because of autoboxing, the following works only in Java 5. Write the equivalent statement for c = a+b (on the

right)that work for all versions of Java.

Double a = new Double(3.3); double b = 2.2; double c = a+b;

8. Write a generic method, swapFirstLast, that accepts an array of generic datatypes (T), and swaps the first and last

elements of the array(the item in index 0, and the item in the last index). The method should determine the index of

the last element.

9. Write the code that times how long it takes to calculate the Fibonacci value of N ( final int N=20;). YOU DO

NOT have to write the “fib” routine, assume it exists. Use the Timings class from Ford and Topp.

10. Mark each statement True or False

________The add(T item) method of all Collections always returns true

________By default, the Java clone method is a “shallow clone”

________In a generic method, this is a legal statement anArray = new T[n];

________To make a deep clone of an object, the object must be tagged “Serializable”

________It is legal to use the “remove” method of a Collection outside of an iterator.