From 37298bbc750fe78bde0a965665b8b21afee1b7b3 Mon Sep 17 00:00:00 2001 From: CoffeeGopher Date: Tue, 30 Aug 2022 17:02:15 -0700 Subject: [PATCH 1/2] Modified variables + print statements as requested by Part 1 --- src/PricelessScript.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PricelessScript.java b/src/PricelessScript.java index 887d88a..5004372 100644 --- a/src/PricelessScript.java +++ b/src/PricelessScript.java @@ -9,18 +9,18 @@ public static void main(String[] args) { // String variables, some with format specifiers String scriptTemplateLine1 = "%d tickets: %s"; String scriptTemplateLine2 = "%d hotdogs, %d popcorn, %d sodas: %s"; - String scriptTemplateLine3 = "1 autographed baseball %s"; - String scriptTemplateLine4 = "real conversation with 11 year old son: %s"; + String scriptTemplateLine3 = "2 autographed baseball %s"; + String scriptTemplateLine4 = "watching the Giants win: %s"; String priceless = "priceless"; // integer variable - int people = 2; + int people = 3; // 32 bit floating point variable float ticketPrice = 14.0f; // 32 Bit, but it does exist! // double precision floating point variable double itemPrice = 9.0; // Double precision // boolean variable - boolean trueOrFalse = true; + boolean trueOrFalse = false; NumberFormat formatter = NumberFormat.getCurrencyInstance(); @@ -33,7 +33,7 @@ public static void main(String[] args) { System.out.printf((scriptTemplateLine2) + "%n", people, people, people, formatter.format(people * itemPrice)); - itemPrice = 45.0; + itemPrice = 90.0; // very sneaky! System.out.println( String.format(scriptTemplateLine3, formatter.format(itemPrice))); From 90c133514996da95e708f205f3f2ce2e4c9fab77 Mon Sep 17 00:00:00 2001 From: CoffeeGopher Date: Tue, 30 Aug 2022 17:38:26 -0700 Subject: [PATCH 2/2] Provided answers for Part 2 in the README --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 863e29d..1e5294d 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,27 @@ Take note of the various variables and their data types. Write a brief summary i * Its data type * and example values you can assign them. +|Name|Type|Example(s)| +|---------------|----------------|----------------| +|scriptTemplateLine1|String|`"Hello!"` +|scriptTemplateLine2|String|`"world!"` +|scriptTemplateLine3|String|`"There are"` +|scriptTemplateLine4|String|`"5"` +|priceless|String|`"strings in this file."` +|people|int|`5, 2, -3` +|ticketPrice|float|`3.2f, -5.0f, 82.25f` +|itemPrice|double|`52.34, 0.0, Double.MAX_VALUE` +|trueOrFalse|boolean|`True, False` +|formatter|NumberFormat|`NumberFormat.getCurrencyInstance()` + Next give TWO example variable names and TWO example variable assignments that are *WRONG* and explain why. * Hint: your IDE can help you discover these! +|Name|Type|Example(s)|Explanation +|---------------|----------------|----------------|---| +|badInt|int|`"3"`|Java does not automatically convert strings to ints. +|badFloat|float|`0.0`|When not given the 'f' at the end, floating point values are seen as doubles by Java. Attempting to run this will give you a compile-time error. + ### Part 3 - Bonus: Play around with Java String Format Specifiers. Pick several of the Java format specifiers below and define variables of the correct type utilize *sout* and *String.format* to view the resulting formats.