diff --git a/ch01/Hello.java b/ch01/Hello.java index 593557b..56eee74 100644 --- a/ch01/Hello.java +++ b/ch01/Hello.java @@ -1,6 +1,8 @@ -public class Hello { +public class Hello +{ - public static void main(String[] args) { + public static void main(String[] args) + { // generate some simple output System.out.println("Hello, World!"); } diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..faa838c --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,38 @@ +public class Date + +{ + + + public static void main(String[] args) + + { + + System.out.println("Todays Date Is: "); + + String day = "Thursday, "; + String month = "January"; + int date = 18 ; + int year = 2018; + + System.out.println("American Format:"); + System.out.print(day); + System.out.print(" "); + System.out.print(month); + System.out.print(" "); + System.out.print(date); + System.out.print(", "); + System.out.println(year); + + System.out.println("European Format:"); + System.out.print(day); + System.out.print(" "); + System.out.print(date); + System.out.print(" "); + System.out.print(month); + System.out.print(" "); + System.out.println(year); + + } + + +} diff --git a/ch02/DoubleByZero.java b/ch02/DoubleByZero.java new file mode 100644 index 0000000..ff5c4e6 --- /dev/null +++ b/ch02/DoubleByZero.java @@ -0,0 +1,25 @@ +public class DoubleByZero +{ + public static void main(String[] args) + + + { + double a = 42.0; + double b = 0.0; + double result = a / b; + + System.out.println("Result is; " + result); + + + + + + + + + + + + } + +} diff --git a/ch02/IntByZero.java b/ch02/IntByZero.java new file mode 100644 index 0000000..b58dd39 --- /dev/null +++ b/ch02/IntByZero.java @@ -0,0 +1,22 @@ +public class IntByZero +{ + + public static void main(String[] args) + + { + int a = 42; + int b = 0; + int result = (b / a); + + + System.out.print("Result: " +result); + + + + + + + + + } +} diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java new file mode 100644 index 0000000..afd4335 --- /dev/null +++ b/ch02/IntExtremes.java @@ -0,0 +1,23 @@ +public class IntExtremes +{ + public static void main(String[] args) + + { + int positiveInt; + positiveInt = 2147483647; + + System.out.println("Positive Int is: " + positiveInt); + + System.out.print("Positive Int Increase by 1: "); + System.out.println(positiveInt + 1); + + int negativeInt; + negativeInt = -2147483648; + + System.out.print("Negative Int Decrease by 1: "); + System.out.println(negativeInt - 1); + + + + } +} diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..30de4cb --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,48 @@ +public class Time +{ + + public static void main(String args[]) + + { + int hour = 9; + int minute = 12; + int second = 23; + + int secondsPerMinute = 60; + int minutesPerHour = 60; + + int secondsSinceMidnight = (hour * minutesPerHour * secondsPerMinute) + (minute * secondsPerMinute) + second; + + System.out.println("Seconds Since Midnight: " + secondsSinceMidnight); + + int hoursPerDay = 24; + + int secondsInDay = hoursPerDay * minutesPerHour * secondsPerMinute; + int secondsRemainingInDay = secondsInDay - secondsSinceMidnight; + + System.out.println("Seconds Remaining in The Day: " + secondsRemainingInDay); + + int percentage = 100 * secondsSinceMidnight / secondsInDay; + + System.out.println("Percent of Day that Has Passed: " + percentage); + + int currentHour = 10; + int currentMinutes = 24; + int currentSeconds = 38; + + System.out.print("Elapsed time of Exercise: " ); + System.out.println((currentHour - hour)+":"+(currentMinutes - minute)+":"+(currentSeconds - second)); + + + + + + + + + } + + + + +} diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java new file mode 100644 index 0000000..7a0c3a0 --- /dev/null +++ b/ch02/Withdrawal.java @@ -0,0 +1,47 @@ +public class Withdrawal + +{ + + public static void main(String[] args) + { + + int withdrawal = 137; + + int twenties = withdrawal/ 20; + System.out.println("Twenties: " + twenties); + int remainderTwenties = withdrawal - twenties * 20; + System.out.println("Left to Go: " + remainderTwenties); + int tens = remainderTwenties/ 10; + System.out.println("Tens: " + tens); + int remainderTens = withdrawal - twenties * 20 - tens * 10; + System.out.println("Left to Go: " + remainderTens); + int fives = remainderTens/ 5; + System.out.println("Fives:" + fives); + int remainderFives = withdrawal - twenties * 20 - tens * 10 - fives * 5; + System.out.println("Left to Go: " + remainderFives); + System.out.println("Ones:" + remainderFives); + + + + + + + + + + + + + + + + + + + + + + } + + +} diff --git a/ch03/ConvertTemp.java b/ch03/ConvertTemp.java new file mode 100644 index 0000000..bec0bc9 --- /dev/null +++ b/ch03/ConvertTemp.java @@ -0,0 +1,43 @@ +import java.util.Scanner; + +public class ConvertTemp +{ + public static void main(String[] args) + + { + double celsius; + + Scanner in = new Scanner(System.in); + + System.out.println("What is the temperature in celsius? "); + celsius = in.nextDouble(); + + + double fahrenheit = (celsius * 9 / 5) + 32; + + System.out.printf("%.1f C = %.1f F\n", celsius, fahrenheit); + + + + + + + + + + + + + + + + + + + + + + + + } +} diff --git a/ch03/ConvertTime.java b/ch03/ConvertTime.java new file mode 100644 index 0000000..7ae134c --- /dev/null +++ b/ch03/ConvertTime.java @@ -0,0 +1,24 @@ +import java.util.Scanner; + +public class ConvertTime +{ + public static void main(String[] args) + + { + + Scanner in = new Scanner(System.in); + System.out.print("Seconds to Input:"); + int inputNumber = in.nextInt(); + + int hour = inputNumber / 3600; + int seconds = inputNumber % 3600; + int minute = seconds / 60; + seconds = seconds % 60; + + System.out.printf("%d seconds = %d hours, %d minutes, and %d seconds", inputNumber, hour, minute, seconds); + + + } + + +} \ No newline at end of file diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index 64984df..55aa82f 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -1,15 +1,28 @@ import java.util.Random; +import java.util.Scanner; -/** - * Starter code for the "Guess My Number" exercise. - */ -public class GuessStarter { - - public static void main(String[] args) { - // pick a random number +public class GuessStarter +{ + public static void main(String[] args) + { Random random = new Random(); - int number = random.nextInt(100) + 1; - System.out.println(number); + int numberGenerated = random.nextInt(100) + 1; + System.out.println("Generated Number is: " + numberGenerated); + + int guessNumber; + int variance; + System.out.println("I'm thinking of a number between 1 and 100. "); + System.out.println("Can you guess what it is? "); + + Scanner scanner = new Scanner(System.in); + System.out.println("Type a number: "); + guessNumber = scanner.nextInt(); + + System.out.println("Your guess is: " + guessNumber); + System.out.println("The number I was thinking of was: " + numberGenerated); + + variance = numberGenerated % guessNumber; + System.out.println("You were off by: " + variance); } -} +} \ No newline at end of file diff --git a/ch04/ConvertAmerican.java b/ch04/ConvertAmerican.java new file mode 100644 index 0000000..dba41ad --- /dev/null +++ b/ch04/ConvertAmerican.java @@ -0,0 +1,30 @@ +public class ConvertAmerican + +{ + private static void printAmerican(String day, String month, int date, int year) + { + System.out.println("American Format: "); + System.out.println(day + ", " + month + " " + date + ", " + year); + System.out.println(); + } + + private static void printEuropean(String day, String month, int date, int year) + { + System.out.println("European Format: "); + System.out.println(day + ", " + date + " " + month + " " + year); + } + + public static void main(String[] args) + { + String day = "Monday"; + String month = "January"; + int year = 2018; + int date = 22; + + printAmerican(day, month, date, year); + printEuropean(day, month, date, year); + + } + + +} diff --git a/ch04/SimpleMethods.java b/ch04/SimpleMethods.java new file mode 100644 index 0000000..4e50ba4 --- /dev/null +++ b/ch04/SimpleMethods.java @@ -0,0 +1,34 @@ +public class SimpleMethods +{ + public static void printCount(int count) + + { + System.out.println("This count is: "); + System.out.println(count); + } + + public static void printSum(int num1, int num2, int num3, int num4) + + { + System.out.println("The First Sum is: "); + System.out.println(num1 + num2); + System.out.println("The Second Sum is: "); + System.out.println(num3 + num4); + } + + public static void main(String[] args) + + { + int count = 5; + int num1 = 4; + int num2 = 6; + int num3 = 7; + int num4 = 2; + printCount(count); + printSum(4, 6, 7, 2); + + + } + + +} diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java new file mode 100644 index 0000000..682307d --- /dev/null +++ b/ch05/LogicMethods.java @@ -0,0 +1,23 @@ +public class LogicMethods +{ + public static void main(String[] args) + + + { + System.out.println("Start of Program"); + printLargest(1); + printLargest(2); + } + + + private static void printLargest(int number) + + { + if (number > 1) + { + System.out.println("The Largest Number is: " + number); + } + + } + +} \ No newline at end of file