Project JETT/Duke 1 Project JETT Java Engagement for Teacher Training Duke University Department of Computer Science Association for Computing Machinery.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Computer Science 209 Software Development Equality and Comparisons.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
Java Syntax Primitive data types Operators Control statements.
Abstract Classes.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
CPS 108 : Spring From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea  Not really a standard language like C++  Arguably.
Arrays And ArrayLists - S. Kelly-Bootle
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
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.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Lists and the Collection Interface Review inheritance & collections.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Object-Oriented Programming Part 3 Bank Account Embezzling Hood College JETT Workshop Dept. of Computer Science February Objectives of this presentation:
Project JETT/Duke 1 Project JETT Java Engagement for Teacher Training Duke University Department of Computer Science Association for Computing Machinery.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
1 ArrayList Starring: Purse, Sr. Co-Starring: Purse, Jr. Poe Numbers.
Java Type System and Object Model Horstmann ch , 7.7.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Written by: Dr. JJ Shepherd
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.
Object Oriented Programming Lecture 2: BallWorld.
Web Design & Development Lecture 9
Sixth Lecture ArrayList Abstract Class and Interface
Java AP Computer Science AB: Tradeoffs, Intuition,
(like an array on steroids)
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Interfaces and Inheritance
TCSS 143, Autumn 2004 Lecture Notes
Polymorphism.
Grouped Data Arrays, and Array Lists.
ArrayLists 22-Feb-19.
Introduction to Data Structure
Java Programming Language
Java String Class String is a class
What can you put into an ArrayList?
Presentation transcript:

Project JETT/Duke 1 Project JETT Java Engagement for Teacher Training Duke University Department of Computer Science Association for Computing Machinery College Board

Project JETT/Duke 2 Goals of JETT l ACM's long-term goal is the creation of a community of computer science teachers, university faculty, undergraduate and graduate students, as well as College Board Lead Teachers. l The short-term objective is to assist high school computer science teachers in making the AP language switch from C++ to Java for the upcoming academic year. l ACM is partnering with the College Board and selected universities to provide teachers with on-site workshops as well as remote training via the ACM K-12 Web Repository.

Project JETT/Duke 3 Java primitives (not objects) Logical : boolean  No boolean/int conversion or cast while(list) {…} Text: char  Unicode, not part of AP subset Integral: byte, short, int, long  By definition: 8, 16, 32, 64 bits long Floating point: double, float  Use double, float not in the subset

Project JETT/Duke 4 Java Object Basics l Class is an object factory  Class can have (static) methods  Defines interface programmers depend on  Part of inheritance hierarchy (every class is-an Object)  May define getter and setter methods for accessing state  Usually defines methods for behavior l An object is an instance of a class  Attributes aka state, usually private, sometimes accessible  Behavior invoked by calling methods  Allocated by calling new

Project JETT/Duke 5 When are objects equal? l For primitive types we test for equality with ==  We expect 7 == 7, and x == y if both are 7 l For objects the == check sees if addresses are the same  Remember for x == y non-primitive, pointers used! l All classes inherit equals(..) from Object, must override in subclasses public boolean equals(Object o) { SimpleRectangle s = (SimpleRectangle) o; return width == s.width && height == s.height; }

Project JETT/Duke 6 Java Array basics l Syntactically similar to C++ arrays int[] list = new int[10]; // values stored? String list2[] = new String[100]; for(int k=0; k < list.length; k++) { list[k] = 3; } Arrays.fill(list2,"Hello"); // not in subset l Once created cannot resize, indexing is range-checked  Array object is-an Object via inheritance  Default value is 0/null, not in subset

Project JETT/Duke 7 ArrayList basics l ArrayList stores Objects, not int, not boolean, yes String …  Don't have syntactic sugar of [] indexing  Must cast when getting object out of ArrayList Except when treating as an object, e.g., using toString()  Can add to end in constant time, in middle with shifting Array grows, use size() method to determine # elements ArrayList list = new ArrayList(); list.add(new String("hello")); list.add(new String("world")); String s = (String) list.get(0); // what is it? list.set(0, new String("big")); list.add(0, new String("great"));

