CS 200 Additional Topics and Review

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Programming Languages and Paradigms Object-Oriented Programming.
Writing Classes (Chapter 4)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Problem of the Day  Why are manhole covers round?
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Modern Programming Tools And Techniques-I
The need for Programming Languages
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Programming Logic and Design Seventh Edition
CprE 185: Intro to Problem Solving (using C)
Chapter No. : 1 Introduction to Java.
CSC 221: Computer Programming I Spring 2010
Yanal Alahmad Java Workshop Yanal Alahmad
CSC 221: Computer Programming I Fall 2005
Testing and Debugging.
CompSci 230 Software Construction
Java Software Structures: John Lewis & Joseph Chase
Comp Sci 200 Programming I Jim Williams, PhD.
CS Programming I Jim Williams, PhD.
CS Week 14 Jim Williams, PhD.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CS 200 Arrays, Loops and Methods
CS 302 Week 15 Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CS Week 6 Jim Williams, PhD.
CS 200 More Classes Jim Williams, PhD.
CS 302 Week 8 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
CS 302 Week 9 Jim Williams.
CS 200 Loops Jim Williams, PhD.
CS 200 Additional Topics and Review
Unit 3 Test: Friday.
CS 200 Primitives and Expressions
CS 200 Arrays Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Additional Topics and Review
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CS 200 Objects and ArrayList
Session 2: Introduction to Object Oriented Programming
CS 200 Creating Classes Jim Williams, PhD.
Review of Previous Lesson
Dr. Sampath Jayarathna Cal Poly Pomona
LCC 6310 Computation as an Expressive Medium
CS 200 More Classes Jim Williams, PhD.
CS Week 2 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS Programming I Jim Williams, PhD.
Week 7 CS 302 Jim Williams.
Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth.
CS 200 Objects and ArrayList
Lecture 1 Introduction to Software Construction
CS Programming I Jim Williams, PhD.
Chapter 7 Objects and Classes
CS 240 – Advanced Programming Concepts
Presentation transcript:

CS 200 Additional Topics and Review Jim Williams, PhD

Week 14 Piazza: New CS Declaration Req. @1142 BP2 Team Lab: Object-Oriented Space Game TA Evaluation Hours Last Week & CS 300 Course Evaluation (emailed link) Lecture: UML, Additional Topics

Recent Course Improvements Redesign of Intro Course (CS302 -> CS200) Study Cycle Problem Solving Tips Team Labs as study groups emphasize reading Java, unexpected behavior Projects emphasize incremental development & testing Increased assistants and office hours

Changes Possible Computer Science is influencing every field, including education. This potential seems exciting but not clear in what ways education will be positively influenced.

Brainstorm on Improving Course

Course Survey Lecture, Homework, Projects, Materials, Feedback Instructor: Overall, Responsiveness, Environment Recommend Course Growth Questions (e.g., bring laptop to Team Lab). What would you expect in order to rate course in the top rating (7) of each category? Or why didn't you give a top rating? Please! describe in the open ended notes.

Course Evaluations

Lots of Jobs if you understand Programming Software Developer/Programmer/Software Engineer Testing/Quality Assurance Technical Support Technical Writing Business/Systems Analyst Software Architect Product Manager Project Manager Development Manager Technical Sales Marketing

Analysis and Design Analysis: Describing the Problem Design: Describing a Solution

UML Diagrams UML (Unified Modeling Language) Many diagrams with lots of details Goal in CS 200 is to recognize: Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

UML Diagrams: References Examples of Diagrams: https://www.uml-diagrams.org/ Simple Drawing Tool: http://www.umlet.com/umletino/umletino.html

Use Case Behavior diagram How external users (actors) interact with the system. https://www.uml-diagrams.org/use-case-diagrams.html

Class Diagram Shows structure of a system with features, constraints and relationships. Independent of time. https://www.uml-diagrams.org/class-diagrams-overview.html

Object Diagram A snapshot of a system at a point in time. Instances of classes with specific attribute values. https://www.uml-diagrams.org/class-diagrams-overview.html

Activity Diagram (Control Flow) Behavior diagram Shows flow of control emphasizing sequence and conditions https://www.uml-diagrams.org/activity-diagrams.html

What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

Problem With a command line program show the files in all subfolders of a folder.

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

