From e6266785f10aed33904bf8f7525e12aba88a644f Mon Sep 17 00:00:00 2001 From: Lina Date: Tue, 5 Sep 2023 20:07:32 -0700 Subject: [PATCH] Lab 002 completed --- README.md | 23 +++++++++++++++++++++++ src/LikeAGirl.java | 33 +++++++++++++++++++++------------ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b866789..88be52e 100644 --- a/README.md +++ b/README.md @@ -59,9 +59,32 @@ 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. +Variable Name: good, emotionalDamage, trueOrFalse + +Its data type: + +good: integer data type + +emotionalDamage: float data type + +trueOrFalse: boolean data type + +Example values you can assign them: + +good: I assigned 1 since it is an integer data type + +emotionalDamage: I assigned 100.0f since it is a float data type + +trueOrFalse: true or false + 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! +int hour: 7.5; this is wrong because integers can only be whole numbers. + +double y = 2/8; a common mistake, the assignments are integers instead they should be +2.0 and 8.0, assign floating-point values to floating-point variables always. + ### 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. diff --git a/src/LikeAGirl.java b/src/LikeAGirl.java index 30b943b..edec718 100644 --- a/src/LikeAGirl.java +++ b/src/LikeAGirl.java @@ -1,3 +1,12 @@ +/** + * + * @author Trevor Hartman + * @author Angelina Perez + * + * @since Version 1.0 + * + */ + import java.util.Scanner; public class LikeAGirl { @@ -13,32 +22,32 @@ public static void main(String[] args) { 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 likeAGirl = "Like a Girl"; + String ansGood = "Good for you.%nEmotional Damage %f"; + String ansBad = "Always wants to change that.%nEmotional Damage %f"; + String likeABoy = "Like a Boy"; // integer variable - int good = 0; - int bad = 1; + int good = 1; + int bad = 2; // 32 bit floating point variable - float emotionalDamage = 0.0f; // 32 Bit, but it does exist! + float emotionalDamage = 100.0f; // 32 Bit, but it does exist! // double precision floating point variable - double dEmotionalDamage = 100.0; // Double precision + double dEmotionalDamage = 50.0; // Double precision // boolean variable boolean trueOrFalse; // Scanner variable for reading input. Scanner s = new Scanner(System.in); System.out.println( - String.format(scriptTemplateLine1, likeAGirl)); + String.format(scriptTemplateLine1, likeABoy)); // Example of using printf and platform specific line separator "%n" to // format instead of String.format - System.out.printf(scriptTemplateLine2, likeAGirl); + System.out.printf(scriptTemplateLine2, likeABoy); - System.out.println(String.format(scriptTemplateLine3, likeAGirl)); + System.out.println(String.format(scriptTemplateLine3, likeABoy)); - System.out.printf(scriptTemplateLine4, likeAGirl, good, bad); + System.out.printf(scriptTemplateLine4, likeABoy, good, bad); int answer = Integer.parseInt(s.nextLine()); @@ -49,7 +58,7 @@ public static void main(String[] args) { ) ); - trueOrFalse = (answer != good); // if answer == 1 (i.e. good), then trueOrFalse should be False + 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); } }