diff --git a/.gitignore b/.gitignore index 427efdb..b70b3e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# IntelliJ Building and Production +out/ + # Compiled class file *.class @@ -18,9 +21,38 @@ *.tar.gz *.rar +# Libraries (Don't ignore) +!junit-platform-console-standalone-*.jar + # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* # Eclipse workspace .metadata .recommenders + +# IntelliJ user-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf +.idea/**/hotswap_agent.xml +.idea/**/discord.xml +.idea/**/markdown-nav*.xml +.idea/**/checkstyleidea-libs/ + +# IntelliJ generated files +.idea/**/contentModel.xml + +# IntelliJ sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# File-based project format +*.iws diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..5373ec0 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Think Java, 2nd Edition \ No newline at end of file diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 0000000..85cd8bc --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,19 @@ + + + + 8.36.2 + JavaOnly + true + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..5f74459 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..aed5149 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/junit_platform_console_standalone_1_2_0.xml b/.idea/libraries/junit_platform_console_standalone_1_2_0.xml new file mode 100644 index 0000000..3f5541b --- /dev/null +++ b/.idea/libraries/junit_platform_console_standalone_1_2_0.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..9b53da9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d5023d6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..78cface --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index a9646f6..e952f18 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,74 @@ +[![Think Java, 2nd Edition](https://greenteapress.com/wp/wp-content/uploads/2020/06/think_java_covr_large-229x300.png)](https://greenteapress.com/wp/think-java-2e/) + # ThinkJavaCode2 -Supporting code for Think Java, 2nd edition. -Copyright (c) 2020 Allen B. Downey and Chris Mayfield. + +Supporting code for Think Java, 2nd edition. +Copyright (c) 2020 Allen B. Downey and Chris Mayfield. This edition was published in December 2019 by [O'Reilly Media](https://www.oreilly.com/library/view/think-java-2nd/9781492072492/) (ISBN 9781492072508). -This repository contains the code examples from the book and starter code for some exercises. -There are several ways you can work with the code: +## Source Code + +This repository contains the code examples from the book and starter code for some exercises. Below are several ways you can work with the code. + +### Using Git (Recommended) + +Using Git to work with the repository is a great way to get into the habit of using Git, which is used by software developers around the globe. Here's how you can use Git to use the source code: + +- You can create a copy of this repository on GitHub by clicking the "Fork" button. + - If you don't already have a GitHub account, you'll need to create one. + - After forking, you'll have your own repository on GitHub that you can use to keep track of code you write. + - Then you can "clone" the repository, which downloads a copy of the files to your computer. -* You can create a copy of this repository on GitHub by clicking the "Fork" button. -If you don't already have a GitHub account, you'll need to create one. -After forking, you'll have your own repository on GitHub that you can use to keep track of code you write. -Then you can "clone" the repository, which downloads a copy of the files to your computer. +- Alternatively, you could clone the original repository without forking. + - If you choose this option, you don't need a GitHub account, but you won't be able to save your changes on GitHub. -* Alternatively, you could clone the original repository without forking. -If you choose this option, you don't need a GitHub account, but you won't be able to save your changes on GitHub. +### Download as ZIP -* If you don't want to use Git at all, you can download the code in a ZIP archive using the "Clone or download" button, or this link: https://thinkjava.org/code2zip. +If you don't want to use Git at all, you can download the code in a ZIP archive using the "Clone or download" button, or this link: [https://thinkjava.org/code2zip](https://thinkjava.org/code2zip). -To clone a repository, you need Git installed on your computer (see https://help.github.com/). +## Cloning the Repository + +To clone a repository, you need Git installed on your computer (see [https://help.github.com/](https://help.github.com/)). If you use Git from the command line, you can clone the original repository like this: - git clone https://github.com/ChrisMayfield/ThinkJavaCode2.git +```cmd +git clone https://github.com/ChrisMayfield/ThinkJavaCode2.git +``` + +## Working with the Code After you clone the repository or unzip the ZIP file, you should have a directory named `ThinkJavaCode2` with a subdirectory for each chapter in the book. -The examples in this book were developed and tested using OpenJDK 11. -If you are using a more recent version, everything should still work. -If you are using an older version, some of the examples might not. +A few notes about the source code regarding what Java version you should use: + +- The examples in this book were developed and tested using OpenJDK 11. +- If you are using a more recent version, everything should still work. +- If you are using an older version, some examples might not work, or may have unexpected consequences. + +There are a few ways you can work with the code: + +### jGRASP + +[jGRASP](https://www.jgrasp.org/) is an IDE used by beginners who are just getting started with Java. It isn't as sophisticated as Eclipse or IntelliJ IDEA, but it has enough features to begin learning Java without feeling too overwhelmed. + +- To open the source code in jGRASP, simply open jGRASP in the `ThinkJavaCode2` folder, or navigate to that folder in jGRASP's sidebar explorer. From there, you should be able to open the different `.java` files within the repository. You do not need to worry about the `.checkstyle`, `.classpath`, `.project`, or `*.iml` files. + +### Eclipse + +[Eclipse](https://www.eclipse.org/) is another IDE used by beginners and professionals alike, and is more feature-rich than jGRASP. + +- To open the source code in Eclipse, change your workspace to the `ThinkJavaCode2` folder. Eclipse should automatically detect the different chapter folders as projects and should add them accordingly. You do not need to worry about the `*.iml` +- If you want to use CheckStyle with these projects, feel free to install the official [Eclipse Checkstyle Plugin](https://checkstyle.org/eclipse-cs/#!/) and then use the `Checkstyle.xml` file in the root folder as the configuration file. + +### IntelliJ IDEA + +[IntelliJ IDEA](https://www.jetbrains.com/idea/) is an IDE mainly used by professionals or ambitious beginners. It's not recommended for beginners to use this IDE due to how convoluted the user interface can be, but it is a great tool down the line for Java development. + +- To open the source code in IntelliJ IDEA, open the `ThinkJavaCode2` folder as a project, as it has already been pre-configured as a project. It should automatically use your installation of JDK 11. Each of the chapter folders have been labeled as modules, so you can work with the code in each one as if they were a different project, similar to an Eclipse workspace. You do not need to worry about the `.checkstyle`, `.classpath`, or `.project` files in each folder, and they are ignored by IntelliJ's build path. +- If you want to use CheckStyle with these projects, feel free to install the [CheckStyle-IDEA](https://plugins.jetbrains.com/plugin/1065-checkstyle-idea) plugin into your project (Settings -> Plugins). The settings for this project have already been pre-configured and should be recognized automatically when you restart your IDE to enable the plugin. + +### Visual Studio Code + +[Visual Studio Code](https://code.visualstudio.com/) is an IDE which supports a wide variety of languages, including Java, and is used by many developers of any skill level. It's not made primarily with Java in mind, but you can install the [Java Extension Pack](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack), which includes plugins by Red Hat and Microsoft, to work with Java projects in this IDE. +- Coming soon... diff --git a/Think Java, 2nd Edition.iml b/Think Java, 2nd Edition.iml new file mode 100644 index 0000000..07e5ce6 --- /dev/null +++ b/Think Java, 2nd Edition.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/appa/.project b/appa/.project index 89577ab..cff9d58 100644 --- a/appa/.project +++ b/appa/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033450 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/appa/Appendix A.iml b/appa/Appendix A.iml new file mode 100644 index 0000000..3aabb4b --- /dev/null +++ b/appa/Appendix A.iml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/appb/.project b/appb/.project index 7658200..172f8c5 100644 --- a/appb/.project +++ b/appb/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033468 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/appb/Appendix B.iml b/appb/Appendix B.iml new file mode 100644 index 0000000..ad5610a --- /dev/null +++ b/appb/Appendix B.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/appc/.project b/appc/.project index 0e3c2da..e6ddcbb 100644 --- a/appc/.project +++ b/appc/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033491 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/appc/Appendix C.iml b/appc/Appendix C.iml new file mode 100644 index 0000000..4ee2204 --- /dev/null +++ b/appc/Appendix C.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/appd/.project b/appd/.project index 7b8a1e5..ced3071 100644 --- a/appd/.project +++ b/appd/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033507 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/appd/Appendix D.iml b/appd/Appendix D.iml new file mode 100644 index 0000000..8df396c --- /dev/null +++ b/appd/Appendix D.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch01/.project b/ch01/.project index d4e68d5..bfbda9d 100644 --- a/ch01/.project +++ b/ch01/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033523 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch01/Chapter 1.iml b/ch01/Chapter 1.iml new file mode 100644 index 0000000..64e504e --- /dev/null +++ b/ch01/Chapter 1.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch01/Hello.java b/ch01/Hello.java index 593557b..7b12188 100644 --- a/ch01/Hello.java +++ b/ch01/Hello.java @@ -3,6 +3,7 @@ public class Hello { public static void main(String[] args) { // generate some simple output System.out.println("Hello, World!"); + System.out.println("How uuuu doinn"); } } diff --git a/ch02/.project b/ch02/.project index c7628eb..43b1a3d 100644 --- a/ch02/.project +++ b/ch02/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033540 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch02/Chapter 2.iml b/ch02/Chapter 2.iml new file mode 100644 index 0000000..16bc5ba --- /dev/null +++ b/ch02/Chapter 2.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch02/exercises2/Date.java b/ch02/exercises2/Date.java new file mode 100644 index 0000000..2ccc361 --- /dev/null +++ b/ch02/exercises2/Date.java @@ -0,0 +1,21 @@ +package exercises2; + +public class Date { + public static void main(String[] args) { + String day; + int date; + String month; + int year; + + day = "Weds"; + date = 24; + month = "May"; + year = 2023; + + System.out.println("American format: " + day +", " + month +" "+ date + ", " + year); + System.out.println("European format: " + day +" " + date +" "+ month + " " + year); + + + + } +} diff --git a/ch02/exercises2/Time.java b/ch02/exercises2/Time.java new file mode 100644 index 0000000..f465e72 --- /dev/null +++ b/ch02/exercises2/Time.java @@ -0,0 +1,24 @@ +package exercises2; + +public class Time { + public static void main(String[] args) { + int hour, minute, second; + + hour = 10; + minute = 7; + second = 42; + + int secondsElapsed = hour*3600 + minute*60 + second; + int secondsleft = 24*3600 - secondsElapsed; + double perCentDayPassed = secondsElapsed*(100.0/(24*3600)); + + System.out.print("Number of seconds since midnight: "); + System.out.println(secondsElapsed); + System.out.print("Seconds left in day: "); + System.out.println(secondsleft); + System.out.println(perCentDayPassed); + + + + } +} diff --git a/ch03/.project b/ch03/.project index 13fab09..343e495 100644 --- a/ch03/.project +++ b/ch03/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033555 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch03/Chapter 3.iml b/ch03/Chapter 3.iml new file mode 100644 index 0000000..348b482 --- /dev/null +++ b/ch03/Chapter 3.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java deleted file mode 100644 index 64984df..0000000 --- a/ch03/GuessStarter.java +++ /dev/null @@ -1,15 +0,0 @@ -import java.util.Random; - -/** - * Starter code for the "Guess My Number" exercise. - */ -public class GuessStarter { - - public static void main(String[] args) { - // pick a random number - Random random = new Random(); - int number = random.nextInt(100) + 1; - System.out.println(number); - } - -} diff --git a/ch03/exercises3/CtoF.java b/ch03/exercises3/CtoF.java new file mode 100644 index 0000000..de78fe9 --- /dev/null +++ b/ch03/exercises3/CtoF.java @@ -0,0 +1,25 @@ +package exercises3; + +import java.util.Scanner; + +/** + * Converts celcius to Fahrenheit + */ +public class CtoF { + public static void main(String[] args) { + double celcius; + double fahrenheit; + final double RATIO = 1.8; + final int SHIFT = 32; + Scanner tempC = new Scanner(System.in); + + System.out.print("What is the temperature in Celcius: "); + celcius = tempC.nextDouble(); + fahrenheit = (celcius*RATIO) + SHIFT; + + System.out.printf("%.1f C = %.1f F",celcius, fahrenheit); + + + + } +} diff --git a/ch03/exercises3/GuessNumberGame.java b/ch03/exercises3/GuessNumberGame.java new file mode 100644 index 0000000..08867e1 --- /dev/null +++ b/ch03/exercises3/GuessNumberGame.java @@ -0,0 +1,7 @@ +package exercises3; + +import java.util.Random; +public class GuessNumberGame {public static void main(String[] args) { + +} +} diff --git a/ch03/exercises3/GuessStarter.java b/ch03/exercises3/GuessStarter.java new file mode 100644 index 0000000..8df246c --- /dev/null +++ b/ch03/exercises3/GuessStarter.java @@ -0,0 +1,35 @@ +package exercises3; + +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) { + // pick a random number + Random random = new Random(); + int number = random.nextInt(100) + 1; + //create variable to hold guess & comparison + int guess; + //where the differences are Guess-Number and Number-Guess respectively + int diffGuessNumb; + int diffNumbGuess; + int diff; + // create Scanner & prompt user to guess + Scanner readGuess = new Scanner(System.in); + System.out.println("Thinking of number between 1 & 100 (inclusive). Can you guess it? "); + System.out.print("Type a number: "); + guess = readGuess.nextInt(); + System.out.println("Your guess is: " + guess); + System.out.println("The number i was thinking of was: " + number); + diffGuessNumb = guess - number; + diffNumbGuess = number - guess; + diff = (((diffGuessNumb+1)/diffGuessNumb)*diffGuessNumb) + (((diffNumbGuess+1)/diffNumbGuess)*diffNumbGuess); + System.out.println("You were off by: " + diff); + + + } + +} diff --git a/ch03/exercises3/secondstohms.java b/ch03/exercises3/secondstohms.java new file mode 100644 index 0000000..ba10bbf --- /dev/null +++ b/ch03/exercises3/secondstohms.java @@ -0,0 +1,22 @@ +package exercises3; + +import java.util.Scanner; + +public class secondstohms {public static void main(String[] args) { + int totalSeconds, hours, minutes, seconds; + final int secsinmins = 60; + final int secsinhours = 3600; + Scanner timeConvert = new Scanner(System.in); + + System.out.print("How many seconds would you like to convert: "); + + totalSeconds = timeConvert.nextInt(); + seconds = totalSeconds; + hours = seconds/(secsinhours); + seconds = seconds % secsinhours; + minutes = seconds/secsinmins; + seconds =seconds % secsinmins; + + System.out.printf("%d seconds = %d hours, %d minutes, and %d seconds",totalSeconds, hours,minutes,seconds); +} +} 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..4c7f0ad --- /dev/null +++ b/ch03/test.out @@ -0,0 +1,2 @@ +193.04 cm = 6 ft, 4 in +Exactly how many cm diff --git a/ch04/.project b/ch04/.project index 9834b0d..abd2981 100644 --- a/ch04/.project +++ b/ch04/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033572 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch04/Chapter 4.iml b/ch04/Chapter 4.iml new file mode 100644 index 0000000..fb2493a --- /dev/null +++ b/ch04/Chapter 4.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch04/Wugga.java b/ch04/Wugga.java index 64ffa49..6514145 100644 --- a/ch04/Wugga.java +++ b/ch04/Wugga.java @@ -16,6 +16,7 @@ public static void main(String[] args) { public static void baffle() { System.out.print("wug"); ping(); + } public static void ping() { diff --git a/ch04/exercises4/DateParam.java b/ch04/exercises4/DateParam.java new file mode 100644 index 0000000..b342c62 --- /dev/null +++ b/ch04/exercises4/DateParam.java @@ -0,0 +1,32 @@ +package exercises4; + +/** + * Prints American and European dates using parametrised methods + */ +public class DateParam { + public static void printAmerican(String day, int date, String month, int year){ + System.out.println("American format: " + day +", " + month +" "+ date + ", " + year); + } + public static void printEuropean(String day, int date, String month, int year){ + System.out.println("European format: " + day +" " + date +" "+ month + " " + year); + + } + public static void main(String[] args) { + String day; + int date; + String month; + int year; + + day = "Weds"; + date = 24; + month = "May"; + year = 2023; + + printAmerican(day, date, month, year ); + printEuropean(day, date, month,year); + + + + + } +} diff --git a/ch04/exercises4/Multadd.java b/ch04/exercises4/Multadd.java new file mode 100644 index 0000000..a126ef9 --- /dev/null +++ b/ch04/exercises4/Multadd.java @@ -0,0 +1,37 @@ +package exercises4; + +public class Multadd { + public static double multadd(double a, double b, double c){ + return a * b + c; + } + public static double expSum(double x){ + double negE = Math.exp(-x); + double root = Math.sqrt(1-negE); + return multadd(x, negE, root); + } + + public static void main(String[] args){ + double a = 1, b = 2 , c = 3; + + System.out.println(multadd(a, b, c)); + + a = Math.cos(Math.PI/4); + b = 1/2; + c=Math.sin(Math.PI/4); + + System.out.println(multadd(a, b, c)); + + a = 2; + b = Math.log(10); + c = Math.log(2); + + System.out.println(multadd(a, b, c)); + + System.out.println(expSum(2.0)); + + + + + } + +} diff --git a/ch05/.project b/ch05/.project index 933b313..f019e0a 100644 --- a/ch05/.project +++ b/ch05/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033588 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch05/Chapter 5.iml b/ch05/Chapter 5.iml new file mode 100644 index 0000000..e537f72 --- /dev/null +++ b/ch05/Chapter 5.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch05/exercises5/Fermat.java b/ch05/exercises5/Fermat.java new file mode 100644 index 0000000..43f92c4 --- /dev/null +++ b/ch05/exercises5/Fermat.java @@ -0,0 +1,30 @@ +package exercises5; +import java.util.Scanner; +public class Fermat { + public static boolean fermatLastTheorem(int a, int b, int c, int n){ + boolean fLTFalse; + double fLT = Math.pow(a,n) + Math.pow(b,n) - Math.pow(c,n); + if (fLT == 0){ + fLTFalse = true; + } else { + fLTFalse =false; + } + return fLTFalse; + } + + public static void main(String[] args){ + int a , b, c, n; + Scanner value = new Scanner(System.in); + System.out.println("Pick 4 numbers. Hit space after each: "); + a = value.nextInt(); + b = value.nextInt(); + c = value.nextInt(); + n = value.nextInt(); + + if (n > 2 && fermatLastTheorem(a,b,c,n) ){ + System.out.print("Holy smokes, Fermat was wrong"); + } else { + System.out.print("No that doesn't work"); + } + } +} diff --git a/ch05/exercises5/GuessStarter.java b/ch05/exercises5/GuessStarter.java new file mode 100644 index 0000000..67d0590 --- /dev/null +++ b/ch05/exercises5/GuessStarter.java @@ -0,0 +1,63 @@ +package exercises5; + +import java.util.Random; +import java.util.Scanner; + +/** + * Starter code for the "Guess My Number" exercise. + */ +public class GuessStarter { + public static boolean guessesLeft(Scanner readGuess, int numOfGuesses, int number){ + boolean correct; + if (numOfGuesses < 1){ + correct = false; + + } else { + System.out.print("Type a number: "); + int guess = readGuess.nextInt(); + numOfGuesses = numOfGuesses -1; + if (guess == number){ + correct = true; + } else { + + correct = false; + if (guess < number){ + System.out.println("Too Low"); + } else { + System.out.println("Too High"); + } + System.out.printf("You have %d guesses left.\n", numOfGuesses); + guessesLeft(readGuess, numOfGuesses,number); + } + } + return correct; + } + + + public static void main(String[] args) { + // pick a random number + Random random = new Random(); + int number = random.nextInt(100) + 1; + //create variable to hold guess + int numOfGuesses = 3; + // create Scanner & prompt user to guess + Scanner readGuess = new Scanner(System.in); + System.out.println("Thinking of number between 1 & 100 (inclusive). Can you guess it? "); + boolean rightAnswer = guessesLeft(readGuess,numOfGuesses,number); + if (!rightAnswer){ + System.out.printf("\nYou are out of guesses. \nThe number was: %d", number); + } else { + System.out.printf("Good guess. My number was: %d", number); + } + + + + + + + + + + } + +} diff --git a/ch05/exercises5/Quadratic.java b/ch05/exercises5/Quadratic.java new file mode 100644 index 0000000..d68a120 --- /dev/null +++ b/ch05/exercises5/Quadratic.java @@ -0,0 +1,62 @@ +package exercises5; +import java.util.Scanner; +public class Quadratic { + public static int discriminant(int a, int b, int c) { + int discr = b * b - 4 * a * c; + return discr; + } + + public static boolean checks(int discr, int a) { + boolean divby0 = false; + boolean negDiscriminant = false; + boolean flag = false; + + if (discr < 0) { + negDiscriminant = true; + System.err.println( + "The Discriminant for this choice of inputs is negative."); + } + if (a == 0) { + divby0 = true; + System.err.println("The Denominator for this choice of inputs is 0."); + } + if (divby0 || negDiscriminant) { + flag = true; + } + return flag; + } + + public static void quadFormula(int a, int b, int discr) { + double xAdd, xSub; + xAdd = (-b + Math.sqrt(discr)) / 2 * a; + xSub = (-b - Math.sqrt(discr)) / 2 * a; + if (xAdd == xSub) { + System.out.printf("Just one answer this time: %f", xAdd); + } else { + System.out.printf( + "The two solutions are %f and %f , for addition and subtraction respectively", + xAdd, xSub); + } + + } + + public static void main(String[] args) { + Scanner coeffs = new Scanner(System.in); + int a, b, c; + System.out.println( + "Give coefficients a,b,c separated by spaces, for a generic quadratic eqn: "); + a = coeffs.nextInt(); + b = coeffs.nextInt(); + c = coeffs.nextInt(); + int discr = discriminant(a, b, c); + + + + if (checks(discr, a)){ + } + else { + quadFormula(a, b, discr); + } + + } +} \ No newline at end of file diff --git a/ch05/exercises5/Rewrite.java b/ch05/exercises5/Rewrite.java new file mode 100644 index 0000000..4791003 --- /dev/null +++ b/ch05/exercises5/Rewrite.java @@ -0,0 +1,19 @@ +package exercises5; + +import java.util.Scanner; + +public class Rewrite { + public static void ifPos(int a){ + if (a > 0 && a < 10) { + System.out.println("Positive single digit number"); + } + } + + public static void main(String[] args){ + Scanner in = new Scanner(System.in); + + System.out.print("Is this a positive single digit number: "); + int a = in.nextInt(); + ifPos(a); + } +} diff --git a/ch05/exercises5/Triangle.java b/ch05/exercises5/Triangle.java new file mode 100644 index 0000000..ff2f95b --- /dev/null +++ b/ch05/exercises5/Triangle.java @@ -0,0 +1,56 @@ +package exercises5; + +import java.util.Scanner; + +public class Triangle { + public static boolean checkGreaterZero(double a) { + boolean greaterZero; + if (a < 0) { + greaterZero = false; + System.out.printf("%f is less than 0", a); + } else { + greaterZero = true; + } + return greaterZero; + } + public static boolean triangleCheck(double a, double b, double c){ + boolean Poss; + if ((checkGreaterZero(a)&&checkGreaterZero(b)&&checkGreaterZero(c))){ + if(a > b + c){ + Poss = false; + System.out.printf("%f is too long\n", a); + } else { + Poss = true; + } + } else {Poss = false; + } + + return Poss;} + + public static void Triangleposs(double a, double b, double c){ + boolean aCheck = checkGreaterZero(a); + boolean bCheck = checkGreaterZero(b); + boolean cCheck = checkGreaterZero(c); + + boolean aTricheck = triangleCheck(a,b,c); + boolean bTricheck = triangleCheck(b,a,c); + boolean cTricheck = triangleCheck(c,a,b); + + if ((aCheck && bCheck && cCheck) && aTricheck && bTricheck && cTricheck){ + System.out.println("A triangle is possible"); + } else { + System.out.println("A triangle is not possible"); + } + } +public static void main(String[] args){ + Scanner triSides = new Scanner(System.in); + System.out.print("Let's see if we can make a triangle. Pick 3 numbers, separated by spaces"); + double a = triSides.nextInt(); + double b = triSides.nextInt(); + double c = triSides.nextInt(); + + + + Triangleposs(a,b,c); + } +} \ No newline at end of file diff --git a/ch06/.project b/ch06/.project index 50d0c5e..d5a6de9 100644 --- a/ch06/.project +++ b/ch06/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033605 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch06/Chapter 6.iml b/ch06/Chapter 6.iml new file mode 100644 index 0000000..96345c0 --- /dev/null +++ b/ch06/Chapter 6.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch06/Exercise.java b/ch06/Exercise.java index 6c7b3df..0a6cb88 100644 --- a/ch06/Exercise.java +++ b/ch06/Exercise.java @@ -1,7 +1,6 @@ public class Exercise { - public static void main(String[] args) { - loop(10); + public static void main(String[] args) {loop(998); } public static void loop(int n) { diff --git a/ch06/exercises6/Abecedarian.java b/ch06/exercises6/Abecedarian.java new file mode 100644 index 0000000..81c3172 --- /dev/null +++ b/ch06/exercises6/Abecedarian.java @@ -0,0 +1,19 @@ +package exercises6; +import java.util.Scanner; +public class Abecedarian { + public static boolean abecedarian(String candidate){ + char letter = candidate.charAt(0); + for(int i = 1;i < candidate.length(); i++){ + if(letter >= candidate.charAt(i)){ + return false; + } else { + letter = candidate.charAt(i);} + } return true; + } + public static void main(String[] args){ + Scanner potential = new Scanner(System.in); + String candidate = potential.next(); + System.out.println(abecedarian(candidate)); + } +} + diff --git a/ch06/exercises6/CanSpell.java b/ch06/exercises6/CanSpell.java new file mode 100644 index 0000000..80939af --- /dev/null +++ b/ch06/exercises6/CanSpell.java @@ -0,0 +1,38 @@ +package exercises6; + +import java.util.Scanner; + +public class CanSpell { + public static String removeChar(String a, int b){ + String afterRemove = ""; + for (int i = 0; i < a.length(); i++){ + if(i != b){ + afterRemove += a.charAt(i); + } + } + +return afterRemove; + } + public static boolean canSpell(String tiles, String word){ + boolean indicator = true; + for (int i = 0; i < word.length(); i++) { + char letter = word.charAt(i); + int index = tiles.indexOf(letter); + if (index != -1) { + removeChar(tiles, index); + } else { + indicator = false; + } + } + return indicator; + } + + + + + public static void main(String[] args) { + System.out.println( canSpell( "skjgfukhlsd","skj")); + + + } +} diff --git a/ch06/exercises6/Doubloon.java b/ch06/exercises6/Doubloon.java new file mode 100644 index 0000000..4f7177a --- /dev/null +++ b/ch06/exercises6/Doubloon.java @@ -0,0 +1,34 @@ +package exercises6; +import java.util.Scanner; +public class Doubloon { + public static boolean doubloon(String c){ + c.toLowerCase(); + for(int i = 0; i < c.length(); i++){ + System.out.println(i); + + for(int j = 0; j < c.length(); i++){ + + int counter = 0; + while(j < c.length()) + if(j == c.indexOf(c.charAt(i)) || j == c.indexOf(c.substring(i+1).charAt(i))) { + counter += 1; + System.out.println(counter); + } + if (counter != 2){ + System.out.println(counter); + return false; + } + } + } + return true; + } + +public static void main(String[] args){ + Scanner possible = new Scanner(System.in); + System.out.print("Ye may have DOUBLOONS. Verily place them here: "); + String c = possible.next(); + System.out.println(doubloon(c)); + + +} +} \ No newline at end of file diff --git a/ch06/exercises6/Gauss.java b/ch06/exercises6/Gauss.java new file mode 100644 index 0000000..530f732 --- /dev/null +++ b/ch06/exercises6/Gauss.java @@ -0,0 +1,26 @@ +package exercises6; +import java.util.Scanner; + +public class Gauss { + public static double gauss(double x, int n){ + double sum = 0; + double diff = - (x*x); + double x0 = 1; + for ( int i = 1 ; i <= n; i++){ + sum += x0; + System.out.printf("%4d,%f",i,sum); + System.out.println(); + + x0 = x0 * diff/i; + System.out.println(x0); + } return sum; + } + public static void main(String[] args){ + Scanner gauss = new Scanner(System.in); + System.out.print("Pick x and n: "); + double x = gauss.nextDouble(); + int n = gauss.nextInt(); + System.out.println(gauss(x,n)); + + } +} diff --git a/ch06/exercises6/Greek.java b/ch06/exercises6/Greek.java new file mode 100644 index 0000000..7cb4499 --- /dev/null +++ b/ch06/exercises6/Greek.java @@ -0,0 +1,12 @@ +package exercises6; + +public class Greek { + public static void main(String[] args) { + System.out.print("Greek alphabet: "); + for (char i = 'A'; i <= 'Z'; i++) { + System.out.print(i); + } + System.out.println(); + + } +} diff --git a/ch06/exercises6/SquareRoot.java b/ch06/exercises6/SquareRoot.java new file mode 100644 index 0000000..c03b97b --- /dev/null +++ b/ch06/exercises6/SquareRoot.java @@ -0,0 +1,22 @@ +package exercises6; +import java.util.Scanner; + +public class SquareRoot { + public static double squareRoot(double a, double approx) { + double b = (approx + a/approx)/2; + return b; + } + public static void main(String[] args){ + Scanner in = new Scanner(System.in); + System.out.print("Enter a number: "); + double a = in.nextDouble(); + double approx = a/2; + double b = squareRoot(a,approx); + while (Math.abs(approx-b) > 0.0001){ + approx = b; + b = squareRoot(a,approx); + } + System.out.println(b); + } + +} diff --git a/ch06/exercises6/looptimeString.java b/ch06/exercises6/looptimeString.java new file mode 100644 index 0000000..4cab99e --- /dev/null +++ b/ch06/exercises6/looptimeString.java @@ -0,0 +1,27 @@ +package exercises6; + +public class looptimeString { + public static String timeString(int hour, int minute) { + String ampm; + if (hour < 12) { + ampm = "AM"; + if (hour == 0) { + hour = 12; //midnight + } + } else { + ampm = "PM"; + hour = hour - 12; + } + return String.format("%02d:%02d %s", hour, minute, ampm); + + } + public static void main(String[] args){ + for (int i = 0; i < 24; i++ ) { + String bigTimeString = ""; + for (int j = 0; j < 60; j++){ + bigTimeString += timeString(i,j) + " " ; + + } System.out.println(bigTimeString); + } + } +} diff --git a/ch07/.project b/ch07/.project index 6887963..2708d54 100644 --- a/ch07/.project +++ b/ch07/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033623 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch07/Chapter 7.iml b/ch07/Chapter 7.iml new file mode 100644 index 0000000..8e976ae --- /dev/null +++ b/ch07/Chapter 7.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch08/.project b/ch08/.project index a78d2dd..fe52232 100644 --- a/ch08/.project +++ b/ch08/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033640 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch08/Chapter 8.iml b/ch08/Chapter 8.iml new file mode 100644 index 0000000..d599502 --- /dev/null +++ b/ch08/Chapter 8.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch09/.project b/ch09/.project index 3dbf792..c2251e4 100644 --- a/ch09/.project +++ b/ch09/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033657 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch09/Chapter 9.iml b/ch09/Chapter 9.iml new file mode 100644 index 0000000..56ffcff --- /dev/null +++ b/ch09/Chapter 9.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch10/.project b/ch10/.project index 5b1937d..5299f4a 100644 --- a/ch10/.project +++ b/ch10/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033674 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch10/Chapter 10.iml b/ch10/Chapter 10.iml new file mode 100644 index 0000000..afc3cda --- /dev/null +++ b/ch10/Chapter 10.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch11/.project b/ch11/.project index dd4d0e0..3716290 100644 --- a/ch11/.project +++ b/ch11/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033691 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch11/Chapter 11.iml b/ch11/Chapter 11.iml new file mode 100644 index 0000000..c4ddb15 --- /dev/null +++ b/ch11/Chapter 11.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch12/.project b/ch12/.project index 4d575af..6b640da 100644 --- a/ch12/.project +++ b/ch12/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033707 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch12/Chapter 12.iml b/ch12/Chapter 12.iml new file mode 100644 index 0000000..c4e0f77 --- /dev/null +++ b/ch12/Chapter 12.iml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch13/.project b/ch13/.project index 5ae6360..2a32f32 100644 --- a/ch13/.project +++ b/ch13/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033722 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch13/Chapter 13.iml b/ch13/Chapter 13.iml new file mode 100644 index 0000000..871edd4 --- /dev/null +++ b/ch13/Chapter 13.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch14/.project b/ch14/.project index 4294f38..0923883 100644 --- a/ch14/.project +++ b/ch14/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033737 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch14/Chapter 14.iml b/ch14/Chapter 14.iml new file mode 100644 index 0000000..c692174 --- /dev/null +++ b/ch14/Chapter 14.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch15/.project b/ch15/.project index 77395cb..33e63c9 100644 --- a/ch15/.project +++ b/ch15/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033753 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch15/Chapter 15.iml b/ch15/Chapter 15.iml new file mode 100644 index 0000000..fc70802 --- /dev/null +++ b/ch15/Chapter 15.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch16/.project b/ch16/.project index ae6ca3f..08fd71f 100644 --- a/ch16/.project +++ b/ch16/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033769 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch16/Chapter 16.iml b/ch16/Chapter 16.iml new file mode 100644 index 0000000..3109edb --- /dev/null +++ b/ch16/Chapter 16.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ch17/.project b/ch17/.project index 7010276..1cc59fb 100644 --- a/ch17/.project +++ b/ch17/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1684913033784 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch17/Chapter 17.iml b/ch17/Chapter 17.iml new file mode 100644 index 0000000..fb4a474 --- /dev/null +++ b/ch17/Chapter 17.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libraries/junit-platform-console-standalone-1.2.0.jar b/libraries/junit-platform-console-standalone-1.2.0.jar new file mode 100644 index 0000000..5446f76 Binary files /dev/null and b/libraries/junit-platform-console-standalone-1.2.0.jar differ