Download presentation
Presentation is loading. Please wait.
Published byŌΣίμων Γιαννόπουλος Modified over 5 years ago
1
A Few Review Questions Dan Fleck CS211 Fall 2007
2
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
3
Sort using Radix Sort: Wed we got here (Fall 2007)
4
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)
5
Insertion Sort
6
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; }
7
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?
8
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));
9
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));
10
Rewrite this in java Notes: // 0 0:ldc1 #10 <String "hello">
// :astore_1 // :aload_1 // :areturn 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
11
Rewrite this in java public String getString() { return “Hello”; }
// :ldc #10 <String "hello"> // :astore_1 // :aload_1 // :areturn public String getString() { return “Hello”; }
12
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(“B Crs is: %s Prof is: %s”,b.course,b.prof);
13
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 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
14
Public class A { public void aMethod() { /* something */ } } Public class B extends A { public void bMethod() { /* something */ } } Which if any are valid? A a = new A(); B b = new B(); A cb = new B(); a.aMethod(); a.bMethod(); b.aMethod(); b.bMethod(); cb.aMethod(); cb.bMethod();
15
Public class A { public void aMethod() { /* something */ } } Public class B extends A { public void bMethod() { /* something */ } } Which if any are valid? A a = new A(); B b = new B(); A cb = new B(); a.aMethod(); a.bMethod(); b.aMethod(); b.bMethod(); cb.aMethod(); cb.bMethod();
16
2. (2 points) Given this interface: public interface CanHear {
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); }
17
public float divide(. float numerator,
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.
18
During debugging why do you look at the call stack
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
19
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.
20
Always remember… these review slides are NOT complete
Always remember… these review slides are NOT complete! You must look at the study plan on the class website for a more thorough plan!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.