1 / 20

A Few Review Questions

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

jalena
Download Presentation

A Few Review Questions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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: 103 227 357 897 34 1009 208 103 34 227 357 897 208 1009 103 208 1009 227 34 357 897 34 1009 103 208 227 357 897 34 103 208 227 357 897 1009

  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 10 7 6 8 2 12 8 7 10 6 8 2 12 8 6 7 10 8 2 12 8 6 7 8 10 2 12 8 2 6 7 8 10 12 8 2 6 7 8 1012 8 2 6 7 8 8 1012

  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 • // 0 0:ldc1 #10 <String "hello"> • // 1 2:astore_1 • // 2 3:aload_1 • // 3 4: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 • // 0 0:ldc1 #10 <String "hello"> • // 1 2:astore_1 • // 2 3:aload_1 • // 3 4: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(“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); } }

  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 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

  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 { 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, 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? (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! You must look at the study plan on the class website for a more thorough plan!

More Related