diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..0727904 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,25 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch via NPM", + "runtimeExecutable": "npm", + "runtimeArgs": [ + "run-script", + "debug" + ], + "port": 9229 + }, + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${file}" + } + ] +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 2/Date.java b/Exercise Solutions/Chapter 2/Date.java new file mode 100644 index 0000000..f9e26bb --- /dev/null +++ b/Exercise Solutions/Chapter 2/Date.java @@ -0,0 +1,18 @@ +public class Date { + + public static void main(String[] args) { + String month = "December"; + String day = "Friday"; + int date = 1; + int year = 2017; + +// American Format output + System.out.println("American Format:"); + System.out.println(day + ", " + month + " " + date + ", " + year); +// European Format: + System.out.println("European Format:"); + System.out.println(day + " " + date + " " + month + " " + year); + + } + +} diff --git a/Exercise Solutions/Chapter 2/Time.java b/Exercise Solutions/Chapter 2/Time.java new file mode 100644 index 0000000..e9f9bf2 --- /dev/null +++ b/Exercise Solutions/Chapter 2/Time.java @@ -0,0 +1,22 @@ +public class Time { + + public static void main(String[] args) { + int hour = 15; + int minute = 46; + int second = 41; + int secondsSinceMidnight = ((hour * 60) *60) + (minute * 60) + second; + int midnight = (24 * 60) * 60; + double midnightDouble = (24.0 * 60.0) * 60; + int oldTime = 55820; + + System.out.println("Number of seconds since Midnight: "); + System.out.println(secondsSinceMidnight); + System.out.println("Number of seconds until Midnight: "); + System.out.println(midnight - secondsSinceMidnight); + System.out.println("Percentage of day that has passed:"); + System.out.println( (secondsSinceMidnight / midnightDouble) * 100 + "%"); + System.out.println("Time elapsed since the start of this project"); + System.out.println((secondsSinceMidnight - oldTime) / 60 + " minutes"); + + } +} diff --git a/Exercise Solutions/Chapter 3/3-1 Solution.txt b/Exercise Solutions/Chapter 3/3-1 Solution.txt new file mode 100644 index 0000000..abcce8c --- /dev/null +++ b/Exercise Solutions/Chapter 3/3-1 Solution.txt @@ -0,0 +1 @@ +You get an Illegal Format Conversion Exeption. For example if printf is expecting a %f and instead it gets a int, when you run the code you get f != java.lang.Integer, and vice versa for a int error. If you don’t have the correct number of arguments you get a missing format argument exception erro diff --git a/Exercise Solutions/Chapter 3/CelsiusConvert.java b/Exercise Solutions/Chapter 3/CelsiusConvert.java new file mode 100644 index 0000000..6bd36ee --- /dev/null +++ b/Exercise Solutions/Chapter 3/CelsiusConvert.java @@ -0,0 +1,21 @@ +import java.util.Scanner; + +//Converting Celsius to Fahrenheit + +public class CelsiusConvert { + + public static void main(String[] args) { + double celsiusTemp; + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + System.out.print("Please input temperature in Celsius: "); + celsiusTemp = in.nextDouble(); + + //Convert and output the result + double fahrenheit = (celsiusTemp *(9.0/5.0)) + 32; + System.out.printf("%.2f Celsius = %.2f Fahrenheit \n", + celsiusTemp, fahrenheit); + } + +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 3/GuessStarter.java b/Exercise Solutions/Chapter 3/GuessStarter.java new file mode 100644 index 0000000..f017722 --- /dev/null +++ b/Exercise Solutions/Chapter 3/GuessStarter.java @@ -0,0 +1,26 @@ +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) { + int userNumber; + int difference; + + // pick a random number + Random random = new Random(); + int number = random.nextInt(100) + 1; + //Ask user for number + System.out.println("Please Enter a number between 0 and 100 "); + Scanner in = new Scanner(System.in); + userNumber = in.nextInt(); + difference = number - userNumber; + System.out.println("The difference between the computer's number and your number is " + difference); + System.out.println("Computer's number: " + number + ", Your number: " + userNumber); + //System.out.println(number); + } + +} diff --git a/Exercise Solutions/Chapter 3/SecondsToHours.java b/Exercise Solutions/Chapter 3/SecondsToHours.java new file mode 100644 index 0000000..0a9c3bc --- /dev/null +++ b/Exercise Solutions/Chapter 3/SecondsToHours.java @@ -0,0 +1,24 @@ +import java.util.Scanner; + +//Converting Celsius to Fahrenheit + +public class SecondsToHours { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + + double userSeconds, hours, minutes, seconds ; + + // prompt the user for number of seconds and get the value + System.out.print("Please input number of seconds: "); + userSeconds = in.nextInt(); + + //Convert and output the result + minutes = userSeconds/60; + hours = (minutes/60); + seconds = userSeconds % 60; + + System.out.printf("%.3f hours, %.0f minutes, and %.0f seconds \n", + hours, minutes, seconds); + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 4/4-1 solution.txt b/Exercise Solutions/Chapter 4/4-1 solution.txt new file mode 100644 index 0000000..171474f --- /dev/null +++ b/Exercise Solutions/Chapter 4/4-1 solution.txt @@ -0,0 +1,5 @@ +No, I wug. +You wugga wug. +I wug. + +If you invoke baffle at the end of the ping method, you will get an infinite loop. \ No newline at end of file diff --git a/Exercise Solutions/Chapter 4/Encapsulate.java b/Exercise Solutions/Chapter 4/Encapsulate.java new file mode 100644 index 0000000..80e37df --- /dev/null +++ b/Exercise Solutions/Chapter 4/Encapsulate.java @@ -0,0 +1,30 @@ +public class Encapsulate { + + public static void printAmerican(String day, String month, int date, int year) { + System.out.println(day + ", " + month + " " + date + ", " + year); + + } + + public static void printEuropean(String day, int date, String month, int year) { + System.out.println(day + " " + date + " " + month + " " + year); + } + + public static void main(String[] args) { + String month = "December"; + String day = "Friday"; + int date = 1; + int year = 2017; + printAmerican(day, month, date, year); + printEuropean(day, date, month, year); + + +//// American Format output +// System.out.println("American Format:"); +// System.out.println(day + ", " + month + " " + date + ", " + year); +//// European Format: +// System.out.println("European Format:"); +// System.out.println(day + " " + date + " " + month + " " + year); + + } + +} diff --git a/Exercise Solutions/Chapter 4/Parameters.java b/Exercise Solutions/Chapter 4/Parameters.java new file mode 100644 index 0000000..a8c6c99 --- /dev/null +++ b/Exercise Solutions/Chapter 4/Parameters.java @@ -0,0 +1,17 @@ +public class Parameters { + + public static void zool(int number, String petName, String street) { + System.out.println("Your number: " + number); + System.out.println("Your first pet: " + petName); + System.out.println("The street: " + street); + } + + public static void main(String[] args) { + int value = 11; + String firstPet = "Chris the chicken"; + String street = "Avalanche road"; + zool(value, firstPet, street); + } + +} + diff --git a/Exercise Solutions/Chapter 5/5-6 solution.txt b/Exercise Solutions/Chapter 5/5-6 solution.txt new file mode 100644 index 0000000..d7ab777 --- /dev/null +++ b/Exercise Solutions/Chapter 5/5-6 solution.txt @@ -0,0 +1,5 @@ +Q #3: "rattle" +Q #4: ik + rattle + ping zoop + boo-wa-ha-ha \ No newline at end of file diff --git a/Exercise Solutions/Chapter 5/BottlesOfBeer.java b/Exercise Solutions/Chapter 5/BottlesOfBeer.java new file mode 100644 index 0000000..f45de95 --- /dev/null +++ b/Exercise Solutions/Chapter 5/BottlesOfBeer.java @@ -0,0 +1,19 @@ +public class BottlesOfBeer { + public static void beerBottles(int num) { + if(num == 0) { + System.out.println("No bottles of beer on the wall,"); + System.out.println("no bottles of beer,"); + System.out.println("ya' cant take one down, ya can't pass it around,"); + System.out.println("'cause thre are no more bottles of beer on the wall!"); + } else { + System.out.println(num + " bottles of beer on the wall,"); + System.out.println(num + " bottles of beer,"); + System.out.println("ya' take one down, ya' pass it around,"); + System.out.println(num + " bottles of beer on the wall."); + beerBottles(num -1); + } + } + public static void main(String[] args) { + beerBottles(99); + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 5/NumberGuess.java b/Exercise Solutions/Chapter 5/NumberGuess.java new file mode 100644 index 0000000..6a40d4a --- /dev/null +++ b/Exercise Solutions/Chapter 5/NumberGuess.java @@ -0,0 +1,30 @@ +import java.util.Scanner; +import java.util.Random; + +public class NumberGuess { + public static void main(String[] args) { + Random random = new Random(); + int randomNumber = random.nextInt(100) + 1; + findUserNumber(randomNumber); + } + public static void findUserNumber(int randomNumber) { + System.out.println("Please Enter a number between 0 and 100 "); + Scanner in = new Scanner(System.in); + int userNumber = in.nextInt(); + System.out.printf("Your number is: %d\n", userNumber); + checkDifference(userNumber, randomNumber); + } + + public static void checkDifference(int userNumber, int randomNumber) { + if(userNumber == randomNumber) { + System.out.println("Your number is correct!"); + } + else if(userNumber > randomNumber) { + System.out.println("Your number is too high, guess again!"); + findUserNumber(randomNumber); + } else { + System.out.println("Your number is too low, guess again!"); + findUserNumber(randomNumber); + } + } +} diff --git a/Exercise Solutions/Chapter 5/OneStatement.java b/Exercise Solutions/Chapter 5/OneStatement.java new file mode 100644 index 0000000..109d212 --- /dev/null +++ b/Exercise Solutions/Chapter 5/OneStatement.java @@ -0,0 +1,10 @@ +public class OneStatement { + public static void main(String[] args){ + positiveSingleDigit(4); + } + public static void positiveSingleDigit(int num) { + if(num > 0 && num < 10){ + System.out.println("Positive single digit number."); + } + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 5/checkFermatExercise.java b/Exercise Solutions/Chapter 5/checkFermatExercise.java new file mode 100644 index 0000000..32ae96a --- /dev/null +++ b/Exercise Solutions/Chapter 5/checkFermatExercise.java @@ -0,0 +1,17 @@ +public class checkFermatExercise { + public static void checkFermat(int a, int b, int c, int n) { + int aPower = (int) Math.pow(a, n); + int bPower = (int) Math.pow(b, n); + int cPower = (int) Math.pow(c, n); + + if(n > 2 && aPower + bPower == cPower) { + System.out.println("Holy smokes, Fermat was wrong!"); + } else { + System.out.println("No, that doesn't work"); + } + } + public static void main(String[] args) { + checkFermat(15, 2, 3, 4); + } +} + \ No newline at end of file diff --git a/Exercise Solutions/Chapter 6/Exercise6.java b/Exercise Solutions/Chapter 6/Exercise6.java new file mode 100644 index 0000000..a096190 --- /dev/null +++ b/Exercise Solutions/Chapter 6/Exercise6.java @@ -0,0 +1,16 @@ +public class Exercise6 { + public static void main(String[] args) { + System.out.println(prod(1,4)); + } + 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; + //return n * (prod(m, n-1); + } + } +} + \ No newline at end of file diff --git a/Exercise Solutions/Chapter 6/Exercise6_5.java b/Exercise Solutions/Chapter 6/Exercise6_5.java new file mode 100644 index 0000000..176e2b9 --- /dev/null +++ b/Exercise Solutions/Chapter 6/Exercise6_5.java @@ -0,0 +1,37 @@ +public class Exercise6_5 { + public static void main(String[] args) { + boolean flag1 = isHoopy(202); + boolean flag2 = isFrabjuous(202); + System.out.println(flag1); + System.out.println(flag2); + + if(flag1 && flag2) { + System.out.println("ping!"); + } + if(flag1 || flag2) { + System.out.println("pong!"); + } + } + + public static boolean isHoopy(int x) { + boolean hoopyFlag; + if(x % 2 == 0) { + hoopyFlag = true; + } else { + hoopyFlag = false; + } + return hoopyFlag; + } + + public static boolean isFrabjuous(int x ) { + boolean frabjuousFlag; + if(x > 0) { + frabjuousFlag = true; + } else { + frabjuousFlag = false; + } + return frabjuousFlag; + } +} + +// Results are: true; true; ping!; pong!; \ No newline at end of file diff --git a/Exercise Solutions/Chapter 6/Exercise6_8.java b/Exercise Solutions/Chapter 6/Exercise6_8.java new file mode 100644 index 0000000..2c937c2 --- /dev/null +++ b/Exercise Solutions/Chapter 6/Exercise6_8.java @@ -0,0 +1,20 @@ +public class Exercise6_8 { + public static void main(String[] args) { + System.out.println(ack(3,2)); + } + public static int ack(int m, int n) { + if(m == 0) { + return n + 1; + } + if(m > 0 && n == 0) { + return ack(m - 1, 1); + } + if(m > 0 && n > 0) { + return ack(m -1, ack(m, n - 1)); + //return 0 if n or m is less than 0; + } else { + return 0; + } + } + +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 6/Exercise6_9.java b/Exercise Solutions/Chapter 6/Exercise6_9.java new file mode 100644 index 0000000..d93aa6f --- /dev/null +++ b/Exercise Solutions/Chapter 6/Exercise6_9.java @@ -0,0 +1,15 @@ +public class Exercise6_9 { + public static void main(String[] args) { + System.out.println(power(2.0, 80)); + } + public static double power(double x, int n) { +// double dn = (double)n; +// Original Exercise +// return x * Math.pow(x, (n - 1)); + +// Optional Exercise + double temp = Math.pow(x, (n/2)); + return Math.pow(temp, 2); + } +} + diff --git a/Exercise Solutions/Chapter 6/Fibonacci.java b/Exercise Solutions/Chapter 6/Fibonacci.java new file mode 100644 index 0000000..74509bc --- /dev/null +++ b/Exercise Solutions/Chapter 6/Fibonacci.java @@ -0,0 +1,11 @@ +public class Fibonacci { + public static void main(String[] args) { + System.out.println(fibonacci(3)); + } + public static int fibonacci(int n) { + if(n == 1 || n == 2) { + return 1; + } + return fibonacci(n - 1) + fibonacci(n - 2); + } +} diff --git a/Exercise Solutions/Chapter 6/FibonacciTest.java b/Exercise Solutions/Chapter 6/FibonacciTest.java new file mode 100644 index 0000000..f5d8b0e --- /dev/null +++ b/Exercise Solutions/Chapter 6/FibonacciTest.java @@ -0,0 +1,22 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class FibonacciTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testFibonacci() { + assertEquals(1, Series.fibonacci(1)); + assertEquals(1, Series.fibonacci(2)); + assertEquals(2, Series.fibonacci(3)); + } + +} diff --git a/Exercise Solutions/Chapter 6/IsDivisible.java b/Exercise Solutions/Chapter 6/IsDivisible.java new file mode 100644 index 0000000..202a835 --- /dev/null +++ b/Exercise Solutions/Chapter 6/IsDivisible.java @@ -0,0 +1,11 @@ +public class IsDivisible { + public static void main(String[] args) { + System.out.println(isDivisible(10, 5)); + } + public static boolean isDivisible(int n, int m) { + if(n % m == 0) { + return true; + } + return false; + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 6/IsDivisibleTest.java b/Exercise Solutions/Chapter 6/IsDivisibleTest.java new file mode 100644 index 0000000..55bfa90 --- /dev/null +++ b/Exercise Solutions/Chapter 6/IsDivisibleTest.java @@ -0,0 +1,20 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class IsDivisibleTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testisDivisible() { + assertTrue(true, Series.isDivisible(10, 5)); + } + +} diff --git a/Exercise Solutions/Chapter 6/IsTriangle.java b/Exercise Solutions/Chapter 6/IsTriangle.java new file mode 100644 index 0000000..7937c76 --- /dev/null +++ b/Exercise Solutions/Chapter 6/IsTriangle.java @@ -0,0 +1,20 @@ +public class IsTriangle { + public static void main(String[] args) { + boolean result = isTriangle(1, 2, 3); + System.out.println(result); + } + public static boolean isTriangle(int a, int b, int c) { + if(a > b + c) { + return false; + } + else if(b > a + c) { + return false; + } + else if(c > a + b) { + return false; + } + else { + return true; + } + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 6/IsTriangleTest.java b/Exercise Solutions/Chapter 6/IsTriangleTest.java new file mode 100644 index 0000000..509b574 --- /dev/null +++ b/Exercise Solutions/Chapter 6/IsTriangleTest.java @@ -0,0 +1,20 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class IsTriangleTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testisTriangle() { + assertEquals(true, Series.isTriangle(1,2,3)); + } + +} diff --git a/Exercise Solutions/Chapter 6/Multadd.java b/Exercise Solutions/Chapter 6/Multadd.java new file mode 100644 index 0000000..a561a9e --- /dev/null +++ b/Exercise Solutions/Chapter 6/Multadd.java @@ -0,0 +1,30 @@ +public class Multadd { + public static void main(String[] args) { + System.out.println(multadd(1.0, 2.0, 3.0)); + + double a = Math.cos(Math.PI/4); + double b = 0.5; + double c = Math.sin(Math.PI/4); + System.out.println(multadd(a, b, c)); + + double a1 = 1; + double b1 = Math.log(20); + double c1 = Math.log(20); + System.out.println(multadd(a1, b1, c1)); + + //expSum + 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 a2 = x; + double b2 = Math.exp(-x); + double c2 = Math.sqrt(1 - Math.exp(-x)); + return multadd(a2, b2, c2); + } + +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 6/OddSum.java b/Exercise Solutions/Chapter 6/OddSum.java new file mode 100644 index 0000000..911593d --- /dev/null +++ b/Exercise Solutions/Chapter 6/OddSum.java @@ -0,0 +1,15 @@ +public class OddSum { + public static void main(String[] args) { + System.out.println(oddSum(15)); + } + public static int oddSum(int n) { + if(n == 0) { + return 0; + } + if(n == 1) { + return 1; + } else { + return oddSum(n-2) + n; + } + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 7/Exercise7_2.java b/Exercise Solutions/Chapter 7/Exercise7_2.java new file mode 100644 index 0000000..f20963b --- /dev/null +++ b/Exercise Solutions/Chapter 7/Exercise7_2.java @@ -0,0 +1,13 @@ +public class Exercise7_2 { + public static void main(String[] args) { + System.out.printf("%.2f\n", squareRoot(25)); + } + + public static double squareRoot(double num) { + double a = num/2; + while(Math.abs(a - Math.sqrt(num)) > 0.0001) { + a = (a + (num/a))/2; + } + return a; + } +} diff --git a/Exercise Solutions/Chapter 7/Exercise7_3.java b/Exercise Solutions/Chapter 7/Exercise7_3.java new file mode 100644 index 0000000..1844f58 --- /dev/null +++ b/Exercise Solutions/Chapter 7/Exercise7_3.java @@ -0,0 +1,13 @@ +public class Exercise7_3{ + public static void main(String[] args) { + //Something + System.out.println(power(4,2)); + } + public static double power(double x, int n) { + double result = x; + for(int i =1; i < n; i++) { + result *= result; + } + return result; + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 7/Exercise7_4.java b/Exercise Solutions/Chapter 7/Exercise7_4.java new file mode 100644 index 0000000..f93923a --- /dev/null +++ b/Exercise Solutions/Chapter 7/Exercise7_4.java @@ -0,0 +1,13 @@ +public class Exercise7_4 { + public static void main(String[] args) { + System.out.println(factorial(4)); + } + public static int factorial(int num) { + int result = 1; + for(int i = num; i > 0; i--) { + result *= i; + } + return result; + } +} + \ No newline at end of file diff --git a/Exercise Solutions/Chapter 7/Exercise7_5.java b/Exercise Solutions/Chapter 7/Exercise7_5.java new file mode 100644 index 0000000..4f75eed --- /dev/null +++ b/Exercise Solutions/Chapter 7/Exercise7_5.java @@ -0,0 +1,29 @@ +//Didn't finish this exercise. Found the phrasing of questions too confusing to understand + +public class Exercise7_5 { + public static void main(String[] args) { + //Insert code + check(1); + } + public static double myexp(double num) { + double numerator = 1; + double result = 1; + double denominator = 1; + for (double i = 1; i < num; i++) { + numerator += numerator * num; + denominator /= i; + result += (numerator/denominator); + } + return result; + } + public static void check(double x) { + System.out.println(x + "\t" + myexp(x) + "\t" + Math.exp(x)); + } + public static int factorial(int num) { + int result = 1; + for(int i = num; i > 0; i--) { + result *= i; + } + return result; + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 7/Exercise7_6.java b/Exercise Solutions/Chapter 7/Exercise7_6.java new file mode 100644 index 0000000..1ae192b --- /dev/null +++ b/Exercise Solutions/Chapter 7/Exercise7_6.java @@ -0,0 +1,17 @@ +public class Exercise7_6 { + public static void main(String[] args) { + System.out.println(gauss(1.0, 4.0)); + } + public static double gauss(double x, double n) { + double result = 1; + double numerator = 1; + double denominator = 1; + + for(double i = 1; i < n; i++) { + numerator = (numerator - 1)*x * x; + denominator = denominator * i; + result += numerator/denominator; + } + return result; + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 8/Exercise 8_2.txt b/Exercise Solutions/Chapter 8/Exercise 8_2.txt new file mode 100644 index 0000000..4e94cfd --- /dev/null +++ b/Exercise Solutions/Chapter 8/Exercise 8_2.txt @@ -0,0 +1,7 @@ +Exercise 8-2 + + +1) The method bananna is traversing through an array, and accumulating product of all of the elements. Kiwi is that accumulator and the variable i is the index of each element. +2) The method grapefruit is traversing through an array, and is search for any index which matches the parameter int grape, if it finds one that matches, it returns the index, if not it returns -1. +The variable a is the array that is being searched, and int grape is the integer it is searching for. +3) The method pineapple is traversing through an array a, with an int parameter apple. Pear is the counter. If the value in the current index matches the value apple, pear is incremented. At the end it returns how many matching values it found. \ No newline at end of file diff --git a/Exercise Solutions/Chapter 8/Exercise8_1.java b/Exercise Solutions/Chapter 8/Exercise8_1.java new file mode 100644 index 0000000..4a6ae5c --- /dev/null +++ b/Exercise Solutions/Chapter 8/Exercise8_1.java @@ -0,0 +1,57 @@ +import java.util.Arrays; +import java.util.Random; + +public class Exercise8_1 { + public static void main(String[] args) { + + //Printing a string representation of an array + //System.out.print(Array.toString(a)); +// System.out.println(Arrays.toString(powArray())); + double[] a = {1,2,3,4}; + System.out.println(Arrays.toString(powArray(a, 3))); + + //creating a random array and storing it + int[] scoreArray = randomArray(100); + System.out.println("Printing out the number of scores for each possible number:"); + System.out.println(Arrays.toString(histogram(scoreArray))); + + } + +// for(int i = 0; i < array.legth; i++) { +// a[i] = Math.pow(a[i], 2.0); + + public static double[] powArray(double[] x, int y){ + + double[] b = new double[x.length]; + + for(int i = 0; i < x.length; i++) { + b[i] = Math.pow(x[i], y); + } + return b; + } + + //Creating a random array of 100 scores + + public static int[] randomArray(int size) { + Random random = new Random(); + int[] a = new int[size]; + for(int i = 0; i < a.length; i++) { + a[i] = random.nextInt(100); + } + return a; + } + +// //Enhanced For Loop Exercise +// int[] counts = new int[100]; +// for(int score: scores); +// counts[score]++; +// + public static int[] histogram(int[]array) { + // create a counter for each of the 100 possible scores + int[] counts = new int[100]; + for(int score : array) { + counts[score]++; + } + return counts; + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 8/Exercise8_4.java b/Exercise Solutions/Chapter 8/Exercise8_4.java new file mode 100644 index 0000000..32ffbb6 --- /dev/null +++ b/Exercise Solutions/Chapter 8/Exercise8_4.java @@ -0,0 +1,38 @@ +import java.util.Random; +import java.util.Arrays; + +public class Exercise8_4 { + public static void main(String[] args) { + int[] arr = randomArray(10); + System.out.print("The array we are using is: "); + System.out.println(Arrays.toString(arr)); + System.out.print("The index of the highest element is: "); + System.out.println(indexOfMax(arr)); + } + + //Create Random Array + public static int[] randomArray(int size){ + Random random = new Random(); + int array[] = new int[size]; + for(int i = 0; i < array.length; i++) { + array[i] = random.nextInt(100); + } + return array; + } + + + //Search Array for highest element at index + public static int indexOfMax(int[] arr) { + int temp = arr[0]; + int tempIndex = 0; + //Find the highest element in the array + //Assign the index to tempIndex + for(int i = 0; i < arr.length; i++) { + if(arr[i] > temp) { + temp = arr[i]; + tempIndex = i; + } + } + return tempIndex; + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 8/Exercise8_5.java b/Exercise Solutions/Chapter 8/Exercise8_5.java new file mode 100644 index 0000000..3f00662 --- /dev/null +++ b/Exercise Solutions/Chapter 8/Exercise8_5.java @@ -0,0 +1,19 @@ +//Have not finished this exercise +import java.util.Arrays; + +public class Exercise8_5 { + public static void main(String[] args) { + //int[arr] = {1, 2, 3, 7, 8}; + System.out.println(sieve(5)); + } + public static boolean[] sieve(int n) { + int[] arr; + //Building the array up to n length; + for(int i =0; i < n; i++) { + arr[i] = i; + } + //Searching for prime numbers + for(int j = 0; j < arr.lenght; j++) { + if(arr[j] % arr[j] == 1 && + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 8/Exercise8_6.java b/Exercise Solutions/Chapter 8/Exercise8_6.java new file mode 100644 index 0000000..7aab036 --- /dev/null +++ b/Exercise Solutions/Chapter 8/Exercise8_6.java @@ -0,0 +1,16 @@ +public class Exercise8_6 { + public static void main(String[] args) { + int[] arr = {3, 6, 9}; + System.out.println(areaFactors(4, arr)); + } + + public static boolean areaFactors(int n, int[] array) { + boolean flag = true; + for(int i = 0; i < array.length; i++){ + if(array[i] % n != 0) { + flag = false; + } + } + return flag; + } +} \ No newline at end of file diff --git a/Exercise Solutions/Chapter 8/Exercise8_7.java b/Exercise Solutions/Chapter 8/Exercise8_7.java new file mode 100644 index 0000000..2f85994 --- /dev/null +++ b/Exercise Solutions/Chapter 8/Exercise8_7.java @@ -0,0 +1,40 @@ +public class Exercise8_7 { + //Find out if the numbers in the array are prime + public static boolean isPrime(int[] arr) { + //looping through the numbers in the array + boolean isPrimeNumber = true; + for(int i =0; i < arr.length; i++) { + //Testing to see if the numbers are prime + for(int j = 2; j < i; j++) { + if(arr[i] % j == 0) { + isPrimeNumber = false; + break; + } + } + } + return isPrimeNumber; + } + // Find the product of the numbers in an array + public static int product(int[] arr) { + int product = 1; + for(int i = 0; i < arr.length; i++) { + product *= arr[i]; + } + //System.out.println(product); + return product; + } + //Check to see if the product of the array matches n and the array is composed of primes numbers + public static boolean arePrimeFactors(int n, int[] arr) { + boolean flag = false; + if(product(arr) == n && isPrime(arr) == true) { + flag = true; + } + return flag; + } + public static void main(String[] args) { + int[] arr = {2, 5, 7, 11}; + System.out.println(arePrimeFactors(770, arr)); + //product(arr); + //System.out.println(isPrime(arr)); + } +} \ No newline at end of file diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index 64984df..f017722 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -1,4 +1,5 @@ import java.util.Random; +import java.util.Scanner; /** * Starter code for the "Guess My Number" exercise. @@ -6,10 +7,20 @@ public class GuessStarter { public static void main(String[] args) { + int userNumber; + int difference; + // pick a random number Random random = new Random(); int number = random.nextInt(100) + 1; - System.out.println(number); + //Ask user for number + System.out.println("Please Enter a number between 0 and 100 "); + Scanner in = new Scanner(System.in); + userNumber = in.nextInt(); + difference = number - userNumber; + System.out.println("The difference between the computer's number and your number is " + difference); + System.out.println("Computer's number: " + number + ", Your number: " + userNumber); + //System.out.println(number); } } diff --git a/ch03/HourConversion.java b/ch03/HourConversion.java new file mode 100644 index 0000000..65cdd90 --- /dev/null +++ b/ch03/HourConversion.java @@ -0,0 +1,24 @@ +import java.util.Scanner; + +//Hour Conversion + +public class HourConversion { + + public static void main(String[] args) { + double celsiusTemp; + int feet, inches, remainder; + + final int IN_PER_FOOT = 12; + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + System.out.print("Please input temperature in Celsius: "); + celsiusTemp = in.nextDouble(); + + //Convert and output the result + double fahrenheit = (celsiusTemp *(9.0/5.0)) + 32; + System.out.printf("%.2f Celsius = %.2f Fahrenheit \n", + celsiusTemp, fahrenheit); + } + +} \ No newline at end of file diff --git a/ch03/Test.java b/ch03/Test.java new file mode 100644 index 0000000..d63505b --- /dev/null +++ b/ch03/Test.java @@ -0,0 +1,10 @@ +public class Test { + + public static void main(String[] args) { + int cm = 1; + double feet = 2.0; + System.out.printf("%.2f cm = %d in\n", + feet); + } + +} \ No newline at end of file diff --git a/ch03/test.exp b/ch03/test.exp new file mode 100644 index 0000000..8ea70d1 --- /dev/null +++ b/ch03/test.exp @@ -0,0 +1 @@ +193.04 cm = 6 ft, 4 in \ No newline at end of file diff --git a/ch03/test.in b/ch03/test.in new file mode 100644 index 0000000..0593bc3 --- /dev/null +++ b/ch03/test.in @@ -0,0 +1 @@ +193.04 \ No newline at end of file diff --git a/ch03/test.out b/ch03/test.out new file mode 100644 index 0000000..29891dc --- /dev/null +++ b/ch03/test.out @@ -0,0 +1 @@ +Exactly how many cm? 193.04 cm = 6 ft, 4 in diff --git a/ch04/PrintTime.java b/ch04/PrintTime.java index 019d579..3a36961 100644 --- a/ch04/PrintTime.java +++ b/ch04/PrintTime.java @@ -13,3 +13,4 @@ public static void main(String[] args) { } } + diff --git a/ch05/Countup.java b/ch05/Countup.java new file mode 100644 index 0000000..626f3a4 --- /dev/null +++ b/ch05/Countup.java @@ -0,0 +1,14 @@ +public class Countup { + public static void main(String[] args) { + System.out.println("Countup beginning"); + countup(3); + } + public static void countup(int n) { + if (n == 0) { + System.out.println("Blastoff!"); + } else { + countup(n - 1); + System.out.println(n); + } + } +} \ No newline at end of file diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 0000000..81ab729 --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,23 @@ +0 info it worked if it ends with ok +1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe', +1 verbose cli 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js', +1 verbose cli 'run-script', +1 verbose cli 'debug' ] +2 info using npm@3.10.10 +3 info using node@v6.11.2 +4 verbose stack Error: ENOENT: no such file or directory, open 'c:\Users\Chris\Documents\Think Java\ThinkJavaCode\package.json' +4 verbose stack at Error (native) +5 verbose cwd c:\Users\Chris\Documents\Think Java\ThinkJavaCode +6 error Windows_NT 6.3.9600 +7 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run-script" "debug" +8 error node v6.11.2 +9 error npm v3.10.10 +10 error path c:\Users\Chris\Documents\Think Java\ThinkJavaCode\package.json +11 error code ENOENT +12 error errno -4058 +13 error syscall open +14 error enoent ENOENT: no such file or directory, open 'c:\Users\Chris\Documents\Think Java\ThinkJavaCode\package.json' +15 error enoent ENOENT: no such file or directory, open 'c:\Users\Chris\Documents\Think Java\ThinkJavaCode\package.json' +15 error enoent This is most likely not a problem with npm itself +15 error enoent and is related to npm not being able to find a file. +16 verbose exit [ -4058, true ]