COMP 114 Recitation Kimberly Noonan Chris VanderKnyff William Luebke.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Software Engineering Implementation Lecture 3 ASPI8-4 Anders P. Ravn, Feb 2004.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Written by: Dr. JJ Shepherd
Graph Coverage for Design Elements 1.  Use of data abstraction and object oriented software has increased importance on modularity and reuse.  Therefore.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CIT 590 Intro to Programming Lecture 2. Agenda ints, floats, strings, booleans Conditionals Loops Functions Testing The concept of scope.
COMP 114 Chris VanderKnyff Kimberly Noonan William Luebke.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
COMP 114 Kimberly Noonan (040) Chris Vanderknyff (040) William Luebke (039)
COMP 114 Kimberly Noonan Chris VanderKnyff William Luebke.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
COMP 114 Weekly Recitation October 10, 2003 Know What’s Happening Monday? first exam The first exam !
Understanding class definitions Looking inside classes.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips
Local Definitions, Scope, Functional Abstraction, and Polymorphism.
Language Evaluation Criteria
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
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.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Polymorphism Giuseppe Attardi Antonio Cisternino.
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
COMP 114 Chris VanderKnyff Kimberly Noonan William Luebke.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Interfaces and Polymorphism CS 162 (Summer 2009).
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Enterprise JavaBeans Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing.
Generic Programming and Inner classes ge·ner·ic 1a : relating or applied to or descriptive of all members of a genus, species, class, or group : common.
Written by: Dr. JJ Shepherd
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Lecture 2: Data Types, Variables, Operators, and Expressions
Java 4/4/2017 Recursion.
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
Variables and Their scope
Recursion.
Announcements & Review
The Recursive Descent Algorithm
Chap 2. Identifiers, Keywords, and Types
Software Design Lecture : 39.
Subtype Substitution Principle
Presentation transcript:

COMP 114 Recitation Kimberly Noonan Chris VanderKnyff William Luebke

What we’re doing today… Questions about the Test Questions about the Test Questions about Wednesday’s Lecture Questions about Wednesday’s Lecture Recursion Recursion Rubric for Project 4 Rubric for Project 4 Examples of bad code (or, how NOT to get the cleverness/elegance points). Examples of bad code (or, how NOT to get the cleverness/elegance points). Hand back Assignment 3 (for those of you who didn’t get it on Monday or Wednesday). Hand back Assignment 3 (for those of you who didn’t get it on Monday or Wednesday).

What is wrong with… abstract class A { public abstract boolean isB(); } class B extends A { public boolean isB() { return true; }} class C extends A { public boolean isB() { return false; }}

Here’s a better example… // Recall Assignment 3... Number compute(Vector v) // v stores the expression to evaluate { if (v.size()%2==0) throw new IllegalArgumentException("v has an even number of elements!"); Number n=(Number)v.get(0); for (int i=1;i<v.size();i+=2) { Operator o=(Operator)v.get(i); Number m=(Number)v.get(i+1); if (o.getTokenString().charAt(0)=='+') { n=new Number(n.getValue()+m.getValue()); } else if (o.getTokenString().charAt(0)=='-') { n=new Number(n.getValue()-m.getValue()); } else if (o.getTokenString().charAt(0)=='*') { n=new Number(n.getValue()*m.getValue()); } else if (o.getTokenString().charAt(0)=='/') { n=new Number(n.getValue()/m.getValue()); }} return n; }

A better way… // Recall Assignment 3... Number compute(Vector v) // v stores the expression to evaluate { if (v.size()%2==0) throw new IllegalArgumentException("v has an even number of elements!"); Number n=(Number)v.get(0); for (int i=1;i<v.size();i+=2) { Operator o=(Operator)v.get(i); Number m=(Number)v.get(i+1); n=o.operate(n,m); // Use dynamic dispatch... not “instanceof” or equivalent } return n; }

What is wrong with… public class StringScanner implements Enumeration { String _s; int _i; String _t; public StringScanner(String s) {_s=s;trimLeadingSpaces();} void trimLeadingSpaces() { for (;_s.length()>0&&_s.charAt(0)==' ';_s=_s.substring(1)) {}} public String nextToken() { for (_i=1;_i<s.length()&&_s.charAt(_i)!=' ';_i++) {}_t=_s.substring(0,_i);_s=_s.substring(_i);trimLeadingSpaces(); return _t; } public boolean hasMoreTokens() { return _s.length()>0; }}

A better way… public class StringScanner implements Enumeration { String _s; // Fewer instance variables... _s is the only one we really need. public StringScanner(String s) {_s=trimLeadingSpaces(s);} String trimLeadingSpaces(String s) // This is now a more generic method. { // We could even make it static... for (;s.length()>0&&s.charAt(0)==' ';s=s.substring(1)) {} return s; } public String nextToken() // i and t (or _i and _t) are really local to this method. { int i=1; for (;i<_s.length()&&_s.charAt(i)!=' ';i++) {} String t=_s.substring(0,i); _s=trimLeadingSpaces(_s.substring(i)); return t; } public boolean hasMoreTokens() { return _s.length()>0; }}

Complexity is Bad! Fewer variables is more elegant: Fewer variables is more elegant: Fewer states to keep track of. Fewer states to keep track of. Easier to read and think about. Easier to read and think about. Less commenting required! Less commenting required! Programs take less memory with fewer variables. Programs take less memory with fewer variables. It will help down the road: It will help down the road: More complicated programs are composed of simple parts. More complicated programs are composed of simple parts. Multithreading/synchronization issues Multithreading/synchronization issues

Complexity is Bad! Smaller code is more elegant: Smaller code is more elegant: Redundant code – avoid! Redundant code – avoid! Takes up more space in memory Takes up more space in memory Is harder to maintain Is harder to maintain In general copying and pasting code is bad. In general copying and pasting code is bad. Simpler code: Simpler code: Less commenting! Less commenting! Easier to understand – every block does one simple task and does it well. Easier to understand – every block does one simple task and does it well.

Complexity is Bad! Smaller interfaces are more elegant: Smaller interfaces are more elegant: Fewer interactions between software components. Fewer interactions between software components. Implementing classes need only implement a small set of methods to participate in polymorphic functions. Implementing classes need only implement a small set of methods to participate in polymorphic functions. Smaller classes are more elegant: Smaller classes are more elegant: “Do one thing, and do it well.” “Do one thing, and do it well.” Promotes reuse: Promotes reuse: Other classes can extend or delegate to simple classes more easily. Other classes can extend or delegate to simple classes more easily. Complicated classes may be too specific to reuse. Complicated classes may be too specific to reuse. “Swiss-army knife classes” are complicated. “Swiss-army knife classes” are complicated.