From db870f5aff6c030deb3bd9f2e8d284e2924d49ec Mon Sep 17 00:00:00 2001 From: Trevor Hartman Date: Fri, 25 Aug 2023 14:29:21 -0700 Subject: [PATCH] Making Java-Lab-002 available. --- README.md | 78 ++++++++++++++++++++++++++++++---------- src/LikeAGirl.java | 55 ++++++++++++++++++++++++++++ src/PricelessScript.java | 45 ----------------------- 3 files changed, 114 insertions(+), 64 deletions(-) create mode 100644 src/LikeAGirl.java delete mode 100644 src/PricelessScript.java diff --git a/README.md b/README.md index c10cdb7..242aa3c 100644 --- a/README.md +++ b/README.md @@ -5,24 +5,52 @@ Be able to explain what variables are. Understand variable types, allowed names, and valid values. Know how to create and use string, integer, floating-point, and boolean variables. -### Part 1 - PricelessScript.java - [MasterCard YouTube Ad](https://www.youtube.com/watch?v=Q_6stXKGuHo) +### Part 1 - LikeAGirl.java - [Like a Girl](https://www.youtube.com/watch?v=5yLXrWLvwAo) -The lab template contains a program that prints the following: +The lab template contains a program that prints the following if you choose "0": ``` -2 tickets: $28.00 -2 hotdogs, 2 popcorn, 2 sodas: $18.00 -1 autographed baseball $45.00 -real conversation with 11 year old son: priceless -true +What does it mean to do something, "Like a Girl"? +Show me what it looks like to run, "Like a Girl." +Show me what it looks like to fight, "Like a Girl." +How do you think it affects them when somebody uses "Like a Girl" as an insult? Choice (good: 0, bad: 1) 0 +You answered 0 +Always wants to change that. +Emotional Damage 100.000000. +Did you answer like a nice person? FALSE +``` +... OR the following if you choose "1": +``` +What does it mean to do something, "Like a Girl"? +Show me what it looks like to run, "Like a Girl." +Show me what it looks like to fight, "Like a Girl." +How do you think it affects them when somebody uses "Like a Girl" as an insult? Choice (good: 0, bad: 1) 1 +You answered 1 +Good for you. +Emotional Damage 0.000000. +Did you answer like a nice person? TRUE ``` -Ignore the code that you don't fully recognize and concentrate on changing the variables to alter the MasterCard *Priceless* script to say: +Ignore the code that you don't fully recognize and concentrate on changing the variables to alter the script to say the following if 1 is chosen: +``` +What does it mean to do something, "Like a Boy"? +Show me what it looks like to run, "Like a Boy." +Show me what it looks like to fight, "Like a Boy." +How do you think it affects them when somebody uses "Like a Boy" as an insult? Choice (good: 1, bad: 2) 1 +You answered 1 +Good for you. +Emotional Damage 50.000000. +Did you answer like a nice person? TRUE ``` -3 tickets: $42.00 -3 hotdogs, 3 popcorn, 3 sodas: $27.00 -2 autographed baseball $90.00 -watching the Giants win: priceless -false +OR to say the following if 2 is chosen: +``` +What does it mean to do something, "Like a Boy"? +Show me what it looks like to run, "Like a Boy." +Show me what it looks like to fight, "Like a Boy." +How do you think it affects them when somebody uses "Like a Boy" as an insult? Choice (good: 1, bad: 2) 2 +You answered 2 +Always wants to change that. +Emotional Damage 100.000000. +Did you answer like a nice person? FALSE ``` ### Part 2 - Interpretation @@ -31,18 +59,30 @@ 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. -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! +Next give TWO example variable names and TWO example variable assignments that are **WRONG** and explain why. +* Hint: your IDE can help you discover wrong assignments or variable names! ### 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. +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. ![Format Specifiers](JavaStringFormatSpecifiers.png) ### Part 4 - Submission -* Just as you did last week (Reference the Lab video in your Week 1 module), create a Spring2023 feature branch of your code +* Just as you did last week (Reference the Lab video in your Week 1 module), create a Feature1 branch of your code * Commit your working code to your local copy -* Push it to your Remote/origin branch (i.e. GitHub: Spring2023 -> origin/Spring2023) +* Push it to your Remote/origin branch (i.e. GitHub: Feature1 -> origin/Feature1) * Then issue a Pull request to my instructor branch - * Make sure to save the Pull request URL and submit it for the lab. + * Make sure to **COPY** the Pull request URL and submit it for the lab/assignment. + + +### Answers to part 2 +* Some Variables I noticed were float, double, and boolean. +* The data type for double is a 64-bit floating point. +* The data type for float is a 32-bit floating point. +* I'm not sure what boolean was but after looking into it, it's a data type that returns two values. +* Double can be assigned to pretty much any integer in a 64 bit processor +* Float can be assigned to any integer in a 32-bit processor +* Boolean can return either "true" or "false" +* An example of a wrong variable would be- double = "50.0". This is wrong because the integer has apostrophes around it. +* Another example would be- boolean = trueorfalse. This is incorrect because of the improper use of lowercase letters. diff --git a/src/LikeAGirl.java b/src/LikeAGirl.java new file mode 100644 index 0000000..8604a62 --- /dev/null +++ b/src/LikeAGirl.java @@ -0,0 +1,55 @@ +import java.util.Scanner; + +public class LikeAGirl { + /** + * "Like a Girl," Super Bowl XLIX (2015) - Like a Girl + * @param args Command line arguments [The source file path, The target file path, ...] + **/ + public static void main(String[] args) { + // String variables, some with format specifiers + String scriptTemplateLine1 = "What does it mean to do something, \"%s\"?"; + String scriptTemplateLine2 = "Show me what it looks like to run, \"%s.\"%n"; + String scriptTemplateLine3 = "Show me what it looks like to fight, \"%s.\""; + String scriptTemplateLine4 = + "How do you think it affects them when somebody uses \"%s\" as an insult? Choice (good: %d, bad: %d) "; + String scriptTemplateLine5 = "You answered %d%n%s."; + String ansGood = "Always wants to change that.%nEmotional Damage %f"; + String ansBad = "Good for you.%nEmotional Damage %f"; + String likeABoy = "Like a boy"; + + // integer variable + int good = 2; + int bad = 1; + // 32 bit floating point variable + float emotionalDamage = 50.0f; // 32 Bit, but it does exist! + // double precision floating point variable + double dEmotionalDamage = 100.0; // Double precision + // boolean variable + boolean trueOrFalse; + // Scanner variable for reading input. + Scanner s = new Scanner(System.in); + + System.out.println( + String.format(scriptTemplateLine1, likeABoy)); + + // Example of using printf and platform specific line separator "%n" to + // format instead of String.format + System.out.printf(scriptTemplateLine2, likeABoy); + + System.out.println(String.format(scriptTemplateLine3, likeABoy)); + + System.out.printf(scriptTemplateLine4, likeABoy, good, bad); + + int answer = Integer.parseInt(s.nextLine()); + + System.out.println( + String.format(scriptTemplateLine5, answer, + (answer == good) ? + String.format(ansGood, dEmotionalDamage) : String.format(ansBad, emotionalDamage) + ) + ); + + trueOrFalse = (answer != good); // if answer == 1 (i.e. good), then trueOrFalse should be False + System.out.printf("Did you answer like a nice person? %B%n", trueOrFalse); + } +} diff --git a/src/PricelessScript.java b/src/PricelessScript.java deleted file mode 100644 index 887d88a..0000000 --- a/src/PricelessScript.java +++ /dev/null @@ -1,45 +0,0 @@ -import java.text.NumberFormat; - -public class PricelessScript { - /** - * Proof that MasterCard marketing is in your DNA - * @param args Command line arguments [The source file path, The target file path, ...] - **/ - 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 priceless = "priceless"; - - // integer variable - int people = 2; - // 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; - - NumberFormat formatter = NumberFormat.getCurrencyInstance(); - - System.out.println( - String.format(scriptTemplateLine1, people, - formatter.format(people * ticketPrice))); - - // Example of using printf and platform specific line separator "%n" to - // format instead of String.format - System.out.printf((scriptTemplateLine2) + "%n", people, people, people, - formatter.format(people * itemPrice)); - - itemPrice = 45.0; - System.out.println( - String.format(scriptTemplateLine3, formatter.format(itemPrice))); - - System.out.println( - String.format(scriptTemplateLine4, priceless)); - - System.out.println(trueOrFalse); - } -}