TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Higher-Order Functions and Loops c. Kathi Fisler,
Object Oriented Programming with Java
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to everything at once! Static Methods Static Methods Stateless Utility.
TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.
TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.
Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
TeachScheme, ReachJava Adelphi University Thursday morning June 25, 2009.
TeachScheme, ReachJava Adelphi University Tuesday afternoon July 13, 2010.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
TeachScheme, ReachJava Adelphi University Wednesday morning June 24, 2008.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Applications Development
TeachScheme, ReachJava Adelphi University Thursday afternoon July 15, 2010.
Functional Programming Language 1 Scheme Language: part 2.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
1 More About Derived Classes and Inheritance Chapter 9.
Examining Two Pieces of Data
Agenda Warmup AP Exam Review: Litvin A2
Variables A piece of memory set aside to store data
CS 302 Week 11 Jim Williams, PhD.
Java Programming with BlueJ
Object Oriented Programming (OOP) LAB # 8
Stacks.
Stacks.
Let's Play "What's the Question"
Presentation transcript:

TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010

Class composition Define a class LogEntry to represent a runner's daily log. It contains the Date of the run, the distance in miles, the time in minutes, and a free-form comment. Include a constructor several examples a toString method an avgSpeed method an addComment method (which takes in a String and returns a LogEntry just like the old one but with the String added onto whatever comments were already there).

Class composition Define a class Circle to represent a circle on the screen. It contains a center (of type Posn), a radius (double), and a color (String). Include a constructor several examples a toString method an area method a contains method that takes in another Posn and returns a boolean indicating whether that Posn is inside the circle a scale method that takes in a double scaling factor and returns a new Circle like this one but with the radius multiplied by the scaling factor.

Class composition Define a class Rectangle to represent a rectangle on the screen. It contains a top-left corner (of type Posn), a width and height (both double), and a color (String). Include a constructor several examples a toString method an area method a contains method that takes in another Posn and returns a boolean indicating whether that Posn is inside the rectangle a scale method that takes in a double scaling factor and returns a new Circle like this one but with the width and height multiplied by the scaling factor.

Definition by choices Define a data type Shape which is either a Circle or a Rectangle. Since Circle and Rectangle both have constructors, Shape doesn't need one.

Definition by choices interface Shape { } … class Circle implements Shape { … }

interface Shape { } class Circle implements Shape { Posn center; double radius; String color; … } class Rectangle implements Shape { Posn topLeft; double width; double height; String color; … }

What can you do with this? A variable of type Shape can hold either a Circle or a Rectangle: Shape shape1 = new Circle (new Posn(3,4),5,"blue"); Shape shape2 = new Rectangle (new Posn (50,20), 30, 40, "orange");

What can't you do with this? shape1.area() doesn't compile! Why not? In Java, every variable has two types: the static type from its declaration, and the dynamic type from what it actually contains. shape1 was declared as a Shape, so that's its static type. Static type is used to decide what's a legal call and what isn't.

To fix this… interface Shape { public double area (); public boolean contains (Posn other); public Shape scale (double factor); }

What can you do with this? Now you can call the area, contains, and scale methods on a Shape variable shape1.area() // should return c shape2.area() // should return 1200 shape1.scale(2.0) // should return // new Circle(new Posn(3,4), 10, "blue") etc.

time check

Lists in Java A StringList is either an EmptyStringList or a NonEmptyStringList (ESL or NESL for short). An ESL has no parts. A NESL has two parts: first (a String) and rest (a StringList).

Lists in Java Write classes ESL and NESL, and interface StringList. For each class, provide a constructor examples a toString method

Lists in Java Write the following methods on StringLists: countStrings : nothing -> int contains : String -> boolean countMatches : String -> int

Recall add-up function (define (add-up nums) (cond [(empty? nums) 0] [(cons? nums) (+ (first nums) (add-up (rest nums)))]))

Trace add-up function (add-up (list )) (+ 3 (add-up (list 5 2 1))) (+ 3 (+ 5 (add-up (list 2 1)))) (+ 3 (+ 5 (+ 2 (add-up (list 1))))) (+ 3 (+ 5 (+ 2 (+ 1 (add-up empty))))) ; lots of pending +'s (+ 3 (+ 5 (+ 2 (+ 1 0)))) (+ 3 (+ 5 (+ 2 1)))) (+ 3 (+ 5 3)) (+ 3 8) 11

Another approach (define (add-up-accum nums so-far) (cond [(empty? nums) so-far] [(cons? nums) (add-up-accum (rest nums) (+ (first nums) so-far))])) (add-up-accum (list ) 0) (add-up-accum (list 5 2 1) (+ 3 0)) (add-up-accum (list 5 2 1) 3) (add-up-accum (list 2 1) (+ 5 3)) (add-up-accum (list 2 1) 8) (add-up-accum (list 1) (+ 2 8)) (add-up-accum (list 1) 10) (add-up-accum empty (+ 1 10)) (add-up-accum empty 11) 11; never more than 1 pending +

Another approach Of course, add-up-accum is less convenient to use. Easy fix: (define (add-up nums) (add-up-accum nums 0))

Another example ; multiply-positives : list-of-numbers -> number (define (multiply-positives nums) (mp-accum nums 1)) (define (mp-accum nums so-far) (cond [(empty? nums) so-far] [(cons? nums) (mp-accum (rest nums) (cond [(> (first nums) 0) (* (first nums) so-far)] [else so-far]))]))

Generalize Both functions look pretty similar. They differ in the answer to the "empty?" case, and how to combine (first nums) with so-far

In Java… See projects June26 v1 through June26 v5 v1: addUp written by structural recursion v2: addUp written by accumulative recursion v3: addUp becomes static, in a separate class v4: addUp written using for-each loop v5: addUp written using while-loop

Are we done yet? Fill out end-of-day survey Fill out end-of-workshop survey Eat Go home Sleep Teach