diff --git a/ch01/Hello.java b/ch01/Hello.java index 593557b..cf4a2ec 100644 --- a/ch01/Hello.java +++ b/ch01/Hello.java @@ -3,6 +3,7 @@ public class Hello { public static void main(String[] args) { // generate some simple output System.out.println("Hello, World!"); + System.out.print(" How are you?") // an extra line I added } } diff --git a/ch02/Date b/ch02/Date new file mode 100644 index 0000000..66ea90b --- /dev/null +++ b/ch02/Date @@ -0,0 +1,18 @@ +public class Date { + public static void main(String[] args) { + System.out.println("Hello, world!"); + String day; + int date; + String month; + int year; + day = "Friday"; + date = 13; + month = "May"; + year = 2016; + + System.out.println(day); + System.out.println(date); + System.out.println(month); + System.out.println(year); + } +} diff --git a/ch02/Time b/ch02/Time new file mode 100644 index 0000000..0936300 --- /dev/null +++ b/ch02/Time @@ -0,0 +1,23 @@ +public class Time { + public static void main(String[] args) { + double hour; + double minute; + double second; + hour = 19.0; + minute = 44.0; + second = 40.0; + System.out.println("Number of seconds since midnight:"); + System.out.println((hour * 60) * 60 + (minute * 60) + second); +double secondsSinceMidnight; +secondsSinceMidnight = ((hour * 60) * 60 + (minute * 60) + second); + System.out.println("Number of seconds remaining in the day:"); +int secondsInTheDay; +secondsInTheDay = ((24 * 60) * 60); + System.out.println(secondsInTheDay - secondsSinceMidnight); + System.out.println("Percentage of the day that has passed:"); + System.out.println((secondsSinceMidnight * 100) / secondsInTheDay); + System.out.println("Time elapsed since I started working on this (seconds)"); + System.out.println(secondsSinceMidnight - 69630); + + } +} diff --git a/ch02/Variables.java b/ch02/Variables.java index a295abf..3a9f77d 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -78,6 +78,7 @@ public static void main(String[] args) { hour = minute + 1; // correct // minute + 1 = hour; // compiler error + } } diff --git a/ch03/ConvertTemp b/ch03/ConvertTemp new file mode 100644 index 0000000..ae94c73 --- /dev/null +++ b/ch03/ConvertTemp @@ -0,0 +1,24 @@ +import java.util.Scanner; + +/** + * Converts Celsius to Fahrenheit + */ +public class Convert { + + public static void main(String[] args) { + double Celsius; + double Fahrenheit; + final double FAHRENHEIT_PER_CELSIUS = 33.8; + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + System.out.print("Exactly how many degrees Celsius? "); + Celsius = in.nextDouble(); + + // convert and output the result + Fahrenheit = (double) (Celsius * FAHRENHEIT_PER_CELSIUS); + System.out.printf("%.1f C = %.1f F", + Celsius, Fahrenheit); + } + +} diff --git a/ch03/ConvertTime b/ch03/ConvertTime new file mode 100644 index 0000000..502dc3b --- /dev/null +++ b/ch03/ConvertTime @@ -0,0 +1,26 @@ +import java.util.Scanner; + +/** + * Converts seconnds to hours, minutes, and seconds + */ +public class Convert { + + public static void main(String[] args) { + int seconds, hours, minutes, remainder; + final int SECONDS_PER_HOUR = 3600; + final int SECONDS_PER_MINUTE = 60; + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + System.out.print("Exactly how many seconds? "); + seconds = in.nextInt(); + + // convert and output the result + hours = (int) (seconds / SECONDS_PER_HOUR); + minutes = ((seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE); + remainder = minutes % SECONDS_PER_MINUTE; + System.out.printf("%d seconds = %d hours, %d minutes, %d seconds", + seconds, hours, minutes, remainder); + } + +} diff --git a/ch03/GuessNumber b/ch03/GuessNumber new file mode 100644 index 0000000..aed0399 --- /dev/null +++ b/ch03/GuessNumber @@ -0,0 +1,33 @@ +import java.util.Scanner; +import java.util.Random; + +/** + * Starter code for the "Guess My Number" exercise. + */ +public class Guess { + + public static void main(String[] args) { + + // prompt the user and get the value + System.out.println("I'm thinking of a number between 1 and 100 (including both)."); + System.out.println("Can you guess what it is?"); + System.out.print("Type a number:"); + + Scanner in = new Scanner(System.in); + int guess, remainder; + guess = in.nextInt(); + + // pick a random number + Random random = new Random(); + int number = random.nextInt(100) + 1; + System.out.print("The number I was thinking of is: "); + System.out.printf("%d\n", + number); + remainder = guess - number; + System.out.print("You were off by: "); + System.out.printf("%d", + remainder); + + } + +} diff --git a/ch04/DateRevisited b/ch04/DateRevisited new file mode 100644 index 0000000..1f2bf34 --- /dev/null +++ b/ch04/DateRevisited @@ -0,0 +1,30 @@ +public class DateRevisited { + public static void main(String[] args) { + + String day; + int date; + String month; + int year; + day = "Friday"; + date = 20; + month = "May"; + year = 2016; + + printAmerican (day, month, date, year); + } + + public static void printEuropean(String day, int date, String month, int year) { + System.out.print(day); + System.out.print(", " + date); + System.out.print(", " + month); + System.out.print(", " + year); + } + + public static void printAmerican(String day, String month, int date, int year) { + System.out.print(day); + System.out.print(", " + month); + System.out.print(" " + date); + System.out.print(", " + year); + } +} + diff --git a/ch04/Methods.java b/ch04/Methods.java index 90c1b7a..75bfc2d 100644 --- a/ch04/Methods.java +++ b/ch04/Methods.java @@ -18,5 +18,10 @@ public static void main(String[] args) { double x3 = Math.exp(Math.log(10.0)); double x4 = Math.pow(2.0, 10.0); } - +public static void zool(int a, String b, String c) { + System.out.println(a); + System.out.println(b); + System.out.println(c); + } + } diff --git a/ch05/GuessNumberRevisited b/ch05/GuessNumberRevisited new file mode 100644 index 0000000..0936d08 --- /dev/null +++ b/ch05/GuessNumberRevisited @@ -0,0 +1,65 @@ +import java.util.Scanner; +import java.util.Random; + +/** + * Starter code for the "Guess My Number" exercise. + */ +public class Guess { + + public static void main(String[] args) { + // prompt the user and get the value + System.out.println("I'm thinking of a number between 1 and 100 (including both)."); + System.out.println("Can you guess what it is?"); + System.out.print("Type a number:"); + + Scanner in = new Scanner(System.in); + int guess; + guess = in.nextInt(); + + // pick a random number + Random random = new Random(); + int number = random.nextInt(100) + 1; + System.out.print("The number I was thinking of is: "); + System.out.printf("%d\n", + number); + + // prompt the user and re-run guessAgain until user guesses correctly + if (guess == number) { + System.out.print("You guessed correctly.\n"); + } else if (guess > number) { + System.out.print("You guessed too high. Try again.\n"); + guessAgain(guess, number); + } else if (guess < number) { + System.out.print("You guessed too low. Try again.\n"); + guessAgain(guess, number); + } + } + + public static void guessAgain(int guess, int number) { + // prompt the user and get the value + System.out.println("I'm thinking of a number between 1 and 100 (including both)."); + System.out.println("Can you guess what it is?"); + System.out.print("Type a number:"); + + Scanner in = new Scanner(System.in); + guess = in.nextInt(); + + // pick a random number + Random random = new Random(); + System.out.print("The number I was thinking of is: "); + System.out.printf("%d\n", + number); + + // prompt the user and re-run guessAgain until user guesses correctly + if (guess == number) { + System.out.print("You guessed correctly.\n"); + } else if (guess > number) { + System.out.print("You guessed too high. Try again.\n"); + guessAgain(guess, number); + } else if (guess < number) { + System.out.print("You guessed too low. Try again.\n"); + guessAgain(guess, number); + } + + } +} diff --git a/ch05/beerBottles b/ch05/beerBottles new file mode 100644 index 0000000..f8de099 --- /dev/null +++ b/ch05/beerBottles @@ -0,0 +1,22 @@ +public class beerBottles { + + public static void main(String[] args) { + int n; + n = 3; + beerBottles(n); + } + public static void beerBottles(int n) { + if (n >= 1) { + System.out.println(n + " bottles of beer on the wall,"); + System.out.println(n + " bottles of beer,"); + System.out.println("ya take one down, ya' pass it around,"); + System.out.println(n + " bottles of beer on the wall.\n"); + beerBottles(n - 1); + } else { + System.out.println("No bottles of beer on the wall,"); + System.out.println("No bottles of beer,"); + System.out.println("ya take one down, ya' pass it around,"); + System.out.println("No bottles of beer on the wall."); + } + } + } diff --git a/ch05/testFermat b/ch05/testFermat new file mode 100644 index 0000000..7841dcd --- /dev/null +++ b/ch05/testFermat @@ -0,0 +1,25 @@ +public class testFermat { + +public static void main(String[] args) { + int a; + int b; + int c; + int n; + a = 3; + b = 4; + c = 5; + n = 2; + testFermat(a, b, c, n); +} + +public static void testFermat(int a, int b, int c, int n) { + if (((Math.pow(a, n) + Math.pow(b, n)) == Math.pow(c, n)) && (n > 2)) { + System.out.println("Holy smokes! Fermat was wrong!"); + } else { + System.out.println("No that doesn't work"); + } + } + + +} + diff --git a/ch06/ExerciseCh6 b/ch06/ExerciseCh6 new file mode 100644 index 0000000..397c0de --- /dev/null +++ b/ch06/ExerciseCh6 @@ -0,0 +1,19 @@ +public class ExerciseCh6 { + + public static void main(String[] args) { + int n = 5; + oddSum(n); + System.out.println("The sum of integers from 1 to n is " + oddSum(n)); + } + + public static int oddSum(int n) { + if (n == 0) { + return 0; + } else if + (n % 2 != 0) { + return n + oddSum(n - 1); + } else { + return oddSum(n - 1); + } +} +} diff --git a/ch06/VoidMethods b/ch06/VoidMethods new file mode 100644 index 0000000..a481050 --- /dev/null +++ b/ch06/VoidMethods @@ -0,0 +1,27 @@ +public class voidmethods { + + public static void main(String[] args) { + int a = 10; + int b = 2; + int c = 7; + isTriangle(a, b, c); + } + + public static boolean isTriangle(int a, int b, int c) { + if ((a + b) < c) { + System.out.print("False"); + return false; + } else if + ((a + c) < b) { + System.out.print("False"); + return false; + } else if + ((b + c < a)) { + System.out.print("False"); + return false; + } + System.out.print("True"); + return true; + } + } +