score = float(input('Input a course score:'))
if course_score >= 70.0:
print('Pass')
else:
print('Fail')
// Java Translation:
public class CourseGrade
{
public static main(String[ ] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Input a course score: ");
double courseScore = Double.parseDouble(
s.nextLine( ));
if (courseScore >= 70.0)
{
System.out.println("Pass");
}
else
{
System.out.println("Fail");
}
}
}
(1) (2) (3)
| | |
V V V
for(initialization; condition; iteration)
{
action;
}
next statement;
(1) The initialization is performed once at the beginning of the loop.
for(int i = 1; i <= 100; i++)
{
System.out.println(i);
}
public static void main argsAns:
public static returnType methodName(
parameterDatatype1 param1,
parameterDatatype2 param2) {
body of method;
return output;
}
# h.
# Use a for loop in Java.
# Python Statements:
def countDown(n):
while n >= 0:
print(n)
n -= 1
print("\nBlastoff")
# Standalone script.
countDown(10)
// Java Statements:
public class CountDown
{
public static void countDown(int n)
{
for(int i = n; i >= 0; i--)
{
System.out.println(i);
}
System.out.println("\nBlastoff");
}
public static void main(String[ ] args)
{
countDown(10);
}
}
# i.
# Python Statements:
def celToFahr(cel):
fahr = 9.0 * cel / 5.0 + 32.0
return fahr
# Standalone script:
c = 20.0
print(celToFahr(c))
// Java Statements:
public class Cel2Fahr
{
public static celToFahr(double cel)
{
double fahr = 9.0 * cel / 5.0 + 32.0;
return fahr;
}
public static void main(String[ ] args)
{
System.out.println(celToFahr(20.0));
}
}
String[ ] a = {"abc", "def", "hij"};
# Method 1: access by value:
for(String s : a)
{
System.out.print(s + " ");
}
# Method 2: access by index:
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
# Python Statements:
import random
for i in range(0, 20):
roll = random.randrange(1, 7)
print(roll, end=" ")
print( )
// Java Statements:
public class DieRolls
{
Random r = new Random( );
for(int i = 0; i <= 19; i++)
{
int roll = r.nextInt(6) + 1;
System.out.print(roll + " ");
}
System.out.println( );
}
public class LongSong
{
public static void main(String[ ] args)
{
// Print 99 verses of the song.
playSong(99);
}
public static void playSong(int numVerses)
{
for (int n = numVerses; n >= 1; n--)
{
System.out.println(n +
" bottles of beer on the wall,");
System.out.println(n +
" bottles of beer.");
System.out.println(
"Take one down and pass it around,");
System.out.println((n - 1) +
" bottles of beer on the wall.\n");
}
}
}
# Python List:
a = [18, 22, 37, 40]
// Java Array:
int[ ] a = {18, 22, 37, 40};
# Python List
a = [0, 0, 0, 0] // or a = list([0] * 4)
a[0] = 18
a[1] = 22
a[2] = 37
a[3] = 40
// Java array
// In Java, the size of array must be
// specified in advance.
int[ ] a = int[4];
a[0] = 18;
a[1] = 22;
a[2] = 37;
a[3] = 40;
# Method 1: by value
for n in a:
print(n)
# Method 2: by index
for i in range(0, len(a)):
print(a[i])
Java uses these same two methods to print an array:
// Method 1: by value
for(int n : a)
{
System.out.print(n + " ");
}
// Method 2: by index
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
def __init__(self, the_name, the_gender, the_age):
self.name = the_name
self.gender = the_gender
self.age = the_age
Java Constructor Definition:
public String name;
public char gender;
public int age
public Person(String theName, char theGender, int theAge)
{
name = theName;
gender = theGender;
age = theAge;
}
Alternatively, the instance variables can be assigned like this:this.name = theName; this.gender = theGender; this.age = theAge;The instance variable this refers to the current object in Java like self refers to the current object in Python.
p = Person("Alice", "F", 11)
Java Constructor Invocation:
Person p = new Person("Alice", 'F', 11);
Python __str__ Method Definition:
def __str__(self):
return self.name + " " + self.gender + " " + str(self.age)
or
def __str__(self):
return f"{self.name} {self.gender} {self.age}"
or
def __str__(self):
return "{0} {1} {2}".format(self.name, self.gender, self.age)
Java toString Definition:
// Java toString method using concatenation.
public String toString( )
{
return name + " " + gender + " " + age;
}
// Java toString method using a format string
String toString( )
{
return String.format("%s %c %d", name, gender, age);
}
Python str Invocation:print(str(p))or
print(p)Java toString Invocation:
System.out.println(p.toString( ));or
System.out.println(p);