170 likes | 239 Views
SOFTWARE AND PROGRAMMING 1. Lecture 23.03.11 TODAY: Python and Java Instructor: Prof. Boris Mirkin web-site http:// www.dcs.bbk.ac.uk/~mirkin/sp109 Revision lecture 11 May: Java main concepts and past exam questions. In-class Test 2 results. Marks improved for those with 20 or greater
E N D
SOFTWARE AND PROGRAMMING 1 Lecture 23.03.11 TODAY: Python and Java Instructor: Prof. Boris Mirkin web-site http://www.dcs.bbk.ac.uk/~mirkin/sp109 Revision lecture 11 May: Java main concepts and past exam questions
In-class Test 2 results • Marks improved for those with 20 or greater • Distribution • 34 or less 9 • 35-50 11 • 51-74 9 • 75 or more 7 _____________________________ • Total 36 PLS – DO COME to SP1 Exam 31 May, even if your expected mark is low: Head count is done over exams only, and our HEFCE funding depends on that
Java/Python: Some history Java: SUN Microsystem’s creation supported by Netscape (currently, Mozilla) web browser – 1996, 2004 Python: one dedicated man’s development, Van Rossum (Netherlands, USA) – 1994, 2000, 2008
Python and Java: Similarities - Object oriented • Typed variables • Static/non-static • Default constructors • User defined types • Assignments • Arithmetic operations • Expressions • If/elseif/else branching • Header-body method/function structure • Local variables • String/array entries indexed from 0,1,…
Python and Java: Differences Java Python Compiler/Interpreter Interpreter Interactive regime Lower level, e.g. Higher level, e.g. System.out.println(); print() Class cannot change Structuring by using Structuring by using ; and {} ident / line
Python and Java: Differences Java Python Loop for: formal Loop for: informal Type: explicit Type: implicit class – always only for user-defined types Work within main method No main method Array & List & Collections (list, set, map) Collections (tuple,set, dictionary)
Python and Java: Differences Java HW.java class HW{ public static void main(String[] args){ System.out.println(“Hello world”);} } Python HW.py >>>print(“Hello world”) *Java’s System.out.print(“Hello world”);} is Python’s print(“Hello world”),
Python and Java: Differences STRINGS: Python is much richer, e.g. >>>s=‘Hello, world’ 0 1 2 3456 7 891011 >>>s.find(‘l’) 2 >>>ns=s.replace(‘o’,’X’) >>>print ns ‘HellX, wXrld’ >>>s.split() [‘Hello,’, ‘world’] >>>s.split(‘l’) [‘He’, ‘o, wor’, ‘d’]
Python and Java: Differences Input from keyboard Java: Scanner class called, Instance created and its method used to feed in Python: input anything (or raw input for all inputs treated as strings) print(“Please enter a number’) R=input() print(“You entered ”, R)
Python and Java: Differences For loop Task: print numbers from 1 to 10 Java for( int count=1;count<11;count++) System.out.println(count); Python for count in range(1,11) print(count)
Python and Java: Differences For loop in array/list Task: print contents of array/list demoar Java for( int count=0;count<demoar.length;count++) System.out.println(“Current item ”+demoar[count]); Python demoar=[‘life’, 5, 6, ‘and’, ‘other’, 333] for count in demoar print(“Current item %s” % count)
Python and Java: Differences METHOD/Function Java: a special structure output_type o_name(typed parameters) { BODY }//, e.g. int square(int x){ return x*x;} Python: an object (flexibility!) def o_name(typed parameters): BODY #, e.g. def square(x): return x*x
Python and Java: Differences Class and instances Java: class Pet{ BODY } Pet p1=new Pet(); Pet p2=new Pet(); //two instances Python: class Pet: BODY p1=Pet() p2=Pet()
Python and Java: Differences Constructor Java: method class_name no_output_type class Pet{ int v1; String v2; Pet(int a, String B){ v1=a; v2=B;} } Pet p1=new Pet(5, “HaHa”); Pet p2=new Pet(3, “AhAh”);
Python and Java: Differences Constructor Python: method __init__() class Pet: int v1; String v2; def __init(self, int a, String B){ self.v1=a self.v2=B # ‘self’ in Python, as ‘this’ in Java, refers to the # instance being created p1= Pet(5, “HaHa”); p2= Pet(3, “AhAh”);
Python and Java: Differences Handling files in Python: Downloading a file import urllib urllib.urlretrieve(http://blah/blah/important.pdf, “nonimportant.pdf”) File input F=open(‘test.txt’, ‘r’) for line in F: print line[0] F.close
Python and Java: Differences Handling files in Python: File output F=open(‘want.txt’, ‘w’) #or, F=open(‘want.txt’, ‘a’) for appending for line in F: F.write line F.close