1) What are Objects and Classes?
new
instances / objects.
2) Intro to Objects (with Turtles)
3) Creating Turtle Objects
4) Programming Challenge : Turtle Drawing
new
again1) Overloading Constructors
2) The World Class Constructors
3) The Turtle Class Constructors
4) Object Variables and References
5) Constructor Signatures
6) Formal and Actual Parameters
12) Programming Challenge: Custom Turtles
1) Procedural Abstraction
2) Programming Challenge : Draw a Letter
2) toString() Methods
3) Methods with Arguments and Return Values
4) Programming Challenge : Turtle Distances
String
"ciao mondo"
1) String Operators - Concatenation
+
operatorString x = "ciao";
String y = " mondo";
String z = x + y;
System.out.println(z);
ciao mondo
1) String Methods: length
, substring
, indexOf
.length()
"Ciao".length()
4
// 012345678
"un dubbio".length()
9
// 012?.....
System.out.println("un\ndubbio");
un dubbio
// 012 345678
"un\ndubbio".length()
9
.substring(a,b)
a
included b
excludedIndexOutOfBoundsException
// 0123456
System.out.println("cartone".substring(2,5))
rto
No chars:
// 0123456
System.out.println("cartone".substring(2,2))
One char:
// 0123456
System.out.println("cartone".substring(2,3))
r
// 0123456
System.out.println("cartone".charAt(2))
r
// 0123456
System.out.println("cartone".substring(2,3).getClass())
class java.lang.String
char c = "cartone".charAt(2); // OK
char c = "cartone".substring(2,3); // WRONG
String s = "cartone".substring(2,3); // OK
String s = "cartone".charAt(2); // WRONG
.indexOf(s)
indexOf
returns the FIRST occurrence of string s
// 0123456
System.out.println("cartone".indexOf("to"));
3
WARNING: ALWAYS ask yourself if your algorithm still works if you have duplicates:
// 0123456789...
System.out.println("arzigogolato".indexOf("go"));
4
2) CompareTo and Equals
Equals:
.equals(otherString)
methodSystem.out.println("Ciao".equals("Ciao")); // correct way
true
// case sensitive
System.out.println("Ciao".equals("Ciao"));
true
==
operator
Don't use ==
with strings!
WARNING: sometimes seems to work because for efficiency Java may cache strings so two identical copies actually share the same memory address
System.out.println("Ciao" == "Ciao"); // WRONG: HERE WORKS *BY LUCK*
true
compareTo
System.out.println("Mario".compareTo("Wario"));
-10
System.out.println("Wario".compareTo("Mario"));
10
System.out.println("Mario".compareTo("Mario"));
0
Different lengths:
System.out.println("Mario".compareTo("MarioAndLuigi"));
-8
System.out.println("MarioAndLuigi".compareTo("Mario"));
8
Comparison with <
and >
is not supported:
// System.out.println("Mario" < "Wario"); // error
Seems like CSAwsome doesn't have many much material on strings, so
DO THESE EXERCISES: https://codingbat.com/java/String-1
2.8. Wrapper Classes - Integer and Double
Integer i = new Integer(2); // the old way
Double d = new Double(3.5);
Integer i = 2; // Autoboxing in Java 9+
Double d = 3.5;
i.getClass()
class java.lang.Integer
QUESTION: does this work?
2.getClass()
Integer.MIN_VALUE
Integer.MAX_VALUE
sqrt
, pow
, abs
, max
, min
Math.sqrt(5)
2.23606797749979
To raise to a power, you can call pow
:
Math.pow(3,2) // 3 power 2
9.0
WARNING: in Java 3^2
does NOT mean power raise!
Math.abs(-4)
4
Math.min(2,6)
2
Math.min(7,3)
3
Math.max(7,3)
7
Note in Java you can only pass two values:
Math.max(2,6,8) // WRONG: does not work in Java!
System.out.println(Math.random()); // number changes at each call
System.out.println(Math.random());
0.9017304454541571 0.1058621772838938
Use Random
with a seed for repeatable results:
int seed = 10;
Random generator = new Random(seed);
System.out.println("seed = " + seed);
System.out.println(generator.nextDouble());
System.out.println(generator.nextDouble());
System.out.println();
seed = 10;
Random generator = new Random(seed);
System.out.println("seed = " + seed);
System.out.println(generator.nextDouble()); // get the same as before
System.out.println(generator.nextDouble());
System.out.println();
seed = 1234; // changed seed
Random generator = new Random(seed);
System.out.println("seed = " + seed);
System.out.println(generator.nextDouble()); // different sequence
System.out.println(generator.nextDouble());
seed = 10 0.7304302967434272 0.2578027905957804 seed = 10 0.7304302967434272 0.2578027905957804 seed = 1234 0.6465821602909256 0.9513577109193919