This file contains hidden or 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
NutritionFacts cocaCola = new NutritionFacts(); | |
cocaCola.setServingSize(240); | |
cocaCola.setServings(8); | |
cocaCola.setCalories(100); | |
cocaCola.setSodium(35); | |
cocaCola.setCarbohydrate(27); |
This file contains hidden or 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
// JavaBeans Pattern - allows inconsistency, mandates mutability | |
public class NutritionFacts { | |
// Parameters initialized to default values (if any) | |
private int servingSize = -1; // Required; no default value | |
private int servings | |
= -1; // Required; no default value | |
private int calories | |
= 0; | |
private int fat | |
= 0; |
This file contains hidden or 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
NutritionFacts cocaCola = | |
new NutritionFacts(240, 8, 100, 0, 35, 27); |
This file contains hidden or 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
// Telescoping constructor pattern - does not scale well! | |
public class NutritionFacts { | |
private final int servingSize; // (mL) | |
required | |
private final int servings; | |
// (per container) required | |
private final int calories; | |
// (per serving) | |
optional | |
private final int fat; |
This file contains hidden or 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
Toast.makeToast(this,"message",Toast.LENGTH_LONG).show() |
This file contains hidden or 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 doubler = multiplier(2) | |
print(doubler(5.6)) // 11.2 |
This file contains hidden or 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
fun multiplier(factor: Double): (Double) -> Double = { number -> number*factor } |
This file contains hidden or 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
circleOperation(5.3, { (2 * Math.PI) * it }) |
This file contains hidden or 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
print(circleOperation(3.0, ::calArea)) // 28.274333882308138 | |
print(circleOperation(3.0, calArea)) // won't compile | |
print(circleOperation(3.0, calArea())) // won't compile | |
print(circleOperation(6.7, ::calCircumference)) // 42.09734155810323 |
This file contains hidden or 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
fun calCircumference(radius: Double) = (2 * Math.PI) * radius | |
fun calArea(radius: Double): Double = (Math.PI) * Math.pow(radius, 2.0) | |
fun circleOperation(radius: Double, op: (Double) -> Double): Double { | |
val result = op(radius) | |
return result | |
} |