diff --git a/.gitignore b/.gitignore index 32858aa..ab674e6 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,14 @@ *.jar *.war *.ear +*/target/* +*/target/** +**/target/* +**/target/** +*/.idea/* +*/.idea/** +.idea +*.iml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..18d93cc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..77b3fe6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..9f0d4b2 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,1165 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1467289584352 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/ch07/Expotential.java + 7 + + + + file://$PROJECT_DIR$/ch08/MyHistogram.java + 39 + + + + file://$PROJECT_DIR$/ch08/RecrusiveMaxInRange.java + 34 + + + + file://$PROJECT_DIR$/ch10/BigIntegerPower.java + 6 + + + + file://$PROJECT_DIR$/ch08/FindPrimeNumbersTest.java + 11 + + + + file://$PROJECT_DIR$/ch08/FindPrimeNumbers.java + 66 + + + + file://$PROJECT_DIR$/ch08/FindPrimeNumbers.java + 67 + + + + file://$PROJECT_DIR$/ch09/LettersHistogram.java + 14 + + + + file://$PROJECT_DIR$/ch11/ForDebuggingCh11.java + 25 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.7 + + + + + + + + ThinkJavaCode + + + + + + + + 1.8 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ThinkJavaCode.iml b/ThinkJavaCode.iml new file mode 100644 index 0000000..8ad2190 --- /dev/null +++ b/ThinkJavaCode.iml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ap01/SeriesTest.java b/ap01/SeriesTest.java index 6309616..53a1248 100644 --- a/ap01/SeriesTest.java +++ b/ap01/SeriesTest.java @@ -1,8 +1,9 @@ -import junit.framework.TestCase; +//import junit.framework.TestCase; /** * Example JUnit test from Appendix A. */ +/* public class SeriesTest extends TestCase { public void testFibonacci() { @@ -12,3 +13,4 @@ public void testFibonacci() { } } +*/ \ No newline at end of file diff --git a/ch04/Exercise.java b/ch04/ExerciseCh04.java similarity index 93% rename from ch04/Exercise.java rename to ch04/ExerciseCh04.java index ba8c2a9..4e1c734 100644 --- a/ch04/Exercise.java +++ b/ch04/ExerciseCh04.java @@ -1,4 +1,4 @@ -public class Exercise { +public class ExerciseCh04 { public static void zoop() { baffle(); diff --git a/ch05/Exercise.java b/ch05/ExerciseCh05.java similarity index 95% rename from ch05/Exercise.java rename to ch05/ExerciseCh05.java index 46774e0..6c7c7db 100644 --- a/ch05/Exercise.java +++ b/ch05/ExerciseCh05.java @@ -1,4 +1,4 @@ -public class Exercise { +public class ExerciseCh05 { public static void zoop(String fred, int bob) { System.out.println(fred); diff --git a/ch06/AckermanFunction.java b/ch06/AckermanFunction.java new file mode 100644 index 0000000..d814892 --- /dev/null +++ b/ch06/AckermanFunction.java @@ -0,0 +1,37 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-07. + */ +public class AckermanFunction { + + public static int selectNumber(String parametername){ + Scanner in = new Scanner(System.in); + System.out.println("Write number " + parametername + ": "); + return in.nextInt(); + } + + 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{ + System.out.println("Wrong input numbers, cannot calculate answer!"); + return 0; + } + } + + public static void main (String[] args){ + int usernumber1 = selectNumber("n"); + int usernumber2 = selectNumber("m"); + /* + System.out.printf("\nun1: %d", usernumber1); + System.out.printf("\nun2: %d", usernumber2); + */ + int answer = ack(usernumber1,usernumber2); + System.out.printf("\nYour answer is: %d", answer); + } +} diff --git a/ch06/Exercise067.java b/ch06/Exercise067.java new file mode 100644 index 0000000..3a8559c --- /dev/null +++ b/ch06/Exercise067.java @@ -0,0 +1,43 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-05. + */ +public class Exercise067 { + + public static boolean isPositiveAndOdd(int checkedNumber){ + if(checkedNumber < 0){ + System.out.print("Input number is less than 0"); + return false; + } else if(checkedNumber%2 == 0){ + System.out.print("Input number is even"); + return false; + } else { + return true; + } + } + + public static int oddSum(int maxComponent){ + if(maxComponent == 1){ + return 1; + } else{ + return maxComponent + oddSum(maxComponent-2); + } + } + + public static void main(String[] args){ + Scanner in = new Scanner(System.in); + int userNumber = 0; + int sum = 1; + int component =1; + + while (!isPositiveAndOdd(userNumber)){ + System.out.print("\nWrite maximal number: "); + userNumber = in.nextInt(); + } + + System.out.printf("\nYou selected: %d", userNumber); + int answer = oddSum(userNumber); + System.out.printf("\nYour answer is: %d", answer); + } +} diff --git a/ch06/Exercise067Test.java b/ch06/Exercise067Test.java new file mode 100644 index 0000000..a94b1cf --- /dev/null +++ b/ch06/Exercise067Test.java @@ -0,0 +1,21 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by media on 2016-07-06. + */ +public class Exercise067Test { + @Test + public void testIsPositiveAndOdd(){ + assertEquals(true, Exercise067.isPositiveAndOdd(3)); + assertEquals(false, Exercise067.isPositiveAndOdd(2)); + assertEquals(false, Exercise067.isPositiveAndOdd(-1)); + } + + @Test + public void oddSum() throws Exception { + + } + +} \ No newline at end of file diff --git a/ch06/Exercise.java b/ch06/ExerciseCh06.java similarity index 96% rename from ch06/Exercise.java rename to ch06/ExerciseCh06.java index b826b44..e19a6c1 100644 --- a/ch06/Exercise.java +++ b/ch06/ExerciseCh06.java @@ -1,4 +1,4 @@ -public class Exercise { +public class ExerciseCh06 { public static void main(String[] args) { boolean flag1 = isHoopy(202); diff --git a/ch06/Multiadd.java b/ch06/Multiadd.java new file mode 100644 index 0000000..624b42c --- /dev/null +++ b/ch06/Multiadd.java @@ -0,0 +1,47 @@ +/** + * Created by media on 2016-06-30. + */ + +import java.util.Scanner; + +public class Multiadd { + + public static double multiadd(double a, double b, double c){ + return a*b+c; + } + + public static void displayMessage(String functionName, boolean ok){ + String information; + if(ok){ + information = "properly."; + } else{ + information = "badly"; + } + System.out.println("Function " + functionName + " works " + information); + } + + public static void main(String[] args){ + boolean isworkingcorrect = false; + String messagetouser; + Scanner in = new Scanner(System.in); + + double test = multiadd(1.0,2.0,3.0); + double result1 = multiadd(Math.cos(180.0/4.0),0.5,Math.sin(180.0/4.0)); + double result2 = multiadd(Math.log10(10.0),1,Math.log10(20.0)); + + System.out.printf("Result 1 is: %.3f\n", result1); + System.out.printf("Result 2 is: %.3f", result2); + + System.out.print("\nWrite number: "); + double result3Input = in.nextDouble(); + double result3 = multiadd(result3Input,Math.exp(-result3Input),Math.sqrt(1-Math.exp(-result3Input))); + System.out.printf("Result 3 is: %.3f", result3); + /* + if(result == 5){ + isworkingcorrect = true; + } + + displayMessage("multiadd", isworkingcorrect); + */ + } +} diff --git a/ch06/MultiaddTest.java b/ch06/MultiaddTest.java new file mode 100644 index 0000000..4cab1c4 --- /dev/null +++ b/ch06/MultiaddTest.java @@ -0,0 +1,14 @@ +import com.sun.org.apache.xpath.internal.operations.Equals; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by media on 2016-06-30. + */ +public class MultiaddTest { + @Test + public void testmultiadd(){ + assertEquals(5.0,Multiadd.multiadd(1.0,2.0,3.0),0.1); + } +} \ No newline at end of file diff --git a/ch06/PowerOfX.java b/ch06/PowerOfX.java new file mode 100644 index 0000000..d3c0ebf --- /dev/null +++ b/ch06/PowerOfX.java @@ -0,0 +1,29 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-07. + */ +public class PowerOfX { + + public static int readInput(String parametername){ + Scanner in = new Scanner(System.in); + System.out.print("Write value of " + parametername + ": "); + return in.nextInt(); + } + + public static int power(int X, int n){ + if(n==0){ + return 1; + } else { + return X*power(X,n-1); + } + } + + public static void main(String[] args){ + int base = readInput("X"); + int exponent = readInput("n"); + + int answer = power(base,exponent); + System.out.printf("\nYour answer is: %d", answer); + } +} diff --git a/ch06/Series.java b/ch06/SeriesCh06.java similarity index 99% rename from ch06/Series.java rename to ch06/SeriesCh06.java index 10d2b41..4c75d7e 100644 --- a/ch06/Series.java +++ b/ch06/SeriesCh06.java @@ -1,7 +1,7 @@ /** * Examples from Chapter 6. */ -public class Series { +public class SeriesCh06 { public static void countup(int n) { if (n == 0) { diff --git a/ch07/Exercise.java b/ch07/ExerciseCh07.java similarity index 92% rename from ch07/Exercise.java rename to ch07/ExerciseCh07.java index 6c7b3df..7160c01 100644 --- a/ch07/Exercise.java +++ b/ch07/ExerciseCh07.java @@ -1,4 +1,4 @@ -public class Exercise { +public class ExerciseCh07 { public static void main(String[] args) { loop(10); diff --git a/ch07/Expotential.java b/ch07/Expotential.java new file mode 100644 index 0000000..0ed2aba --- /dev/null +++ b/ch07/Expotential.java @@ -0,0 +1,80 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-08. + */ +public class Expotential { + + public static double readInput(String paramname){ + Scanner in = new Scanner(System.in); + boolean okay; + do{ + System.out.print("Write value of " + paramname + ": "); + if(in.hasNextDouble()){ + okay = true; + }else { + okay = false; + System.err.println(in.toString() + " is not a number!"); + } + }while (!okay); + return in.nextDouble(); + } + + public static float factorial(int number){ + + if(number == 0){ + return (float) 1.0; + } + else { + return number*factorial(number-1); + } + } + + public static float myexp(int x, int n){ + float result = 0; + while(n >= 0){ + result += Math.pow(x,n)/factorial(n); + n--; + } + return result; + } + + public static float calculateComponent(int x, int n){ + if(n == 0){ + return 1; + } else{ + //System.out.printf("\nDo mnozenia: %f", (4.0/n)); + float y = (float) x; + return ((y/n)*calculateComponent(x,n-1)); + } + } + + public static float myexpSimplified(int x, int n){ + float result = 0; + while(n>=0){ + result += calculateComponent(x,n); + n--; + } + return result; + } + + public static void check(double x){ + int n = 25; + float exp = (float) Math.exp(x); + int tmp_x = (int) x; + float myexp = myexpSimplified(tmp_x,n); + System.out.printf("X = %d \t Computer result = %f \t My result = %f",(int)x,exp,myexp); + } + + public static void main(String[] args){ + double expotential = readInput("x"); + //int numberofelements = readInput("n"); + /* + float answer = myexp(expotential,numberofelements); + float answersimplified = myexpSimplified(expotential,numberofelements); + System.out.printf("\nYour answer is: %f", answer); + System.out.printf("\nYour simplified answer is: %f", answersimplified); + */ + check(expotential); + } +} diff --git a/ch07/ExpotentialTest.java b/ch07/ExpotentialTest.java new file mode 100644 index 0000000..dd68c82 --- /dev/null +++ b/ch07/ExpotentialTest.java @@ -0,0 +1,25 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by media on 2016-07-08. + */ +public class ExpotentialTest { + @Test + public void readInput() throws Exception { + + } + + @Test + public void factorial(){ + assertEquals(1.0,Expotential.factorial(0),0.001); + assertEquals(120.0,Expotential.factorial(5),0.001); + } + + @Test + public void myexp() throws Exception { + + } + +} \ No newline at end of file diff --git a/ch07/GaussFunction.java b/ch07/GaussFunction.java new file mode 100644 index 0000000..e5bf879 --- /dev/null +++ b/ch07/GaussFunction.java @@ -0,0 +1,56 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-09. + */ +public class GaussFunction { + + public static int readInput(String name){ + Scanner in = new Scanner(System.in); + boolean okay; + do{ + System.out.print("Write value of " + name + ": "); + if(in.hasNextInt()){ + okay = true; + }else{ + okay = false; + System.err.print(in.toString() + " is not a number!"); + } + }while (!okay); + return in.nextInt(); + } + + public static float selectSign(int n){ + if((n % 2) == 0){ + return 1; + }else { + return -1; + } + } + + public static float calculateComponent(int x, int n){ + if(n == 0){ + return 1; + }else{ + float tmp_n = (float) n; + float tmp_x = (float) x; + return (x/n)*calculateComponent(x,n-1); + } + } + + public static float gauss(int x, int n){ + float result = 0; + while(n>0){ + result += selectSign(n)*calculateComponent(x,2*n); + n--; + } + return result; + } + + public static void main(String[] args){ + int base = readInput("x"); + int accuracy = readInput("n"); + float answer = gauss(base,accuracy); + System.out.printf("\nYour answer is: %f", answer); + } +} diff --git a/ch07/IterativeFactorial.java b/ch07/IterativeFactorial.java new file mode 100644 index 0000000..5825492 --- /dev/null +++ b/ch07/IterativeFactorial.java @@ -0,0 +1,38 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-08. + */ +public class IterativeFactorial { + + public static int readInput(){ + Scanner in = new Scanner(System.in); + boolean okay; + do{ + System.out.println("Write number: "); + if(in.hasNextInt()){ + okay = true; + } else { + okay = false; + System.err.println(in.toString() + " is not a number"); + } + } while (!okay); + return in.nextInt(); + } + + public static int factorial(int input){ + int result = 1; + while (input > 0){ + result *= input; + input--; + } + return result; + } + + public static void main(String[] args){ + int base = readInput(); + int answer = factorial(base); + System.out.printf("\nYour answer is: %d", answer); + + } +} diff --git a/ch07/IterativePower.java b/ch07/IterativePower.java new file mode 100644 index 0000000..77af5f6 --- /dev/null +++ b/ch07/IterativePower.java @@ -0,0 +1,38 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-08. + */ +public class IterativePower { + + public static int readInput(String numberName){ + Scanner in = new Scanner(System.in); + boolean okay; + do{ + System.out.println("Write " + numberName + ": "); + if(in.hasNextInt()){ + okay = true; + } else { + okay = false; + System.err.println(in.toString() + " is not a number!"); + } + } while(!okay); + return in.nextInt(); + } + + public static int iterativePowerOfX(int X, int n){ + int result = 1; + while(n>0){ + result *= X; + n--; + } + return result; + } + + public static void main(String[] args){ + int base = readInput("X"); + int exponent = readInput("n"); + int answer = iterativePowerOfX(base,exponent); + System.out.printf("\nYour answer is: %d", answer); + } +} diff --git a/ch07/SquareRootApproximation.java b/ch07/SquareRootApproximation.java new file mode 100644 index 0000000..0f75a2d --- /dev/null +++ b/ch07/SquareRootApproximation.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-07. + */ +public class SquareRootApproximation { + + public static double readInput(){ + Scanner in = new Scanner(System.in); + boolean okay; + do { + System.out.print("Write number: "); + if(in.hasNextDouble()){ + okay = true; + } else{ + okay = false; + String word = in.next(); + System.err.println(word + " is not a number!"); + } + }while (!okay); + return in.nextDouble(); + } + + public static double calculateNextGuess(double previous, double number){ + return (previous+number/previous)/2.0; + } + + public static double squareRoot(double number){ + double x0 = number/2.0; + double x1 = calculateNextGuess(x0,number); + while(Math.abs(x0-x1)>0.0001){ + x0 = x1; + x1 = calculateNextGuess(x0,number); + } + return x1; + } + + public static void main(String[] args){ + double usernumber = readInput(); + System.out.printf("\nYou write: %.2f", usernumber); + double answer = squareRoot(usernumber); + System.out.printf("\nYour answer: %f", answer); + } +} diff --git a/ch07/Validate.java b/ch07/Validate.java index e825705..d6f59e1 100644 --- a/ch07/Validate.java +++ b/ch07/Validate.java @@ -51,4 +51,9 @@ public static double addNumbers() { return sum; } + public static void main(String[] args){ + double x = scanDouble(); + //Sys + } + } diff --git a/ch08/Encapsulation.java b/ch08/Encapsulation.java new file mode 100644 index 0000000..b05c08f --- /dev/null +++ b/ch08/Encapsulation.java @@ -0,0 +1,56 @@ +import java.util.Arrays; +import java.util.Random; +import java.util.Scanner; + +/** + * Created by media on 2016-07-10. + */ +public class Encapsulation { + + public static int readInput(String paramname){ + Scanner in = new Scanner(System.in); + boolean okay; + do{ + System.out.printf("Write " + paramname + ": "); + if(in.hasNextInt()){ + okay = true; + } else { + okay = false; + } + }while(!okay); + return in.nextInt(); + } + public static double[] powArray(double[] a, double power){ + double[] results = new double[a.length]; + for (int i=0; i value){ + index = i; + value = array[i]; + } + } + return index; + } + + public static void main(String[] args){ + int size = 10; + int[] numbers = new int[size]; + generateRandom(numbers); + System.out.println("Array: " + Arrays.toString(numbers)); + System.out.printf("\nIndex of max value: %d", indexOfMax(numbers)); + } +} diff --git a/ch08/MyHistogram.java b/ch08/MyHistogram.java new file mode 100644 index 0000000..daf0cb3 --- /dev/null +++ b/ch08/MyHistogram.java @@ -0,0 +1,112 @@ +import java.util.Arrays; +import java.util.Random; +import java.util.Scanner; + +/** + * Created by media on 2016-07-10. + */ +public class MyHistogram { + + public static int readInput(String paramname){ + Scanner in = new Scanner(System.in); + boolean okay; + do{ + System.out.print("Write " + paramname); + if(in.hasNextInt()){ + okay = true; + }else { + okay = false; + System.err.println("Wrong input value!"); + } + }while (!okay); + return in.nextInt(); + } + + /* + * returns array of random integers with minimal value 1 + */ + public static int[] randomArray(int size){ + Random random = new Random(); + int[] initializedarray = new int[size]; + for(int i=0; imaxInRange(array,li,mi-1)){ + return array[mi]; + } else{ + return maxInRange(array,li,mi-1); + } + } + } + + /* + * This method returns max element of input array + */ + public static double max(int[] array){ + double result = (double) maxInRange(array,0,array.length-1); + return result; + } + + public static void main(String[] args){ + int arraySize = readInput("array size: "); + int[] userArray = new int[arraySize]; + initArray(userArray); + //int lowIndex = readInput("lower index: "); + //int highIndex = readInput("higher index: "); + double maxElement = max(userArray); + System.out.printf("Result is: %f", maxElement); + } +} diff --git a/ch09/Abecedarian.java b/ch09/Abecedarian.java new file mode 100644 index 0000000..d794982 --- /dev/null +++ b/ch09/Abecedarian.java @@ -0,0 +1,51 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-17. + */ +public class Abecedarian { + + /* + * Returns String write by user in console + */ + public static String readInput(String paramName){ + Scanner in = new Scanner(System.in); + System.out.print("Write " + paramName); + return in.next(); + } + + /* + * returns TRUE when second letter is string is bigger than first + */ + public static boolean isNextLetterBigger(String s){ + if(s.charAt(0) < s.charAt(1)){ + return true; + } else{ + return false; + } + } + + /* + * Returns TRUE when letters in word are alphabetical + */ + public static boolean isAbecedarian(String s){ + s = s.toUpperCase(); + if(s.length() == 2){ + return isNextLetterBigger(s); + } else{ + String tmp = s.substring(0,2); + if(isNextLetterBigger(tmp)){ + s = s.substring(1,s.length()); + return isAbecedarian(s); + } else { + return false; + } + } + } + + + public static void main(String[] args){ + String userWord = readInput("your word: "); + System.out.printf("Your answer: " + isAbecedarian(userWord) ); + } +} diff --git a/ch09/Anagram.java b/ch09/Anagram.java new file mode 100644 index 0000000..663d1de --- /dev/null +++ b/ch09/Anagram.java @@ -0,0 +1,118 @@ +import java.util.Scanner; + +/** + * Created by media on 2016-07-17. + */ +public class Anagram { + + public static String readInput(String paramName){ + Scanner in = new Scanner(System.in); + System.out.print("\nWrite " + paramName + ": "); + return in.next(); + } + + /* + * Returns TRUE when input char is in array + */ + public static boolean inCharArray(char letter, char[] array){ + boolean okay = false; + for(int i=0; i0; i--){ + BigInteger tmp = BigInteger.valueOf(i); + result = result.multiply(tmp); + } + return result; + } + + public static void main(String[] args){ + + for (int i=0; i<31; i++){ + System.out.print("\nResult for " + i + " = " + bigIntegerFactorial(i)); + } + + + // Test of mutability + /* + BigInteger big1 = BigInteger.valueOf(3); + BigInteger big2 = big1; + big1 = BigInteger.valueOf(99); + System.out.print("Big 1 = " + big1); + System.out.print("\nBig 2 = " + big2); + */ + + // Test of Big Integer class + /* + int x = 34; + int y = 123456789; + + BigInteger big1 = BigInteger.valueOf(x); + BigInteger big2 = BigInteger.valueOf(y); + + BigInteger add = big2.add(big1); + BigInteger multiply = big2.multiply(big1); + BigInteger pow = big1.pow(3); + + System.out.print("Add = " + add); + System.out.print("\nMultiply = " + multiply); + System.out.print("\nPower = " + pow); + */ + } +} diff --git a/ch10/BigIntegerPower.java b/ch10/BigIntegerPower.java new file mode 100644 index 0000000..6acb9de --- /dev/null +++ b/ch10/BigIntegerPower.java @@ -0,0 +1,27 @@ +import java.math.BigInteger; + +/** + * Created by Comarch on 2016-07-19. + */ +public class BigIntegerPower { + public static BigInteger pow(int x, int n){ + if(n==0){ + return BigInteger.valueOf(1); + } + BigInteger t = pow(x, n/2); + + if(n%2 == 0){ + return t.multiply(t); + } else{ + BigInteger result = t.multiply(t); + for(int i=0; i divider){ + result = number % (i*divider); + } + return result; + } + + private static int findGCD(int numerator, int denominator){ + if(numerator - denominator == 0){ + return numerator; + } else if(numerator - denominator > 0){ + return findGCD(numerator-denominator, denominator); + } else{ + return findGCD(numerator,denominator-numerator); + } + } + + public static Rational reduce(Rational number){ + Rational result = new Rational(number.getNumerator(),number.getDenominator()); + int gcd = findGCD(number.numerator, number.denominator); + result.setNumerator(result.getNumerator()/gcd); + result.setDenominator(result.getDenominator()/gcd); + return result; + } + + public static void main(String[] args){ + Rational r = new Rational(1071,462); + Rational result = reduce(r); + System.out.println("Result: " + result); + System.out.println("r: " + r); + System.exit(0); + } +} diff --git a/ch11/Rational.java b/ch11/Rational.java new file mode 100644 index 0000000..252a658 --- /dev/null +++ b/ch11/Rational.java @@ -0,0 +1,116 @@ +/** + * Created by Comarch on 2016-07-21. + */ +public class Rational { + int numerator; + int denominator; + + /* + * Setters and getters + */ + public int getNumerator(){ + return this.numerator; + } + + public int getDenominator(){ + return this.denominator; + } + + public void setNumerator(int numerator){ + this.numerator = numerator; + } + + public void setDenominator(int denominator){ + this.denominator = denominator; + } + + /* + * Construct rational object with default values + */ + public Rational(){ + this.numerator = 0; + this.denominator = 1; + } + + /* + * Construct rational object with input values + */ + public Rational(int numerator, int denominator){ + this.numerator = numerator; + this.denominator = denominator; + } + + /* + * Print value in user-friendly format + */ + public void printRational(Rational rational){ + System.out.println(rational.numerator + "/" + rational.denominator); + } + + /* + * returns string with values of object + */ + public String toString() { + return "numerator = " + this.numerator + "; denominator = " + this.denominator; + } + + /* + * Changes sign of rational number + */ + public void negate(){ + this.numerator *= -1; + } + + /* + * Swaps numerator with denominator + */ + public void swap(){ + int tmp = this.numerator; + this.numerator = this.denominator; + this.denominator = tmp; + } + + /* + * Converts rational number to double + */ + public double toDouble(Rational that){ + double tmp = (double) that.denominator; + return that.numerator / tmp; + } + + /* + * Returns greatest common divisor with Euclidean algorhitm + */ + private static int findGCD(int numerator, int denominator){ + if(numerator - denominator == 0){ + return numerator; + } else if(numerator - denominator > 0){ + return findGCD(numerator-denominator, denominator); + } else{ + return findGCD(numerator,denominator-numerator); + } + } + + /* + * Reduces rational number to its lowest term + */ + public static Rational reduce(Rational number){ + Rational result = new Rational(number.getNumerator(),number.getDenominator()); + int gcd = findGCD(number.numerator, number.denominator); + result.setNumerator(result.getNumerator()/gcd); + result.setDenominator(result.getDenominator()/gcd); + return result; + } + + /* + * Adds input rational number to this object + */ + public void add(Rational that){ + this.numerator += that.getNumerator(); + this.denominator += that.getDenominator(); + Rational tmp = reduce(this); + this.numerator = tmp.getNumerator(); + this.denominator = tmp.getDenominator(); + } + +} diff --git a/ch11/RationalClient.java b/ch11/RationalClient.java new file mode 100644 index 0000000..fca931e --- /dev/null +++ b/ch11/RationalClient.java @@ -0,0 +1,31 @@ +/** + * Created by Comarch on 2016-07-21. + */ +public class RationalClient { + public static void main(String[] args){ + Rational number = new Rational(767,1900); + Rational toAdd = new Rational(100,89); + /* + number.setDenominator(2); + number.setNumerator(9); + */ + /* + number.printRational(number); + String s = number.toString(); + System.out.println(s); + + number.negate(); + s = number.toString(); + System.out.println("After negation: " + s); + number.negate(); + + number.swap(); + s = number.toString(); + System.out.println("After swap: " + s); + + double result = number.toDouble(number); + */ + number.add(toAdd); + System.out.println("Result: " + number); + } +} diff --git a/ch11/Scrabble.java b/ch11/Scrabble.java new file mode 100644 index 0000000..a7d6b9a --- /dev/null +++ b/ch11/Scrabble.java @@ -0,0 +1,64 @@ +/** + * Created by media on 2016-07-20. + */ +public class Scrabble { + + private char letter; + private int value; + + /* + * Getters and setters + */ + public char getLetter(){ + return this.letter; + } + + public int getValue(){ + return this.value; + } + + public void setLetter(char letter){ + this.letter = letter; + } + + public void setValue(int value){ + this.value = value; + } + + /* + * Constructor with parameters + */ + public Scrabble(char letter, int value){ + this.letter = letter; + this. value = value; + } + + /* + * Prints letter and it's value + */ + public void printTile(Scrabble Tile){ + System.out.print("\nLetter: " + Tile.letter + " has value; " + Tile.value); + } + + /* + * Method for testing objects + */ + public void testTile(){ + Scrabble tile = new Scrabble('z',10); + printTile(tile); + } + + /* + * Overriding method toString + */ + public String toString(){ + return String.format(this.letter + ": " + "%02d", this.value); + } + + /* + * returns TRUE when input tiles has the same valuies of fields + */ + public boolean equals(Scrabble tile1, Scrabble tile2){ + return (tile1.letter == tile2.letter) && (tile1.value == tile2.value); + } +} diff --git a/ch11/Time.java b/ch11/Time.java index 53afa0d..b8303fa 100644 --- a/ch11/Time.java +++ b/ch11/Time.java @@ -88,7 +88,12 @@ public Time add(Time t2) { * Adds the given number of seconds to this object (modifier). */ public void increment(double seconds) { - this.second += seconds; + this.second = (this.second + seconds) % 60; + int tmp = (int) seconds/60; + this.minute = (this.minute + tmp) % 60; + tmp = (int) seconds/3600; + this.hour = (this.hour + tmp) % 60; + /* while (this.second >= 60.0) { this.second -= 60.0; this.minute += 1; @@ -97,6 +102,7 @@ public void increment(double seconds) { this.minute -= 60; this.hour += 1; } + */ } } diff --git a/ch11/TimeClient.java b/ch11/TimeClient.java index aa90401..6be5bb5 100644 --- a/ch11/TimeClient.java +++ b/ch11/TimeClient.java @@ -28,6 +28,12 @@ public static void main(String[] args) { // using the instance method endTime = startTime.add(runningTime); + + // testing new implementation of increment + Time t1 = new Time(12, 00, 0.0); + t1.increment(12059); + String s1 = t1.toString(); + System.out.print("\nResult: " + s1); } } diff --git a/ch12/Card.java b/ch12/Card.java index e7a0661..21d555e 100644 --- a/ch12/Card.java +++ b/ch12/Card.java @@ -34,6 +34,12 @@ public int compareTo(Card that) { if (this.suit > that.suit) { return 1; } + if(this.rank == 1){ + return 1; + } + if(that.rank == 1){ + return -1; + } if (this.rank < that.rank) { return -1; } diff --git a/ch12/Search.java b/ch12/Search.java index 2f83023..724cf07 100644 --- a/ch12/Search.java +++ b/ch12/Search.java @@ -84,6 +84,38 @@ public static int binarySearch(Card[] cards, Card target, } } + /* + * Returns histogram of deck + */ + public static int[] suitHist(Card[] suit){ + int[] histogram = new int[52]; + for(int i=0; i= 5){ + return true; + } + } + return false; + } + /** * Demonstrates how to call the search methods. */ @@ -107,6 +139,25 @@ public static void main(String[] args) { System.out.println("Recursive binary search"); System.out.println(binarySearch(cards, jack, 0, 51)); System.out.println(); + + Card[] suit = new Card[8]; + suit[0] = new Card(2,3); + suit[1] = new Card(3,2); + suit[2] = new Card(4,0); + suit[3] = new Card(1,1); + suit[4] = new Card(2,1); + suit[5] = new Card(3,1); + suit[6] = new Card(4,1); + suit[7] = new Card(1,3); + + + int[] hist = suitHist(suit); + + for(int i=0; i +Copyright (C) 1999 Markus F.X.J. Oberhumer + +This cardset is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 of +the License, or (at your option) any later version. diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/back001.gif b/out/production/ThinkJavaCode/cardset-oxymoron/back001.gif new file mode 100644 index 0000000..463c0b0 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/back001.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/back101.gif b/out/production/ThinkJavaCode/cardset-oxymoron/back101.gif new file mode 100644 index 0000000..4bb2c95 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/back101.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/back102.gif b/out/production/ThinkJavaCode/cardset-oxymoron/back102.gif new file mode 100644 index 0000000..82b25f7 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/back102.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/back111.gif b/out/production/ThinkJavaCode/cardset-oxymoron/back111.gif new file mode 100644 index 0000000..d50079d Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/back111.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/back191.gif b/out/production/ThinkJavaCode/cardset-oxymoron/back191.gif new file mode 100644 index 0000000..cbd3a5a Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/back191.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/back192.gif b/out/production/ThinkJavaCode/cardset-oxymoron/back192.gif new file mode 100644 index 0000000..e7111b3 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/back192.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/bottom01.gif b/out/production/ThinkJavaCode/cardset-oxymoron/bottom01.gif new file mode 100644 index 0000000..dd9dcf7 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/bottom01.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/bottom02.gif b/out/production/ThinkJavaCode/cardset-oxymoron/bottom02.gif new file mode 100644 index 0000000..cf9c90b Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/bottom02.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/bottom03.gif b/out/production/ThinkJavaCode/cardset-oxymoron/bottom03.gif new file mode 100644 index 0000000..5357ddc Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/bottom03.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/bottom04.gif b/out/production/ThinkJavaCode/cardset-oxymoron/bottom04.gif new file mode 100644 index 0000000..85eca98 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/bottom04.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/bottom05.gif b/out/production/ThinkJavaCode/cardset-oxymoron/bottom05.gif new file mode 100644 index 0000000..c564d79 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/bottom05.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/bottom06.gif b/out/production/ThinkJavaCode/cardset-oxymoron/bottom06.gif new file mode 100644 index 0000000..fc5b61a Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/bottom06.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/bottom07.gif b/out/production/ThinkJavaCode/cardset-oxymoron/bottom07.gif new file mode 100644 index 0000000..8c8f330 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/bottom07.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/config.txt b/out/production/ThinkJavaCode/cardset-oxymoron/config.txt new file mode 100644 index 0000000..cc42839 --- /dev/null +++ b/out/production/ThinkJavaCode/cardset-oxymoron/config.txt @@ -0,0 +1,6 @@ +PySol solitaire cardset;4;.gif;1;52;0 +1000_oxymoron_073097;Oxymoron +73 97 8 +18 18 7 7 +back001.gif +back001.gif;back101.gif;back102.gif;back111.gif;back191.gif;back192.gif diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/l01.gif b/out/production/ThinkJavaCode/cardset-oxymoron/l01.gif new file mode 100644 index 0000000..da0b226 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/l01.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/l02.gif b/out/production/ThinkJavaCode/cardset-oxymoron/l02.gif new file mode 100644 index 0000000..6684d21 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/l02.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/l03.gif b/out/production/ThinkJavaCode/cardset-oxymoron/l03.gif new file mode 100644 index 0000000..c8ec2ad Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/l03.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/l04.gif b/out/production/ThinkJavaCode/cardset-oxymoron/l04.gif new file mode 100644 index 0000000..cc88f42 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/l04.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shade.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shade.gif new file mode 100644 index 0000000..ca96877 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shade.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow00.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow00.gif new file mode 100644 index 0000000..cc0560b Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow00.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow01.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow01.gif new file mode 100644 index 0000000..21e8cb2 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow01.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow02.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow02.gif new file mode 100644 index 0000000..5bbf57b Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow02.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow03.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow03.gif new file mode 100644 index 0000000..5b58fe5 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow03.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow04.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow04.gif new file mode 100644 index 0000000..13ac221 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow04.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow05.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow05.gif new file mode 100644 index 0000000..5996b2d Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow05.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow06.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow06.gif new file mode 100644 index 0000000..df18acb Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow06.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow07.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow07.gif new file mode 100644 index 0000000..b6cd41d Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow07.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow08.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow08.gif new file mode 100644 index 0000000..dea6b92 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow08.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow09.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow09.gif new file mode 100644 index 0000000..3c85f2d Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow09.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow10.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow10.gif new file mode 100644 index 0000000..2b47bfa Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow10.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow11.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow11.gif new file mode 100644 index 0000000..85bedaa Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow11.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow12.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow12.gif new file mode 100644 index 0000000..0f5ab82 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow12.gif differ diff --git a/out/production/ThinkJavaCode/cardset-oxymoron/shadow13.gif b/out/production/ThinkJavaCode/cardset-oxymoron/shadow13.gif new file mode 100644 index 0000000..3d17c17 Binary files /dev/null and b/out/production/ThinkJavaCode/cardset-oxymoron/shadow13.gif differ