System.out
belongs to the java.io.PrintStream
classSystem.in
belongs to the java.io.InputStream
classSystem.out
is an instance of the java.io.PrintStream
classsc = new java.util.Scanner(System.in);
sc
object that we created above? How will you find out?choice
function, e.g.
from random import choice num = choice( range( 1, 101 ) ) print( num )– generates a random integer between 1 and 100 inclusive, stores it, prints it
java.util.Random
new
class PrintRandom { public static void main(String[] args) { java.util.Random randy = new java.util.Random(); int num = randy.nextInt(101); System.out.println(num); } }
import
statementsimport
statement allows us to 'abbreviate' the names, e.g.
import java.util.Random; class PrintRandom { public static void main(String[] args) { Random randy = new Random(); int num = randy.nextInt(101); System.out.println(num); } }
import
statements for anything from java.lang
System.out
without an import
In the Japanese dice game of Kitsune Bakuchi, three dice are rolled. If all three show the same value, the player wins four times his/her stake; otherwise he or she loses his/her stake.
Code this game in Java.
But first you need to know how to do if
-statements in Java...
Python | Java |
---|---|
if x < y: print('x is smaller than y') elif x == y: print('x is equal to y') else: print('x is larger than y') |
if (x < y) { System.out.println('x is smaller than y'); } else if (x == y) { System.out.println('x is equal to y'); } else { System.out.println('x is larger than y'); } |
In the Java, note:
elif
does not exist&&
, ||
and !
and
, or
and not
import java.util.Scanner; import java.util.Random; class KitsuneBakuchi { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Random dice = new Random(); System.out.print("How much do you want to wager: "); int wager = sc.nextInt(); int roll1 = dice.nextInt(6) + 1; int roll2 = dice.nextInt(6) + 1; int roll3 = dice.nextInt(6) + 1; int winnings = 0; if (roll1 == roll2 && roll2 == roll3) { winnings = 4 * wager; } System.out.println("Your rolled " + roll1 + " " + roll2 + " " + roll3); System.out.println("You win " + winnings); } }
"Hello"
are objects
String
class (java.lang.String
)
String
objects:
String s1; s1 = new String("Hello"); String s2 = new String("Bye");
String
objects are so common that their creation has a short-cut:
String s1; s1 = "Hello"; String s2 = "Bye";
String
objects0 | 1 | 2 | 3 | 4 | ||
" | H | e | l | l | o | " |
s3
which is the empty string (length 0) with s4
,
which is not empty:
String s3 = ""; String s4 = " ";
Q: What do we learn from the following?
Python | Java |
---|---|
s = "Goodbye" n = len(s) |
String s = "Goodbye"; int n = s.length(); |
String
interfacepublic int length()
public String concat(String str)
public int indexOf(String str)
public String toLowerCase()
public String toUpperCase()
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
String
methodsString s5 = "John"; String s6 = " and "; String s7 = "Edward"; String s8 = s7.substring( s7.indexOf("war") ); String s9 = s7.concat(s6).concat(s5);
String s11 = "ha"; String s12 = s11.concat(s11);
String s12 = s11 + s11;
"ha" * 3
)String
s and other values in a concatenation expression,
Java converts the other values to String
s, e.g.
String s12 = "This is CS" + 2514;(whereas Python would need to use
str(2514)
)println
, e.g.:
int age = sc.nextInt(); System.out.println("Are ye really " + age + " years old?");
int x = 25; int y = 14; System.out.println("My favourite number is " + x + y); System.out.println(x + y + " is my favourite number");
String
s are immutableString
objects are immutable: they aren't changed by methods. Instead,
new objects get created:
String s13 = "John"; s13.toUpperCase(); System.out.println(s13); String s14 = s13.toUpperCase(); System.out.println(s14); s13.concat(" and Edward"); System.out.println(s13); String s15 = s13.concat(" and Edward"); System.out.println(s15);
String
s
String s = ""; while (...) { String input = sc.readLine(); s = s + input; }
StringBuilder
. They are mutable: their append
method modifies the object
StringBuilder sb = new StringBuilder(); while (...) { String input = sc.readLine(); sb.append(input); }