A Few Review Questions Dan Fleck CS211 Fall 2007

advertisement
A Few Review Questions
Dan Fleck
CS211 Fall 2007
Sort using Radix Sort:
What is the algorithm?
• Group by least significant digit, maintaining order in
the groups
• Group by next least significant digit maintaining order
in the groups
• Repeat until you have no more digits
• Note: If a digit doesn’t exist, move to the front of the
list
Sort using Radix Sort:
103 227 357 897
103
34 227 357
103 208 1009 227
34 1009 103 208
34 103 208 227
34 1009 208
897 208 1009
34
357 897
227 357 897
357 897 1009
Insertion Sort

What is the algorithm?
Partition the array into two regions, sorted and
unsorted.
While unsorted values remain, copy the unsorted value
and insert it into the array in the correct order (shifting
array values as required)
Insertion Sort
10
7
6
6
2
2
2
7
10
7
7
6
6
6
6
6
10
8
7
7
7
8
8
8
10
8
8
8
2
2
2
2
10
10
8
12
12
12
12
12
12
10
8
8
8
8
8
8
12
What is wrong with this?
What are two ways to fix it?
The following class is located in directory:
/usr/dfleck/javacode/debug/src/
package edu.gmu.test;
public class MyTest {
public int add(int a, int b) {
return a+b;
}
}
To make a Jar executable what
does it need to know?
Answer: The class to run when it is
executed. This is done by setting the
main-class attribute in the MANIFEST.MF
file in the jar.
 How do I get a list of files in a jar?

Three compiler errors are present with
this code. What are they?
public class MyTest {
public int add(int a, int b) {
return a+b;
}
public static void main(String []args) {
int v1 = args[0];
int v2 = args[1];
System.out.printf(“the sum of %d and %d is = %d”,
v1, v2, add(v1, v2));
}
Three compiler errors are present with
this code. What are they?
public class MyTest {
public static int add(int a, int b) {
return a+b;
}
public static void main(String []args) {
int v1 = Integer.parseInt(args[0]);
int v2 = Integer.parseInt(args[1]);
System.out.printf(“the sum of %d and %d is = %d”,
v1, v2, add(v1, v2));
}
Rewrite this in java




//
//
//
//
0
1
2
3
0:ldc1
2:astore_1
3:aload_1
4:areturn
#10 <String "hello">
Notes:
-
-
-
ldc1 -- load a constant onto the stack (String constant in this
example)
astore -- pop the stack and store the object (reference) as a local
var X
aload -- push an object (reference) onto the stack
areturn -- return from this method the object on top of the stack
Rewrite this in java




//
//
//
//
0
1
2
3
0:ldc1
2:astore_1
3:aload_1
4:areturn
#10 <String "hello">
public String getString() {
return “Hello”;
}
public class MyClass {
static String course=“CS211”;
String prof = “Fleck”;
private void updateCourse() {
course = “CS310”;
prof = “Nordstrum”;
}
public void main(String []args) {
MyClass a = new MyClass();
MyClass b = new MyClass();
System.out.println(“A Crs is: %s Prof is: %s”,a.course,a.prof);
b.updateCourse();
System.out.println(“A Crs is: %s Prof is: %s”,a.course,a.prof);
System.out.println(“B Crs is: %s Prof is: %s”,b.course,b.prof);
}
}
public class MyClass {
static String course=“CS211”;
String prof = “Fleck”;
private void updateCourse() {
course = “CS310”;
prof = “Nordstrum”;
}
public void main(String []args) {
MyClass a = new MyClass();
MyClass b = new MyClass();
System.out.printf(“\n A Crs is: %s Prof is: %s”,a.course,a.prof);
b.updateCourse();
System.out.printf(“\n A Crs is: %s Prof is: %s”,a.course,a.prof);
System.out.printf(“\n B Crs is: %s Prof is: %s”,b.course,b.prof);
}
}
A Crs is: CS211 Prof is:Fleck
A Crs is: CS310 Prof is:Fleck
B Crs is: CS310 Prof is:Nordstrum
Public class A {
public void aMethod() { /* something */ } }
Public class B extends A {
public void bMethod() { /* something */ } }
Which if any are valid?
1. A a = new A();
2. B b = new B();
3. A cb = new B();
4. a.aMethod();
5. a.bMethod();
6.
7.
8.
9.
b.aMethod();
b.bMethod();
cb.aMethod();
cb.bMethod();
Public class A {
public void aMethod() { /* something */ } }
Public class B extends A {
public void bMethod() { /* something */ } }
Which if any are valid?
1. A a = new A();
2. B b = new B();
3. A cb = new B();
4. a.aMethod();
5. a.bMethod();
6.
7.
8.
9.
b.aMethod();
b.bMethod();
cb.aMethod();
cb.bMethod();
2. (2 points) Given this interface:
public interface CanHear {
public void listen(int volume);
public boolean heardSomething();
public int getNumberOfEars();}
Fix the AlwaysHears class by writing the code necessary for
it to compile.
/** The AlwaysHears class ALWAYS hears something.
* If someone asks if it heard something, it will always
return true. */
public class AlwaysHears implements CanHear {
public int numberOfEars;
public AlwaysHears(int numberOfEars) {
this.numberOfEars = numberOfEars;
}
public void listen(int volume) {
System.out.println(
"I'm listening at volume :"+volume);
}
public float divide(
float numerator,
float denominator)
{
if (denominator == 0) {
throw new Exception(
"You cannot divide by zero!");
}
float returnValue = numerator/denominator;
return returnValue;
}
Does this compile?
If not, give me two ways to fix it.
During debugging why do you look at the
call stack? (and what is the call stack?)
The call stack is the list of methods that
were called to get to the current location
in the code.
 You look at it to confirm you got to this
point in the code by calling the methods
you expected

Can you: Write a recursive method
that takes a parameter n and
will return the sum of integers
1 through n. For example, given
n=3 it should return 6
(3+2+1). This code should work
for any value of n >= 1.

Always remember… these review slides
are NOT complete! You must look at the
study plan on the class website for a more
thorough plan!
Download