Project JETT/Duke 8 Arrays and the AP subset l One and two-dimensional arrays in subset  Two-dimensional arrays will move to AB only int[][] grid = new int[6][10]; int rows = int.length; int cols = int[0].length; Initialization in subset, e.g., int[] list = {1,2,3,4,5}; l No java.util.Arrays methods in subset  sort, binarySearch, fill, asList, …

Project JETT/Duke 9 ArrayList and the AP subset l Inheritance hiearchy (List in java.util) is AB only  Iterator and ListIterator are AB only l Downcast from Object to expected type is in subset list.add(new String(“hello”)); String s = (String) list.get(0); Required methods :  size(), get(int), set(int, Object),  add(Object), add(int, Object), remove(int) l NOT required:  remove(Object), addAll(Collection), clear()

Project JETT/Duke 10 What is an Iterator? l What problems do Iterators address?  Access elements independently of implementation  Client programs written in terms of generic component public void print(Collection c) { Iterator it = c.iterator(); while (it.hasNext()) { System.out.println(it.next()); } l How do you add all elements of Set to a List?

Project JETT/Duke 11 What is an interface? l Indication to programmers and code that a class implements some specified functions, most likely with requirements on behavior  Iterator is an interface: what do we expect from classes that implement the Iterator interface?  Comparable: are some objects incomparable? Why?  Why isn’t Equatable an interface? Where is.equals() ? l A class can implement multiple interfaces  Comparable, Cloneable, Tickable, … l Think twice before developing inheritance hierarchy  Single inheritance, problems with protected/private data

Project JETT/Duke 12 What is Comparable? String a = “hello”; String b = “zebra”; int x = a.compareTo(b); // what values assigned? int y = b.compareTo(a); int z = a.compareTo(a); Contract: compareTo() should be consistent with equals()  What’s the simple way to write equals for Comparable? l See also java.util.Comparator  Not in subset, useful for sorting on other criteria

Project JETT/Duke 13 Guidelines for using inheritance l Create a base/super/parent class that specifies the behavior that will be implemented in subclasses  Some functions in base class may be abstract Subclasses required to implement, or cannot create object  Consider using an interface if there’s no default behavior or state to provide l Inheritance models “is-a” relationship, a subclass is-a parent- class, can be used-as-a, is substitutable-for  Standard examples include animals and shapes Square should NOT derive/inherit from rectangle A square is NOT a rectangle in programming terms

Project JETT/Duke 14 Inheritance (language independent) l First view: exploit common interfaces in programming  Streams in C++, Iterator in Java Iterators in STL/C++ share interface by convention/templates  Implementation varies while interface stays the same l Second view: share code, factor code into parent class  Code in parent class shared by subclasses  Subclasses can override inherited method Can subclasses override and call? l Polymorphism/late(runtime) binding (compare: static)  Actual function called determined when program runs, not when program is compiled

Project JETT/Duke 15 Top 10: Choosing array vs ArrayList 10. a.length has same number of characters as a.size() but doesn’t require using the shift key 9. Too many options: int[] list compared to int list[] 8. Array initialization with int[] list = {1,2,3,4,5}; 7. Freedom not to choose: for(int k=0; k < a.size(); k++) … Iterator it = a.iterator(); while (it.hasNext())…

Project JETT/Duke 16 Top-10: choosing array vs ArrayList 6. ArrayList, what’s that: a list or an array? 5. “Take away my Integer, but leave me my int” --- Puff Daddy 4. You can add a String to an ArrayList, but you can’t get a String out of an ArrayList (well, ok, you can with a cast). 3. Collections.sort is stable, Arrays.sort isn’t (mostly) 2. list[k] = list[j] vs. list.set(k,list.get(k)); 1. No method to shuffle an array ( Collections.shuffle )