CMP211
Problems
For Classes of David B. Sher
·
Array
Manipulation
- Write a method that
returns the largest number in its parameter, an array of floats.
- Write a method that takes
a parameter that is an array of floats and a float parameter
that is the limit. If any element of the array is greater than the limit
then the method returns true otherwise it returns false.
- Write a method that
returns true if a String parameter contains the letter A.
- Write a method that counts
how many c’s are in a String parameter (and returns the count).
- Write a method that
returns the first letter in a String that is a capitol letter. If there is no such letter it should
throw an Exception.
·
Style
Problems
- Comment this method:
public static float calculate(int x, float y)
{
float twice_x = 2.0*x;
float three_y = 3.0*y;
if(twice_x > three_y) return
twice_x;
else return three_y;
}
- Which of these names are
good method names and why
xxx,
standardDeviation, z3, return, doesIt, retrieveNews, Product
1. Write a class that implements
this interface:
public interface Lesson
{
/** returns first string in
lesson */
public String first ();
/** returns last string in
lesson */
public String last ();
/** returns true if all
strings are equal */
public boolean allSame();
/** replaces first string with
parameter */
public void setFirst(String
first);
/** replaces last string with
parameter */
public void setLast(String
last);
} // end lesson
2. Write a method that has two
Lesson parameters that returns true if all the strings in both Lessons are
equal.
3. Write a class that has two
Lesson instance variables. It should
have methods to set and get the two lessons.
It should also have a method that returns true if both Lessons have
equal first strings.
· Stack Problems
- Write accessors of an
implementation of the stack interface that return:
- The number of elements of
the stack that are equal to the top element
- True if all the elements
of the stack are = and false otherwise.
- The first element of the
stack that is equal to the parameter.
- The largest of the
elements of the stack.
- True if the stack has
some elements larger than its parameter, False otherwise.
- True if the stack's
elements are all larger than the parameter, False otherwise.
- The number of elements in
the stack larger than the parameter.
· Unsorted List Problems
- Write modifiers of the
implementation of the list interface that:
- Delete the nth
element of the list (n is a parameter of the member).
- Delete the first element
of the list that is larger than the parameter of the member.
- Insert the first
parameter before the first element that is larger than second parameter
of the member.
- Delete all the elements
of the list that are larger than the parameter of the member.
· Queue (Wrap around) Problems
- Write accessors of an
implementation of the queue interface using the wrap around
technique that return:
- The number of elements of
the queue that are equal to the top element
- True if all the elements
of the queue are = and false otherwise.
- The first element of the
queue that is equal to the parameter.
- The largest of the
elements of the queue.
- True if the queue has
some elements larger than its parameter, False otherwise.
- True if the queue’s
elements are all larger than the parameter, False otherwise.
- The number of elements in
the queue larger than the parameter.
· Sorting Problems
- How can you efficiently modify
the selection sort algorithm to find the 5 largest elements in the
list? Can you modify insert sort to do the same thing?
- Change bubble sort to find the
n largest elements of a list (n is a parameter).
· Linked Stack Problems
- Write accessors of a linked
list implementation of the stack interface that returns:
- The number of elements of
the stack that are equal to the top element
- True if all the elements
of the stack are = and false otherwise.
- The first element of the
stack that is equal to the parameter.
- The largest of the
elements of the stack.
- True if the stack has
some elements larger than its parameter, False otherwise.
- True if the stack's
elements are all larger than the parameter, False otherwise.
- The number of elements in
the stack larger than the parameter.
· Linked List Problems
- Write modifiers of a linked
list implementation of the list interface that:
- Delete the nth
element of the list (n is a parameter of the member).
- Delete the first element
of the list that is larger than the parameter of the member.
- Insert the first
parameter before the first element that is larger than second parameter
of the member.
- Delete all the elements
of the list that are larger than the parameter of the member.
· Circularly Linked Queue Problems
- Write accessors of the circularly
linked implementation of the queue interface that return:
- The number of elements of
the queue that are equal to the top element
- True if all the elements
of the queue are = and false otherwise.
- The first element of the
queue that is equal to the parameter.
- The largest of the
elements of the queue.
- True if the queue has
some elements larger than its parameter, False otherwise.
- True if the queue’s
elements are all larger than the parameter, False otherwise.
- The number of elements in
the queue larger than the parameter.
· Recursive Method Problems
1.
public static int recurse(int x) { if(x<=1)
return x; else return recurse(x-1)+2*recurse(x-2); }
what is recurse (1), recurse(2), recurse(4), recurse(8)?
2.
public static int recurse2(int y) { if(y <=
2) return y+3; else return recurse(y-1)+recurse(y/2);
what is recurse (1), recurse(2), recurse(5), recurse(10)?
3.
Write a member method of recursive list type that returns
the sum of all the items in a list that are > than its parameter?
· Tree Problems
1.
Output the characters of this tree in
prefix, postfix and infix.
2. Write a method
that returns the product of items in a tree.
· Recursive Searching Problem
|
1021
2102
2011
1102
2011
1122
1212
2012
|
1.
Show the list after each merge of merge sort.
2.
Show the list after each split and each merge of quick sort
|