03 From Python To Java
03 From Python To Java
CISC124
1
double is the usual choice for routine calculations
involving floating-point values
2
A double literal is any number written using a decimal
and not ending in F or f
it is a compile-time error if the numeric value is
outside the range that an double can represent
double x = 1.0;
double y = 2.;
// scientific notation
double alsoZ = 1.00000099e6;
3
An arithmetic operation involving two double values
always produces a double value
double x = 1.0;
double y = 3.0;
double z = 13.0;
double result = x + y; // 4.0
result = x – y; // -2.0
result = x * y; // 3.0
result = x / y; // 0.33...
result = z / y; // 4.33...
result = (x + y + z) / 3; // 5.66...
4
Unlike the integer types, floating-point computations do
not wrap when values exceed the range of double
values saturate at Double.POSITIVE_INFINITY and
Double.NEGATIVE_INFINITY
5
Python has user-defined functions
procedures that can be called to perform a specific
task, e.g., find the max of two integer values
6
Java has no functions
the closest analog is a public static method
a method is similar to a function but it must be
defined inside of a class (or interface)
7
To call the max2 method, a method in another class
must import the Lecture2 class and then call the max2
method using the syntax
Lecture2.max2(arg1, arg2)
import Lecture2;
8
In Python, indentation is fundamental to the language
required to denote the body of a function, if
statement, for loop, etc.
9
In Java, indentation is used only for readability
the compiler does not care if your code is indented
but use the examples in the slides and course notes for
guidance on good Java programming style
10
Java uses braces to delimit the bodies of classes,
methods, if statements, and loops
the braces are required
11
For the time being, all of our classes will be public
the public access modifier on a top-level class means
that the class is visible to all other classes
12
For the time being, all of our methods will be public and
static
the public access modifier on a method means that
the method is callable from within any class
13
For the time being, all of our methods will be public and
static
the static modifier on a method means that the
method is associated with its class
which is why you use the class name in addition to the
method name when calling a static method
14
The parameters of a Java method must have types
e.g., max2 has two parameters of type int
15
A Java method is allowed to return zero or one value
if a method returns a value, then it must declare the
type of the value that it returns
the type should appear immediately before the method
name
e.g., max2 returns an int value
16
A Java method is allowed to return zero or one value
if a method does not return a value, then it must
declare that it returns the type void
e.g., a main always returns void in Java
import Lecture2;
17
There must be a return statement in a method that
returns a value
the method stops running immediately after the
return statement finishes running
18
A void method has no return statement
import Lecture2;
19
A void method has no return statement
but an empty return statement is legal
import Lecture2;
20
A Python if statement changes program flow based on
one or more conditions:
21
The corresponding method in Java:
23
The corresponding method in Java
24
The conditions must appear inside parentheses
25
The scope of a variable is the region of code where a
declared variable name is usable
in Java, the scope starts where the variable was declared and
goes to the end of the block that the variable was declared in
26
The scope of a variable is the region of code where a
declared variable name is usable
in Java, the scope starts where the variable was declared and
goes to the end of the block that the variable was declared in
27
The scope of a variable is the region of code where a
declared variable name is usable
in Java, the scope starts where the variable was declared and
goes to the end of the block that the variable was declared in
28
A method may have more than one return statement
but there cannot be reachable code after a return statement
some consider multiple return statements to be bad form
29
A method that returns a value must always return a value
most compilers do not attempt logical deduction
30
In Java, && is the logical AND operator
31
In Java, || is the logical OR operator
&& has higher precedence than ||
32
Use parentheses if you want to be explicit about
precedence
33
Start training yourself to avoid writing an if statement
when one is not required:
34
In Java, ! is the logical NOT operator
assume isLeapYear is defined in the class Lecture3
35
Python allows the programmer to chain comparison
operators:
def print_letter(grade):
if grade >= 80:
result = 'A'
elif 70 <= grade < 80:
result = 'B'
# and so on
print(result)
36
Java does not allow chained comparisons. The
programmer must use && and two separate comparisons.
System.out.println(result);
}
37