This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public record AnimalImpl(String name, String noise) implements Animal { | |
@Override public String getName() { return this.name; } | |
@Override public String getNoise() { return this.noise;} | |
} | |
public static <T> void also(T thing, Consumer<T> block) { | |
final T finalThing = thing; | |
block.accept(finalThing); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Animal { | |
val name: String | |
val noise: String | |
fun makeNoise(): String = "$name made a noise: $noise" | |
} | |
fun useThing(thing: Any?) { | |
if (thing is Animal) { | |
println("thing is an animal!! what kind?? ${thing.name}") | |
println(thing.makeNoise()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Animal { | |
String getName(); | |
String getNoise(); | |
default String makeNoise() { return getName() + " made a noise: " + getNoise(); } | |
} | |
public static void useThing(Object thing) { | |
if (thing instanceof Animal animalThing) { | |
System.out.println("thing is an animal!! what kind?? " + animalThing.getName()); | |
System.out.println(animalThing.makeNoise()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val random = Random | |
val positiveInts = 1 .. Int.MAX_VALUE | |
val negativeInts = -1 downTo Int.MIN_VALUE | |
val sign = when(val randomNumber = random.nextInt()) { | |
in positiveInts -> "positive $randomNumber" | |
in negativeInts -> "negative $randomNumber" | |
else -> "zero" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val random = Random | |
val randomNumber = random.nextInt() | |
val positiveInts = 1 .. Int.MAX_VALUE | |
val negativeInts = -1 downTo Int.MIN_VALUE | |
val sign = when(randomNumber) { | |
in positiveInts -> "positive" | |
in negativeInts -> "negative" | |
else -> "zero" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val random = Random | |
val randomNumber = random.nextInt() | |
val positiveInts = 1 .. Int.MAX_VALUE | |
val negativeInts = -1 downTo Int.MIN_VALUE | |
val zero = 0 .. 0 | |
val sign = when { | |
randomNumber in positiveInts -> "positive" | |
randomNumber in negativeInts -> "negative" | |
else -> "zero" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val random = Random | |
val randomNumber = random.nextInt() | |
val sign = if (randomNumber > 0) { | |
"positive" | |
} else if (randomNumber < 0) { | |
"negative" | |
} else { | |
"zero" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val random = Random() | |
val randomNumber = random.nextInt() | |
val message = "Number is {0}" | |
val sign: String | |
if (randomNumber > 0) { | |
sign = "positive" | |
} else if (randomNumber < 0) { | |
sign = "negative" | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var random = new Random(); | |
var randomNumber = random.nextInt(); | |
var message = "Number is {0}"; | |
String sign; | |
if (randomNumber > 0) { | |
sign = "positive"; | |
} else if (randomNumber < 0) { | |
sign = "negative"; | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val Contestant.congratulation: String | |
get() = "Congratulations to the winner of the thing: $contestantName!!!" |