From daba51102fc195ee74b8ed47429a26831cdfa231 Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Fri, 1 Dec 2017 15:25:37 -0800 Subject: [PATCH 01/13] Exercise Solution folder Created Exercise solution folder for all chapter exercises. Also created Chapter 2 exercise folder --- Exercise Solutions/Chapter 2/Date.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Exercise Solutions/Chapter 2/Date.java 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); + + } + +} From a02193e5ff0e004d5dbe496008f1721fa243c523 Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Fri, 1 Dec 2017 15:50:05 -0800 Subject: [PATCH 02/13] Solutions to chapter 2 exercise --- Exercise Solutions/Chapter 2/Time.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Exercise Solutions/Chapter 2/Time.java diff --git a/Exercise Solutions/Chapter 2/Time.java b/Exercise Solutions/Chapter 2/Time.java new file mode 100644 index 0000000..0b8d030 --- /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); + + } +} From 569da971b9299a13f63056daa3328100a21ee52c Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Fri, 1 Dec 2017 15:57:06 -0800 Subject: [PATCH 03/13] Completed solutions for chapter 2 --- Exercise Solutions/Chapter 2/Time.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exercise Solutions/Chapter 2/Time.java b/Exercise Solutions/Chapter 2/Time.java index 0b8d030..e9f9bf2 100644 --- a/Exercise Solutions/Chapter 2/Time.java +++ b/Exercise Solutions/Chapter 2/Time.java @@ -16,7 +16,7 @@ public static void main(String[] args) { 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); + System.out.println((secondsSinceMidnight - oldTime) / 60 + " minutes"); } } From 04dc09c0c13fa3330317ca897d8fab83a7a58c44 Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Fri, 1 Dec 2017 17:47:50 -0800 Subject: [PATCH 04/13] solutions for exercise 3.1 and 3.2 --- ch03/CelsiusConvert.java | 24 ++++++++++++++++++++++++ ch03/Test.java | 10 ++++++++++ ch03/test.exp | 1 + ch03/test.in | 1 + ch03/test.out | 1 + 5 files changed, 37 insertions(+) create mode 100644 ch03/CelsiusConvert.java create mode 100644 ch03/Test.java create mode 100644 ch03/test.exp create mode 100644 ch03/test.in create mode 100644 ch03/test.out diff --git a/ch03/CelsiusConvert.java b/ch03/CelsiusConvert.java new file mode 100644 index 0000000..19fce63 --- /dev/null +++ b/ch03/CelsiusConvert.java @@ -0,0 +1,24 @@ +import java.util.Scanner; + +//Converting Celsius to Fahrenheit + +public class CelsiusConvert { + + 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 From c52630bb8864b5aedeb3eb4f6782e05f14d68da0 Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Fri, 1 Dec 2017 18:18:15 -0800 Subject: [PATCH 05/13] Added solution to exercises 3.3 and put solution to exercise 3.2 in the correct directory --- ch03/{CelsiusConvert.java => HourConversion.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename ch03/{CelsiusConvert.java => HourConversion.java} (90%) diff --git a/ch03/CelsiusConvert.java b/ch03/HourConversion.java similarity index 90% rename from ch03/CelsiusConvert.java rename to ch03/HourConversion.java index 19fce63..65cdd90 100644 --- a/ch03/CelsiusConvert.java +++ b/ch03/HourConversion.java @@ -1,8 +1,8 @@ import java.util.Scanner; -//Converting Celsius to Fahrenheit +//Hour Conversion -public class CelsiusConvert { +public class HourConversion { public static void main(String[] args) { double celsiusTemp; From 02f59edc5fed00e226a8097ec6ee1b4772470aee Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Fri, 1 Dec 2017 18:20:23 -0800 Subject: [PATCH 06/13] fixing formatting fixed solutions --- Exercise Solutions/Chapter 3/3-1 Solution.txt | 1 + .../Chapter 3/CelsiusConvert.java | 21 ++++++++++++++++ .../Chapter 3/SecondsToHours.java | 24 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 Exercise Solutions/Chapter 3/3-1 Solution.txt create mode 100644 Exercise Solutions/Chapter 3/CelsiusConvert.java create mode 100644 Exercise Solutions/Chapter 3/SecondsToHours.java 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/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 From 1a0454f60a23207125a0d038d4cc339bf1b97d5e Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Fri, 1 Dec 2017 18:46:59 -0800 Subject: [PATCH 07/13] Solution for all problems now added --- ch03/GuessStarter.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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); } } From 464f1035283f50589b8c9e2996f7b56bfa8b58b2 Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Fri, 1 Dec 2017 18:53:21 -0800 Subject: [PATCH 08/13] Final Version --- .../Chapter 3/GuessStarter.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Exercise Solutions/Chapter 3/GuessStarter.java 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); + } + +} From ee0a5293ebf1b4073ce67a86d4a73d857a4b69a4 Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Fri, 1 Dec 2017 23:16:44 -0800 Subject: [PATCH 09/13] Exercise 4 Solutions --- Exercise Solutions/Chapter 4/4-1 solution.txt | 5 ++++ Exercise Solutions/Chapter 4/Encapsulate.java | 30 +++++++++++++++++++ Exercise Solutions/Chapter 4/Parameters.java | 17 +++++++++++ ch04/PrintTime.java | 2 +- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 Exercise Solutions/Chapter 4/4-1 solution.txt create mode 100644 Exercise Solutions/Chapter 4/Encapsulate.java create mode 100644 Exercise Solutions/Chapter 4/Parameters.java 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/ch04/PrintTime.java b/ch04/PrintTime.java index 019d579..5581da1 100644 --- a/ch04/PrintTime.java +++ b/ch04/PrintTime.java @@ -12,4 +12,4 @@ public static void main(String[] args) { printTime(hour, minute); } -} +}git \ No newline at end of file From cf6b3fee600e592becfdcc48ddd4adf19e589fce Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Tue, 5 Dec 2017 16:31:58 -0800 Subject: [PATCH 10/13] final solutions to chapter 5 exercises --- .vscode/launch.json | 25 ++++++++++++++++ Exercise Solutions/Chapter 5/5-6 solution.txt | 5 ++++ .../Chapter 5/BottlesOfBeer.java | 19 ++++++++++++ Exercise Solutions/Chapter 5/NumberGuess.java | 30 +++++++++++++++++++ .../Chapter 5/OneStatement.java | 10 +++++++ .../Chapter 5/checkFermatExercise.java | 17 +++++++++++ ch04/PrintTime.java | 3 +- ch05/Countup.java | 14 +++++++++ npm-debug.log | 23 ++++++++++++++ 9 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json create mode 100644 Exercise Solutions/Chapter 5/5-6 solution.txt create mode 100644 Exercise Solutions/Chapter 5/BottlesOfBeer.java create mode 100644 Exercise Solutions/Chapter 5/NumberGuess.java create mode 100644 Exercise Solutions/Chapter 5/OneStatement.java create mode 100644 Exercise Solutions/Chapter 5/checkFermatExercise.java create mode 100644 ch05/Countup.java create mode 100644 npm-debug.log 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 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/ch04/PrintTime.java b/ch04/PrintTime.java index 5581da1..3a36961 100644 --- a/ch04/PrintTime.java +++ b/ch04/PrintTime.java @@ -12,4 +12,5 @@ public static void main(String[] args) { printTime(hour, minute); } -}git \ No newline at end of file +} + 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 ] From 7561161163ca132cb2d2407e17ca0049fe71af8b Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Sat, 9 Dec 2017 16:57:33 -0800 Subject: [PATCH 11/13] Exercise solutions for chapter 6 plus tests --- Exercise Solutions/Chapter 6/IsDivisible.java | 11 ++++++ .../Chapter 6/IsDivisibleTest.java | 20 ++++++++++ Exercise Solutions/Chapter 6/IsTriangle.java | 20 ++++++++++ .../Chapter 6/IsTriangleTest.java | 20 ++++++++++ ch06/Exercise6.java | 16 ++++++++ ch06/Exercise6_5.java | 37 +++++++++++++++++++ ch06/Exercise6_8.java | 20 ++++++++++ ch06/Exercise6_9.java | 15 ++++++++ ch06/Fibonacci.java | 11 ++++++ ch06/FibonacciTest.java | 22 +++++++++++ ch06/Multadd.java | 30 +++++++++++++++ ch06/OddSum.java | 15 ++++++++ 12 files changed, 237 insertions(+) create mode 100644 Exercise Solutions/Chapter 6/IsDivisible.java create mode 100644 Exercise Solutions/Chapter 6/IsDivisibleTest.java create mode 100644 Exercise Solutions/Chapter 6/IsTriangle.java create mode 100644 Exercise Solutions/Chapter 6/IsTriangleTest.java create mode 100644 ch06/Exercise6.java create mode 100644 ch06/Exercise6_5.java create mode 100644 ch06/Exercise6_8.java create mode 100644 ch06/Exercise6_9.java create mode 100644 ch06/Fibonacci.java create mode 100644 ch06/FibonacciTest.java create mode 100644 ch06/Multadd.java create mode 100644 ch06/OddSum.java 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/ch06/Exercise6.java b/ch06/Exercise6.java new file mode 100644 index 0000000..a096190 --- /dev/null +++ b/ch06/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/ch06/Exercise6_5.java b/ch06/Exercise6_5.java new file mode 100644 index 0000000..176e2b9 --- /dev/null +++ b/ch06/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/ch06/Exercise6_8.java b/ch06/Exercise6_8.java new file mode 100644 index 0000000..2c937c2 --- /dev/null +++ b/ch06/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/ch06/Exercise6_9.java b/ch06/Exercise6_9.java new file mode 100644 index 0000000..b89e346 --- /dev/null +++ b/ch06/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); + } +} + \ No newline at end of file diff --git a/ch06/Fibonacci.java b/ch06/Fibonacci.java new file mode 100644 index 0000000..74509bc --- /dev/null +++ b/ch06/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/ch06/FibonacciTest.java b/ch06/FibonacciTest.java new file mode 100644 index 0000000..f5d8b0e --- /dev/null +++ b/ch06/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/ch06/Multadd.java b/ch06/Multadd.java new file mode 100644 index 0000000..a561a9e --- /dev/null +++ b/ch06/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/ch06/OddSum.java b/ch06/OddSum.java new file mode 100644 index 0000000..911593d --- /dev/null +++ b/ch06/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 From e9bcbaecbeea07c56f78238bea86dfb38229c157 Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Mon, 11 Dec 2017 18:11:38 -0800 Subject: [PATCH 12/13] finished through chapter 7 exercises --- .../Chapter 6}/Exercise6.java | 0 .../Chapter 6}/Exercise6_5.java | 0 .../Chapter 6}/Exercise6_8.java | 0 .../Chapter 6}/Exercise6_9.java | 2 +- .../Chapter 6}/Fibonacci.java | 0 .../Chapter 6}/FibonacciTest.java | 0 .../Chapter 6}/Multadd.java | 0 .../Chapter 6}/OddSum.java | 0 Exercise Solutions/Chapter 7/Exercise7_2.java | 13 +++++++++ Exercise Solutions/Chapter 7/Exercise7_3.java | 13 +++++++++ Exercise Solutions/Chapter 7/Exercise7_4.java | 13 +++++++++ Exercise Solutions/Chapter 7/Exercise7_5.java | 29 +++++++++++++++++++ Exercise Solutions/Chapter 7/Exercise7_6.java | 17 +++++++++++ 13 files changed, 86 insertions(+), 1 deletion(-) rename {ch06 => Exercise Solutions/Chapter 6}/Exercise6.java (100%) rename {ch06 => Exercise Solutions/Chapter 6}/Exercise6_5.java (100%) rename {ch06 => Exercise Solutions/Chapter 6}/Exercise6_8.java (100%) rename {ch06 => Exercise Solutions/Chapter 6}/Exercise6_9.java (93%) rename {ch06 => Exercise Solutions/Chapter 6}/Fibonacci.java (100%) rename {ch06 => Exercise Solutions/Chapter 6}/FibonacciTest.java (100%) rename {ch06 => Exercise Solutions/Chapter 6}/Multadd.java (100%) rename {ch06 => Exercise Solutions/Chapter 6}/OddSum.java (100%) create mode 100644 Exercise Solutions/Chapter 7/Exercise7_2.java create mode 100644 Exercise Solutions/Chapter 7/Exercise7_3.java create mode 100644 Exercise Solutions/Chapter 7/Exercise7_4.java create mode 100644 Exercise Solutions/Chapter 7/Exercise7_5.java create mode 100644 Exercise Solutions/Chapter 7/Exercise7_6.java diff --git a/ch06/Exercise6.java b/Exercise Solutions/Chapter 6/Exercise6.java similarity index 100% rename from ch06/Exercise6.java rename to Exercise Solutions/Chapter 6/Exercise6.java diff --git a/ch06/Exercise6_5.java b/Exercise Solutions/Chapter 6/Exercise6_5.java similarity index 100% rename from ch06/Exercise6_5.java rename to Exercise Solutions/Chapter 6/Exercise6_5.java diff --git a/ch06/Exercise6_8.java b/Exercise Solutions/Chapter 6/Exercise6_8.java similarity index 100% rename from ch06/Exercise6_8.java rename to Exercise Solutions/Chapter 6/Exercise6_8.java diff --git a/ch06/Exercise6_9.java b/Exercise Solutions/Chapter 6/Exercise6_9.java similarity index 93% rename from ch06/Exercise6_9.java rename to Exercise Solutions/Chapter 6/Exercise6_9.java index b89e346..d93aa6f 100644 --- a/ch06/Exercise6_9.java +++ b/Exercise Solutions/Chapter 6/Exercise6_9.java @@ -12,4 +12,4 @@ public static double power(double x, int n) { return Math.pow(temp, 2); } } - \ No newline at end of file + diff --git a/ch06/Fibonacci.java b/Exercise Solutions/Chapter 6/Fibonacci.java similarity index 100% rename from ch06/Fibonacci.java rename to Exercise Solutions/Chapter 6/Fibonacci.java diff --git a/ch06/FibonacciTest.java b/Exercise Solutions/Chapter 6/FibonacciTest.java similarity index 100% rename from ch06/FibonacciTest.java rename to Exercise Solutions/Chapter 6/FibonacciTest.java diff --git a/ch06/Multadd.java b/Exercise Solutions/Chapter 6/Multadd.java similarity index 100% rename from ch06/Multadd.java rename to Exercise Solutions/Chapter 6/Multadd.java diff --git a/ch06/OddSum.java b/Exercise Solutions/Chapter 6/OddSum.java similarity index 100% rename from ch06/OddSum.java rename to Exercise Solutions/Chapter 6/OddSum.java 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 From 7d282c888b6d548d2c4236cde4815c7d81d32050 Mon Sep 17 00:00:00 2001 From: CKHarrison Date: Tue, 12 Dec 2017 16:53:47 -0800 Subject: [PATCH 13/13] Chapter 8 exercises --- Exercise Solutions/Chapter 8/Exercise 8_2.txt | 7 +++ Exercise Solutions/Chapter 8/Exercise8_1.java | 57 +++++++++++++++++++ Exercise Solutions/Chapter 8/Exercise8_4.java | 38 +++++++++++++ Exercise Solutions/Chapter 8/Exercise8_5.java | 19 +++++++ Exercise Solutions/Chapter 8/Exercise8_6.java | 16 ++++++ Exercise Solutions/Chapter 8/Exercise8_7.java | 40 +++++++++++++ 6 files changed, 177 insertions(+) create mode 100644 Exercise Solutions/Chapter 8/Exercise 8_2.txt create mode 100644 Exercise Solutions/Chapter 8/Exercise8_1.java create mode 100644 Exercise Solutions/Chapter 8/Exercise8_4.java create mode 100644 Exercise Solutions/Chapter 8/Exercise8_5.java create mode 100644 Exercise Solutions/Chapter 8/Exercise8_6.java create mode 100644 Exercise Solutions/Chapter 8/Exercise8_7.java 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