cit 590 intro to programming lecture 13. topics for midterm lecture notes chapters 1 through 9 the...

16
CIT 590 Intro to Programming Lecture 13

Upload: ella-barrett

Post on 28-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

CIT 590Intro to Programming

Lecture 13

Page 2: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Topics for midterm• Lecture notes • Chapters 1 through 9• The book is really good. Do not worry about reading from

any place else• Some concepts like recursion are explained in greater

detail in slides• Read the unit testing page at http://www.cis.upenn.edu/~

matuszek/cit590-2013/Pages/unit-testing-in-python.html• In the files chapter, the concepts of reading from a URL

and persistence and pickle can be safely ignored

Page 3: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Topics for midterm• Ignore the starred portions of the Classes chapter (chapter

7)• Functional programming – skip the last 2 pages

• Pay special attention to list comprehensions

• If you want a more detailed example of classes

http://code.activestate.com/recipes/286239-binary-ordered-tree/• Today’s lecture is included in the midterm• While studying, if you have questions please post on Piazza• Turn to the internet only as a last resort – mainly because

unless you are an experienced programmer the internet tends to confound more than explain

Page 4: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Agenda• Intro to Eclipse• Java• Python to Java translation

• http://www.cis.upenn.edu/~matuszek/cit590-2013/Pages/python-to-java.html has most of the content of today’s lecture

Page 5: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Java reference books• No need to purchase any book• I will use Dr Dave’s slides from this point forth and they

have most/all required content• Unlike the Python book, there isn’t a compact Java book• Popular books

• Head first Java• Java in a nutshell (O’Reilly book) is a good desktop reference• http://docs.oracle.com/javase/7/docs/api/ (Javadocs)

• Do not buy ‘Java – the complete reference’. It is rarely used ….

Page 6: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Eclipse• IDE (integrated development environment) – way more

fully featured than Python IDLE• Installation involves installing Java and then installing

Eclipse• Remember to have either only 32 bit installs or only 64 bit

installs

Page 7: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Java• Been around for over 15 years• Class-based, object-oriented• Named after the large amounts of Java coffee consumed

by the creators• Portability was a big goal when Java was made.

• Computer programs written in the Java language must run similarly on any hardware/operating-system platform

• Achieved by compiling down to Java bytecode as opposed to machine specific bytecode

Page 8: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Making the transition from Python• Every variable has a type• int x = 0;• Statements end with a semi colon• Everything is within a class!• Statements are written within a method and a method is

written within a class• Braces used instead of indentation

for (int x =0; x<10; x++){

//do something

}

Page 9: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Style (please follow these)• While indentation will not make the program fail the way it

does in Python, do not stop indenting your programs• Variables and method names begin with a lower case

letter, and are camelCase.• Names of classes and interfaces begin with a capital

letter, and are CamelCase• Constants (final variables) are written

in ALL_CAPS_WITH_UNDERSCORES.• Opening braces, {, go at the end of a line, not on a line by

themselves. This differs from the C/C++ convention

Page 10: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

General program outlinepackage myPackage; // optional package declaration

import java.util.*; // imports go here; util is needed for ArrayList, HashSet

public class MyClass { // file must be named myPackage/MyClass.java (note capitalization!)

int myInstanceVar; // declare instance variables here

public static void main(String[] args) {

new MyClass().myStartingMethod();

}

void myStartingMethod() {

// declare local variables here

// statements go here; can call other methods

}

}

}

Page 11: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Strings and comments• “arvind” is a string• ‘a’ is a character• There is a difference between a single character and a

single character string. Unlike Python, please be careful about when you are using single versus double quotes

• String concat still works via the + operator• docStrings – just use Eclipse to generate the template

and you will never make a mistake• Single line comments are made with //

Page 12: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Variables and types

double distance;

String firstName;

int count = 0;

char[][] letters = new char[5][5];

declares an array of arrays AND creates it

ArrayList<String> students;

declares but does NOT create an ArrayList

final int CLASS_LIMIT = 38; // constant

Page 13: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Python lists = java arrays• int[] numbers = {1, 2, 3, 4};•  new int[] {1, 2, 3, 4}• Indexing works in the same manner as Python• However there is no slicing

Page 14: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Python lists = java ArrayList• ArrayList<String> languages = new ArrayList<String>();• languages.add("Python");• languages.set(0, "Java");

• You do not have to declare the size of an ArrayList• You can append to an existing ArrayList• More flexible but if you have use cases where you have

specific dimensions you will use an array.

Page 15: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Sets in python = java hashset• When making a new HashSet you will start getting

warning messages if you declare it without a type.• What do these angle brackets actually really mean?• These correspond to Java Generics• More detailed discussion at

http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

Page 16: CIT 590 Intro to Programming Lecture 13. Topics for midterm Lecture notes Chapters 1 through 9 The book is really good. Do not worry about reading from

Dictionaries in python = Hashmap• Hashmap<String,Integer> map = new

Hashmap<String,Integer>()

• Put (key, value)• Get(key)