1 / 47

CS 200 Additional Topics and Review

This lecture covers additional topics such as recursive algorithms and the Fibonacci sequence. It also includes a review of key principles, tools, and best practices in programming. Prepare for the final exam and enhance your understanding of programming concepts.

matthewt
Download Presentation

CS 200 Additional Topics and Review

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. CS 200 Additional Topics and Review Jim Williams, PhD

  2. Week 14 • Final Exam: Conflict Alternatives Emailed • Sunday, May 6th at 7:45 AM (Morning), Location? • CS Learning Center extended hours • P7 due Thursday • Team Lab: Object Oriented Space Game • Hours Last Week • Course Evaluation • Lecture: Additional Topics and Review

  3. Course Evaluations

  4. Recursive Algorithm • Repeated applications of an algorithm on smaller versions of the same problem. • Eventually reach a base case which is simply solved.

  5. Recall Fibonacci Sequence 1 1 2 3 5 8 13 21 … Each is the sum of the previous 2. https://en.wikipedia.org/wiki/Fibonacci_number

  6. Iterative solution public static void main(String[] args) { int n = 10; int [] fib = new int[n]; fib[0] = 1; fib[1] = 1; for (int i = 2; i < n; i++) { fib[i] = fib[i-1] + fib[i-2]; } System.out.println("fib "+n+"= " + fib[n-1]); }

  7. Recursive Solution static int fib(int num) { if ( num <= 1) return 1; //base case else return fib( num -1) + fib( num -2); } public static void main(String[] args) { System.out.println("fibonacci 4: " + fib(4)); }

  8. How many times is fib method called? static int fib(int num) { if ( num <= 1) return 1; else return fib( num -1) + fib( num -2); } public static void main(String[] args) { System.out.println("fibonacci 4: " + fib(4)); }

  9. Course Review Key Principles, Tools, Diagrams, Data Types, Operators, Keywords, Control Flow, Programming Paradigms, Debugging Techniques, File Input/Output, Commenting & Style, Unit Testing, Memory, Best Practices, Learning Programming

  10. Tools zyBooks, JavaVisualizer, DiffChecker, Command Prompt, notepad, javac, java, Eclipse IDE (editor, compiler, vm, debugger)

  11. Diagrams truth tables, memory model diagrams, control flow charts (activity diagrams), class diagrams, object diagrams, and use-case diagrams.

  12. Data Types Primitive & Reference Primitive: 8 Reference: existing, classes you write.

  13. Operators

  14. Evaluating Java Expressions Precedence Associativity Sub-expressions left-to-right

  15. Key Principles Algorithms Abstraction

  16. Control Flow Sequence Methods Conditionals Loops

  17. Data Structures Arrays: single and multi-dimensional ArrayLists ArrayLists of Arrays

  18. Development Process Edit-Compile-Run cycle Analysis - understand the problem Design - pseudocode

  19. Best Practices • Incremental, systematic, frequent deliverables • Test frequently • Test bench with testing methods

  20. Debugging Techniques • Stepping through in Java Visualizer • print statements to verify and narrow down problem • print statements with DEBUG flag • Eclipse debugger

  21. Programming Paradigms Structured Programming Object-Oriented Programming (brief)

  22. Program Commenting & Style

  23. Learning Programming • lots of rules and interrelated concepts

  24. Notes • Final Exam: Sunday, May 6th, 7:45 AM morning • Location: 125 Agricultural Hall • CS Learning Center extended hours • Friday, May 4: noon until 9 pm • Saturday, May 5: noon until 9 pm • Course Survey • Today: Review

  25. Study Tips • Labs are essentially weekly Study Guides • Trace and Explain are intended to teach specific concepts and challenging aspects. • Preparing to teach is a very effective way to learn. • Imagine you are preparing yourself to teach this material to others this summer or soon. • Go through the labs, explaining to someone (maybe imaginary) what each trace and explain segment is doing.

  26. Exam Tips • Come early, many to scan in. • Assume there is a bug in each code segment • You have to be able to determine what it is and explain exactly how it currently works in order to fix it.

  27. Memory areas static: holds static variables when class is loaded into memory. heap: where instances/objects are allocated stack: where local variables and parameters are allocated each time a method is called.

  28. Memory public class M { int varName = 1; static int cVar = 2; public M(int varName ) { this.varName = varName; } public static void main(String []args) { M[] varName = new M[3]; varName[0] = new M(3); varName[1] = new M(4); } }

  29. Course Evaluations

  30. How many Bug instances in list? ArrayList<Bug> list; list = new ArrayList<Bug>(); list.add( new Bug()); Bug aBug = new Bug(); list.add( aBug); list.add( 0, aBug);

  31. Exceptions new Exception() //records the stack trace throw //starts live exception handling throws //method may throw an exception try-catch //stops live exception handling finally //code to always execute

  32. Exception Handling 3 Categories Error - internal system errors Unchecked Exceptions RuntimeException Checked Exceptions Exception http://www.javamex.com/tutorials/exceptions/exceptions_hierarchy.shtml

  33. Programming Process & Errors Naming/Saving Runtime & Logic Syntax/Compile time Users (Virtual) Machine Editor Compiler Hello.class Hello.java Computer Files Programmer

  34. Classes Class vs Instance Members (from docs) Creating your own

  35. CS 300 Suggestion Setup your own weekly study group reviewing & discussing previous weeks material.

  36. Good Luck on Exam! Have a Great Summer!

  37. Which is false? class Pizza { static String toppings; }

  38. toppings is a(n) class Pizza { private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } }

  39. getToppings() is a(n) class Pizza { private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } }

  40. The visibility modifier private is class Pizza { private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } }

  41. Which is false? class Pizza { private String toppings; public void setToppings( String tops) { if ( tops != null && tops.length() > 0) toppings = tops; } }

  42. Does this print true or false? class Person { private boolean something = false; boolean getThing(boolean something) { return this.something; } public static void main(String []args) { Person p = new Person(); System.out.println( p.getThing( true)); } }

  43. Which is true? class Light { } // in some method Light aLight = new Light(); System.out.println( aLight);

  44. What will print out? class Employee { private static int employeeCount = 0; private final int id; Employee() { this.id = ++employeeCount; } public static void main(String []args) { Employee anEmployee = new Employee(); System.out.println( anEmployee.id); } }

  45. Does this print 0, 1, other or error? public class Person { static int count = 0; private int id; Person() { this.id = ++count; } public static void main(String []args) { System.out.println( Person.count); } }

  46. What will print out? class Employee { private static int employeeCount = 0; private final int id; Employee() { this.id = employeeCount++; } public static void main(String []args) { Employee anEmployee = new Employee(); System.out.println( anEmployee.id); } }

  47. Bike Design a bike class. Instance Fields: numWheels, Color, unique id Class Field: numBikesCreated, used to assign unique id’s to each bike. Constructor: numWheels and Color, automatically sets the unique identifier. Instance Methods: Number of Wheels and id can be accessed but not changed. Color can be changed. Add a toString() method to return all instance field values in String form. Class Method: returns the number of bikes created. Draw the UML diagram and then write the code. Create a BikeShop class that creates 10 bikes and stores in an array. Print out each bike’s number of wheels, color and id using the toString method.

More Related