Docsity

Introduction to Java for Python Programmers - Computer Science I | CS 110, Assignments of Computer Science

Toggle sidebar
bg1
CS 110: Introduction to Computer Science Spring 2008
Introduction to Java for Python Programmers
THOUGHT FOR THE DAY:
PROGRAMMING LANGUAGES ARE VERY MUCH THE SAME. ONCE
YOU'VE HAD ONE, YOU'VE HAD THEM ALL!
Having said that, let's discuss some of the basic differences
between Java and Python...
Running a Program
Python is interpreted, meaning you compile and run program in one
step:
python prog.py
Java has two phases. First, you compile the source code into
java
byte code
. Then you ‘run’ the java byte code.
javac Prog.java
java Prog
Purely Object-Oriented
With Java, all code is written within a class. Each file has one class
defined in it, and the file name must be the same as the class name
(with .java appended)
All methods are defined subordinate to a class.
All variables are defined subordinate to a class or method (no evil
globals!)
Types
With Java, you must declare the type of everything: parameters,
locals, and return values.
pf2

Partial preview of the text

Download Introduction to Java for Python Programmers - Computer Science I | CS 110 and more Assignments Computer Science in PDF only on Docsity!

CS 110: Introduction to Computer Science Spring 2008

Introduction to Java for Python Programmers

THOUGHT FOR THE DAY:

PROGRAMMING LANGUAGES ARE VERY MUCH THE SAME. ONCE YOU'VE HAD ONE, YOU'VE HAD THEM ALL!

Having said that, let's discuss some of the basic differences between Java and Python...

Running a Program

Python is interpreted, meaning you compile and run program in one step: python prog.py Java has two phases. First, you compile the source code into byte code. Then you ‘run’ the java byte code. java

javac Prog.java java Prog

Purely Object-Oriented

With Java, all code is written within a class. Each file has one class defined in it, and the file name must be the same as the class name (with .java appended) All methods are defined subordinate to a class. All variables are defined subord globals!) inate to a class or method (no evil

Types

With Java, you must declare the type of everything: parameters, locals, and return values.

CS 110: Introduction to Computer Science Spring 2008

In-Class worksheet

Take a look at the provided Coordinate programs in Java and Python:

http://www.cs.usfca.edu/~wolber/courses/110.f07/javaSamples/C oordinate.java

http://www.cs.usfca.edu/~wolber/courses/110.f07/python/coord.p y How are the two languages different? In particular, answer the questions on the next page:

  1. How are constructors defined differently in the two languages? How are they named?
  2. How are Python? What do you see in the function definitions of Java that you functions defined differently in Java compared to don't see in Python? What does 'self' in Python and 'this' in Java refer to?
  3. Are function calls the same in Java and Python?
  4. How is the ‘main’ function de Python? fined differently in Java than it is in

In Class Assignment members ‘name’ and ‘age’ (name is of type String), two: With Java, write a class Person with data

constructors (one with no parameters and one with two), and a method ‘older’ which returns true if the object itself is older than the person sent in as a parameter. class, create two persons with data, call older, and print out a result In the main program for the statement.