From 79b2459e972e5a32b598dc6936a123f7d748c7cc Mon Sep 17 00:00:00 2001 From: "John P. Baugh" Date: Wed, 26 Jan 2022 23:41:07 -0500 Subject: [PATCH] Add files via upload --- section2/ArithmeticFun.java | 34 +++++++++++++++ section2/CommentFun.java | 18 ++++++++ section2/ConversionFun.java | 10 +++++ section2/HelloWorld.java | 9 ++++ section2/KeyboardInput.java | 31 ++++++++++++++ section2/LogicalFun.java | 20 +++++++++ section2/Proj2_1_AverageOfThree.java | 23 ++++++++++ section2/Proj2_2_MadLibsClone.java | 63 ++++++++++++++++++++++++++++ section2/RelationalFun.java | 36 ++++++++++++++++ section2/VariableFun.java | 17 ++++++++ 10 files changed, 261 insertions(+) create mode 100644 section2/ArithmeticFun.java create mode 100644 section2/CommentFun.java create mode 100644 section2/ConversionFun.java create mode 100644 section2/HelloWorld.java create mode 100644 section2/KeyboardInput.java create mode 100644 section2/LogicalFun.java create mode 100644 section2/Proj2_1_AverageOfThree.java create mode 100644 section2/Proj2_2_MadLibsClone.java create mode 100644 section2/RelationalFun.java create mode 100644 section2/VariableFun.java diff --git a/section2/ArithmeticFun.java b/section2/ArithmeticFun.java new file mode 100644 index 0000000..f62e1ea --- /dev/null +++ b/section2/ArithmeticFun.java @@ -0,0 +1,34 @@ + + +public class ArithmeticFun { + public static void main(String[] args) { + int a = 10; + int b = 15; + + //e.g., BINARY operators + int result = a + b; + int difference = a - b; + int product = a * b; + int quotient = b / a; + int remainder = b % a; + + System.out.println("result is " + result); + System.out.println("diff is " + difference); + System.out.println("product is " + product); + System.out.println("quotient is " + quotient); + System.out.println("remainder is " + remainder); + + result += 20; //result = result + 20 + System.out.println("result is now " + result); + + //UNARY operators + result++; //++result; + System.out.println("result++ " + result); + + result--; //--result; + System.out.println("result-- " + result); + + product *= 2; //product = product * 2; + System.out.println("final value of product is " + product); + } +} diff --git a/section2/CommentFun.java b/section2/CommentFun.java new file mode 100644 index 0000000..d8889c2 --- /dev/null +++ b/section2/CommentFun.java @@ -0,0 +1,18 @@ +/* + John Baugh + Learning Some Java 101 + This is a multiline comment + July 30, 2060 + */ + +public class CommentFun { + /* + main is the entry point to the application. + */ + + public static void main(String[] args) { + System.out.println("Printing stuff"); //print stuff to console + //this doesn't print + //these are single line comments + }//end main +} diff --git a/section2/ConversionFun.java b/section2/ConversionFun.java new file mode 100644 index 0000000..aa1cafe --- /dev/null +++ b/section2/ConversionFun.java @@ -0,0 +1,10 @@ + + +public class ConversionFun { + public static void main(String[] args) { + double myDouble = 3.14; + float myFloat = 3.14f; //narrowing/lossy conversion + double yourDouble = myFloat; //widening/lossless conversion + + }//end main +} diff --git a/section2/HelloWorld.java b/section2/HelloWorld.java new file mode 100644 index 0000000..3430c76 --- /dev/null +++ b/section2/HelloWorld.java @@ -0,0 +1,9 @@ + +public class HelloWorld { + + public static void main(String[] args) { + System.out.println("Hello world!"); + System.out.println("Hello John!"); + + } +} diff --git a/section2/KeyboardInput.java b/section2/KeyboardInput.java new file mode 100644 index 0000000..8a798fb --- /dev/null +++ b/section2/KeyboardInput.java @@ -0,0 +1,31 @@ +import java.util.Scanner; + +public class KeyboardInput { + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + String name; + String city; + int age; + double dubInput; + + System.out.println("What is your name?"); + name = keyboard.nextLine(); + + System.out.println("What's your age?"); + age = keyboard.nextInt(); + keyboard.nextLine(); //consume the newline + + System.out.println("What's your real number?"); + dubInput = keyboard.nextDouble(); + keyboard.nextLine(); + dubInput *= 2; //dubInput = dubInput * 2; + + System.out.println("What city do you live in?"); + city = keyboard.nextLine(); + + System.out.println("Hello, " + name); + System.out.println("age is " + age); + System.out.println("city is " + city); + System.out.println("Twice your number is " + dubInput); + }//end main +} diff --git a/section2/LogicalFun.java b/section2/LogicalFun.java new file mode 100644 index 0000000..30eecb6 --- /dev/null +++ b/section2/LogicalFun.java @@ -0,0 +1,20 @@ + + +public class LogicalFun { + public static void main(String[] args) { + boolean isRaining = false; + boolean isWarm = false; + + boolean combined = isRaining && isWarm; + + System.out.println("Is it raining and warm?: " + combined); + + combined = isRaining || isWarm; + + System.out.println("Is it raining OR warm?: " + combined); + + combined = !isRaining; + + System.out.println("Is it NOT raining outside?: " + combined); + }//end main +} diff --git a/section2/Proj2_1_AverageOfThree.java b/section2/Proj2_1_AverageOfThree.java new file mode 100644 index 0000000..82b9348 --- /dev/null +++ b/section2/Proj2_1_AverageOfThree.java @@ -0,0 +1,23 @@ +import java.util.Scanner; + +public class Proj2_1_AverageOfThree { + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + + double num1; + double num2; + double num3; + double average; + + System.out.println("Please enter three numbers"); + num1 = keyboard.nextDouble(); + num2 = keyboard.nextDouble(); + num3 = keyboard.nextDouble(); + keyboard.nextLine(); //good habit + + average = (num1 + num2 + num3) / 3.0; + + System.out.println("Average is " + average); + + }//end main +} diff --git a/section2/Proj2_2_MadLibsClone.java b/section2/Proj2_2_MadLibsClone.java new file mode 100644 index 0000000..738f038 --- /dev/null +++ b/section2/Proj2_2_MadLibsClone.java @@ -0,0 +1,63 @@ +import java.util.Scanner; + + +public class Proj2_2_MadLibsClone { + + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + + String adjective1; + String girlsName; + String adjective2; + String occupation1; + String placeName; + String clothing; + String hobby; + String adjective3; + String occupation2; + String boysName; + String mansName; + + System.out.print("Enter an adjective\t"); + adjective1 = keyboard.nextLine(); + + System.out.print("Enter a girl's name:\t"); + girlsName = keyboard.nextLine(); + + System.out.print("Enter another adjective:\t"); + adjective2 = keyboard.nextLine(); + + System.out.print("Enter an occupation:\t"); + occupation1 = keyboard.nextLine(); + + System.out.print("Enter the name of a place:\t"); + placeName = keyboard.nextLine(); + + System.out.print("Enter the name of a piece of clothing:\t"); + clothing = keyboard.nextLine(); + + System.out.print("Enter the name of a hobby:\t"); + hobby = keyboard.nextLine(); + + System.out.print("Enter another adjective:\t"); + adjective3 = keyboard.nextLine(); + + System.out.print("Enter another occupation:\t"); + occupation2 = keyboard.nextLine(); + + System.out.print("Enter a boy's name:\t"); + boysName = keyboard.nextLine(); + + System.out.print("Enter a man's name:\t"); + mansName = keyboard.nextLine(); + + System.out.println(); + + System.out.println("There once was a " + adjective1 + " girl named " + girlsName + " who was a " + + adjective2 + " " + occupation1 + " in the Kingdom of " + placeName + "."); + + System.out.println("She loved to wear " + clothing + " and to " + hobby + ". She wanted to marry the " + + adjective3 + " " + occupation2 + " named " + boysName + " but her father, King " + mansName + + " forbid her from seeing him."); + }//end main +} diff --git a/section2/RelationalFun.java b/section2/RelationalFun.java new file mode 100644 index 0000000..172f355 --- /dev/null +++ b/section2/RelationalFun.java @@ -0,0 +1,36 @@ + +public class RelationalFun { + public static void main(String[] args) { + boolean myBool = true; + boolean yourBool = false; + int myAge = 37; + int yourAge = 20; + int bobsAge = 20; + + String myName = "John"; + String yourName = "Johnny"; + + System.out.println("myBool is " + myBool); + System.out.println("yourBool is " + yourBool); + + //relational operations + boolean ageComparison = myAge > yourAge; + System.out.println("myAge > yourAge?: " + ageComparison); + + ageComparison = yourAge > bobsAge; + System.out.println("yourAge > bobsAge?:" + ageComparison); + + ageComparison = yourAge == bobsAge; + System.out.println("yourAge == bobsAge?: " + ageComparison); + + boolean nameComparison = myName.equals(yourName); + System.out.println("do names match?: " + nameComparison); + + int currentAge = 37; + + boolean isGreater21 = currentAge >= 21; + + System.out.println("currentAge >= 21?: " + isGreater21); + + }//end main +} diff --git a/section2/VariableFun.java b/section2/VariableFun.java new file mode 100644 index 0000000..aac71fa --- /dev/null +++ b/section2/VariableFun.java @@ -0,0 +1,17 @@ + + +public class VariableFun { + public static void main(String[] args) { + int myAge; //declaration + myAge = 19; //initialization + + final int SOME_NUM = 150; + + String name = "Billy"; + + String myHomeTown = "Dearborn"; + + System.out.println(name + " is " + myAge); + System.out.println(myHomeTown); + } +}