diff --git a/ch01/ExampleProgram.java b/ch01/ExampleProgram.java new file mode 100644 index 0000000..91a56cc --- /dev/null +++ b/ch01/ExampleProgram.java @@ -0,0 +1,6 @@ +//A Very Simple Example +class ExampleProgram { + public static void main(String[] args){ + System.out.println("I'm a Simple Program"); + } +} diff --git a/ch01/Hello.java b/ch01/Hello.java index 593557b..375c9da 100644 --- a/ch01/Hello.java +++ b/ch01/Hello.java @@ -3,6 +3,10 @@ public class Hello { public static void main(String[] args) { // generate some simple output System.out.println("Hello, World!"); + System.out.print("A whole new world! "); + System.out.println("A new fantastic point of view!"); } - } +//here is a single line commment +/* here is a +multi-line comment*/ diff --git a/ch01/HelloWorldApp.java b/ch01/HelloWorldApp.java new file mode 100644 index 0000000..2f15fe8 --- /dev/null +++ b/ch01/HelloWorldApp.java @@ -0,0 +1,9 @@ +/** + * The HelloWorldApp class implements an application that + * simply prints "Hello World!" to standard output. + */ +class HelloWorldApp { + public static void main(String[] args){ + System.out.println("Hello World!"); // Display the string. + } +} diff --git a/ch01/ex1.1 b/ch01/ex1.1 new file mode 100644 index 0000000..9914622 --- /dev/null +++ b/ch01/ex1.1 @@ -0,0 +1,9 @@ +1. A "statement" specifies one step of an algorithm. + A "comment" is for informational purposes only and is ignored when the program is run. + +2. A program is "portable" if it has the ability to run on more than one kind of computer. + +3. The word "compile" means to translate a program into assembly language, + all at once, so it may be executed later. + +4. An "executable" is a file or program that is ready/able to run on specific hardware. diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..5d71279 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,13 @@ +public class Date { + + public static void main(String[] args) { + String day = "Wednesday"; + String date = "2"; + String month = "August"; + String year = "2017"; + System.out.println("American format:"); + System.out.println(day + ", " + month + " " + date + ", " + year); + System.out.println("European format:"); + System.out.println(day + " " + date + " " + month + " " + year); + } +} diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..17c010b --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,23 @@ +public class Time { + + public static void main(String[] args) { + double hourStart = 16.0; + double minuteStart = 20.0; + double secondStart = 25.0; + System.out.print("Seconds since midnight: " ); + double secondsSinceMidnight = hourStart * 3600 + minuteStart * 60 + secondStart; + System.out.println(secondsSinceMidnight); + System.out.print("Seconds remaining in the day: "); + double secondsRemainingInDay = 86400 - secondsSinceMidnight; + System.out.println(secondsRemainingInDay); + System.out.print("Percentage of the day that has passed: "); + double percentageOfDayPassed = secondsSinceMidnight / 86400 * 100; + System.out.println(percentageOfDayPassed); + double hourEnd = 16.0; + double minuteEnd = 50.0; + double secondEnd = 11.0; + System.out.print("Seconds elapsed since I started this exercise: "); + double secondsSinceStart = (hourEnd * 3600 + minuteEnd * 60 + secondEnd) - secondsSinceMidnight; + System.out.println(secondsSinceStart); + } +} diff --git a/ch03/ConvertSecondToHourMinuteSecond.java b/ch03/ConvertSecondToHourMinuteSecond.java new file mode 100644 index 0000000..fa63893 --- /dev/null +++ b/ch03/ConvertSecondToHourMinuteSecond.java @@ -0,0 +1,18 @@ +import java.util.Scanner; + +public class ConvertSecondToHourMinuteSecond { + + public static void main(String[] args){ + int totalSeconds, hours, minutes, seconds; + Scanner in = new Scanner(System.in); + + System.out.print("How many seconds? "); + totalSeconds = in.nextInt(); + + hours = totalSeconds / 3600; + minutes = (totalSeconds % 3600) / 60; + seconds = (totalSeconds % 3600) % 60; + System.out.printf("%d seconds = %d hours, %d minutes, and %d seconds", + totalSeconds, hours, minutes, seconds); + } +} diff --git a/ch03/ConvertTemperature.java b/ch03/ConvertTemperature.java new file mode 100644 index 0000000..231e1fe --- /dev/null +++ b/ch03/ConvertTemperature.java @@ -0,0 +1,15 @@ +import java.util.Scanner; + +public class ConvertTemperature { + + public static void main(String[] args){ + double celsius, fahrenheit; + Scanner in = new Scanner(System.in); + + System.out.print("What is the temperature in degrees Celsius? "); + celsius = in.nextDouble(); + + fahrenheit = celsius * 9 / 5 + 32; + System.out.printf("%.1f C = %.1f F", celsius, fahrenheit); + } +} diff --git a/ch03/GuessMyNumber.java b/ch03/GuessMyNumber.java new file mode 100644 index 0000000..d4e77c6 --- /dev/null +++ b/ch03/GuessMyNumber.java @@ -0,0 +1,21 @@ +import java.util.Scanner; +import java.util.Random; + +public class GuessMyNumber{ + + public static void main(String[] args){ + int guessNumber, randomNumber, difference; + Scanner in = new Scanner(System.in); + Random random = new Random(); + + System.out.print("I'm thinking of a number between 1 and 100, inclusive. "); + System.out.println("Can you guess what it is?"); + System.out.print("Type a number: "); + guessNumber = in.nextInt(); + System.out.printf("Your guess is: %d\n", guessNumber); + randomNumber = random.nextInt(100) + 1; + System.out.printf("The number I was thinking of is: %d\n", randomNumber); + difference = guessNumber - randomNumber; + System.out.printf("You were off by: %d", difference); + } +} diff --git a/ch04/Date.java b/ch04/Date.java new file mode 100644 index 0000000..cf38411 --- /dev/null +++ b/ch04/Date.java @@ -0,0 +1,22 @@ +public class Date { + + public static void main(String[] args) { + String day = "Monday"; + int date = 14; + String month = "August"; + int year = 2017; + printAmerican(day, date, month, year); + printEuropean(day, date, month, year); + } + + public static void printAmerican(String day, int date, String month, int year) { + System.out.print("Today's date (American format) is: "); + System.out.println(day + ", " + month + " " + date + ", " + year); + } + + public static void printEuropean(String day, int date, String month, int year) { + System.out.print("Today's date (European format) is: "); + System.out.println(day + " " + date + " " + month + " " + year); + } + +} diff --git a/ch04/Zool.java b/ch04/Zool.java new file mode 100644 index 0000000..0fb1c9b --- /dev/null +++ b/ch04/Zool.java @@ -0,0 +1,15 @@ +public class Zool { + + public static void main (String[] args) { + int value = 11; + String firstPet = "First Pet"; + String streetName = "Home Street"; + zool(value, firstPet, streetName); + } + + public static void zool(int value, String firstPet, String streetName) { + System.out.println("The value is " + value); + System.out.println("My first pet's name was " + firstPet); + System.out.println("I grew up on " + streetName); + } +} diff --git a/ch05/BottlesOfBeerSong.java b/ch05/BottlesOfBeerSong.java new file mode 100644 index 0000000..2e96a93 --- /dev/null +++ b/ch05/BottlesOfBeerSong.java @@ -0,0 +1,29 @@ +public class BottlesOfBeerSong { + + public static void main(String[] args) { + sing(99); + } + + public static void sing(int bottles) { + if (bottles == 0) { + System.out.println("No bottles of beer on the wall,"); + System.out.println("no bottles of beer,"); + System.out.println("ya' can't take one down, ya' can't pass it around"); + System.out.println("'cause there are no more bottles of beer on the wall!"); + } else if (bottles == 1) { + System.out.println(bottles + " bottle of beer on the wall,"); + System.out.println(bottles + " bottle of beer,"); + System.out.println("ya' take it down, ya' pass it around,"); + System.out.println("no bottles of beer on the wall."); + System.out.println(); + sing(bottles - 1); + } else { + System.out.println(bottles + " bottles of beer on the wall,"); + System.out.println(bottles + " bottles of beer,"); + System.out.println("ya' take one down, ya' pass it around,"); + System.out.println(bottles - 1 + " bottles of beer on the wall."); + System.out.println(); + sing(bottles - 1); + } + } +} diff --git a/ch05/Buzz.java b/ch05/Buzz.java index 95e1c2a..69fcccd 100644 --- a/ch05/Buzz.java +++ b/ch05/Buzz.java @@ -1,22 +1,27 @@ public class Buzz { public static void baffle(String blimp) { - System.out.println(blimp); - zippo("ping", -5); + System.out.println(blimp); // 4 + zippo("ping", -5); // 5 } public static void zippo(String quince, int flag) { if (flag < 0) { - System.out.println(quince + " zoop"); + System.out.println(quince + " zoop"); // 6 } else { - System.out.println("ik"); - baffle(quince); - System.out.println("boo-wa-ha-ha"); + System.out.println("ik"); // 2 + baffle(quince); // 3 + System.out.println("boo-wa-ha-ha"); // 7 } } public static void main(String[] args) { - zippo("rattle", 13); + zippo("rattle", 13); // 1 } } + +/* ik + rattle + ping zoop + boo-wa-ha-ha */ diff --git a/ch05/Ex4.java b/ch05/Ex4.java new file mode 100644 index 0000000..40ebc19 --- /dev/null +++ b/ch05/Ex4.java @@ -0,0 +1,15 @@ +public class Ex4 { + + public static void main(String[] args) { + checkFermat(2, 2, 2, 2); + } + + public static void checkFermat(int a, int b, int c, int n) { + boolean fermatTheorem = (Math.pow(a, n) + Math.pow(b, n) == Math.pow(c, n)); + if (n > 2 && fermatTheorem) { + System.out.println("Holy smokes, Fermat was wrong!"); + } else if (!fermatTheorem) { + System.out.println("No, that doesn't work."); + } + } +} diff --git a/ch05/GuessMyNumber.java b/ch05/GuessMyNumber.java new file mode 100644 index 0000000..b0317e2 --- /dev/null +++ b/ch05/GuessMyNumber.java @@ -0,0 +1,33 @@ +import java.util.Scanner; +import java.util.Random; + +public class GuessMyNumber{ + + public static void main(String[] args){ + Random random = new Random(); + int randomNumber = random.nextInt(100) + 1; + System.out.print("I'm thinking of a number between 1 and 100, inclusive. "); + System.out.println("Can you guess what it is?"); + guessMyNumber(randomNumber); + } + + public static void guessMyNumber(int randomNumber) { + Scanner in = new Scanner(System.in); + System.out.print("Type a number: "); + int guessNumber = in.nextInt(); + System.out.printf("Your guess is: %d\n", guessNumber); + checkDifference(guessNumber, randomNumber); + } + + public static void checkDifference(int guessNumber, int randomNumber) { + if (guessNumber > randomNumber) { + System.out.println("Too high. Guess again!"); + } else if (guessNumber < randomNumber) { + System.out.println("Too low. Guess again!"); + } else { + System.out.println("Your guess was correct!"); + return; + } + guessMyNumber(randomNumber); + } +} diff --git a/ch05/PositiveSingleDigit.java b/ch05/PositiveSingleDigit.java new file mode 100644 index 0000000..a3d05ab --- /dev/null +++ b/ch05/PositiveSingleDigit.java @@ -0,0 +1,12 @@ +public class PositiveSingleDigit { + + public static void main(String[] args) { + checkIfPositiveSingleDigit(5); + } + + public static void checkIfPositiveSingleDigit(int x) { + if (x > 0 && x < 10) { + System.out.println("positive single digit number."); + } + } +} diff --git a/ch06/Ex1.java b/ch06/Ex1.java new file mode 100644 index 0000000..4bbfc5d --- /dev/null +++ b/ch06/Ex1.java @@ -0,0 +1,11 @@ +public class Ex1 { + + public static void main(String[] args) { + illegalMethod(1); +// System.out.println("boo!") + 7; + } + + public static int illegalMethod(int value) { + int result = 2 * value; + } +} diff --git a/ch06/Ex2.java b/ch06/Ex2.java new file mode 100644 index 0000000..f0873eb --- /dev/null +++ b/ch06/Ex2.java @@ -0,0 +1,15 @@ +public class Ex2 { + + public static void main(String[] args) { + isDivisible(6, 2); + } + + public static boolean isDivisible(int n, int m) { + /* + boolean result = (n % m == 0); + System.out.println(result); + return result; + */ + return n % m == 0; + } +} diff --git a/ch06/Ex3.java b/ch06/Ex3.java new file mode 100644 index 0000000..1024c52 --- /dev/null +++ b/ch06/Ex3.java @@ -0,0 +1,15 @@ +public class Ex3 { + + public static void main(String[] args) { + isTriangle(3, 4, 5); + } + + public static boolean isTriangle(double a, double b, double c) { + /* + boolean result = (a + b > c && a + c > b && b + c > a); + System.out.println(result); + return result; + */ + return (a + b > c && a + c > b && b + c > a); + } +} diff --git a/ch06/Ex7.java b/ch06/Ex7.java new file mode 100644 index 0000000..14a2e21 --- /dev/null +++ b/ch06/Ex7.java @@ -0,0 +1,18 @@ +public class Ex7 { + + public static void main(String[] args) { + System.out.println(oddSum(5)); + } + + public static int oddSum(int n) { + if (n == 1) { + return n; + } else if (n > 1 && (n % 2 == 1)) { + return n + oddSum(n - 2); + } else { + return oddSum(Math.abs(n) - 1); // this doesn't work for negative numbers + // but I can't return a string that says + // to only provide a positive odd integer + } + } +} diff --git a/ch06/Ex8.java b/ch06/Ex8.java new file mode 100644 index 0000000..9e77b05 --- /dev/null +++ b/ch06/Ex8.java @@ -0,0 +1,18 @@ +public class Ex8 { + + public static void main(String[] args) { + System.out.println(ack(4, 4)); + } + + public static int ack(int m, int n) { + if (m == 0) { + return n + 1; + } else if (m > 0 && n == 0) { + return ack(m - 1, 1); + } else if (m > 0 && n > 0) { + return ack(m - 1, ack(m, n - 1)); + } else { + return 0; + } + } +} diff --git a/ch06/Ex9.java b/ch06/Ex9.java new file mode 100644 index 0000000..f2eca5b --- /dev/null +++ b/ch06/Ex9.java @@ -0,0 +1,16 @@ +public class Ex9 { + + public static void main(String[] args) { + System.out.println(power(2, 3)); + } + + public static double power(double x, int n) { + if (n == 0) { + return 1; + } else if (n % 2 == 0) { + return Math.pow(power(x, n/2), 2); + } else { + return x * power(x, n-1); + } + } +} diff --git a/ch06/Exercise.java b/ch06/Exercise.java index b826b44..fb7fa41 100644 --- a/ch06/Exercise.java +++ b/ch06/Exercise.java @@ -3,13 +3,13 @@ public class Exercise { public static void main(String[] args) { boolean flag1 = isHoopy(202); boolean flag2 = isFrabjuous(202); - System.out.println(flag1); - System.out.println(flag2); + System.out.println(flag1); // true + System.out.println(flag2); // true if (flag1 && flag2) { - System.out.println("ping!"); + System.out.println("ping!"); // ping! } if (flag1 || flag2) { - System.out.println("pong!"); + System.out.println("pong!"); // pong! } } diff --git a/ch06/Multadd.java b/ch06/Multadd.java new file mode 100644 index 0000000..a8f6947 --- /dev/null +++ b/ch06/Multadd.java @@ -0,0 +1,35 @@ +public class Multadd { + + public static void main(String[] args) { + /* simple parameters + double a1 = 1.0; + double b1 = 2.0; + double c1 = 3.0; + System.out.println(multadd(a1, b1, c1)); + */ + /* sin(pi/4) + 1/2 cos(pi/4) + double a2 = 0.5; + double b2 = Math.cos(Math.PI/4.0); + double c2 = Math.sin(Math.PI/4.0); + System.out.println(multadd(a2, b2, c2)); + */ + // log10 + log20 + double a3 = 1.0; + double b3 = Math.log10(10); + double c3 = Math.log10(20); + System.out.println(multadd(a3, b3, c3)); + + System.out.println(expSum(2)); + } + + public static double multadd(double a, double b, double c) { + return a * b + c; + } + + public static double expSum(double x) { + double a4 = x; + double b4 = Math.exp(-x); + double c4 = Math.sqrt(1 - Math.exp(-x)); + return multadd(a4, b4, c4); + } +} diff --git a/ch06/Recursive.java b/ch06/Recursive.java index f045844..3f97689 100644 --- a/ch06/Recursive.java +++ b/ch06/Recursive.java @@ -8,10 +8,9 @@ public static int prod(int m, int n) { if (m == n) { return n; } else { - int recurse = prod(m, n - 1); - int result = n * recurse; - return result; +// int recurse = prod(m, n - 1); +// int result = n * recurse; + return n * prod(m, n - 1); } } - } diff --git a/ch07/Ex2.java b/ch07/Ex2.java new file mode 100644 index 0000000..cf4e4b1 --- /dev/null +++ b/ch07/Ex2.java @@ -0,0 +1,14 @@ +public class Ex2 { + + public static void main(String[] args) { + squareRoot(25.0); + } + + public static void squareRoot(double x) { + double root = x / 2; + while (Math.abs(root - Math.sqrt(x)) > 0.0001) { + root = (root + x / root) / 2; + } + System.out.println(root); + } +} diff --git a/ch07/Ex3.java b/ch07/Ex3.java new file mode 100644 index 0000000..9fe6548 --- /dev/null +++ b/ch07/Ex3.java @@ -0,0 +1,15 @@ +public class Ex3 { + + public static void main(String[] args) { + power(2, 4); + } + + public static void power(double number, int exponent) { + double multiplier = number; + for (int i = exponent - 1; i > 0; i--) { + number = multiplier * number; + } + System.out.printf("%s to the %s power = ", multiplier, exponent); + System.out.println(number); + } +} diff --git a/ch07/Ex4.java b/ch07/Ex4.java new file mode 100644 index 0000000..8a02ee1 --- /dev/null +++ b/ch07/Ex4.java @@ -0,0 +1,14 @@ +public class Ex4 { + + public static void main(String[] args) { + factorial(10); + } + + public static void factorial(int x) { + int result = 1; + for (int i = x; i > 0; i--) { + result = result * i; + } + System.out.println(x + "! = " + result); + } +} diff --git a/ch07/Ex5.java b/ch07/Ex5.java new file mode 100644 index 0000000..77efafe --- /dev/null +++ b/ch07/Ex5.java @@ -0,0 +1,30 @@ +public class Ex5 { + + public static void main(String[] args) { + check(-0.1); + } + + public static void check(double x) { + if (Math.abs(x) <= 100) { + System.out.println(x + "\t" + myexp(x) + "\t" + eToTheX(x)); + x = x * 10; + check(x); + } + } + + public static double myexp(double terms) { + double exponent = 1; + double result = 1; + double multiplier = 1; + for (double i = 1; i < terms; i++) { + multiplier = multiplier * exponent; + result += (multiplier / i); + } + return result; + } + + public static double eToTheX(double exponent) { + double equals = Math.exp(exponent); + return equals; + } +} diff --git a/ch07/Ex6.java b/ch07/Ex6.java new file mode 100644 index 0000000..8d51c60 --- /dev/null +++ b/ch07/Ex6.java @@ -0,0 +1,18 @@ +public class Ex6 { + + public static void main(String[] args) { + System.out.println(gauss(3, 100)); + } + + public static double gauss(double exponent, double terms) { + double result = 1; + double numerator = 1; + double denominator = 1; + for (double i = 1; i < terms; i++) { + numerator = numerator * (-1) * exponent * exponent; + denominator = denominator * i; + result += numerator / denominator; + } + return result; + } +} diff --git a/ch08/Ex1.java b/ch08/Ex1.java new file mode 100644 index 0000000..80ac069 --- /dev/null +++ b/ch08/Ex1.java @@ -0,0 +1,43 @@ +import java.util.Arrays; +import java.util.Random; + +public class Ex1 { + + public static void main(String[] args) { + double[] values = {1.0, 2.0, 3.0, 4.0, 5.0}; + System.out.print("Original array: "); + System.out.println(Arrays.toString(values)); + powArray(values, 2); + int[] scores = createScoresArray(100); + histogram(scores); + } + + public static void powArray(double[] array, int power) { + double[] newArray = Arrays.copyOf(array, array.length); + for (int i = 1; i < array.length; i++) { + newArray[i] = Math.pow(array[i], power); + } + System.out.print("Original array to the power of " + power + ": "); + System.out.println(Arrays.toString(newArray)); + } + + public static int[] createScoresArray(int size) { + Random randomScore = new Random(); + int[] randomArray = new int[size]; + for (int i = 0; i < randomArray.length; i++) { + randomArray[i] = randomScore.nextInt(100); + } + System.out.print(size + " random scores: "); + System.out.println(Arrays.toString(randomArray)); + return randomArray; + } + + public static void histogram(int[] scores) { + System.out.print("Distribution of scores: "); + int[] scoreDistribution = new int[scores.length]; + for (int score : scores) { + scoreDistribution[score]++; + } + System.out.println(Arrays.toString(scoreDistribution)); + } +} diff --git a/ch08/Ex4.java b/ch08/Ex4.java new file mode 100644 index 0000000..f612959 --- /dev/null +++ b/ch08/Ex4.java @@ -0,0 +1,34 @@ +import java.util.Arrays; +import java.util.Random; +public class Ex4 { + + public static void main(String[] args) { + int[] randomArray = createRandomArray(10); + System.out.print("The max value is located at index: "); + System.out.println(indexOfMax(randomArray)); + } + + public static int[] createRandomArray(int length) { + Random random = new Random(); + int[] randomArray = new int[length]; + for (int i = 0; i < randomArray.length; i++) { + randomArray[i] = random.nextInt(100); + } + System.out.print("Random array created: "); + System.out.println(Arrays.toString(randomArray)); + return randomArray; + } + + public static int indexOfMax(int[] numbers) { + int max = 0; + int maxIndex = 0; + for (int i = 0; i < numbers.length; i++) { + if (numbers[i] > max) { + max = numbers[i]; + maxIndex = i; + } + } + return maxIndex; + } + +} diff --git a/ch08/Ex6.java b/ch08/Ex6.java new file mode 100644 index 0000000..4d8cf80 --- /dev/null +++ b/ch08/Ex6.java @@ -0,0 +1,19 @@ +public class Ex6 { + + public static void main(String[] args) { + int[] numbers = {3, 6, 9, 12, 15}; + isFactor(2, numbers); + } + + public static boolean isFactor(int factor, int[] numbers) { + boolean result = true; + for (int i = 0; i < numbers.length; i++) { + if (numbers[i] % factor != 0) { + result = false; + } + } + System.out.println(result); + return result; + } + +} diff --git a/ch08/Ex7.java b/ch08/Ex7.java new file mode 100644 index 0000000..9c00aad --- /dev/null +++ b/ch08/Ex7.java @@ -0,0 +1,34 @@ +import java.util.Arrays; + +public class Ex7 { + + public static void main(String[] args) { + int[] array = {4, 8, 12, 16, 20}; + isPrimeFactor(2, array); + } + + public static boolean isPrimeFactor(int integer, int[] array) { + boolean result = isPrime(integer) == true && isFactor(integer, array) == true; + System.out.println("Therefore is " + integer + " a prime factor? " + result); + return result; + } + + public static boolean isPrime(int integer) { + boolean[] primes = Ex5.sieveOfEratosthenes(integer); + System.out.println("Is " + integer + " prime? " + primes[integer - 1]); + if (primes[integer - 1]) { + return true; + } else { + return false; + } + } + + public static boolean isFactor(int integer, int[] numbers) { + System.out.print("Is " + integer + " a factor of "); + System.out.print(Arrays.toString(numbers)); + System.out.print("? "); + boolean factor = Ex6.isFactor(integer, numbers); + return factor; + } + +} diff --git a/ch08/Ex8.java b/ch08/Ex8.java new file mode 100644 index 0000000..9f1dbba --- /dev/null +++ b/ch08/Ex8.java @@ -0,0 +1,22 @@ +public class Ex8 { + + public static void main(String[] args) { + int[] array = Ex4.createRandomArray(10); + maxInRange(array, 3, 7); + } + + public static void maxInRange(int[] array, int lowIndex, int highIndex) { + if (lowIndex <= highIndex) { + if (array[lowIndex] <= array[highIndex]) { + int max = array[lowIndex]; + System.out.println(max); + maxInRange(array, lowIndex + 1, highIndex); + } else if (array[lowIndex] >= array[highIndex]) { + int max = array[highIndex]; + System.out.println(max); + maxInRange(array, lowIndex, highIndex - 1); + } + } + } + +} diff --git a/ch08/Fruit.java b/ch08/Fruit.java index 50428c5..da68a0b 100644 --- a/ch08/Fruit.java +++ b/ch08/Fruit.java @@ -3,33 +3,33 @@ */ public class Fruit { - public static int banana(int[] a) { - int kiwi = 1; - int i = 0; - while (i < a.length) { - kiwi = kiwi * a[i]; - i++; + public static int banana(int[] a) { // defines a function "banana," which takes the argument integer array "a" and returns an integer + int kiwi = 1; // defines an integer "kiwi" and sets it equal to 1 + int i = 0; // defines a new integer "i" and sets it equal to 0 + while (i < a.length) { // while "i" is less than the number of elements in "a" + kiwi = kiwi * a[i]; // set "kiwi" equal to its existing value times the ith element in "a" + i++; // increase i and repeat until the "while" condition is no longer satisified } - return kiwi; + return kiwi; // return the final integer value of "kiwi," which is all of the elements in "a" multiplied together } - public static int grapefruit(int[] a, int grape) { - for (int i = 0; i < a.length; i++) { - if (a[i] == grape) { - return i; + public static int grapefruit(int[] a, int grape) { // defines a function "grapefruit," which takes the arguments integer array "a" and integer "grape," and returns an integer + for (int i = 0; i < a.length; i++) { // for integer "i" starting at 0, if "i" is less than the number of elements in "a," then iterate again + if (a[i] == grape) { // if the current index is equal to the argument "grape" + return i; // then return the current integer value of "i," which is the array index containing the matching value } } - return -1; + return -1; // otherwise, return "-1" to indicate the search method failed to find the argument "grape" in the provided array } - public static int pineapple(int[] a, int apple) { - int pear = 0; - for (int pine: a) { - if (pine == apple) { - pear++; + public static int pineapple(int[] a, int apple) { // defines a function "pineapple," which takes the arguments integer array "a" and integer "apple," and returns an integer + int pear = 0; // defines an integer "pear" and sets it equal to 0 + for (int pine: a) { // call each value of array "a" "pine" + if (pine == apple) { // and if that current value, "pine," is equal to the provided integer "apple" + pear++; // add 1 to "pear" } } - return pear; + return pear; // return the final value of "pear," which represents the number of times the integer "apple" occurs in the array "a" } } diff --git a/ch08/sieveOfEratosthenes.java b/ch08/sieveOfEratosthenes.java new file mode 100644 index 0000000..f5f892c --- /dev/null +++ b/ch08/sieveOfEratosthenes.java @@ -0,0 +1,41 @@ +import java.util.Arrays; + +public class sieveOfEratosthenes { + + public static void main(String[] args) { + sieveOfEratosthenes(100); + } + + public static boolean[] sieveOfEratosthenes(int limit) { + int[] numbers = makeIntArray(limit); + boolean[] primes = makeBooleanArray(limit); + System.out.println("Prime numbers up to " + limit + " :"); + System.out.printf("%6d ", numbers[1]); + System.out.println(primes[1]); + for (int i = 2; i < limit; i++) { + for (int j = 1; j < (limit - i); j++) { + if (numbers[i + j] % i == 0) { + primes[i + j] = false; + } + } + System.out.printf("%6d ", (i + 1)); + System.out.println(primes[i]); + } + return primes; + } + + public static int[] makeIntArray(int size) { + int[] array = new int[size]; + for (int i = 0; i < array.length; i++) { + array[i] = i + 1; + } + return array; + } + + public static boolean[] makeBooleanArray(int size) { + boolean[] array = new boolean[size]; + Arrays.fill(array, true); + return array; + } + +} diff --git a/ch09/Abecedarian.java b/ch09/Abecedarian.java new file mode 100644 index 0000000..bbe6c05 --- /dev/null +++ b/ch09/Abecedarian.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +public class Abecedarian { + + public static void main(String[] args) { + String stringIn; + Scanner input = new Scanner(System.in); + System.out.print("Please enter a word: "); + stringIn = input.nextLine(); + + isAbecedarian(stringIn); + } + + public static void isAbecedarian(String s) { + int i = 0; + boolean result = false; + String lowerS = s.toLowerCase(); + + do { + if (s.charAt(i) < s.charAt(i + 1)) { + result = true; + i++; + } else { + result = false; + break; + } + } while (i < ((s.length()) - 1)); + + System.out.println("Is \"" + s + "\" abecedarian? " + result); + } + +} diff --git a/ch09/Anagram.java b/ch09/Anagram.java new file mode 100644 index 0000000..fdedcfc --- /dev/null +++ b/ch09/Anagram.java @@ -0,0 +1,46 @@ +import java.util.Scanner; + +public class Anagram { + + public static void main(String[] args) { + String firstWord, secondWord; + int[] firstWordHist, secondWordHist; + boolean result; + + Scanner input = new Scanner(System.in); + + System.out.println("Let's find out if two words are anagrams."); + + System.out.print("Please enter the first word: "); + firstWord = input.nextLine(); + + System.out.print("Please enter the second word: "); + secondWord = input.nextLine(); + + firstWordHist = createLetterHistogram(firstWord); + secondWordHist = createLetterHistogram(secondWord); + + result = isAnagram(firstWordHist, secondWordHist); + + System.out.print("Are " + firstWord + " and " + secondWord + " anagrams? "); + System.out.println(result); + } + + public static int[] createLetterHistogram(String word) { + int[] wordHist; + wordHist = Ex2.letterHist(word); + return wordHist; + } + + public static boolean isAnagram(int[] firstWordHist, int[] secondWordHist) { + boolean anagram = true; + for (int i = 0; i < firstWordHist.length; i++) { + if (firstWordHist[i] != secondWordHist[i]) { + anagram = false; + break; + } + } + return anagram; + } + +} diff --git a/ch09/Doubloon.java b/ch09/Doubloon.java new file mode 100644 index 0000000..2904172 --- /dev/null +++ b/ch09/Doubloon.java @@ -0,0 +1,31 @@ +import java.util.Scanner; + +public class Doubloon { + + public static void main(String[] args) { + String word; + int[] letterHistogram; + Scanner input = new Scanner(System.in); + System.out.print("Please enter a word: "); + word = input.nextLine(); + letterHistogram = Ex2.letterHist(word); + isDoubloon(word); + } + + public static void isDoubloon(int[] letterHistogram){ + String uniformWord = word.toLowerCase(); + int i = 0; + boolean result = false; + do { + if (letterHistogram[i] == letterHistogram[i + 1]) { + result = true; + i++; + } else { + result = false; + break; + } + } while (i < (letterHistogram.length - 1)); + System.out.println(result); + } + +} diff --git a/ch09/Ex1cont.java b/ch09/Ex1cont.java new file mode 100644 index 0000000..c6fadbb --- /dev/null +++ b/ch09/Ex1cont.java @@ -0,0 +1,11 @@ +public class Ex1cont { + + public static void main(String[] args) { + String string = "Duck duck goose"; + char character = string.charAt(0); + String empty = ""; + int five = 5; + System.out.println(empty + five); + } + +} diff --git a/ch09/Ex2.java b/ch09/Ex2.java new file mode 100644 index 0000000..34d2e0b --- /dev/null +++ b/ch09/Ex2.java @@ -0,0 +1,31 @@ +import java.util.Arrays; + +public class Ex2 { + + public static void main(String[] args) { + String myString = "I'm just a string, yes I'm only a string"; + letterHist(myString); + } + + public static int[] letterHist(String textString) { + String upperCaseString = textString.toUpperCase(); + String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + int[] histogram = createEmptyArray(alphabet.length()); + + for (int i = 0; i < upperCaseString.length(); i++) { + char stringCharacter = upperCaseString.charAt(i); + int whichLetterOfTheAlphabet = alphabet.indexOf(stringCharacter); + if (whichLetterOfTheAlphabet >= 0) { + histogram[whichLetterOfTheAlphabet] += 1; + } + } +// System.out.println(Arrays.toString(histogram)); + return histogram; + } + + public static int[] createEmptyArray(int size) { + int[] emptyArray = new int[size]; + return emptyArray; + } + +} diff --git a/ch09/Exercise.java b/ch09/Exercise.java index d8a605d..3d1230d 100644 --- a/ch09/Exercise.java +++ b/ch09/Exercise.java @@ -1,10 +1,21 @@ /** * Exercise on encapsulation and generalization. */ + +import java.util.Scanner; + public class Exercise { public static void main(String[] args) { - String s = "((3 + 7) * 2)"; + Scanner input = new Scanner(System.in); + + System.out.print("Enter a string of text, such as a simple math calculation with parentheses: "); + String s = input.nextLine(); + openCloseParentheses(s); + } + + public static void openCloseParentheses(String s) { + System.out.println(s); int count = 0; for (int i = 0; i < s.length(); i++) { @@ -15,8 +26,8 @@ public static void main(String[] args) { count--; } } - - System.out.println(count); + System.out.println("Number of open parentheses: " + count); } + } diff --git a/ch09/Recurse.java b/ch09/Recurse.java index c4ba2fb..a0bda99 100644 --- a/ch09/Recurse.java +++ b/ch09/Recurse.java @@ -1,8 +1,28 @@ /** * Recursion exercise. */ + +import java.util.Scanner; + public class Recurse { + public static void main(String[] args) { + String s, sBackward; + Scanner stringIn = new Scanner(System.in); + + System.out.print("Enter a string of text. "); + s = stringIn.nextLine(); + + System.out.println("First character: " + first(s)); + System.out.println("Remainder of the string: " + rest(s)); + System.out.println("Middle of the string: " + middle(s)); + System.out.println("Length: " + length(s)); + + printString(s); + sBackward = printBackward(s); + isPalindrome(s, sBackward); + } + /** * Returns the first character of the given String. */ @@ -31,4 +51,25 @@ public static int length(String s) { return s.length(); } + public static void printString(String s) { + for (int i = 0; i < s.length(); i++) { + System.out.println(s.charAt(i)); + } + } + + public static String printBackward(String s) { + String backward = ""; + for (int i = (s.length() - 1); i >= 0; i--) { + backward += s.charAt(i); + } + return backward; + } + + public static boolean isPalindrome(String s, String sBackward) { + boolean check = s.equals(sBackward); + System.out.print("Is your string a palindrome? "); + System.out.println(check); + return check; + } + } diff --git a/ch09/Scrabble.java b/ch09/Scrabble.java new file mode 100644 index 0000000..252685d --- /dev/null +++ b/ch09/Scrabble.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + +public class Scrabble { + + public static void main(String[] args) { + String word, letters; + int[] letterHist, wordHist; + boolean result; + + Scanner input = new Scanner(System.in); + + System.out.println("Let's play Scrabble. Can you spell your word with the letters you have?"); + + System.out.print("What letters do you have? "); + letters = input.nextLine(); + + System.out.print("What word are you trying to spell? "); + word = input.nextLine(); + + letterHist = createLetterHistogram(letters); + wordHist = createLetterHistogram(word); + + result = canSpell(wordHist, letterHist); + System.out.print("Can \"" + word + "\" be spelled with " + letters + "? "); + System.out.println(result); + } + + public static int[] createLetterHistogram(String string) { + int[] stringHist = Ex2.letterHist(string); + return stringHist; + } + + public static boolean canSpell(int[] word, int[] letters) { + boolean enoughLetters = true; + for (int i = 0; i < word.length; i++) { + if (letters[i] < word[i]) { + enoughLetters = false; + break; + } + } + return enoughLetters; + } + +} diff --git a/ch10/Big.java b/ch10/Big.java new file mode 100644 index 0000000..4e7be54 --- /dev/null +++ b/ch10/Big.java @@ -0,0 +1,24 @@ +import java.math.BigInteger; +import java.util.Scanner; + +public class Big { + + public static void main(String[] args) { + // int startNum = askUserForInteger(); + // int endNum = factorial(startNum); + for (int i = 0; i <= 30; i ++) { + factorial(i); // over ~15 these values get wonky because they get too big for the int data type + } // I think BigInteger is mutable because I can construct a new BigInteger and set the attribute + } + + public static BigInteger factorial(int x) { + int result = 1; + BigInteger bigResult = BigInteger.valueOf(result); + for (int i = x; i > 0; i--) { + BigInteger counter = BigInteger.valueOf(i); + bigResult = bigResult.multiply(counter); + } + System.out.println(x + "! = " + bigResult); + return bigResult; + } +} diff --git a/ch10/Pow.java b/ch10/Pow.java index 9ba0952..c699e74 100644 --- a/ch10/Pow.java +++ b/ch10/Pow.java @@ -1,24 +1,36 @@ /** * BigInteger exercise. */ +import java.math.BigInteger; + public class Pow { + public static void main(String[] args) { + System.out.println(pow(27, 13)); + } /** * Integer exponentiation. */ - public static int pow(int x, int n) { - if (n == 0) return 1; + public static BigInteger pow(int x, int n) { + BigInteger one = BigInteger.valueOf(1); + if (n == 0) return one; // find x to the n/2 recursively - int t = pow(x, n / 2); + BigInteger bigX = BigInteger.valueOf(x); + BigInteger t = pow(x, n / 2); // if n is even, the result is t squared // if n is odd, the result is t squared times x + // if (n % 2 == 0) { + // return t * t; + // } else { + // return t * t * x; + // } + if (n % 2 == 0) { - return t * t; + return t.multiply(t); } else { - return t * t * x; + return t.multiply(t.multiply(bigX)); } } - }