Recursion Example public class Show { public static void show(File file, String depth) { System.out.printf("%s%s\n", depth, file.getName()); if ( file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { show(files[i], depth + " "); } public static void main(String[] args) { File file = new File("."); show(file, " "); import java.io.File; public class Show { public static void show(File file, String depth) { System.out.printf("%s%s\n", depth, file.getName()); if ( file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { show(files[i], depth + " "); } public static void main(String[] args) { File file = new File("."); show(file, " ");

Recall Fibonacci Sequence 1 1 2 3 5 8 13 21 … First 2: 1, 1 (base case) Others: sum of previous 2 (recursive step) Example: If we want to find the 5th number: 5th = 4th + 3rd https://en.wikipedia.org/wiki/Fibonacci_number

Recursive Solution Base case: Can be simply solved Recursive step: Break into smaller problems 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));

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]);

Iteration vs Recursion Each may be better (simpler, clearer, more efficient) for a problem. Some problems, such as navigating trees, recursion is frequently a helpful and elegant solution.

How does recursion end? base case that simply solves the problem.

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

Key Principles Algorithms Abstraction

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

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

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

Operators

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

Control Flow Sequence Methods Conditionals Loops

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

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

Best Practices Incremental, systematic, frequent deliverables Test frequently Test bench with testing methods Add additional tests as requirements are determined or bugs are found.

Debugging Techniques Stepping through code Problem decomposition Tips Java Visualizer Eclipse debugger Problem decomposition Print statements, debugger Tips

Programming Paradigms Structured Programming Object-Oriented Programming (brief)

Applied Study Cycle frequent, varied interactions with material

zyBooks Participation Activities Assigned readings with small activities due before lecture

Lecture - Activities Definitions & Explanations Drawing Diagrams Demonstration Stories Retrieval Practice Questions, including TopHat

zyBooks Challenge Activities Application of previous week's material to small programs.

Team Lab Weekly paired and small group study with mentor nearby. Required interaction with mentor on key topics. Variety of activities to learn Terms, Read, Write, Test & Debug code.

Weekly Programs Application of programming concepts to design algorithms and write code to solve problems. Small programs early, large programs with milestones later. Incrementally developing skills on larger and more complex programs.

Exams Assess careful reading and tracing skills.

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.

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

How many Bug instances in list? 2 2 copies of reference to 1 bug none, error, no list 3 ArrayList<Bug> list; list = new ArrayList<Bug>(); list.add( new Bug()); Bug aBug = new Bug(); list.add( aBug); list.add( 0, aBug); import java.util.ArrayList; class Bug { private static int count = 0; private final int id; Bug() { id = ++count; } public String toString() { return "Bug:" + id; public class ClassNameHere { public static void main(String[] args) { ArrayList<Bug> list = new ArrayList<>(); list.add( new Bug()); Bug aBug = new Bug(); list.add( aBug); list.add( 0, aBug); System.out.println( list);

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

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

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

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

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

Good Luck on Exam! Have a Great Summer!

Which is false? every instance will have the exact same toppings toppings is a class variable can be accessed by any method cannot be changed class Pizza { static String toppings; } A, B & C are true D is false since toppings would have to be 'final' to not be changed.

toppings is a(n) instance (non-static) variable class Pizza { private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } instance (non-static) variable class (static) variable parameter local variable A is true, B, C and D are false

getToppings() is a(n) method getter class Pizza { private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } method getter accessor provides read-only access to toppings field All of the above All are true.

The visibility modifier private is appropriate should be public doesn't allow a user of this class to change the field (attribute) directly allows only methods within this class to change the field. class Pizza { private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } A, C & D are true B is false

Which is false? is a class method class Pizza { can change toppings (write access) is a setter is a mutator class Pizza { private String toppings; public void setToppings( String tops) { if ( tops != null && tops.length() > 0) toppings = tops; } B, C and D are all true. A is false as setToppings does not have the 'static' keyword in the header before void.

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)); true false error/other try it and see.

Which is true? class Light { } // in some method error - no constructor Object classes' toString() will be called an instance of Light has nothing in it error class Light { } // in some method Light aLight = new Light(); System.out.println( aLight); A: false, since there is no constructor the default, no-arg constructor is provided by the compiler best answer: B: Object classes' toString() will be called C: false, since Light implicitly extends from Object and therefore when instantiated has all the instance fields of class Object within it. D: no error noted, although code isn't complete as shown, but with reasonable assumptions should run.

What will print out? 1 can't access a private field outside the class 1 can't access a private field outside the class error 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); try it and see

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); 1 other error try it and see

What will print out? 1 can't access a private field outside the class 1 can't access a private field outside the class error 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); try it and see

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. Can you write the code and draw the diagram?