├── .DS_Store
├── Appendix
└── Readme
├── Bonus Chapters
├── .DS_Store
├── Errata
└── Readme
├── Chapter1
├── .DS_Store
├── .metadata
│ └── .plugins
│ │ ├── org.eclipse.core.resources
│ │ └── .history
│ │ │ └── d5
│ │ │ └── 2082ab030c030014105fc5f4c212048e
│ │ ├── org.eclipse.debug.core
│ │ └── .launches
│ │ │ ├── BinaryQuiz.launch
│ │ │ ├── ComplexExpressions.launch
│ │ │ ├── EscapeCharacterTest.launch
│ │ │ ├── Exercise.launch
│ │ │ ├── Exercise2_20.launch
│ │ │ ├── Exercise2_21.launch
│ │ │ ├── Exercise2_25.launch
│ │ │ ├── Exercise3_16.launch
│ │ │ ├── Exercise3_30.launch
│ │ │ ├── Exercise3_35.launch
│ │ │ ├── Exercise3_9.launch
│ │ │ ├── Exercise4_19.launch
│ │ │ ├── Exercise4_25.launch
│ │ │ ├── Exercise4_26.launch
│ │ │ ├── Exercise4_37.launch
│ │ │ ├── Exercise4_47.launch
│ │ │ ├── Exercise5_17.launch
│ │ │ ├── PrintPrimes.launch
│ │ │ └── ShortForLoop.launch
│ │ └── org.eclipse.ui.workbench
│ │ └── workingsets.xml
├── CH1Readme.md
├── Chapter1
│ ├── .DS_Store
│ ├── .classpath
│ ├── .gitignore
│ ├── .project
│ ├── Chapter1Exercises.class
│ └── Chapter1Exercises.java
└── Chapter3
│ └── .gitignore
├── Chapter2
├── .classpath
├── .gitignore
├── .project
├── Readme.md
└── src
│ ├── EscapeCharacterTest.java
│ ├── Exercise2_20.java
│ ├── Exercise2_21.java
│ └── Exercise2_25.java
├── Chapter3
├── Readme.md
└── src
│ ├── ComplexExpressions.java
│ ├── Exercise3_16.java
│ ├── Exercise3_30.java
│ ├── Exercise3_35.java
│ ├── Exercise3_9.java
│ └── complexExpressionExample.java
├── Chapter4
├── Readme.md
└── src
│ ├── BinaryQuiz.java
│ ├── Exercise4_19.java
│ ├── Exercise4_25.java
│ ├── Exercise4_26.java
│ ├── Exercise4_37.java
│ ├── Exercise4_46.java
│ ├── Exercise4_47.java
│ ├── GuessNumber.java
│ ├── GuessNumberFor.java
│ ├── PrintPrimes.java
│ └── ShortForLoop.java
├── Chapter5
├── Readme.md
└── src
│ ├── Exercise5_17.java
│ ├── Exercise5_26.java
│ ├── Exercise5_27.java
│ ├── Exercise5_31.java
│ ├── MethodExample.java
│ ├── Readme.md
│ └── StringMethods.java
├── Chapter6
├── Readme.md
└── src
│ ├── ArrayCreation.java
│ ├── ArrayModification.java
│ ├── BinarySearch.java
│ ├── Exercise6_12.java
│ ├── Exercise6_15.java
│ ├── Exercise6_25.java
│ ├── Exercise6_27.java
│ ├── InsertionSort.java
│ ├── LinearSearch.java
│ └── SelectionSort.java
├── Chapter7
├── Readme.md
└── src
│ ├── Exercise7_25.java
│ ├── Exercise7_28.java
│ ├── Exercise7_36.java
│ ├── Exercise7_5.java
│ └── Latin Square
│ ├── Exercise7_36.java
│ └── Readme.md
├── Chapter8
└── Readme.md
├── Errata
├── README.md
└── Supplements
├── .DS_Store
├── Case Studies
├── AddressBook.pdf
├── KnightTour.html
├── KnightTour.pdf
└── Sudoku.pdf
├── Database Supplements
├── .DS_Store
├── Access.pdf
├── AdvancedSQL.pdf
├── DatabaseDesign.pdf
├── IntroductionToDB.pdf
├── MySQL.pdf
├── Oracle10g.pdf
├── RelationalDataModel.pdf
├── SQLBasics.pdf
├── createsampletables_access.txt
├── createsampletables_mysql.txt
└── createsampletables_oracle.txt
├── Glossary.pdf
├── Java Supplements
├── .DS_Store
├── AdditionalBooleanOperarators.pdf
├── Assertion.pdf
├── CommandLineArgument.pdf
├── DatagramSocket.pdf
├── Designpatterns.pdf
├── EnumeratedType.pdf
├── FormatingString.pdf
├── GridBagLayout.pdf
├── HidingDataFieldsAndStaticMethods.pdf
├── ImmutableObjects.pdf
├── InitializationBlock.pdf
├── InternalFrame.pdf
├── JEditorPane.pdf
├── JNI_Example.zip
├── JavaCharacteristics.pdf
├── JavaWebStart.pdf
├── LiangAppFv3.pdf
├── Logger.pdf
├── ObjectClass.pdf
├── OpearatorEvaluation.pdf
├── OverlayLayout.pdf
├── Overriding.pdf
├── Package.pdf
├── PackageArchive.pdf
├── PluggableLookAndFeel.pdf
├── Profiler.pdf
├── RegularExpression.pdf
├── SignedApplet.pdf
├── SpringLayout.pdf
├── StatementLabel.pdf
├── StringTokenizer.pdf
├── SwingWorkerJProgressBar.pdf
├── TextIOReaderWriter.pdf
├── UMLNotation.pdf
├── UnitTest.pdf
└── javadoccomments.pdf
├── Supplement1cJDKonDOS.pdf
├── Supplement1fUsingPackage.pdf
└── Web Programming Supplements
├── CSS.pdf
├── HTML.pdf
├── JavaXML.pdf
├── M41.pdf
├── Tomcat5.5.9.pdf
└── XML.pdf
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/.DS_Store
--------------------------------------------------------------------------------
/Appendix/Readme:
--------------------------------------------------------------------------------
1 | Appendices
2 | Appendix A Java Keywords
3 | Appendix B The ASCII Character Set
4 | Appendix C Operator Precedence Chart
5 | Appendix D Java Modifiers
6 | Appendix E Special Floating-Point Values
7 | Appendix F Number Systems
8 | Appendix G Bitwise Operations
--------------------------------------------------------------------------------
/Bonus Chapters/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Bonus Chapters/.DS_Store
--------------------------------------------------------------------------------
/Bonus Chapters/Errata:
--------------------------------------------------------------------------------
1 | Errata
2 |
3 | Chapter 1
4 | Page 23, Figure 1.18, the title in the screen shot should be Message, not "Display Message".
5 |
6 | Chapter 2
7 | Page 42, line 4 and 5, change int to double.
8 |
9 | Chapter 3
10 | Page 97, click here to see a new BMI table.
11 |
12 | Page 100, line 10, insert the symbol " at the end of the line.
13 |
14 | Page 104, three lines before check point 3.18, change "Java also provides conditional" to "Java also provides unconditional".
15 |
16 | Page 106, in the output box for the first sample run for Listing 3.8, change 2012 to 2008.
17 |
18 | Page 131, line 3, change Package 1 to Package 2.
19 |
20 | Chapter 4
21 | Page 136, in Listing 4.1, lines 5-6, change % to *.
22 |
23 | Page 167, in Exercise 4.5, delete "and that 1 pound is .45 kilograms."
24 |
25 | Page 168, in Exercise 4.6, delete "and that 1 kilometer is .621 miles."
26 |
27 | Chapter 5
28 | Page 182, 10 line above Figure 5.3, change "and then" to "and then m2 calls".
29 |
30 | Page 186, line -2, remove the question mark (?).
31 |
32 | Chapter 6
33 | Page 229, in line 11, missing a semicolon after myList[i] = myList[j].
34 |
35 | Page 259, in the sample run box for Exercise 6.21, change 7 to 8.
36 |
37 | Chapter 7
38 | Page 291, line 3, change 3-by-4 to 3-by-3.
39 |
40 | Page 291, in the first line after the method header in Exercise 7.29, change "enter two lists" to "enter two arrays."
41 |
42 | Chapter 9
43 | Page 360, in line 14 in Listing 9.5, change it to "Split items from a string".
44 |
45 | Chapter 10
46 | Page 382, in Figure 10.6, swap the last two numerals 1 and 1.. 3 in the diagram.
47 |
48 | Page 394, in Figure 10.14, longVlaue should be longValue.
49 |
50 | Chapter 11
51 | Page 422, the first line in the output box for Listing 11.5, change "Color is white" to ""Color is red."
52 |
53 | Chapter 12
54 | Page 473, line 1, change java.swing to javax.swing.
55 |
56 | Chapter 13
57 | Page 504, Insert the following paragraph before Section 13.10.
58 |
59 | "The current hour, minute, and second is obtained by using the GregorianCalendar class (lines 102-110). The GregorianCalendar class in the Java API enables you to create a Calendar instance for the current time using its no-arg constructor. You can then use its methods get(Calendar.HOUR), get(Calendar.MINUTE), and get(Calendar.SECOND) to return the hour, minute, and second from a Calendar object. The classes Calendar and GregorianCalendar class will be further discussed in Section 15.4."
60 |
61 | Page 513, line -2, change hexagon to octagon.
62 |
63 | Chapter 14
64 | Page 539, line 13 in Listing 14.12, change CircleWithException to CircleWithCustomException.
65 |
66 | Page 540, change 0 to 1 in the output box for Listing 14.12.
67 |
68 | Page 544, in Listing 14.13, line 2, change IoException to Exception.
69 |
70 | Chapter 15
71 | Page 566, line -2, Change the sentence to "Key Point for Section 15.7 to "If the doubleValue() method were not defined in the Number class, you will not ..."
72 |
73 | Page 577, Replace Check Point Question 15.21 with a new one here.
74 |
75 | Page 577, Change the Key Point for Section 15.7 to "The Cloneable interface specifies that an object can be cloned."
76 |
77 | Chapter 16
78 | Page 603, Table 16.1, "select a new item ..." is listed twice. delete the first one.
79 |
80 | Chapter 18
81 | Page 704, in Figure 18.28, change number of slots in the text field from 7/9/12 to 6/8/11.
82 |
83 | Page 706, Change "Redraw Rectangles" to "Redraw Triangles" in Figure 18.32c.
84 |
85 | Chapter 19
86 | Page 715, code listing line 23 in Listing 19.1, change "Close the output stream" to "Close the input stream."
87 |
88 | Page 726, line 10 in Listing 19.6, change the comment to "Read a string, double value, and object from the file".
89 |
90 | Chapter 21
91 | Page 777, the comment line in line 41, change list[i+1...list.length-2] to list[i...list.length-1].
92 |
93 | Chapter 22
94 | Page 827, Figure 22.20, change "experssion" to "expression".
95 |
96 | Chapter 23
97 | Page 831, two lines before Listing 23.1, change an iterator to a for-each loop.
98 |
99 | Chapter 25
100 | Page 922, line 7, change descending to ascending.
101 |
102 | Chapter 26
103 | Page 939, In Figure 26.8, change "last = head" to "tail = head".
104 |
105 | Chapter 27
106 | Page 995, Programming Exercise 27.22, change the number of objects from 5 to 6 in the Sample Output.
107 |
108 | Page 995, Programming Exercise 27.23, change the number of objects from 5 to 6 in the Sample Output.
109 |
110 | Chapter 28
111 | Page 1004, in Figure 28.6, change key:27 to key:28 and change key:23 to key:21.
112 |
113 | Chapter 32
114 | Page 1038, in line 2 in the first paragraph in Section 32.6, change "the most" to "most".
115 |
116 | Chapter 34
117 | Page 1235, first line in the second paragraph in Section 34.5, change preparedStatement to prepareStatement.
118 |
119 | Page 1236, three lines before Listing 34.3, change preparedStatement to prepareStatement.
--------------------------------------------------------------------------------
/Bonus Chapters/Readme:
--------------------------------------------------------------------------------
1 | #Chapters 35 - 50 are bonus chapters not included in the book
2 | ##**I have included the docs for use only by myself and others who possess the access code**
3 | ##**Do not access these chapters if you do not own an access code**
4 |
5 | The problems contained within these chapters may be improved with provided solutions
6 |
7 | ###**Bonus Chapters 35–50**
8 | - Chapter 35 Internationalization
9 | - Chapter 36 JavaBeans
10 | - Chapter 37 Containers, Layout Managers, and Borders
11 | - Chapter 38 Menus, Toolbars, and Dialogs
12 | - Chapter 39 MVC and Swing Models
13 | - Chapter 40 JTable and JTree
14 | - Chapter 41 Advanced Database Programming
15 | - Chapter 42 Servlets
16 | - Chapter 43 JavaServer Pages
17 | - Chapter 44 JavaServer Faces
18 | - Chapter 45 Web Services
19 | - Chapter 46 Remote Method Invocation
20 | - Chapter 47 2-4 Trees and B-Trees
21 | - Chapter 48 Red-Black Trees
22 | - Chapter 49 Java 2D
23 | - Chapter 50 Testing Using JUnit
--------------------------------------------------------------------------------
/Chapter1/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Chapter1/.DS_Store
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.core.resources/.history/d5/2082ab030c030014105fc5f4c212048e:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | public class GuessNumberFor {
3 | public static void main(String[] args) {
4 | int number = (int)(Math.random() * 100); //0 - 100
5 | Scanner input = new Scanner(System.in);
6 | System.out.println("Try to guess the number between 0 and 100");
7 |
8 | for (int guess = -1; guess != number; guess = input.nextInt()){
9 | if (guess > number){
10 | System.out.println("Your guess is too high");
11 | }
12 | else if (guess >= 0){
13 | System.out.println("Your guess is too low");
14 | }
15 | System.out.println("\nEnter your guess: ");
16 | }
17 | System.out.println("Correct, the number is: " + number);
18 |
19 | }
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/BinaryQuiz.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/ComplexExpressions.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/EscapeCharacterTest.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise2_20.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise2_21.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise2_25.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise3_16.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise3_30.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise3_35.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise3_9.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise4_19.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise4_25.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise4_26.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise4_37.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise4_47.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/Exercise5_17.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/PrintPrimes.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.debug.core/.launches/ShortForLoop.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter1/.metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Chapter1/CH1Readme.md:
--------------------------------------------------------------------------------
1 | You may be wondering why I'm putting this much effort into reviewing a textbook in which most of the coding problems have solutions provided.
2 |
3 | That is a good question.
4 |
5 | #####Do I think I am more knowledgeable or more talented than the author? Speaking stricly about Java programming, the answer is no. However, I do believe I can be a more effective teacher. I am approaching this book as a self-proclaimed intermediate to accomplish several goals:
6 |
7 | 1. Gain additional experience with Java
8 | 2. Gain experience with Git
9 | 3. Provide a resource for students (especially students I tutor)
10 | 4. Create a library of effective and resuable code
11 |
12 | This is my first attempt at what I hope will turn out to be a long series of educational repositories that also encourage collaboration.
13 |
14 | If you don't own the book- You should be able to follow along to a degree if this is your first time. If you are somewhat proficient with Java, all of the code provided will also provide the question and assorted notes.
15 |
16 | There are a few things I don't like about this book. None are incredibly obvious to the absolute beginner. One of the things that I like the least is that the author wants you to use the command line. I'm not sure why a beginner would start with that rather than an Integrated Development Environment.
17 |
18 | I suggested IntelliJ in the main readme, though Eclipse is also very popular and has less of a learning curve. They are both free and are both powerful. You can program in the terminal environment using vim, emacs, or nano, but that isn't a great way to start learning. I will primarily use IntelliJ though may switch to other editors on occasion.
19 |
20 | As time goes on, you will find that compiling and executing your programs from the terminal has a distinct set of benefits. Enough of that, let me give a quick summary of Chapter 1.
21 |
22 | Before you even open the book, you'll want to have Java installed on your computer and your IDE ready to go.
23 |
24 | ##Chapter 1 is an introductory chapter, and most college intro courses will skip it completely. Read through. Here's all you need to worry about until later in your programming journey:
25 |
26 | 1. Bits, bytes, storage, and memory optimization used to be important to programmers. The situation isn't nearly as life-or-death as it once was, though we strive for efficiency and to maximize readability.
27 |
28 | 2. What the hell are those 1's and 0's? Binary digits: on/off, yes/no, true/false; binary numbers are the foundation of modern (and, ultimately, all) computing, but they don't always come into play in an intro class.
29 |
30 | 3. Programming languages: machine language is binary code, assembly language consists of somewhat understandable basic commands that then are translated to binary, and high-level languages are closest to human language but then is intrepreted and compiled into machine code.
31 |
32 | 4. Java is a high-level language. It is easy to read and easy to write, especially as you
33 | spend more time working with it. Other popular languages used in colleges are C++ and Python for classes of similar scope, Javascript and PHP for classes focusing on web development, and C for classes based around low-level applications such as hardware interaction.
34 |
35 | 5. Should I learn as many programming languages as possible in order to get a job? No. Lucky for you, Java is extremely popular. It makes a great first language. The ideal first language, in my opinion, is C, but it is significantly more frustrating for a beginner to learn. Most colleges will start off with C++ or Java, or in rare cases, Python. Most employers are just looking for a talented programmer who fits the job description. You aren't wasting your time learning Java.
36 |
37 | 6. The API defines the library of Java classes and interfaces. I know the layout of the library doesn't make things fun, but it gets easier once you use it more. You will be using it quite a bit.
38 |
39 | 7. Let's get coding.
40 |
41 | ```
42 | public class Chapter1 {
43 | public static void main(String[] args) {
44 | System.out.println("Hello, World!");
45 | System.out.println("This is my first Java Program");
46 | //A comment isn't interpreted by the compiler. It is for human eyes only and begins with two slashes
47 | }
48 | }
49 | ```
50 | Java has the unique distinction of having one of the most cumbersome structures for simple programs. Man, does this confuse people. Look through and try to determine what the program is doing. You'll be able to run it soon.
51 |
52 | You save your program as a .java file. When it is compiled, it becomes a .class file, which is then able to be executed. The textbook actually suggests using notepad to create and modify sourcecode. You can close your IDE and try that, but you'll quickly see that it's a bad idea and a misguided suggestion for beginners.
53 |
54 | If you've eliminated all syntax errors, you should be able to compile and run your code to see the two printed lines. Or perhaps one line, perhaps seven, depending on what exactly you typed in. No need to go crazy here, but exploring is probably the best way to become a great programmer. Along within practicing. So do it.
55 |
56 | Why is there a section on JOptionPane? Nobody really knows. You can fool around with it or just ignore it. You'll barely use it at all in this book and it doesn't make any sense to have in the first chapter.
57 |
58 | #Style is importation
59 | I included a link to Google Style. You should strive for that. Bad style sucks for everyone involved. I see it all the time when grading programs. Commenting is useful, but commenting every line is excessive- trust me. Your style will improve with time. I recommend taking a look at style guides at regular intervals depending on how frequently you're programming. I don't mean every five minutes- perhaps once a week or something along those lines until you internalize it.
60 |
61 | ###Errors suck, Uncaught errors suck even more
62 | Errors can exist at run time as well as compile time. You'll learn what common errors to detect and other interesting uses for the features built around catching errors. Every good program handles errors. Your professors will run input against your programs in an attempt to break them. Don't be that kid that gets ten points off every assignment because of uncaught errors. The only reason they are covered in the intro chapter is because it's quite easy to encounter them, especially compiler errors.
63 |
64 | //http://upload.wikimedia.org/wikipedia/en/d/d3/Tiobe_index.png
65 |
66 |
--------------------------------------------------------------------------------
/Chapter1/Chapter1/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Chapter1/Chapter1/.DS_Store
--------------------------------------------------------------------------------
/Chapter1/Chapter1/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Chapter1/Chapter1/.gitignore:
--------------------------------------------------------------------------------
1 | /Chapter1Exercises.class
2 |
--------------------------------------------------------------------------------
/Chapter1/Chapter1/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Chapter1
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Chapter1/Chapter1/Chapter1Exercises.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Chapter1/Chapter1/Chapter1Exercises.class
--------------------------------------------------------------------------------
/Chapter1/Chapter1/Chapter1Exercises.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 1.7 Approximate pi
3 | * This will make more sense after Chapter 2
4 | */
5 | public class Chapter1Exercises {
6 | public static void main(String[] args){
7 | System.out.println(4 * (1.0 - 1.0/3 + 1.0/5 - 1.0/7 +
8 | 1.0/9 - 1.0/11 + 1.0/13));
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Chapter1/Chapter3/.gitignore:
--------------------------------------------------------------------------------
1 | /bin
2 |
--------------------------------------------------------------------------------
/Chapter2/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Chapter2/.gitignore:
--------------------------------------------------------------------------------
1 | /bin
2 |
--------------------------------------------------------------------------------
/Chapter2/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Chapter2
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Chapter2/Readme.md:
--------------------------------------------------------------------------------
1 | Chapters 2 through 4 are extremely important to form a basic understanding of the language. The book does a good job of explaining these concepts. My notes for these chapters should be treated as a supplement.
2 |
3 | For many programming assignments you will receive, reading input from the console is very important. You may be thinking that none of the programs you use on a daily basis take input in this manner. As programs become more advanced, they take input input in a variety of different and more effective ways. This includes command line args, reading from files, communicating with other programs, or much more aesthetically pleasing GUI textboxes and buttons, which can be seen all over your web browser.
4 |
5 | Let's look at the supplied program that computes an average:
6 |
7 | ```
8 | import java.util.Scanner;
9 | public class ComputeAVerage {
10 | public static void main(String[] args) {
11 | Scanner input = new Scanner(System.in);
12 |
13 | System.out.println("Enter three numbers: ");
14 | double number1 = input.nextDouble();
15 | double number2 = input.nextDouble();
16 | double number3 = input.nextDouble();
17 |
18 | double average = (number1 + number2 + number3) / 3;
19 |
20 | System.out.println("Average is: " + average);
21 | }
22 | }
23 | ```
24 |
25 | There's your first program. You still have work to do. Depending on the input format, this program could be written in even fewer lines with even more powerful and more easily modified tools.
26 |
27 | ##Identifiers
28 | My first quiz in my Intro to Java class was entirely on identifiers. We had to determine which were valid or not. It was kind of cruel, but I only got one question wrong.
29 |
30 | -identifiers can only start with letters, underscores, or dollar signs. The dollar sign is what I got wrong.
31 | -identifiers can't be reserved words (like int, double, class, static) or true/false/null
32 | -identifiers can only consist of letters, numbers, underscores, and dollar signs. Every other character is used by Java.
33 |
34 | ##Naming Conventions
35 | camelcase is standard and is a key part of formatting. Spaces screw up the parsing of most language, so programmers tend to use underscores (for limited applications) or, most commonly, no space at all. The way that you can best represent breaks between words is by using diferent cases. Looking at the phrase thebigbluehouse is confusing, so programmers would write it as TheBigBlueHouse or theBigBlueHouse. Capitalizing the first letter of the first word is used for class names, while keeping the first word all lowercase is used for pretty much everything else. You name a constant like THE_BIG_BLUE_HOUSE. These standards persist, in some way, throughout all programming languages. You will pick up naming conventions rather quickly as you read and write more code.
36 |
37 | ##Operators
38 | **+ - * / %**
39 | % is the modulus. It returns a remainder z when x is divided by y. So, while 20 / 7 = 2 (as a truncated integer), 20 % 7 = 6.
40 | If you are a university student you will have plenty of assignments involving this. Believe it or not, it is really useful and has a wide range of applications.
41 | Note that the caret symbol is not present. It isn't the power operator. Finding the power or square root isn't as simple as using a symbolic operator, the Math package provides all of that and more.
42 |
43 | You can also use operators to augment assignment to variables, changing i = i + 2 to i += 2 yields the same result, and is common practice.
44 |
45 | ##Operator Precedence
46 | PEMDAS still applies, with two caveats:
47 | 1. There is no exponent operator symbol (so it loses its high precedence)
48 | 2. Expressions are evaluated from left to right for symbols of the same precedence
49 |
50 | ##Increments and Decrements
51 | i++ increments i after i is evaluated
52 | ++i increments i before i is evaluated
53 | i-- and --i are used the same way
54 |
55 | ###Software Development Lifecycle
56 | This is an interesting topic to include in Chapter 2 of an Intro book. The fact of the matter is that you aren't going into the industry tomorrow, and though you'll have to learn eventually, now, regardless of what stage of learning you're in, you want to develop programs in a way that is most conducive to your learning and immediate success.
57 |
58 | ##Escape Characters
59 | How do you quote something already in quotes?
60 | ```
61 | System.out.println(""Mayonnaise colored Benz, I push Miracle Whips" -Kanye West");
62 | ```
63 | *Doesn't work*
64 |
65 | Using single quotes or some other contrived solution is not the answer. Just use an escape character, which is any character preceded by a backslash \.
66 |
67 | Let's try this again.
68 | ```
69 | System.out.println("\"And what you plannin' for this summer, I did it prior\" -Drake");
70 | ```
71 | That works. Let's try it with the most popular escape character in existence- the newline character: \n
72 |
73 | ```
74 | System.out.println("\"I'm in your house, got the key to your home\n"+
75 | "I am Jeopardy asked, Wikipedia known\n"+
76 | "I am A.A. driven and Expedia flown\n"+
77 | "So stop jackin\' my style\n"+
78 | "You\'re in need of ya own, man\"\n"+
79 | "-Drake");
80 | ```
81 |
82 | ##Strings
83 | Strings are a fascinating part of Java. The book explains them sufficiently, but it is worthwhile to understand many of the built-in string manipulaiton methods. CodingBat (a website linked in the readme) has great challenges that will help you increase your knowledge.
84 |
--------------------------------------------------------------------------------
/Chapter2/src/EscapeCharacterTest.java:
--------------------------------------------------------------------------------
1 |
2 | public class EscapeCharacterTest {
3 | public static void main(String[] args){
4 | System.out.println("\"I'm in your house, got the key to your home\n"+
5 | "I am Jeopardy asked, Wikipedia known\n"+
6 | "I am A.A. driven and Expedia flown\n"+
7 | "So stop jackin\' my style\n"+
8 | "You\'re in need of ya own, man\"\n"+
9 | "-Drake");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Chapter2/src/Exercise2_20.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Financial application: calculate interest of next monthly payment
3 | * interest = balance * (annualRate / 1200)
4 | */
5 | import java.util.Scanner;
6 | public class Exercise2_20 {
7 | public static void main(String[] args){
8 | Scanner input = new Scanner(System.in);
9 | System.out.println("Enter Balance and interest rate: ");
10 | double balance = input.nextDouble();
11 | double interestRate = input.nextDouble();
12 |
13 | double interest = balance * (interestRate / 1200);
14 |
15 | System.out.println("The interest is: " + interest);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter2/src/Exercise2_21.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Financial application: Calculate future investment value
3 | * This is a good demonstration of the pow function
4 | * FV = PV * (1 + i)^t
5 | * Note that i is monthly rate, so you must divide by 12
6 | */
7 | import java.util.Scanner;
8 | public class Exercise2_21 {
9 | public static void main(String[] args){
10 | Scanner input = new Scanner(System.in);
11 | //notice that you don't need a new scanner object each time you prompt for input
12 | System.out.print("Enter investment amount: ");
13 | double investmentAmount = input.nextDouble();
14 | System.out.print("Enter annual interest rate as a percentage: ");
15 | double interestRate = input.nextDouble()/100;
16 | System.out.print("Enter number of years: ");
17 | int years = input.nextInt();
18 |
19 | double accumulatedValue = investmentAmount * Math.pow((1.0 + interestRate/12), (years*12));
20 |
21 | System.out.println("Accumulated value is: " + accumulatedValue);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Chapter2/src/Exercise2_25.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Financial application: Payroll
3 | * write a program that reads the following information and prints a payroll statement
4 | * employee's name, number of hours worked per week, hourly pay rate, federal tax withholding rate
5 | * state tax withholding rate
6 | */
7 | import java.util.Scanner;
8 | //It is a best practice to import only what you need and avoid use of wildcard operators
9 | public class Exercise2_25 {
10 | public static void main(String args[]) {
11 | Scanner input = new Scanner(System.in);
12 | System.out.print("Enter Employee's name:");
13 | String name = input.next();
14 | System.out.print("Enter number of hours worked:");
15 | double hoursWorked = input.nextDouble();
16 | System.out.print("Enter hourly pay rate:");
17 | double hourlyRate = input.nextDouble();
18 | System.out.print("Enter federal tax withholding rate:");
19 | double fedTaxWithholdingRate = input.nextDouble();
20 | System.out.print("Enter state tax withholding rate:");
21 | double stateTaxWithholdingRate = input.nextDouble();
22 |
23 | //note the usefulness of setting variables here
24 | double grossPay = hourlyRate * hoursWorked;
25 | double fedWithholding = grossPay * fedTaxWithholdingRate;
26 | double stateWithholding = grossPay * stateTaxWithholdingRate;
27 | double totalDeduction = fedWithholding + stateWithholding;
28 | double netPay = grossPay - totalDeduction;
29 |
30 | System.out.println("Employee name: " + name);
31 | System.out.println("Hours Worked: " + hoursWorked);
32 | System.out.println("Pay Rate: " + hourlyRate);
33 | System.out.println("Gross pay: " + grossPay);
34 | System.out.println("Deductions: ");
35 | System.out.println("\tFed Withholding (" + fedTaxWithholdingRate*100 +
36 | "): " + fedWithholding);
37 | System.out.println("\tState Withholding (" + stateTaxWithholdingRate*100 +
38 | "): " + stateWithholding);
39 | System.out.println("\tTotal Deduction: " + totalDeduction);
40 | System.out.println("Net Pay: " + netPay);
41 |
42 |
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/Chapter3/Readme.md:
--------------------------------------------------------------------------------
1 | #Selections
2 |
3 | ```
4 | if (false){
5 | System.out.println("This chapter isn't important");
6 | }
7 | else{
8 | System.out.println("This chapter is important");
9 | }
10 | ```
11 | Choose which statement you think gets evaluated.
12 |
13 | ##Booleans are big
14 | Remember those binary numbers we talked about in Chapter 1? It's a bit fuzzy to me, too. All you have to remember that 1 is true and 0 is false. It's one of the central pillars of computer science. In Java, there is a data type called Boolean that evaluates to either true or false, nothing else.
15 |
16 | Here is how you declare a boolean:
17 | ```
18 | boolean thisIsTrue = true;
19 | boolean thisIsFalse = false
20 | ```
21 | That might not seem too useful, but that is only the most basic declaration. As the chapter goes on, things will get more complicated and the power of logic will become clear to you.
22 |
23 | ##If, Else
24 | ```
25 | //... code excerpt ...
26 | double totalAmountRemaining = input.nextDouble();
27 | if (totalAmount < 0){
28 | System.out.println("Amount cannot be less than 0!");
29 | }
30 | else{
31 | System.out.println("The total amount remaining is: " + totalAmountRemaining);
32 | if (true){
33 | System.out.println("This is called nesting");
34 | }
35 | }
36 | ```
37 | This barely scratches the surface of the usefulness of if-else statements. I will do many of the practice problems.
38 |
39 | ##Random Number Generation
40 | ```
41 | int randomNumber = (int)(Math.random() * 10);
42 | //generates a number between 0 and 9
43 | ```
44 |
45 | ##Logical operators
46 | ```
47 | ! not
48 | && and
49 | || or
50 | ^ exclusive or
51 | ```
52 | Now things start to heat up. Suddenly, truth tables are abound. This book isn't anywhere near as thorough with truth tables as some are. They are a great tool for evaluating complex logical expressions. If you ever take a discrete math class, you'll be shocked at how prevalent they are.
53 |
54 | ```
55 | boolean x = true;
56 | boolean y = false;
57 | if (((x && y) ^ ((y ^ x) || !y)) && (y ^ !y) && (x ^ !x || !y || !(x^x))){
58 | System.out.println("Yes, this is a valid expression (it's true!)");
59 | }
60 | ```
61 |
62 | ###Printf
63 | A slightly less intuitive yet significantly more powerful statement to print (formatted) output
64 |
--------------------------------------------------------------------------------
/Chapter3/src/ComplexExpressions.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Try to evaluate these complex logic expressions
3 | * don't worry about the code
4 | * run the program and focus on evaluating the expressions
5 | * These are pretty hard and unrealistic.
6 | */
7 | import java.util.Scanner;
8 | public class ComplexExpressions {
9 | public static void main(String args[]){
10 | Scanner input = new Scanner(System.in);
11 | System.out.println("Enter \"true\" if true, \"false\" if false");
12 | String expr1 = "(!(true && false) && (!false ^ (false ^ !false))";
13 | boolean expressionOne = Boolean.parseBoolean(expr1);
14 | System.out.println(expr1);
15 | System.out.print("Answer: ");
16 | boolean answer = Boolean.parseBoolean(input.next());
17 |
18 | if (answer == expressionOne){
19 | System.out.println("Correct!\n");
20 | String expr2 = "true && !(true ^ false || true) && !false ^ false && (!(false == true)";
21 | boolean expressionTwo = Boolean.parseBoolean(expr2);
22 | System.out.println(expr2);
23 | System.out.print("Answer: ");
24 | answer = Boolean.parseBoolean(input.next());
25 |
26 | if (answer == expressionTwo){
27 | System.out.println("Correct!\n");
28 | String expr3 = "(true) ^ (!(false || false) && (!true ^ (false != (true ^ (!(false ^ true != (false && true) != true && false ))) && true)))";
29 | boolean expressionThree = Boolean.parseBoolean(expr3);
30 | System.out.println(expr3);
31 | System.out.print("Answer: ");
32 | answer = Boolean.parseBoolean(input.next());
33 | if (answer == expressionThree){
34 | System.out.println("You got everything correct!");
35 | }
36 | else{
37 | System.out.println("Wrong! So close!");
38 | }
39 | }
40 | else{
41 | System.out.println("You got the question wrong!");
42 | }
43 | }
44 | else{
45 | System.out.println("You got the question wrong!");
46 | }
47 |
48 |
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Chapter3/src/Exercise3_16.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Random character: write a program that displays a random uppercase character
3 | * Capital letters start at 65. Let's explore a few ways to approach this
4 | */
5 | public class Exercise3_16 {
6 | public static void main(String[] args){
7 | char randomCapital = (char)(65 + Math.random()*26);
8 | System.out.println(randomCapital);
9 | }
10 | }
11 |
12 |
--------------------------------------------------------------------------------
/Chapter3/src/Exercise3_30.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Current time. Display hour using a 12-hour clock
3 | */
4 | public class Exercise3_30 {
5 | public static void main(String[] args){
6 | long totalMilliseconds = System.currentTimeMillis();
7 | long totalSeconds = totalMilliseconds / 1000;
8 | long currentSecond = totalSeconds % 60;
9 | long totalMinutes = totalSeconds / 60;
10 | long currentMinute = totalMinutes % 60;
11 | long totalHours = totalMinutes / 60;
12 | long currentHour = totalHours % 24;
13 |
14 | String amOrPM = "AM";
15 |
16 | if (currentHour > 12){
17 | currentHour -= 12;
18 | amOrPM = "PM";
19 | }
20 | System.out.println(currentHour + ":" + currentMinute + ":" + currentSecond + " " + amOrPM + " GMT");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Chapter3/src/Exercise3_35.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Decimal to hex
3 | * Write a program that prompts the user to enter an integer between 0 and 15 and displays
4 | * its corresponding hex number.
5 | * Example: 11 -> B, 5 -> 5, 31 ?
6 | */
7 | import java.util.Scanner;
8 | public class Exercise3_35 {
9 | public static void main(String[] main){
10 | Scanner input = new Scanner(System.in);
11 | System.out.println("Enter a number between 0 and 15: ");
12 | int inputNumber = input.nextInt();
13 |
14 | if (inputNumber >= 0 && inputNumber <= 15){
15 | if (inputNumber >= 10){
16 | char outputChar = (char)(65 + inputNumber - 10);
17 | System.out.println("Hex value is: " + outputChar);
18 | }
19 | else{
20 | System.out.println("Hex value is: " + inputNumber);
21 | }
22 | }
23 | else{
24 | System.out.println("Error, must enter a number between 0 and 15");
25 | }
26 | char test = (char)65;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Chapter3/src/Exercise3_9.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Business: check ISBN-10
3 | * Last digit of an ISBN is a checksum, calculated from the other nine:
4 | * (d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11
5 | * There are a variety of ways to approach this (the wonders of programming)
6 | */
7 | import java.util.Scanner;
8 | public class Exercise3_9 {
9 | public static void main(String[] args){
10 | Scanner input = new Scanner(System.in);
11 | /*
12 | * book instructs you to take input as an int while preserving leading zeros
13 | * This is impossible to do without using loops or a lot of if statements for
14 | * input, especially when accounting for numbers such as 000000013. By my way of
15 | * thinking, the examples given in the book are impossible to achieve given
16 | * the constraints. Sure, we can handle leading zeros, but there is no way to tell
17 | * if 9 digits were actually entered without splitting up the statements (going digit
18 | * by digit) or by using strings. Let's assume no leading zeros to simplify things.
19 | */
20 | System.out.println("Enter 9 digit ISBN with no leading zero(s):");
21 | int nineDigitISBN = input.nextInt();
22 | if (nineDigitISBN >= 100000000){
23 | int nineDigitISBNTemp = nineDigitISBN;
24 | int checksum = 0;
25 | int digitNine = nineDigitISBNTemp % 10;
26 | nineDigitISBNTemp /= 10;
27 | int digitEight = nineDigitISBNTemp % 10;
28 | nineDigitISBNTemp /= 10;
29 | int digitSeven = nineDigitISBNTemp % 10;
30 | nineDigitISBNTemp /= 10;
31 | int digitSix = nineDigitISBNTemp % 10;
32 | nineDigitISBNTemp /= 10;
33 | int digitFive = nineDigitISBNTemp % 10;
34 | nineDigitISBNTemp /= 10;
35 | int digitFour = nineDigitISBNTemp % 10;
36 | nineDigitISBNTemp /= 10;
37 | int digitThree = nineDigitISBNTemp % 10;
38 | nineDigitISBNTemp /= 10;
39 | int digitTwo = nineDigitISBNTemp % 10;
40 | nineDigitISBNTemp /= 10;
41 | int digitOne = nineDigitISBNTemp % 10;
42 | nineDigitISBNTemp /= 10;
43 |
44 | checksum = (digitOne + 2*digitTwo + 3*digitThree + 4*digitFour +
45 | 5*digitFive + 6*digitSix + 7*digitSeven + 8*digitEight +
46 | 9*digitNine) % 11;
47 |
48 | if (checksum == 10){
49 | System.out.println("ISBN is: " + nineDigitISBN + "X");
50 | }
51 | else{
52 | System.out.println("ISBN is: " + nineDigitISBN + checksum);
53 | }
54 | }
55 | else {
56 | System.out.println("You didn't ender a nine digit integer");
57 | }
58 |
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/Chapter3/src/complexExpressionExample.java:
--------------------------------------------------------------------------------
1 |
2 | public class complexExpressionExample{
3 | public static void main(String args[]){
4 | boolean x = true;
5 | boolean y = false;
6 | if (((x && y) ^ ((y ^ x) || !y)) && (y ^ !y) && (x ^ !x || !y || !(x^x))){
7 | System.out.println("Yes, this is a valid expression (it's true!)");
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Chapter4/Readme.md:
--------------------------------------------------------------------------------
1 | You might have noticed that some of the programs in the last chapter got a bit long. For example, if you were to prompt a user for each ISBN digit separately, that would result in a lot of code that performs the same basic operation.
2 |
3 | Learning how loops work will open up the world of programming to you by simplifying redundant operations. You can harness all of the information from chapters 2 to 4 to write decently powerful programs. The code wouldn't be pretty, and would still be packed with redundance. What I'm trying to say is that loops are extremely important, and, unlike boolean expressions, are a bit more confusing. Spend a lot of time reviewing this chapter.
4 |
5 | ##While
6 |
7 | ```
8 | int count = 0;
9 | while (count < 10){
10 | System.out.println("Count is: " + count);
11 | count++;
12 | }
13 | ```
14 |
15 | If you don't understand what this does at first, that is fine. When you run the code, it will result in the following:
16 | ```
17 | Count is: 0
18 | Count is: 1
19 | Count is: 2
20 | Count is: 3
21 | Count is: 4
22 | Count is: 5
23 | Count is: 6
24 | Count is: 7
25 | Count is: 8
26 | Count is: 9
27 | ```
28 | count < 10 is a boolean condition just like you learned in the last chapter. So, *while* the condition is true, the loop body will be executed. What stops a loop from running? Good question. Typically, a variable will be incremented until it causes the condition to no longer be true, at which point the loop will stop running.
29 |
30 | It is common to initialize a loop variable with the number zero, as if you are working with arrays, the first index can be accessed with 0 (though I'm sure there are other applications).
31 |
32 | ####Listing 4.3 GuessNumber.Java
33 | ```
34 | import java.util.Scanner;
35 |
36 | public class GuessNumber {
37 | public static void main(String[] args) {
38 | int number = (int)(Math.random() * 100); //0 - 100
39 | Scanner input = new Scanner(System.in);
40 | System.out.println("Try to guess the number between 0 and 100");
41 |
42 | int guess = -1; //initializing with a number not in the range makes sense.
43 | while (guess != number){
44 | //the loop will iterate (that's geek speak for run once) until the number is guessed correctly
45 | System.out.println("\nEnter your guess: ");
46 | guess = input.nextInt();
47 |
48 | if (guess == number){
49 | System.out.println("Correct, the number is: " + number);
50 | }
51 | else if (guess > number){
52 | System.out.println("Your guess is too high");
53 | }
54 | else{
55 | System.out.println("Your guess is too low");
56 |
57 | }
58 | }
59 |
60 | }
61 | }
62 | ```
63 | Above is a great program for putting together what you've learned so far.
64 |
65 | ##Do-While
66 |
67 | Do while is like a while except it will always run the loop body once. You may be surprised to learn that there are an awful lot of applications of this loop. Computer Science professors have a talent for finding examples where it makes sense to use the loop. It's certainly worth your time to learn how these loops work.
68 |
69 | #For
70 |
71 | For loops provide the most concise syntax for writing loops. They may be a bit intimidating, but are, by far, the most widely used loops in Java. I recommend practicing with While loops for some time before moving to for loops.
72 |
73 | Here is a for loop header:
74 | ```
75 | for (int i = 0; i < 10; i++){
76 | //Body
77 | }
78 | ```
79 | It is (functionally) equivalent to this while loop:
80 | ```
81 | int i = 0;
82 | while (i < 10){
83 | //body
84 | i++;
85 | }
86 |
87 | ```
88 | You will notice that I most often use the letter 'i' in my loops. As I nest my loops, which is exactly like it sounds, I will descend down the alphabet, going to 'j', 'k', 'l', 'm', and so on. I don' think I've ever gotten past 'm' in a practical example.
89 |
90 | I'm going to jump right into some more advanced for loop examples. Here is my attempt at converting GuessNumber's while loop to a for loop. It works perfectly, though the loop header is ugly and isn't something you'd typically see in practice.
91 | ```
92 | import java.util.Scanner;
93 | public class GuessNumberFor {
94 | public static void main(String[] args) {
95 | int number = (int)(Math.random() * 100); //0 - 100
96 | Scanner input = new Scanner(System.in);
97 | System.out.println("Try to guess the number between 0 and 100");
98 |
99 | for (int guess = -1; guess != number; guess = input.nextInt()){
100 | if (guess > number){
101 | System.out.println("Your guess is too high");
102 | }
103 | else if (guess >= 0){
104 | System.out.println("Your guess is too low");
105 | }
106 | System.out.println("\nEnter your guess: ");
107 | }
108 | System.out.println("Correct, the number is: " + number);
109 |
110 | }
111 | }
112 | ```
113 | Remember binary numbers? They represent 2 to the *nth* power. Let's make a binary quiz game next.
114 | ```
115 | import java.util.Scanner;
116 | public class BinaryQuiz {
117 | public static void main(String[] args){
118 | Scanner input = new Scanner(System.in);
119 | System.out.println("Welcome to the Binary Quiz");
120 | System.out.println("Try to make it to 2^31!");
121 |
122 | for (int i = 0; i < 32; i++){ //this will iterate 32 times
123 | System.out.println("What is 2^" + i + "?");
124 | int answer = input.nextInt();
125 | if (answer == Math.pow(2, i)){
126 | System.out.println(answer + " is correct!");
127 | }
128 | else{
129 | System.out.println("Incorrect!");
130 | i--;
131 | /*
132 | * See what happens here?
133 | * On the next iteration, i will be the same as it was on this iteration.
134 | * No skipping questions!
135 | */
136 | }
137 | if (i >= 16){ //delete this if you want to play all the way
138 | System.out.println("Just kidding, we'll stop there.");
139 | i = 31; //this will terminate the loop
140 | //there is another way to terminate the loop, it's called break.
141 | }
142 | }
143 | System.out.println("You won!");
144 |
145 | }
146 | }
147 | ```
148 | It would have been cruel to go to 2147483648. Next time you're sitting in class, try to work your way up to 2^31. It keeps your mind sharp. Actually, maybe you should focus on the lecture.
149 |
150 | This next one is easy (though the code is complex), you can just enter a number n and watch it return all prime numbers up to and including that one. This is an extremely popular assignment early in intro classes.
151 | ```
152 | /* This is an advanced program that finds prime numbers up to the nth prime
153 | * There are many, many ways to approach this problem
154 | * Though this is not the most efficient implementation, it is far from worst case
155 | */
156 | import java.util.Scanner;
157 | public class PrintPrimes {
158 | public static void main(String[] args) {
159 | Scanner input = new Scanner(System.in);
160 | System.out.println("Which nth prime number would you like to print up to?");
161 | int numberPrime = input.nextInt(); //this is how many primes will be printed
162 | for (int i = 1, number = 1; i <= numberPrime; number++) {
163 | //this loop initializes two variables and increments one.
164 | //"i" is the number prime, "number" is the current number
165 | boolean isPrime = true;
166 | //this can be done more efficiently with a method, we'll get there soon
167 | for (int j = 2; j * j <= number; j++){
168 | //factors are never greater than the sqrt of the number
169 | //this adds serious efficiency
170 | if (number % j == 0 && j != number){ //if a number is evenly divided by anything other than
171 | isPrime = false; //the number isn't prime
172 | }
173 | }
174 | if (isPrime) {
175 | System.out.println("Prime number " + i + " is " + number);
176 | i++; //increments i now that a prime has been found
177 | }
178 | }
179 | }
180 | }
181 | ```
182 |
183 | Loop evaluation is something you will be frequently tested on, mainly because being able to create and evaluate loops efficiently is huge part of being a good programmer. It is also probably the most useful skill to possess going into the AP Computer Science exam.
184 |
185 | Here's an interesting (and, by all means, ridiculous) for loop that I wrote
186 | ```
187 | for (int i = 0; i++ < 1; System.out.println("This works, but is laughably contrived"));
188 | ```
--------------------------------------------------------------------------------
/Chapter4/src/BinaryQuiz.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Here is an example of a for loop that I came up with
3 | * Once you understand it, can you think of better approaches?
4 | * Do you know your powers of 2?
5 | */
6 | import java.util.Scanner;
7 | public class BinaryQuiz {
8 | public static void main(String[] args){
9 | Scanner input = new Scanner(System.in);
10 | System.out.println("Welcome to the Binary Quiz");
11 | System.out.println("Try to make it to 2^31!");
12 |
13 | for (int i = 0; i < 32; i++){ //this will iterate 32 times
14 | System.out.println("What is 2^" + i + "?");
15 | int answer = input.nextInt();
16 | if (answer == Math.pow(2, i)){
17 | System.out.println(answer + " is correct!");
18 | }
19 | else{
20 | System.out.println("Incorrect!");
21 | i--;
22 | /*
23 | * See what happens here?
24 | * On the next iteration, i will be the same as it was on this iteration.
25 | * No skipping questions!
26 | */
27 | }
28 | if (i >= 16){ //delete this if you want to play all the way
29 | System.out.println("Just kidding, we'll stop there.");
30 | i = 31; //this will terminate the loop
31 | //there is another way to terminate the loop, it's called break.
32 | }
33 | }
34 | System.out.println("You won!");
35 |
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Chapter4/src/Exercise4_19.java:
--------------------------------------------------------------------------------
1 | /* This is NOT Pascal's triangle
2 | * Display numbers in a pyramid pattern
3 | * Write a nested for loop that prints the following output...
4 | * Page 169
5 | *
6 | * This problem requires careful planning before you approach it
7 | * Go row by row, number by number, look for the obvious patterns
8 | * I'm going to use arrays, my solution is far from perfect but works
9 | *
10 | * If you aren't comfortable enough yet using printf statements,
11 | * just use print statements and try to get spacing as close as possible
12 | * The code to get it to look perfect using print statements gets outrageous
13 | *
14 | * This took me much longer than I'd like to admit
15 | */
16 | public class Exercise4_19 {
17 | public static void main(String args[]){
18 | for (int i = 0; i < 9; i++){
19 | for (int l = 8 - i; l > 0; l--){
20 | System.out.print(" "); //roughly 3 per line besides the last line.
21 | }
22 | for (int j = 0; j < i; j++){
23 | System.out.printf("%3d ", (int)Math.pow(2,j));
24 | }
25 | for (int k = i; k > 1; k--){
26 | System.out.printf("%3d ", (int)Math.pow(2,k-2));
27 | }
28 | System.out.println();
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Chapter4/src/Exercise4_25.java:
--------------------------------------------------------------------------------
1 | /*(Compute pi)
2 | *
3 | * Approximate pi (again, now using a loop!)
4 | * Write a program that displays the pi value for i - 10000 20000 and 100000
5 | * Remember the formula?
6 | *
7 | * pi = 4(1 +(-1)^(i+1)/(2i-1))
8 | * We're going to switch it up a little.
9 | */
10 | public class Exercise4_25 {
11 | public static void main(String[] args){
12 | final double PI = Math.PI;
13 | int numTo = 10000000; //ten million
14 | double piApproximate = 1;
15 |
16 | for (int i = 2; i < numTo; i++){
17 | piApproximate += Math.pow(-1, i + 1) / ( 2* i - 1);
18 | }
19 | piApproximate *= 4;
20 |
21 | System.out.println("Pi's value is: " + PI);
22 | System.out.println("You approximated to be: " + piApproximate);
23 | double difference = PI - piApproximate;
24 | System.out.println("The difference is: " + difference);
25 | //That's small!
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter4/src/Exercise4_26.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Approximate e
3 | * e = 1 + 1/1! + 1/2! + 1/i!
4 | * This can be done more efficiently if the book's hint is followed
5 | */
6 | public class Exercise4_26 {
7 | public static void main(String args[]){
8 | int numTo = 100000;
9 | final double E = Math.E;
10 | double eApproximate = 1;
11 |
12 | for (int i = 1; i < numTo; i++){
13 | double factorial = 1;
14 | for (int j = i; j >= 1; j--){ //this is for calculating the factorial
15 | factorial *= j;
16 | }
17 | eApproximate += 1 / factorial;
18 | }
19 | System.out.println("The value of E is: " + E);
20 | System.out.println("e was approximated as: " + eApproximate);
21 | double difference = E - eApproximate;
22 | System.out.println("The difference is: " + difference);
23 | //that's small!
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Chapter4/src/Exercise4_37.java:
--------------------------------------------------------------------------------
1 | /* Decimal to binary
2 | * Convert a given decimal number (base 10) to a binary number
3 | *
4 | * This is a great exercise to test your understanding
5 | * There are many, many, ways to approach this problem,
6 | * just don't use Integer.toBinaryString(int). That's cheating!!!
7 | */
8 | import java.util.Scanner;
9 | public class Exercise4_37 {
10 | public static void main(String[] args){
11 | Scanner input = new Scanner(System.in);
12 | System.out.println("Enter an integer to be converted to binary:");
13 | int numberToConvert = input.nextInt();
14 | String binaryNumber = "";
15 |
16 | while (numberToConvert >= 1){
17 | if (numberToConvert % 2 == 1){ //this is a conversion shortcut
18 | //
19 | binaryNumber = "1" + binaryNumber;
20 | }
21 | else {
22 | binaryNumber = "0" + binaryNumber;
23 | }
24 | numberToConvert /= 2;
25 | }
26 |
27 | System.out.println("Binary number is: " + binaryNumber);
28 |
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Chapter4/src/Exercise4_46.java:
--------------------------------------------------------------------------------
1 | /*(Computer architecture: bit-level operations)
2 | *
3 | * A short value is stored in 16 bits.
4 | * Write a program that prompts the user to enter a short integer and displays the 16 bits
5 | * for the integer.
6 | * See sample runs pages 174 and 175
7 | *
8 | * NOTE: Currently not complete
9 | *
10 | */
11 | public class Exercise4_46 {
12 | public static void main(String args[]){
13 |
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Chapter4/src/Exercise4_47.java:
--------------------------------------------------------------------------------
1 | /*(Statistics: Computer mean and standard deviation)
2 | *
3 | * Write a program that prompts the user to enter ten numbers,
4 | * and displays the mean and standard deviations of these numbers using the following formulas
5 | *
6 | * See page 175 or google formulas for mean or standard deviation
7 | *
8 | * Believe it or not, this is quite straightforward.
9 | *
10 | */
11 | import java.util.Scanner;
12 | public class Exercise4_47 {
13 | public static void main(String args[]){
14 | Scanner input = new Scanner(System.in);
15 | final int INPUT_NUMBER = 10; //this is a good example of when to use a constant
16 | System.out.println("Enter " + INPUT_NUMBER + " numbers (they can be doubles):");
17 | double total = 0;
18 | double totalSquares = 0; //can you think of a more efficient way to find this?
19 |
20 | for (int i = 0; i < INPUT_NUMBER; i++){
21 | double number = input.nextDouble();
22 | total += number;
23 | totalSquares += Math.pow(number,2);
24 | }
25 | double mean = total / INPUT_NUMBER;
26 | double stdDeviation = Math.sqrt((totalSquares - (Math.pow(total, 2) / INPUT_NUMBER)) /
27 | (INPUT_NUMBER - 1));
28 | //Since you've been learning math your entire life, formulas should be nothing new to you
29 | //being able to break them into code is a valuable skill. Later in the book, we'll try
30 | //more advanced geometry, statistics, and calculus.
31 |
32 | System.out.println("The mean is: " + mean);
33 | System.out.println("The Standard Deviation is: " + stdDeviation);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Chapter4/src/GuessNumber.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This is the guess number method from the book
3 | * It is a great example of a while loop
4 | */
5 | import java.util.Scanner;
6 | public class GuessNumber {
7 | public static void main(String[] args) {
8 | int number = (int)(Math.random() * 100); //0 - 100
9 | Scanner input = new Scanner(System.in);
10 | System.out.println("Try to guess the number between 0 and 100");
11 |
12 | int guess = -1; //initializing with a number not in the range makes sense.
13 | while (guess != number){
14 | //the loop will iterate (that's geek speak for run once) until the number is guessed correctly
15 | System.out.println("\nEnter your guess: ");
16 | guess = input.nextInt();
17 |
18 | if (guess == number){
19 | System.out.println("Correct, the number is: " + number);
20 | }
21 | else if (guess > number){
22 | System.out.println("Your guess is too high");
23 | }
24 | else{
25 | System.out.println("Your guess is too low");
26 |
27 | }
28 | }
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Chapter4/src/GuessNumberFor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This is the last method reinterpreted using a for loop
3 | * For loop implementation is just to show that it works
4 | * The code isn't too clear, but is fully functional
5 | */
6 |
7 | import java.util.Scanner;
8 | public class GuessNumberFor {
9 | public static void main(String[] args) {
10 | int number = (int)(Math.random() * 100); //0 - 100
11 | Scanner input = new Scanner(System.in);
12 | System.out.println("Try to guess the number between 0 and 100");
13 |
14 | for (int guess = -1; guess != number; guess = input.nextInt()){
15 | if (guess > number){
16 | System.out.println("Your guess is too high");
17 | }
18 | else if (guess >= 0){
19 | System.out.println("Your guess is too low");
20 | }
21 | System.out.println("\nEnter your guess: ");
22 | }
23 | System.out.println("Correct, the number is: " + number);
24 |
25 | }
26 | }
27 |
28 |
--------------------------------------------------------------------------------
/Chapter4/src/PrintPrimes.java:
--------------------------------------------------------------------------------
1 | /* This is an advanced program that finds prime numbers up to the nth prime
2 | * There are many, many ways to approach this problem
3 | * Though this is not the most efficient implementation, it is far from worst case
4 | */
5 | import java.util.Scanner;
6 | public class PrintPrimes {
7 | public static void main(String[] args) {
8 | Scanner input = new Scanner(System.in);
9 | System.out.println("Which nth prime number would you like to print up to?");
10 | int numberPrime = input.nextInt(); //this is how many primes will be printed
11 | for (int i = 1, number = 1; i <= numberPrime; number++) {
12 | //this loop initializes two variables and increments one.
13 | //"i" is the number prime, "number" is the current number
14 | boolean isPrime = true;
15 | //this can be done more efficiently with a method, we'll get there soon
16 | for (int j = 2; j * j <= number; j++){
17 | //factors are never greater than the sqrt of the number
18 | //this adds serious efficiency
19 | if (number % j == 0 && j != number){ //if a number is evenly divided by anything other than
20 | isPrime = false; //the number isn't prime
21 | }
22 | }
23 | if (isPrime) {
24 | System.out.println("Prime number " + i + " is " + number);
25 | i++; //increments i now that a prime has been found
26 | }
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------
/Chapter4/src/ShortForLoop.java:
--------------------------------------------------------------------------------
1 |
2 | public class ShortForLoop {
3 | public static void main(String[] args){
4 | for (int i = 0; i++ < 1; System.out.println("This works, but is laughably contrived"));
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/Chapter5/Readme.md:
--------------------------------------------------------------------------------
1 | #Methods
2 |
3 | Methods make code reusable
4 |
5 | Let's say that you have a program that counts from 1 - 100 four ways: normally, by 2's, by 7's and by .655.
6 |
7 | Your code could look like this:
8 |
9 | ```
10 | public class MethodExample {
11 | public static void main(String[] args){
12 | for (int i = 0; i <= 100; i++){
13 | System.out.println(i);
14 | }
15 | System.out.println();
16 |
17 | for (int i = 0; i <= 100; i+=2){
18 | System.out.println(i);
19 | }
20 | System.out.println();
21 |
22 | for (int i = 0; i <= 100; i+=7){
23 | System.out.println(i);
24 | }
25 | System.out.println();
26 |
27 | for (double i = 0; i <= 100; i+=.655){
28 | System.out.println(i);
29 | }
30 | System.out.println();
31 |
32 | }
33 | }
34 | ```
35 |
36 | That works. And up until this point, that's how you would write a program with that functionality. Notice the similarities in the for loops. They're all doing pretty much the same thing. We can simplify this using a method. Just like loops and if statements, methods will take some practicing before you get comfortable with them. Unlike loops and if statements, you can get by without using methods. Barely, but you can get by. We've done alright so far, haven't we?
37 |
38 | Perhaps this is a poor example, but this is an example of what the above code could look like if you used methods. This example touches on a few key concepts of methods.
39 |
40 | ```
41 | public class MethodExample {
42 | public static void main(String[] args){
43 |
44 | printSequence(100);
45 | printSequence(2, 100);
46 | printSequence(7, 100);
47 | printSequence(.655, 100);
48 |
49 | }
50 | public static void printSequence(int max){
51 | for (int i = 0; i <= max; i++){
52 | System.out.println(i);
53 | }
54 | System.out.println();
55 | }
56 | public static void printSequence(int increment, int max){
57 | for (int i = 0; i <= max; i += increment){
58 | System.out.println(i);
59 | }
60 | System.out.println();
61 | }
62 | public static void printSequence(double increment, double max){
63 | for (double i = 0; i <= max; i += increment){
64 | System.out.println(i);
65 | }
66 | System.out.println();
67 | }
68 | }
69 |
70 | ```
71 |
72 | I like the way the book introduces methods. I recommend a thorough read through of this chapter, I will do an abundant number of practice problems and further explain some concepts there.
73 |
74 | ##Note on Usage
75 | While the book's author uses the correct term 'method', I occassionally use the word 'function'. The way I see it, Java only has methods, which are tied to objects. Though, functions (think of a mathematical function) are very similar, methods and functions aren't the same thing. I am admitting that my use of the word function is incorrect.
76 |
77 | I also use 'parameter' and 'argument' interchangeably. This usage is incorect as well. A parameter is a variable declared when defining a method, such as `public void myMethod(int thisIsAParameter)`. An argument is what gets passed in when you call the function: `myMethod(thisIsAnArgument)`. Now you know.
78 |
79 | ##Modularization
80 | I love this word, and not just for how smart it makes me sound when I use it. Modularizing code using methods is so great, especially when you're working on a project, see a lot of messy code, and then realize that it can be further modularized. Think of a modular home. Like building a modular home, we strive to build complete, functional, programs using code grouped into succinct methods (and classes, more on that later).
81 |
82 | ##Java String Methods
83 | Every possible string manipulation method is already built in to Java, but there has to be underlying code making them work, right? In the included programs, I've attempted to recreate some of these methods. I realize this is a bit premature and may use concepts that haven't been covered yet, but I'll include the source file in future chapters as well. I'll start getting my feet wet now.
--------------------------------------------------------------------------------
/Chapter5/src/Exercise5_17.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (Display a matrix of 0s and 1s)
3 | * Write a method that displays an n-by-n matrix using the following header:
4 | * public static void printMatrix(int n)
5 | * Each element is 1 or 0, generated randomly
6 | * n is how many rows and columns
7 | *
8 | */
9 | import java.util.Scanner;
10 | public class Exercise5_17 {
11 | public static void main(String args[]){
12 | Scanner input = new Scanner(System.in);
13 | System.out.print("Enter a number n: ");
14 | int inputNumber = input.nextInt();
15 | printMatrix(inputNumber);
16 | }
17 | public static void printMatrix(int n){
18 | for (int i = 0; i < n; i++){
19 | for (int j = 0; j < n; j++){
20 | int randomNum = (int)(Math.random()*2);
21 | System.out.print(randomNum + " ");
22 | }
23 | System.out.println();
24 | }
25 | //That was simple, wasn't it?
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter5/src/Exercise5_26.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (Palindromic Primes)
3 | * ten numbers per line
4 | *
5 | * This is a great chance to modify the code from CH4's prime number finder
6 | */
7 | import java.util.Scanner;
8 | public class Exercise5_26 {
9 | public static void main(String args[]){
10 | Scanner input = new Scanner(System.in);
11 | System.out.println("Enter how many palindromic primes you'd like to print:");
12 | int numberPrime = input.nextInt();
13 |
14 | for (int i = 1, number = 2; i <= numberPrime; number++){
15 | if (isPrime(number) && isPalindrome(number)){
16 | System.out.println("Prime number " + i + " is " + number);
17 | i++;
18 | }
19 | }
20 |
21 | }
22 | public static boolean isPrime(int x){
23 | for (int i = 2; i * i <= x; i++){
24 | if (x % i == 0 && i != x){
25 | return false;
26 | }
27 | }
28 | return true;
29 | }
30 |
31 | public static boolean isPalindrome(int x){
32 | char[] charArray = ("" + x).toCharArray();
33 | int lastIndex = charArray.length - 1;
34 | for (int i = 0; i <= lastIndex - i; i++){
35 | if (charArray[i] != charArray[lastIndex - i]){
36 | return false;
37 | }
38 | }
39 | return true;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Chapter5/src/Exercise5_27.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | /*
4 | * (emirp)
5 | *
6 | * Prime numbers that are nonpalindromic but are still prime when reversed
7 | */
8 | public class Exercise5_27 {
9 | public static void main(String args[]){
10 | Scanner input = new Scanner(System.in);
11 | System.out.println("Enter how many palindromic primes you'd like to print:");
12 | int numberPrime = input.nextInt();
13 |
14 | for (int i = 1, number = 2; i <= numberPrime; number++){
15 | if (isEmirp(number)){
16 | System.out.println("Prime number " + i + " is " + number);
17 | i++;
18 | }
19 | }
20 |
21 | }
22 | public static boolean isPrime(int x){
23 | for (int i = 2; i * i <= x; i++){
24 | if (x % i == 0 && i != x){
25 | return false;
26 | }
27 | }
28 | return true;
29 | }
30 | public static boolean isPalindrome(int x){
31 | char[] charArray = ("" + x).toCharArray();
32 | int lastIndex = charArray.length - 1;
33 | for (int i = 0; i <= lastIndex - i; i++){
34 | if (charArray[i] != charArray[lastIndex - i]){
35 | return false;
36 | }
37 | }
38 | return true;
39 | }
40 | public static boolean isEmirp(int x){
41 | char[] charArray = ("" + x).toCharArray();
42 | int lastIndex = charArray.length - 1;
43 | for (int i = 0; i <= lastIndex - i; i++){
44 | char temp = charArray[i];
45 | charArray[i] = charArray[lastIndex - i];
46 | charArray[lastIndex - i] = temp;
47 | }
48 | int xReversed = Integer.parseInt(new String(charArray));
49 | //int xReversed = charArray.reverse();
50 | //yes, I realize I could have used .reverse instead
51 |
52 | return !isPalindrome(x) && isPrime(x) && isPrime(xReversed);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Chapter5/src/Exercise5_31.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (Financial: credit card number validation)
3 | *
4 | * Rather than typing the entire description of the problem, I will instead point you to Google,
5 | * as there are many informative guides on the internet if you don't know how validation works
6 | *
7 | * This is a great practice problem. I've seen a class assignment in a data structures class modeled
8 | * around card validation (though it was considerably more complex).
9 | *
10 | * Method headers are given. Uses Mod 10/Luhn check
11 | */
12 | import java.util.Scanner;
13 | public class Exercise5_31 {
14 | public static void main(String args[]){
15 | Scanner input = new Scanner(System.in);
16 | System.out.print("Enter a 13 - 16 digit credit card number: ");
17 | long ccNumber = input.nextLong();
18 |
19 | if (isValid(ccNumber)){
20 | System.out.println(ccNumber + " is valid");
21 | }
22 | else{
23 | System.out.println(ccNumber + " is not valid");
24 | }
25 | }
26 | public static boolean isValid(long number){
27 | boolean validPrefix = (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 37) || prefixMatched(number, 6));
28 | boolean validSize = getSize(number) >= 13 && getSize(number) <= 16;
29 | boolean checksumIsValid = ((sumOfOddPlaces(number) + sumOfDoubleEvenPlace(number)) % 10 == 0);
30 |
31 | return validPrefix && validSize && checksumIsValid;
32 | }
33 | public static int sumOfDoubleEvenPlace(long number){
34 | int sum = 0;
35 | number /= 10;
36 | for (int i = 2; number > 0; i+=2){
37 | sum += getDigit((int)(2*(number % 10)));
38 | number /= 100;
39 | }
40 | return sum;
41 | }
42 | //assumes no more than two digits
43 | public static int getDigit(int number){
44 | if (number > 9){
45 | return (number / 10) + (number % 10);
46 | }
47 | return number;
48 |
49 | }
50 | public static int sumOfOddPlaces(long number){
51 | int sum = 0;
52 | for (int i = 1; number > 0; i+=2){
53 | sum += number % 10;
54 | //getDigit((int)(number % 10));
55 | number /= 100;
56 | }
57 | return sum;
58 | }
59 |
60 | public static boolean prefixMatched(long number, int d){
61 | return getPrefix(number, getSize(d)) == d;
62 | }
63 |
64 | public static int getSize(long d){
65 | int size = 0;
66 | for (long i = d; i > 0; i /= 10, size++){}
67 | return size;
68 | }
69 |
70 | public static long getPrefix(long number, int k){
71 | if (getSize(number) < k){
72 | return number;
73 | }
74 | return (long)(number / Math.pow(10, (getSize(number) - k)));
75 |
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/Chapter5/src/MethodExample.java:
--------------------------------------------------------------------------------
1 |
2 | public class MethodExample {
3 | public static void main(String[] args){
4 |
5 | /* for (int i = 0; i <= 100; i++){
6 | System.out.println(i);
7 | }
8 | System.out.println();
9 |
10 | for (int i = 0; i <= 100; i+=2){
11 | System.out.println(i);
12 | }
13 | System.out.println();
14 |
15 | for (int i = 0; i <= 100; i+=7){
16 | System.out.println(i);
17 | }
18 | System.out.println();
19 |
20 | for (double i = 0; i <= 100; i+=.655){
21 | System.out.println(i);
22 | }
23 | System.out.println();*/
24 |
25 | printSequence(100);
26 | printSequence(2, 100);
27 | printSequence(7, 100);
28 | printSequence(.655, 100);
29 |
30 | }
31 | public static void printSequence(int max){
32 | for (int i = 0; i <= max; i++){
33 | System.out.println(i);
34 | }
35 | System.out.println();
36 | }
37 | public static void printSequence(int increment, int max){
38 | for (int i = 0; i <= max; i += increment){
39 | System.out.println(i);
40 | }
41 | System.out.println();
42 | }
43 | public static void printSequence(double increment, double max){
44 | for (double i = 0; i <= max; i += increment){
45 | System.out.println(i);
46 | }
47 | System.out.println();
48 | }
49 | }
50 |
51 |
--------------------------------------------------------------------------------
/Chapter5/src/Readme.md:
--------------------------------------------------------------------------------
1 | #Suggestion For Additional Practice
2 |
3 | Revisit some of the programs from previous chapters and try to modularize the code as best as you can.
--------------------------------------------------------------------------------
/Chapter5/src/StringMethods.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Java has a lot of built-in methods for String manipulation
3 | * But, someone had to write them, right?
4 | *
5 | * I'm going to try to implement some of the methods without calling the String method itself
6 | * There is really no point to these methods, and most would be silly to ever implement
7 | *
8 | * Most of this is trivial and is
9 | */
10 | public class StringMethods {
11 | public static void main(String args[]){
12 |
13 | String testString = "Hello, World!";
14 | String testSuffix = "ld!";
15 | final int CHAR_INDEX = 3;
16 | System.out.println("Length of " + testString + " is " + length(testString));
17 | System.out.println("Character at " + CHAR_INDEX + " is " + charAt(testString, CHAR_INDEX));
18 | System.out.println("Does " + testString + " end with \"" + testSuffix + "\"? " + endsWith(testString,testSuffix));
19 | System.out.println("Hash code: " + hashCode(testString));
20 | System.out.println("Is the String empty? " + isEmpty(testString));
21 | System.out.println("To upper case: " + toUpperCase(testString));
22 | System.out.println("To lower case: " + toLowerCase(testString));
23 |
24 | }
25 |
26 |
27 | public static int length(String s){
28 | return s.toCharArray().length;
29 | }
30 |
31 | public static char charAt(String s, int i){
32 | return s.toCharArray()[i];
33 | }
34 |
35 | public static boolean endsWith(String s, String suffix){
36 | if (s.length() < suffix.length()){
37 | return false;
38 | }
39 | return s.substring(s.length()-suffix.length(),s.length()).equals(suffix);
40 | }
41 |
42 | //replicates hashCode method
43 | public static int hashCode(String s){
44 | int length = s.length();
45 | char[] charArray = s.toCharArray();
46 | int hashCode = 0;
47 | for (int i = 0; i < length; i++){
48 | hashCode = 31 * hashCode + charArray[i];
49 | }
50 | return hashCode;
51 | }
52 |
53 | public static boolean isEmpty(String s){
54 | return s.length() == 0;
55 | }
56 |
57 | public static String toUpperCase(String s){
58 | char[] charArray = s.toCharArray();
59 | for (int i = 0; i < charArray.length; i++){
60 | if (charArray[i] >= 'a' && charArray[i] <= 'z'){
61 | charArray[i] = (char)(charArray[i] - 'a' + 'A');
62 | }
63 | }
64 | return new String(charArray);
65 | }
66 |
67 | public static String toLowerCase(String s){
68 | char[] charArray = s.toCharArray();
69 | for (int i = 0; i < charArray.length; i++){
70 | if (charArray[i] >= 'A' && charArray[i] <= 'Z'){
71 | charArray[i] = (char)(charArray[i] - 'A' + 'a');
72 | }
73 | }
74 | return new String(charArray);
75 | }
76 |
77 |
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/Chapter6/Readme.md:
--------------------------------------------------------------------------------
1 | #Single Dimensional Arrays
2 |
3 | You may have seen me use arrays in my solutions to problems from the last few chapters. Sorry to confuse you! Arrays are data types that can hold other variables and objects. You can have an array of ints, an array of strings, an array of objects, even an array of arrays. Arrays and data structures that can be represented by arrays are used to store massive amounts of data, and are easy to work with.
4 |
5 | Arrays are declared with a fixed size, for example `double[] numbers = new double[100];`. Guess how many empty cells there will be in the array? You can also declare an array literally like `double[] numbers = {3, 4.5, 6.1};`. I have also seen arrays declared in this format: `double[] numbers = new double[]{3, 4.5, 6.1}` but I have yet to be convinced of the merits of this method. Stick to the first two.
6 |
7 | There are many useful methods that allow you to manipulate data into and within arrays. I've already used a few popular ones, though more will surface throughout this chapter.
8 |
9 | Let's create an array of x randomely generated ints. Note that from here on out, I will greatly favor sharing methods rather than full classes when providing examples.
10 | ```
11 | public static int[] randomArray(int size){
12 | //how to populate?
13 | //initializing it literally doesn't make sense
14 | int[] randomNumArray = new int[size];
15 | for (int i = 0; i < size; i++){
16 | randomNumArray[i] = (int)(Math.random()*101);
17 | }
18 | return randomNumArray;
19 | }
20 | ```
21 |
22 | ###For-each
23 |
24 | Don't think that you can get through an intro class without being asked to write some for-each loops. The syntax was something I struggled with and I don't frequently use the loop now. The for-each loop allows you to traverse a loop sequentially without using an index.
25 |
26 | So, this:
27 | ```
28 | for (int i = 0 i < intArray.length; i++){
29 | System.out.println(array[i]);
30 | }
31 | ```
32 | becomes this:
33 | ```
34 | for (int i: intArray){
35 | System.out.prontln(i);
36 | }
37 | ```
38 |
39 | Are there restrictions? Sure. The syntax doesn't allow for much customization, including traversing the array in a different order. You also can't use a for-each loop to modify the contents of an array.
40 |
41 | What I'm saying is that if you try to use a for each loop to populate an array, it won't work:
42 | ```
43 | public static int[] randomArray(int size){
44 | //how to populate?
45 | //initializing it literally doesn't make sense
46 | int[] randomNumArray = new int[size];
47 | /* for (int i = 0; i < size; i++){
48 | randomNumArray[i] = (int)(Math.random()*101);
49 | }*/
50 | for (int i: randomNumArray){ //this doesn't work
51 | i = (int)(Math.random()*101);
52 | }
53 | return randomNumArray;
54 | }
55 | ```
56 |
57 | ###Passing Arrays as arguments
58 | When you pass an array as an argument, any changes made within the method persist outside of it. Let's revisit the random number array generator with this knowledge.
59 | ```
60 | //Same functionality as "ArrayCreation" used to show how values are passed.
61 | public class ArrayModification {
62 | public static void main(String[] args){
63 | int[] newArray = new int[150];
64 | randomArray(newArray);
65 | for (int i = 0; i < newArray.length; i++){
66 | System.out.println(newArray[i]);
67 | }
68 | }
69 | public static void randomArray(int[] array){
70 | for (int i = 0; i < array.length; i++){
71 | array[i] = (int)(Math.random()*101);
72 | }
73 | }
74 | }
75 | ```
76 | It works the exact same way. Note what's different and what hasn't changed.
77 |
78 | #Searching and Sorting
79 | Every intro class will have an assignment that revolves around searching (and sorting). This is where computer science starts to get technical. Efficiency is suddenly a major concern. You may be thinking, "if we're working with a list of ten elements, who cares how fast we search and sort it"?
80 |
81 | Understanding these algorithms (and I know you've been dying to hear that word) provides a glimpse into what can be accomplished by a good programmer. You'll be asked to reproduce and analyze algorithms in job interviews and, with further study, will see just how pervasive these mathematical processes are in all areas of study.
82 |
83 | The term Brute-force is often used incorrectly. In the realm of Computer Science, it just means that you approach a problem exhaustively, meaning that you try everything. A brute-force password crack program would try every possible combination of numbers, letters, and symbols until a match was found. Imagine how long that takes.
84 |
85 | How do we use "brute-force" to describe an approach to writing algorithms? Here's what I think: you want to search through an array but implement the most basic, fundamental, sequential search. It's inefficient, but it (probably) works, and, most importantly for someone who is just learning, *you* understand what's going on. I approached learning algorithms in a way such that I made sure I understood what needed to be done fundamentally before I tried to implement the most elegant solution. Furthermore, if you're considering using a more 'advanced' algorithm, it's important to understand *why* it is preferrable to use it.
86 |
87 | As you are learning, try to approach algorithmic problems in a way that you understand and have internalized. Once it works, then try more efficient solutions. Let's begin, and, no, I'm not going to tell you how much time I spent on pages 245 - 252.
88 |
89 | ####Linear Search
90 | This is inefficient, but it works, and it's very, very easy to understand:
91 | ```
92 | public static int linearSearch(int[] list, int key){
93 | for (int i = 0; i < list.length; i++){
94 | if (key == list[i]){
95 | return i;
96 | }
97 | }
98 | return -1;
99 | }
100 | ```
101 | The average case for finding the index will scan roughly half the index locations. That's fine for small applications, but scanning hundreds of thousands or millions of data elements? Things get sticky, and performance suffers.
102 |
103 | ####Binary Search
104 | Binary search is incredibly more efficient. While the improvement isn't apparent in small lists, it is exponentially more efficient as lists grow in size.
105 |
106 | The only caveat being that the numeric list must first be sorted in ascending order. That's easy, you may think, and you're right. The point, however, is that this search takes advantage of increased specificity of the problem.
107 |
108 | Think about the password cracker that tries every possible password combination. If we knew that the password used only lower case letters and one numeric digit, the algorithm could be adjusted accordingly. It is brute force's effectiveness when dealing with the complete unknown that makes it so impressive. Imagine that we additionally know the password is 7 characters ending with a single number. A human still could never figure it out, but the algorithm could be further optimized. Then, if we learned that the characters spelled out an english word or phrase, the time it would take to deduce the password would decay exponentially.
109 |
110 | Visit https://howsecureismypassword.net/ for a more interactive look at what I'm trying to explain
111 |
112 | What I'm saying is that Binary search is powerful, but its power is derived from a condition that isn't always (in fact, is almost never) inherently true. If you populate an array like we did above it wouldn't be sorted properly. Keep this in mind.
113 |
114 | Here's binary search:
115 | ```
116 | public static int binarySearch(int[] list, int key){
117 | int low = 0, high = list.length - 1;
118 | while (high >= low){
119 | int mid = (low + high) / 2;
120 | if (key < list[mid]){
121 | high = mid - 1;
122 | }
123 | else if (key == list[mid]){
124 | return mid;
125 | }
126 | else{
127 | low = mid + 1;
128 | }
129 | }
130 | }
131 | ```
132 |
133 | When possible, you should implement binary sort. I firmly believe that it's important to approach problems in a manner that you are comfortable with. As you get more experienced, implementing the algorithm will become second nature. Moreover, searching and sorting algorithms also exist as built-in methods that you can call. When you're tasking with implementing one, though, calling the built-in method typically won't fly.
134 |
135 | ####Selection Sort
136 | ```
137 | public static void selectionSort(double[] list){
138 | for (int i = 0; i < list.length - 1; i++){
139 | double currentMin = list[i];
140 | int currentMinIndex = i;
141 |
142 | for (int j = i + 1; j < list.length; j++){
143 | if (currentMin > list[j]){
144 | currentMin = list[j];
145 | currentMinIndex = j;
146 | }
147 | }
148 |
149 | if (currentMinIndex != i){
150 | list[currentMinIndex] = list[i];
151 | list[i] = currentMin;
152 | }
153 | }
154 | }
155 | ```
156 |
157 | ####Insertion Sort
158 | ```
159 | public static void insertionSort(double[] list){
160 | for (int i = 1; i < list.length; i++){
161 | double currentElement = list[i];
162 | int k;
163 | for (k = i - 1; k >= 0 && list[k] > currentElement; k--){
164 | list[k + 1] = list[k];
165 | }
166 | }
167 | }
168 | ```
169 |
--------------------------------------------------------------------------------
/Chapter6/src/ArrayCreation.java:
--------------------------------------------------------------------------------
1 |
2 | public class ArrayCreation {
3 | public static void main(String[] args){
4 | int[] newArray = randomArray(150);
5 | for (int i = 0; i < newArray.length; i++){
6 | System.out.println(newArray[i]);
7 | }
8 | }
9 | public static int[] randomArray(int size){
10 | //how to populate?
11 | //initializing it literally doesn't make sense
12 | int[] randomNumArray = new int[size];
13 | for (int i = 0; i < size; i++){
14 | randomNumArray[i] = (int)(Math.random()*101);
15 | }
16 | // for (int i: randomNumArray){ This doesn't work
17 | // i = (int)(Math.random()*101);
18 | // }
19 | return randomNumArray;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Chapter6/src/ArrayModification.java:
--------------------------------------------------------------------------------
1 | //Same functionality as "ArrayCreation" used to show how values are passed.
2 | public class ArrayModification {
3 | public static void main(String[] args){
4 | int[] newArray = new int[150];
5 | randomArray(newArray);
6 | for (int i = 0; i < newArray.length; i++){
7 | System.out.println(newArray[i]);
8 | }
9 | }
10 | public static void randomArray(int[] array){
11 | for (int i = 0; i < array.length; i++){
12 | array[i] = (int)(Math.random()*101);
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Chapter6/src/BinarySearch.java:
--------------------------------------------------------------------------------
1 |
2 | public class BinarySearch {
3 | public static void main(String args[]){
4 | int[] exampleArray = {1,2,3,4,5,6};
5 | System.out.println(binarySearch(exampleArray,6));
6 | }
7 | public static int binarySearch(int[] list, int key){
8 | int low = 0, high = list.length - 1;
9 | while (high >= low){
10 | int mid = (low + high) / 2;
11 | if (key < list[mid]){
12 | high = mid - 1;
13 | }
14 | else if (key == list[mid]){
15 | return mid;
16 | }
17 | else{
18 | low = mid + 1;
19 | }
20 | }
21 | return -1;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Chapter6/src/Exercise6_12.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Reverse an array
3 | */
4 | public class Exercise6_12 {
5 | public static void main(String args[]){
6 | int[] intArray = {1,2,3,4,5,6,6};
7 | reverseArray(intArray);
8 | for (int i: intArray){
9 | System.out.println(i);
10 | }
11 | }
12 | public static void reverseArray(int[] array){
13 | for (int i = 0, end = array.length -1; i < array.length / 2; i++, end--){
14 | if (i >= end){
15 | break;
16 | }
17 | int temp = array[i];
18 | array[i] = array[end];
19 | array[end] = temp;
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Chapter6/src/Exercise6_15.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Eliminate all duplicates in an array
3 | * WITHOUT using lists
4 | * There are many ways to do this
5 | */
6 | public class Exercise6_15 {
7 | public static void main(String args[]){
8 | int[] intArray = {1,3,1,2,2,3,3,2};
9 | int[] dupsRemoved = eliminateDuplicates(intArray);
10 |
11 | System.out.println();
12 | for (int i: dupsRemoved){
13 | System.out.println(i);
14 | }
15 | }
16 | public static int[] eliminateDuplicates(int[] list){
17 | int[] tempList = new int[list.length];
18 | int newListSize = 0;
19 | for (int i = 0; i < list.length; i++){
20 | if (hasNextDuplicate(i, list) && !arrayContainsNumber(tempList, list[i])){
21 | tempList[newListSize] = list[i];
22 | newListSize++;
23 | }
24 | }
25 |
26 | int[] returnList = new int[newListSize++];
27 | System.arraycopy(tempList, 0, returnList, 0, returnList.length);
28 | return returnList;
29 | }
30 | public static boolean hasNextDuplicate(int index, int[] list){ //returns true if there is a duplicate to the right
31 | for (int i = index + 1; i < list.length; i++){
32 | if (list[i] == list[index]){
33 | return true;
34 | }
35 | }
36 | return false;
37 | }
38 | public static boolean arrayContainsNumber(int[] list, int number){ //we're going to implement a slightly modified linear search
39 | for (int i = 0; i < list.length; i++){
40 | if (list[i] == number){
41 | return true;
42 | }
43 | }
44 | return false;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Chapter6/src/Exercise6_25.java:
--------------------------------------------------------------------------------
1 |
2 | public class Exercise6_25 {
3 |
4 | }
5 |
--------------------------------------------------------------------------------
/Chapter6/src/Exercise6_27.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Write a program that checks if two arrays have the same contents
3 | * no restrictions
4 | * given header: public static boolean equals(int[] list1, int[] list2)
5 | *
6 | * I'm going to show you the easy way first
7 | * Then, try it without sorting at all, manually or using a built in method
8 | * Then, if you haven't already, try it without creating another array
9 | *
10 | * These are just for practice and aren't necessarily the most efficient
11 | */
12 | import java.util.Scanner;
13 | public class Exercise6_27 {
14 | public static void main(String args[]){
15 | int[] list1 = {1,2,3,4,5};
16 | int[] list2 = {3,4,1,5,2};
17 | boolean testEasy = equalsEasy(list1, list2);
18 | System.out.println(testEasy);
19 |
20 | //the arrays got changed, remember?
21 | //you can't reset an initialized array using literal notation
22 | int[] list3 = {1,2,3,4,5};
23 | int[] list4 = {3,4,1,5,2};
24 | boolean testHard = equalsHard(list3, list4);
25 | System.out.println(testHard);
26 | }
27 | public static boolean equalsEasy(int[] list1, int[] list2){
28 | java.util.Arrays.sort(list1);
29 | java.util.Arrays.sort(list2);
30 |
31 | if (list1.length != list2.length){
32 | return false;
33 | }
34 |
35 | for (int i = 0; i < list1.length; i++){
36 | if (list1[i] != list2[i]){
37 | return false;
38 | }
39 | }
40 |
41 | //can you implement a for each loop instead of the above?
42 | //do you understand why this doesn't work?
43 | /*for (int i: list1){
44 | for (int j: list2){
45 | if (i != j){
46 | return false;
47 | }
48 | }
49 | }
50 | //*/
51 | return true;
52 | }
53 | //now lets try again
54 | public static boolean equalsHard(int[] list1, int[] list2){
55 | if (list1.length != list2.length){
56 | return false;
57 | }
58 | boolean[] indexMatch = new boolean[list1.length];
59 | for (int i = 0; i < list1.length; i++){
60 | for (int j = 0; j < list2.length; j++){
61 | if (list1[i] == list2[j] && !indexMatch[i] && !indexMatch[j]){
62 | indexMatch[i] = true;
63 | indexMatch[j] = true;
64 | }
65 | }
66 | }
67 | for (boolean x: indexMatch){
68 | if (!x){
69 | return false;
70 | }
71 | }
72 | return true;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/Chapter6/src/InsertionSort.java:
--------------------------------------------------------------------------------
1 |
2 | public class InsertionSort {
3 | public static void main(String[] args){
4 | double[] doubleArray = {1.2, 0.5, 5.1, 1.3};
5 | insertionSort(doubleArray);
6 | for (double i: doubleArray){
7 | System.out.println(i);
8 | }
9 | }
10 | public static void insertionSort(double[] list){
11 | for (int i = 1; i < list.length; i++){
12 | double currentElement = list[i];
13 | int k;
14 | for (k = i - 1; k >= 0 && list[k] > currentElement; k--){
15 | list[k + 1] = list[k];
16 | }
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Chapter6/src/LinearSearch.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Example of LinearSearch similar to what is found in the book
3 | */
4 | public class LinearSearch {
5 | public static void main(String args[]){
6 | int[] exampleArray = {1,2,3};
7 | System.out.println(linearSearch(exampleArray,2));
8 | }
9 | public static int linearSearch(int[] list, int key){
10 | for (int i = 0; i < list.length; i++){
11 | if (key == list[i]){
12 | return i;
13 | }
14 | }
15 | return -1;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter6/src/SelectionSort.java:
--------------------------------------------------------------------------------
1 | //Selection Sort from the book
2 | public class SelectionSort {
3 | public static void main(String[] args){
4 | double[] doubleArray = {1.2, 0.5, 5.1, 1.3};
5 | selectionSort(doubleArray);
6 | for (double i: doubleArray){
7 | System.out.println(i);
8 | }
9 | }
10 | public static void selectionSort(double[] list){
11 | for (int i = 0; i < list.length - 1; i++){
12 | double currentMin = list[i];
13 | int currentMinIndex = i;
14 |
15 | for (int j = i + 1; j < list.length; j++){
16 | if (currentMin > list[j]){
17 | currentMin = list[j];
18 | currentMinIndex = j;
19 | }
20 | }
21 |
22 | if (currentMinIndex != i){
23 | list[currentMinIndex] = list[i];
24 | list[i] = currentMin;
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Chapter7/Readme.md:
--------------------------------------------------------------------------------
1 | #Multidimensional Arrays
2 |
3 | Arrays get deeper- Two dimensions, three dimensions, x dimensions. It can get crazy. Luckily, with a solid understanding of one dimensional arrays, nothing here is too difficult. It's worth mentioning that you won't be able to get out of an intro course without being assigned a program that deals with 2d arrays. Emulating various games seems to be a popular choice.
4 |
5 | This is a 2d array `int[][] matrix = new int[5][5];`. Picture it as a 5x5 grid- that's what it is. If a data set can be represented as a grid/graph, a 2d array can (probably) do it.
6 |
7 | ##Visualization
8 | 1d arrays aren't complex enough to warrant visualization. How do you picture a 1d array in your mind? It's a line. There's nothing to picture. A 2d is a grid, x and y or row and column are commonly used. For beginners, working with a 2d array can get confusing just due to the added complexity. I recommend visualizing the graph, and visualizing it consistently. Though how you imagine something doesn't have any effect on the function of the program, it will greatly benefit your understanding of the concepts, especially as they are still new to you.
9 |
10 | A popular method is to picture the 0th row, or origin row, at the top, and then picture each column descending from each location on the 0th row [0][i]. Here, the first location represents the row, and the second represents the column.
11 |
12 | Another way is to picture it like the first quadrant of a graph. the origin is in the bottom left, rather than the top left. There aren't any negative index locations, so this stays just as simple as the first method. Some people just find it easier work with this mental model.
13 |
14 | ##Working with 2d arrays
15 | You're probably finding this simple- great. I don't have too much to say. Nested for loops are how you're going to traverse these arrays.
16 | ```
17 | for (int i = 0; i < matrix.length; i++){
18 | for (int j = 0; j < matrix[i].length; j++){
19 | System.out.println();
20 | }
21 | }
22 | ```
23 | ###Multidimensional
24 | Anything greater than a 2d array is considered multidimensional. A 3d array can be pictured as a cube, a 2d grid with depth. 4d, well, I think of a cube with each cell having a diagonal offshoot that isn't parallel to any plane, but that doesn't scale properly when imagining the entire shape.
25 |
26 | ###Literal Initialization
27 | ```
28 | int[][] grid = {{1,2,3},
29 | {4,5,6},
30 | {7,8,9}}
31 | ```
32 |
33 | Believe it or not, that's about it. Multidimensional arrays have a wide variety of applications and you'll be expected to know them well. Let's try some practice problems.
--------------------------------------------------------------------------------
/Chapter7/src/Exercise7_25.java:
--------------------------------------------------------------------------------
1 | /*Markov Matrix
2 | * n * n matric is a markov matrix if each element is positive and the sum of elements in each column is 1
3 | * Won't work with some decimals
4 | */
5 | public class Exercise7_25 {
6 | public static void main(String args[]){
7 | double[][] a = {{0,.75,1},
8 | {.5,.25,0},
9 | {.5,0,0}};
10 | System.out.println(isMarkovMatrix(a));
11 | }
12 | public static boolean isMarkovMatrix(double[][] m){
13 | if (m.length != m[0].length){
14 | return false;
15 | }
16 | for (int i = 0; i < m.length; i++){
17 | double sum = 0;
18 | for (int j = 0; j < m[i].length; j++){
19 | sum += m[j][i];
20 | }
21 | if (sum != 1){
22 | return false;
23 | }
24 | }
25 | return true;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter7/src/Exercise7_28.java:
--------------------------------------------------------------------------------
1 | /*Strictly Identical Arrays
2 | * check if two arrays are exactly identical
3 | */
4 | public class Exercise7_28 {
5 | public static void main(String args[]){
6 |
7 | int[][] a = {{1,2,3},
8 | {4,5,6},
9 | {7,8,9}};
10 |
11 | int[][] b = {{1,2,3},
12 | {4,5,6},
13 | {7,8,9}};
14 |
15 | System.out.println(equals(a,b));
16 |
17 | }
18 | public static boolean equals(int[][] m1, int[][] m2){
19 | if (m1.length != m2.length){
20 | return false;
21 | }
22 | for (int i = 0; i < m1.length; i++){
23 | for (int j = 0; j < m1[i].length; j++){
24 | if (m1[i].length != m2[i].length){
25 | return false;
26 | }
27 | if (m1[i][j] != m2[i][j]){
28 | return false;
29 | }
30 | }
31 | }
32 | return true;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Chapter7/src/Exercise7_36.java:
--------------------------------------------------------------------------------
1 | /*Latin Square
2 | * Basically sudoku with letters
3 | * http://en.wikipedia.org/wiki/Latin_square
4 | *
5 | * Also inclues isReducedForm and reduceLatinSquare
6 | */
7 | public class Exercise7_36 {
8 | public static void main(String args[]){
9 | char[][] square = {{'A', 'B', 'C'},
10 | {'B', 'C', 'A'},
11 | {'C', 'A', 'B'}};
12 | System.out.println(isLatinSquare(square));
13 | System.out.println(isInReducedForm(square));
14 | }
15 | public static boolean isLatinSquare(char[][] square){
16 | if (square.length != square[0].length){
17 | return false;
18 | }
19 | for (int i = 0; i < square.length; i++){
20 | for (int j = 0; j < square[i].length; j++){
21 |
22 | for (int k = 0; k < square[i].length; k++){
23 | if (j != k && square[i][k] == square [i][j])
24 | return false;
25 | }
26 | for (int l = 0; l < square[i].length; l++){
27 | if (i != l && square[l][j] == square [i][j])
28 | return false;
29 | }
30 |
31 | }
32 | }
33 | return true;
34 | }
35 | public static boolean isInReducedForm(char[][] square){
36 | //A latin square is in reduced form if the first row and column are in natural order (ABC, for example)
37 | if (!isLatinSquare(square)){
38 | return false;
39 | }
40 | for (int i = 1; i < square.length; i++){
41 | if (square[i-1][0] > square[i][0]){
42 | return false;
43 | }
44 | }
45 | for (int j = 1; j < square[0].length; j++){
46 | if (square[0][j-1] > square[0][j]){
47 | return false;
48 | }
49 | }
50 | return true;
51 | }
52 | public static char[][] reduceLatinSquare(char[][] square){
53 | //Think about how to do this by breaking the problem into pieces
54 | //Assume that the square input is already a latin square
55 | //returning to this later
56 | if (isInReducedForm(square)){
57 | return square;
58 | }
59 |
60 | return square;
61 |
62 | }
63 | }
64 |
65 |
--------------------------------------------------------------------------------
/Chapter7/src/Exercise7_5.java:
--------------------------------------------------------------------------------
1 | /*Write a method to add two matrices
2 | *
3 | */
4 | public class Exercise7_5 {
5 | public static void main(String args[]){
6 | double[][] a = {{1,2,3},
7 | {4,5,6},
8 | {7,8,9}};
9 |
10 | double[][] b = {{1,2,3},
11 | {4,5,6},
12 | {7,8,9}};
13 |
14 | double[][] c = addMatrix(a, b);
15 |
16 | for (int i = 0; i < a.length; i++){
17 | for (int j = 0; j < a[i].length; j++){
18 | System.out.printf("%6.2f ", c[i][j]);
19 | }
20 | System.out.println();
21 | }
22 | }
23 | public static double[][] addMatrix(double[][] a, double[][] b){
24 | //assume same dimensions
25 | double[][] sumMatrix = new double[a.length][a[1].length];
26 | for (int i = 0; i < a.length; i++){
27 | for (int j = 0; j < a[i].length; j++){
28 | sumMatrix[i][j] = a[i][j] + b[i][j];
29 | }
30 | }
31 | return sumMatrix;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Chapter7/src/Latin Square/Exercise7_36.java:
--------------------------------------------------------------------------------
1 | /*Latin Square
2 | * Basically sudoku with letters
3 | * http://en.wikipedia.org/wiki/Latin_square
4 | *
5 | * Also inclues isReducedForm and reduceLatinSquare
6 | */
7 | public class Exercise7_36 {
8 | public static void main(String args[]){
9 | char[][] square = {{'A', 'B', 'C'},
10 | {'B', 'C', 'A'},
11 | {'C', 'A', 'B'}};
12 | System.out.println("Latin Square:" + isLatinSquare(square));
13 | System.out.println("Is Reduced: " + isInReducedForm(square));
14 | printLatinSquare(square);
15 |
16 | char[][] generatedSquare = generateFilledLatinSquare(3);
17 | System.out.println("Latin Square: " + isLatinSquare(generatedSquare));
18 | System.out.println("Is Reduced: " + isInReducedForm(generatedSquare));
19 | printLatinSquare(generatedSquare);
20 |
21 | char[][] generatedReducedSquare = generateReducedLatinSquare(3);
22 | System.out.println("Latin Square: " + isLatinSquare(generatedReducedSquare));
23 | System.out.println("Is Reduced: " + isInReducedForm(generatedReducedSquare));
24 | printLatinSquare(generatedReducedSquare);
25 | }
26 | public static boolean isLatinSquare(char[][] square){
27 | if (square.length != square[0].length){
28 | return false;
29 | }
30 | for (int i = 0; i < square.length; i++){
31 | for (int j = 0; j < square[i].length; j++){
32 |
33 | for (int k = 0; k < square[i].length; k++){
34 | if (j != k && square[i][k] == square [i][j])
35 | return false;
36 | }
37 | for (int l = 0; l < square[i].length; l++){
38 | if (i != l && square[l][j] == square [i][j])
39 | return false;
40 | }
41 |
42 | }
43 | }
44 | return true;
45 | }
46 | public static boolean isInReducedForm(char[][] square){
47 | //A latin square is in reduced form if the first row and column are in natural order (ABC, for example)
48 | if (!isLatinSquare(square)){
49 | return false;
50 | }
51 | for (int i = 1; i < square.length; i++){
52 | if (square[i-1][0] > square[i][0]){
53 | return false;
54 | }
55 | }
56 | for (int j = 1; j < square[0].length; j++){
57 | if (square[0][j-1] > square[0][j]){
58 | return false;
59 | }
60 | }
61 | return true;
62 | }
63 | public static char[][] reduceLatinSquare(char[][] square){
64 | //Think about how to do this by breaking the problem into pieces
65 | //Assume that the square input is already a latin square
66 | //returning to this later
67 | if (isInReducedForm(square)){
68 | return square;
69 | }
70 |
71 | return square;
72 |
73 | }
74 |
75 | public static char[][] generateFilledLatinSquare(int size){
76 | //all numbers exist an equal amount of times
77 | //this is a brute force method
78 | char[][] square = new char[size][size];
79 | int[] charUsesLeft = populateCharUsesLeft(size);
80 | int tries = 0;
81 | while (!isFilled(square) && tries < Math.pow(2, size + 2)){
82 | //System.out.println("Square isn't filled");
83 | char letter = genChar(size);
84 | //System.out.println("Character Generated is: " + letter);
85 |
86 | while (!charHasUsesLeft(letter, charUsesLeft)){
87 | letter = genChar(size);
88 | //System.out.println("Character Generated is: " + letter);
89 | }
90 |
91 | int x = genRandomCoordinate(size);
92 | int y = genRandomCoordinate(size);
93 | //System.out.println("X: " + x + " and Y: " + y);
94 | if (!isLetter(square[x][y])
95 | && !conflictExists(x, y, letter, square)){
96 | //System.out.println("#Character: " + letter + " Placed in " + x + ", " + y);
97 | square[x][y] = letter;
98 | charUsesLeft[(int)(letter - 65)]--;
99 | }
100 |
101 | if (tries++ == Math.pow(2, size + 1)){
102 | //System.out.println("Resetting Board");
103 | square = new char[size][size];
104 | charUsesLeft = populateCharUsesLeft(size);
105 | tries = 0;
106 | }
107 |
108 | }
109 | return square;
110 | }
111 | public static char[][] generateReducedLatinSquare(int size){
112 | //all numbers exist an equal amount of times
113 | //this is a brute force method
114 | char[][] square = new char[size][size];
115 | int[] charUsesLeft = populateCharUsesLeft(size);
116 | int tries = 0;
117 | while (!isInReducedForm(square)){
118 | while (!isFilled(square) && tries < Math.pow(2, size + 2)){
119 | //System.out.println("Square isn't filled");
120 | char letter = genChar(size);
121 | //System.out.println("Character Generated is: " + letter);
122 |
123 | while (!charHasUsesLeft(letter, charUsesLeft)){
124 | letter = genChar(size);
125 | //System.out.println("Character Generated is: " + letter);
126 | }
127 |
128 | int x = genRandomCoordinate(size);
129 | int y = genRandomCoordinate(size);
130 | //System.out.println("X: " + x + " and Y: " + y);
131 | if (!isLetter(square[x][y])
132 | && !conflictExists(x, y, letter, square)){
133 | //System.out.println("#Character: " + letter + " Placed in " + x + ", " + y);
134 | square[x][y] = letter;
135 | charUsesLeft[(int)(letter - 65)]--;
136 | }
137 |
138 | if (tries++ == Math.pow(2, size + 1)){
139 | //System.out.println("Resetting Board");
140 | square = new char[size][size];
141 | charUsesLeft = populateCharUsesLeft(size);
142 | tries = 0;
143 | }
144 |
145 | }
146 | if (!isInReducedForm(square)){
147 | System.out.println("Square created, though not reduced");
148 | }
149 | }
150 | return square;
151 | }
152 |
153 | public static boolean conflictExists(int x, int y, char ch, char[][] square){
154 | //test row, x fixed
155 | for (int i = 0; i < square.length; i++){
156 | if (i != y && square[x][i] == ch)
157 | return true;
158 | }
159 | //test column, y fixed
160 | for (int j = 0; j < square.length; j++){
161 | if (j != x && square[j][y] == ch)
162 | return true;
163 | }
164 | return false;
165 | }
166 | public static boolean isFilled(char[][] array){
167 | for (int i = 0; i < array.length; i++){
168 | for (int j = 0; j < array[i].length; j++){
169 | if (!isLetter(array[i][j])){
170 | return false;
171 | }
172 | }
173 | }
174 | return true;
175 | }
176 |
177 | public static boolean isLetter(char ch){
178 | return Character.isLetter(ch);
179 | }
180 |
181 | public static char genChar(int size){
182 | return (char)(65 + (int)(Math.random()*(size)));
183 | }
184 |
185 | public static int genRandomCoordinate(int size){
186 | return (char)(int)(Math.random() * (size));
187 | }
188 |
189 | public static boolean charHasUsesLeft(char ch, int usesLeft[]){
190 | return usesLeft[(int)(ch - 65)] > 0;
191 | }
192 |
193 | public static int[] populateCharUsesLeft(int size){
194 | int[] charUsesLeft = new int[size];
195 | for (int i = 0; i < size; i++){
196 | charUsesLeft[i] = size;
197 | }
198 | return charUsesLeft;
199 | }
200 |
201 | public static void printLatinSquare(char[][] square){
202 | for (int i = 0; i < square.length; i++){
203 | for (int j = 0; j < square[i].length; j++){
204 | System.out.print(square[i][j] + " ");
205 | }
206 | System.out.println();
207 | }
208 | }
209 |
210 | }
211 |
212 |
--------------------------------------------------------------------------------
/Chapter7/src/Latin Square/Readme.md:
--------------------------------------------------------------------------------
1 | #Latin Square
2 |
3 | First attempt at generating and reducing latin squares
4 |
5 | I can generate latin squares up to 5 or 6
6 |
7 | But can only generate reduced latin squares up to 2x2
8 |
9 | Method for reducing an already complete square has not been attempted
--------------------------------------------------------------------------------
/Chapter8/Readme.md:
--------------------------------------------------------------------------------
1 | #Objects and Classes
2 |
3 | After you finish this chapter, you'll have enough knowledge to be considered proficient in Java. Don't get me wrong, there's still plenty of value in this book, but, with an understanding of objects and classes, you'll be ready to develop real programs.
4 |
5 | The above statement is a bit of a stretch, but the point is to get you to devour this chapter and to realize that you can do practically anything with Java. You can program anything, it's just a matter of breaking problems down and then writing code. You might be thinking, "Well, how am I going to make if all I'm doing is writing loops that print out patterns and solve trivial math problems?"
6 |
7 | What you've learned so far is only the tip of the iceberg, and it only gets better from here. While most would agree that it's not a good idea to stop reading after chapter 8, you're well on your way, and it only gets more fun from here on out.
8 |
9 | A Class is a template for creating objects. Like in the book, you can create a class called Circle which will provide a type of blueprint (including constructors) for creating Circle objects.
10 |
11 | ```
12 | class Circle{
13 | double radius = 1;
14 |
15 | Circle(double newRadius){
16 | radius = newRadius;
17 | }
18 | double getArea(){
19 | return radius * radius * Math.PI;
20 | }
21 | double setRadius(double newRadius){
22 | radius = newRadius;
23 | }
24 | }
25 | ```
26 | Creating an object of a certain type is comparable to initializing an array, which is an object. `String[] stringArray = new String[5];` could be used as a reference for when you instantiate an object- `Circle myCircle = new Circle(5);`
27 |
28 | Let's combine the two examples in the most contrived manner possible. Assuming that myCircle has been created: `String[] stringArray = new String[(int)myCircle.getArea()];` That works. Onward.
29 |
30 | A file can only have one public class, the name of which must match the file name.
31 |
32 | ###Constructors
33 | A constructor has the same name as the class it is constructing objects of, has no return type, and are invoked using the new operator (as seen above).
34 |
35 | The dot operator is used heavily when dealing with objects. It is used to reference variables `object.dataField` and invoke methods `object.doSomething()`. Instance methods and variables are invoked and exist within the object they are created. For example, obj1.setRadius(5) would only change the radius for obj1, not obj2.
36 |
37 | ##Using Classes from the Java Library
38 |
39 | The Java API is huge, expansive, and extremely intimidating. The good news is that you probably won't be quizzed on the functions of a particular class at any time. While some classes (Date, Random, Math) are used extensively in intro classes, you don't have to commit every detail to memory. The great news is that the API is what makes Java such a great language- there is just so much at your fingertips ready to be imported, and even more available online.
40 |
41 | ##Static Keyword
42 |
43 | This is one of the most important parts of understanding objects. Just like instance variables are unique to an instance, static variables aren't- they're shared by all objects of the class. Likewise, static methods affect static variables, and can't touch instance variables. It took me much longer than I'd like to admit to fully understand this concept.
44 |
45 | With the help of the compiler and through a good amount of practice, you should be fine. After long enough, it just starts to make sense, and the errors will be caught, often just needing a keyword appended or deleted.
46 |
47 | It's worth mentioning that instance methods can access and invoke instance and static variables and methods. Teachers love to quiz on that.
48 |
49 |
50 |
--------------------------------------------------------------------------------
/Errata:
--------------------------------------------------------------------------------
1 | Errata
2 |
3 | Chapter 1
4 | Page 23, Figure 1.18, the title in the screen shot should be Message, not "Display Message".
5 |
6 | Chapter 2
7 | Page 42, line 4 and 5, change int to double.
8 |
9 | Chapter 3
10 | Page 97, click here to see a new BMI table.
11 |
12 | Page 100, line 10, insert the symbol " at the end of the line.
13 |
14 | Page 104, three lines before check point 3.18, change "Java also provides conditional" to "Java also provides unconditional".
15 |
16 | Page 106, in the output box for the first sample run for Listing 3.8, change 2012 to 2008.
17 |
18 | Page 131, line 3, change Package 1 to Package 2.
19 |
20 | Chapter 4
21 | Page 136, in Listing 4.1, lines 5-6, change % to *.
22 |
23 | Page 167, in Exercise 4.5, delete "and that 1 pound is .45 kilograms."
24 |
25 | Page 168, in Exercise 4.6, delete "and that 1 kilometer is .621 miles."
26 |
27 | Chapter 5
28 | Page 182, 10 line above Figure 5.3, change "and then" to "and then m2 calls".
29 |
30 | Page 186, line -2, remove the question mark (?).
31 |
32 | Chapter 6
33 | Page 229, in line 11, missing a semicolon after myList[i] = myList[j].
34 |
35 | Page 259, in the sample run box for Exercise 6.21, change 7 to 8.
36 |
37 | Chapter 7
38 | Page 291, line 3, change 3-by-4 to 3-by-3.
39 |
40 | Page 291, in the first line after the method header in Exercise 7.29, change "enter two lists" to "enter two arrays."
41 |
42 | Chapter 9
43 | Page 360, in line 14 in Listing 9.5, change it to "Split items from a string".
44 |
45 | Chapter 10
46 | Page 382, in Figure 10.6, swap the last two numerals 1 and 1.. 3 in the diagram.
47 |
48 | Page 394, in Figure 10.14, longVlaue should be longValue.
49 |
50 | Chapter 11
51 | Page 422, the first line in the output box for Listing 11.5, change "Color is white" to ""Color is red."
52 |
53 | Chapter 12
54 | Page 473, line 1, change java.swing to javax.swing.
55 |
56 | Chapter 13
57 | Page 504, Insert the following paragraph before Section 13.10.
58 |
59 | "The current hour, minute, and second is obtained by using the GregorianCalendar class (lines 102-110). The GregorianCalendar class in the Java API enables you to create a Calendar instance for the current time using its no-arg constructor. You can then use its methods get(Calendar.HOUR), get(Calendar.MINUTE), and get(Calendar.SECOND) to return the hour, minute, and second from a Calendar object. The classes Calendar and GregorianCalendar class will be further discussed in Section 15.4."
60 |
61 | Page 513, line -2, change hexagon to octagon.
62 |
63 | Chapter 14
64 | Page 539, line 13 in Listing 14.12, change CircleWithException to CircleWithCustomException.
65 |
66 | Page 540, change 0 to 1 in the output box for Listing 14.12.
67 |
68 | Page 544, in Listing 14.13, line 2, change IoException to Exception.
69 |
70 | Chapter 15
71 | Page 566, line -2, Change the sentence to "Key Point for Section 15.7 to "If the doubleValue() method were not defined in the Number class, you will not ..."
72 |
73 | Page 577, Replace Check Point Question 15.21 with a new one here.
74 |
75 | Page 577, Change the Key Point for Section 15.7 to "The Cloneable interface specifies that an object can be cloned."
76 |
77 | Chapter 16
78 | Page 603, Table 16.1, "select a new item ..." is listed twice. delete the first one.
79 |
80 | Chapter 18
81 | Page 704, in Figure 18.28, change number of slots in the text field from 7/9/12 to 6/8/11.
82 |
83 | Page 706, Change "Redraw Rectangles" to "Redraw Triangles" in Figure 18.32c.
84 |
85 | Chapter 19
86 | Page 715, code listing line 23 in Listing 19.1, change "Close the output stream" to "Close the input stream."
87 |
88 | Page 726, line 10 in Listing 19.6, change the comment to "Read a string, double value, and object from the file".
89 |
90 | Chapter 21
91 | Page 777, the comment line in line 41, change list[i+1...list.length-2] to list[i...list.length-1].
92 |
93 | Chapter 22
94 | Page 827, Figure 22.20, change "experssion" to "expression".
95 |
96 | Chapter 23
97 | Page 831, two lines before Listing 23.1, change an iterator to a for-each loop.
98 |
99 | Chapter 25
100 | Page 922, line 7, change descending to ascending.
101 |
102 | Chapter 26
103 | Page 939, In Figure 26.8, change "last = head" to "tail = head".
104 |
105 | Chapter 27
106 | Page 995, Programming Exercise 27.22, change the number of objects from 5 to 6 in the Sample Output.
107 |
108 | Page 995, Programming Exercise 27.23, change the number of objects from 5 to 6 in the Sample Output.
109 |
110 | Chapter 28
111 | Page 1004, in Figure 28.6, change key:27 to key:28 and change key:23 to key:21.
112 |
113 | Chapter 32
114 | Page 1038, in line 2 in the first paragraph in Section 32.6, change "the most" to "most".
115 |
116 | Chapter 34
117 | Page 1235, first line in the second paragraph in Section 34.5, change preparedStatement to prepareStatement.
118 |
119 | Page 1236, three lines before Listing 34.3, change preparedStatement to prepareStatement.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Intro-to-Java-Programming-9E
2 | ============================
3 |
4 | My solutions to book problems, descriptions of concepts, simple projects, and all of the bonus chapters
5 |
6 |
7 | ####**Introduction to Java Programming 9th Edition**
8 | ####**Y. Daniel Liang**
9 |
10 |
11 | I've decided to revisit this textbook and share my solutions to various exercises, some projects inspired by the book, and in-depth explanations of difficult concepts. All work is my own unless marked or provided by Liang in the book. While Liang does indeed provide answers to even numbered problems, I have not referenced or included any of his answers. My depth of coverage will be sporadic.
12 |
13 | This project should be treated as a continual work in progress. I hope this will help you to better understand Java concepts, regardless of your current knowledge or interest level. Programming is one of my greatest joys and, if it isn't already one of yours, perhaps this will bring you one step closer.
14 |
15 | The 10th Edition is now available, and I'm sure that many of the chapters, problems, and, especially, concepts, will be almost identical.
16 |
17 | ###**Here are some online resources that I like:**
18 |
19 | - [Book Website](http://bit.ly/1x8ldY3)
20 | - [New Book](http://amzn.to/1pFMWzv)
21 | - [API Docs](http://bit.ly/1dhrzxG)
22 | - [Java Style](http://bit.ly/1ltYduk)
23 | - [Introduction to Programming Using Java 6E](http://bit.ly/1rRCIsS)
24 | - [Best Free IDE](http://bit.ly/1960oTf)
25 | - [Interactive Practice](http://bit.ly/1jQLwbR)
26 | - [Interactive Tutorials](http://bit.ly/1jBaDRx)
27 | - [Reddit Community](http://bit.ly/1pUBJXz)
28 |
29 |
30 | ###**Other Suggested Repos:**
31 |
32 | Building . . .
33 |
34 | ###**Table of Contents:**
35 |
36 | - Chapter 1 Introduction to Computers, Programs, and Java
37 | - Chapter 2 Elementary Programming
38 | - Chapter 3 Selections
39 | - Chapter 4 Loops
40 | - Chapter 5 Methods
41 | - Chapter 6 Single-Dimensional Arrays
42 | - Chapter 7 Multidimensional Arrays
43 | - Chapter 8 Objects and Classes
44 | - Chapter 9 Strings
45 | - Chapter 10 Thinking in Objects
46 | - Chapter 11 Inheritance and Polymorphism
47 | - Chapter 12 GUI Basics
48 | - Chapter 13 Graphics
49 | - Chapter 14 Exception Handling and Text I/O
50 | - Chapter 15 Abstract Classes and Interfaces
51 | - Chapter 16 Event-Driven Programming
52 | - Chapter 17 GUI Components
53 | - Chapter 18 Applets and Multimedia
54 | - Chapter 19 Binary I/O
55 | - Chapter 20 Recursion
56 | - Chapter 21 Generics
57 | - Chapter 22 Lists, Stacks, Queues, and Priority Queues
58 | - Chapter 23 Sets and Maps
59 | - Chapter 24 Developing Efficient Algorithms
60 | - Chapter 25 Sorting
61 | - Chapter 26 Implementing Lists, Stacks, Queues, and Priority Queues
62 | - Chapter 27 Binary Search Trees
63 | - Chapter 28 Hashing
64 | - Chapter 29 AVL Trees
65 | - Chapter 30 Graphs and Applications
66 | - Chapter 31 Weighted Graphs and Applications
67 | - Chapter 32 Multithreading and Parallel Programming
68 | - Chapter 33 Networking
69 | - Chapter 34 Java Database Programming
70 |
71 | ###**Bonus Chapters 35–50:**
72 |
73 | - Chapter 35 Internationalization
74 | - Chapter 36 JavaBeans
75 | - Chapter 37 Containers, Layout Managers, and Borders
76 | - Chapter 38 Menus, Toolbars, and Dialogs
77 | - Chapter 39 MVC and Swing Models
78 | - Chapter 40 JTable and JTree
79 | - Chapter 41 Advanced Database Programming
80 | - Chapter 42 Servlets
81 | - Chapter 43 JavaServer Pages
82 | - Chapter 44 JavaServer Faces
83 | - Chapter 45 Web Services
84 | - Chapter 46 Remote Method Invocation
85 | - Chapter 47 2-4 Trees and B-Trees
86 | - Chapter 48 Red-Black Trees
87 | - Chapter 49 Java 2D
88 | - Chapter 50 Testing Using JUnit
89 |
90 | ###**Appendix:**
91 |
92 | - Appendix A Java Keywords
93 | - Appendix B The ASCII Character Set
94 | - Appendix C Operator Precedence Chart
95 | - Appendix D Java Modifiers
96 | - Appendix E Special Floating-Point Values
97 | - Appendix F Number Systems
98 | - Appendix G Bitwise Operations
99 |
--------------------------------------------------------------------------------
/Supplements/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/.DS_Store
--------------------------------------------------------------------------------
/Supplements/Case Studies/AddressBook.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Case Studies/AddressBook.pdf
--------------------------------------------------------------------------------
/Supplements/Case Studies/KnightTour.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Knight Tour by Y. Daniel Liang (Note: the keys are integers)
6 |
7 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Supplements/Case Studies/KnightTour.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Case Studies/KnightTour.pdf
--------------------------------------------------------------------------------
/Supplements/Case Studies/Sudoku.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Case Studies/Sudoku.pdf
--------------------------------------------------------------------------------
/Supplements/Database Supplements/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Database Supplements/.DS_Store
--------------------------------------------------------------------------------
/Supplements/Database Supplements/Access.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Database Supplements/Access.pdf
--------------------------------------------------------------------------------
/Supplements/Database Supplements/AdvancedSQL.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Database Supplements/AdvancedSQL.pdf
--------------------------------------------------------------------------------
/Supplements/Database Supplements/DatabaseDesign.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Database Supplements/DatabaseDesign.pdf
--------------------------------------------------------------------------------
/Supplements/Database Supplements/IntroductionToDB.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Database Supplements/IntroductionToDB.pdf
--------------------------------------------------------------------------------
/Supplements/Database Supplements/MySQL.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Database Supplements/MySQL.pdf
--------------------------------------------------------------------------------
/Supplements/Database Supplements/Oracle10g.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Database Supplements/Oracle10g.pdf
--------------------------------------------------------------------------------
/Supplements/Database Supplements/RelationalDataModel.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Database Supplements/RelationalDataModel.pdf
--------------------------------------------------------------------------------
/Supplements/Database Supplements/SQLBasics.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Database Supplements/SQLBasics.pdf
--------------------------------------------------------------------------------
/Supplements/Database Supplements/createsampletables_access.txt:
--------------------------------------------------------------------------------
1 | /* Student information database tables */
2 | drop table Department;
3 | drop table Enrollment;
4 | drop table TaughtBy;
5 | drop table Faculty;
6 | drop table Subject;
7 | drop table Student;
8 | drop table Course;
9 | drop table College;
10 |
11 | create table Department (
12 | deptId char(4) not null,
13 | name varchar(25) unique,
14 | chairId varchar(9),
15 | collegeId varchar(4),
16 | constraint pkDepartment primary key (deptId));
17 |
18 | create table Enrollment (
19 | ssn char(9) not null,
20 | courseId char(5) not null,
21 | dateRegistered date,
22 | grade char(1),
23 | constraint pkEnrollment primary key (ssn, courseId));
24 |
25 | create table TaughtBy (
26 | courseId char(5),
27 | ssn char(9));
28 |
29 | create table Faculty(
30 | ssn char(9) not null,
31 | firstName varchar(25),
32 | mi char(1),
33 | lastName varchar(25),
34 | phone char(10),
35 | email varchar(50),
36 | office varchar(15),
37 | startTime date,
38 | rank varchar(20),
39 | salary double,
40 | deptId char(4),
41 | constraint pkFaculty primary key (ssn));
42 |
43 | create table Subject (
44 | subjectId char(4) not null,
45 | name varchar(25),
46 | startTime date,
47 | deptId char(4),
48 | constraint pkSubject primary key (subjectId));
49 |
50 | create table Course(
51 | courseId char(5) not null,
52 | subjectId char(4) not null,
53 | courseNumber integer,
54 | title varchar(50) not null,
55 | numOfCredits integer,
56 | constraint pkCourse primary key (courseId),
57 | constraint fkSubjectId foreign key (subjectId)
58 | references Subject(subjectId));
59 |
60 | create table Student (
61 | ssn char(9) not null,
62 | firstName varchar(25),
63 | mi char(1),
64 | lastName varchar(25),
65 | phone char(11),
66 | birthDate date,
67 | street varchar(100),
68 | zipCode char(5),
69 | deptId char(4),
70 | constraint pkStudent primary key (ssn),
71 | constraint fkDeptId foreign key (deptId)
72 | references Department(deptId));
73 |
74 | create table College(
75 | collegeId char(4) not null,
76 | name varchar(25) not null,
77 | since date,
78 | deanId char(9),
79 | constraint pkCollege primary key (collegeId),
80 | constraint fkDeanId foreign key (deanId)
81 | references Faculty(ssn));
82 |
83 | insert into Department values (
84 | 'CS', 'Computer Science', '111221115', 'SC');
85 | insert into Department values (
86 | 'MATH', 'Mathematics', '111221116', 'SC');
87 | insert into Department values (
88 | 'BIOL', 'Biology', '111221118', 'SC');
89 | insert into Department values (
90 | 'CHEM', 'Chemistry', '111221119', 'SC');
91 |
92 | insert into Enrollment values (
93 | '444111110', '11111', date(), 'A');
94 | insert into Enrollment values (
95 | '444111110', '11112', date(), 'B');
96 | insert into Enrollment values (
97 | '444111110', '11113', date(), 'C');
98 | insert into Enrollment values (
99 | '444111111', '11111', date(), 'D');
100 | insert into Enrollment values (
101 | '444111111', '11112', date(), 'F');
102 | insert into Enrollment values (
103 | '444111111', '11113', date(), 'A');
104 | insert into Enrollment values (
105 | '444111112', '11114', date(), 'B');
106 | insert into Enrollment values (
107 | '444111112', '11115', date(), 'C');
108 | insert into Enrollment values (
109 | '444111112', '11116', date(), null);
110 | insert into Enrollment values (
111 | '444111113', '11111', date(), null);
112 | insert into Enrollment values (
113 | '444111113', '11113', date(), null);
114 | insert into Enrollment values (
115 | '444111114', '11115', date(), null);
116 | insert into Enrollment values (
117 | '444111115', '11115', date(), null);
118 | insert into Enrollment values (
119 | '444111115', '11116', date(), null);
120 | insert into Enrollment values (
121 | '444111116', '11111', date(), null);
122 | insert into Enrollment values (
123 | '444111117', '11111', date(), null);
124 | insert into Enrollment values (
125 | '444111118', '11111', date(), null);
126 | insert into Enrollment values (
127 | '444111118', '11112', date(), null);
128 | insert into Enrollment values (
129 | '444111118', '11113', date(), null);
130 |
131 | insert into TaughtBy values (
132 | '11111', '111221111');
133 | insert into TaughtBy values (
134 | '11112', '111221111');
135 | insert into TaughtBy values (
136 | '11113', '111221111');
137 | insert into TaughtBy values (
138 | '11114', '111221115');
139 | insert into TaughtBy values (
140 | '11115', '111221110');
141 | insert into TaughtBy values (
142 | '11116', '111221115');
143 | insert into TaughtBy values (
144 | '11117', '111221116');
145 | insert into TaughtBy values (
146 | '11118', '111221112');
147 |
148 | insert into Subject values (
149 | 'CSCI', 'Computer Science', '1980-08-01', 'CS');
150 | insert into Subject values (
151 | 'ITEC', 'Information Technology', '2002-01-01', 'CS');
152 | insert into Subject values (
153 | 'MATH', 'Mathematical Science', '1935-08-01', 'MATH');
154 | insert into Subject values (
155 | 'STAT', 'Statistics', '1980-08-01', 'MATH');
156 | insert into Subject values (
157 | 'EDUC', 'Education', '1980-08-01', 'EDUC');
158 |
159 | insert into Faculty values (
160 | 111221110, 'Patty', 'R', 'Smith', '9129215555',
161 | 'patty@yahoo.com', 'SC129', '1976-10-11',
162 | 'Full Professor', 60000, 'MATH');
163 | insert into Faculty values (
164 | 111221111, 'George', 'P', 'Franklin', '9129212525',
165 | 'george@yahoo.com', 'SC130', '1986-10-12',
166 | 'Associate Professor', 65000, 'CS');
167 | insert into Faculty values (
168 | 111221112, 'Jean', 'D', 'Yao', '9129215556',
169 | 'jean@yahoo.com', 'SC131', '1976-08-13',
170 | 'Full Professor', 65000, 'MATH');
171 | insert into Faculty values (
172 | '111221113', 'Frank', 'E', 'Goldman', '9129215557',
173 | 'frank@yahoo.com', 'SC132', '1996-01-14',
174 | 'Assistant Professor', 60000, 'BUSS');
175 | insert into Faculty values (
176 | '111221114', 'Steve', 'T', 'Templeton', '9129215558',
177 | 'steve@yahoo.com', 'UH132', '1998-01-01',
178 | 'Assistant Professor', 60000, 'BUSS');
179 | insert into Faculty values (
180 | '111221115', 'Alex', 'T', 'Bedat', '9129215559',
181 | 'alex@yahoo.com', 'SC133', '2000-01-01',
182 | 'Full Professor', 95000, 'CS');
183 | insert into Faculty values (
184 | '111221116', 'Judy', 'T', 'Woo', '9129215560',
185 | 'judy@yahoo.com', 'SC133', '2000-01-01',
186 | 'Full Professor', 55000, 'MATH');
187 | insert into Faculty values (
188 | '111221117', 'Joe', 'R', 'Chang', '9129215561',
189 | 'joe@yahoo.com', 'ED133', 'JAN-2000-01-01',
190 | 'Full Professor', 55000, 'EDUC');
191 | insert into Faculty values (
192 | '111221118', 'Francis', 'R', 'Chin', '9129215562',
193 | 'joe@yahoo.com', 'ED133', '1989-01-01',
194 | 'Full Professor', 75000, 'BIOL');
195 | insert into Faculty values (
196 | '111221119', 'Ray', 'R', 'Smith', '9129215563',
197 | 'ray@yahoo.com', 'SC133', '1994-01-01',
198 | 'Full Professor', 85000, 'CHEM');
199 |
200 | insert into Student values (
201 | '444111110', 'Jacob', 'R', 'Smith', null,
202 | '1985-04-09', '99 Kingston Street', '31435', 'BIOL');
203 | insert into Student values (
204 | '444111111', 'John', 'K', 'Stevenson', '9129219434',
205 | null, '100 Main Street', '31411', 'BIOL');
206 | insert into Student values (
207 | '444111112', 'George', 'R', 'Heintz', '9129213454',
208 | '1974-10-10', '1200 Abercorn Street', '31419', 'CS');
209 | insert into Student values (
210 | '444111113', 'Frank', 'E', 'Jones', '9125919434',
211 | '1970-09-09', '100 Main Street', '31411', 'BIOL');
212 | insert into Student values (CHE
213 | '444111114', 'Jean', 'K', 'Smith', '9129219434',
214 | '1970-02-09', '100 Main Street', '31411', 'CHEM');
215 | insert into Student values (
216 | '444111115', 'Josh', 'R', 'Woo', '7075989434',
217 | '1970-02-09', '555 Franklin Street', '31411', 'CHEM');
218 | insert into Student values (
219 | '444111116', 'Josh', 'R', 'Smith', '9129219434',
220 | '1973-02-09', '100 Main Street', '31411', 'BIOL');
221 | insert into Student values (
222 | '444111117', 'Joy', 'P', 'Kennedy', '9129229434',
223 | '1974-03-19', '103 Bay Street', '31412', 'CS');
224 | insert into Student values (
225 | '444111118', 'Toni', 'R', 'Peterson', '9129229434',
226 | '1964-04-29', '103 Bay Street', '31412', 'MATH');
227 | insert into Student values (
228 | '444111119', 'Patrick', 'R', 'Stoneman', null,
229 | '1969-04-29', '101 Washington Street', '31435', 'MATH');
230 | insert into Student values (
231 | '444111120', 'Rick', 'R', 'Carter', null,
232 | '1986-04-09', '19 West Ford Street', '31411', 'BIOL');
233 |
234 | insert into Course values (
235 | '11111', 'CSCI', '1301', 'Intro to Java I', 4);
236 | insert into Course values (
237 | '11112', 'CSCI', '1302', 'Intro to Java II', 3);
238 | insert into Course values (
239 | '11113', 'CSCI', '4720', 'Database Systems', 3);
240 | insert into Course values (
241 | '11114', 'CSCI', '4750', 'Rapid Java Application', 3);
242 | insert into Course values (
243 | '11115', 'MATH', '2750', 'Calculus I', 5);
244 | insert into Course values (
245 | '11116', 'MATH', '3750', 'Calculus II', 5);
246 | insert into Course values (
247 | '11117', 'EDUC', '1111', 'Reading', 3);
248 | insert into Course values (
249 | '11118', 'ITEC', '1344', 'Database Administration', 3);
250 |
251 | insert into College values (
252 | 'SC', 'Science', '1994-01-01','111221110');
253 | insert into College values (
254 | 'NURS', 'Nursing', '1994-01-01',null);
255 | insert into College values (
256 | 'EDUC', 'Education', '1994-01-01', '111221117');
257 | insert into College values (
258 | 'BUSS', 'Business', '1994-01-01', '111221114');
259 |
260 | /* For exercise 34.7 in intro6e */
261 | drop table csci4990;
262 | drop table csci1301;
263 | drop table csci1302;
264 |
265 | create table csci4990 (
266 | ssn char(11),
267 | name varchar2(25),
268 | score number);
269 |
270 | insert into csci4990 values (
271 | '111-22-3333', 'John F Smith', 85.4);
272 |
273 | create table csci1301 (
274 | ssn char(11),
275 | name varchar2(25),
276 | score number);
277 |
278 | insert into csci1301 values (
279 | '111-22-3333', 'John F Smith', 75.4);
280 |
281 | create table csci1302 (
282 | ssn char(11),
283 | name varchar2(25),
284 | score number);
285 |
286 | insert into csci1302 values (
287 | '111-22-3333', 'John F Smith', 55.4);
288 |
289 | /* For exercise 34.8 in intro6e */
290 | drop table Account;
291 |
292 | create table Account (
293 | username varchar(20),
294 | password varchar2(25),
295 | name varchar(20));
296 |
297 | insert into Account values (
298 | 'javaman', 'namavaj', 'John F. Smith');
299 |
300 | /* For the example in ch34 in intro6e */
301 | create table Address (
302 | firstname varchar(25),
303 | mi char(1),
304 | lastname varchar(25),
305 | street varchar(40),
306 | city varchar(20),
307 | state varchar(2),
308 | zip varchar(5),
309 | telephone varchar(10),
310 | email varchar(30)
311 | )
312 |
313 | /* For the AGS */
314 | create table AGSStudent (
315 | username varchar(50) not null,
316 | password varchar(50) not null,
317 | fullname varchar(100) not null,
318 | email varchar(100) not null,
319 | instructorEmail varchar(100) not null,
320 | whichBook varchar(50),
321 | whenCreated datetime,
322 | constraint pkAGSStudent primary key (username)
323 | );
324 |
325 | /* For the AGS */
326 | create table AGSInstructor (
327 | username varchar(50),
328 | password varchar(50),
329 | fullname varchar(100),
330 | instructorEmail varchar(100),
331 | school varchar(50),
332 | state varchar(25),
333 | country varchar(25),
334 | whichBook varchar(50),
335 | whenCreated datetime,
336 | emailOn bit,
337 | constraint pkAGSInstructor primary key (instructorEmail)
338 | );
339 |
340 | /* For the AGS */
341 | create table AGSLog (
342 | username varchar(50),
343 | exerciseName varchar(15),
344 | sourceCode varchar(4000),
345 | runCorrect bit,
346 | lastRun datetime,
347 | constraint pkLog primary key (username, exerciseName)
348 | );
349 |
350 | /* For the AGS */
351 | create table AGSStudentRole (
352 | username varchar(25),
353 | rolename varchar(25),
354 | constraint pkAGSStudentRole primary key (username, rolename)
355 | );
356 |
357 | create table Student1 (
358 | username varchar(50) not null,
359 | password varchar(50) not null,
360 | fullname varchar(200) not null,
361 | constraint pkStudent primary key (username)
362 | );
363 |
364 | insert into Student1 values ('abc1', 'abc', 'John Smith');
365 | insert into Student1 values ('abc2', 'abc', 'Peter Smith');
366 | insert into Student1 values ('abc3', 'abc', 'Jane Paul');
367 |
368 | create table Student2 (
369 | username varchar(50) not null,
370 | password varchar(50) not null,
371 | firstname varchar(100),
372 | lastname varchar(100),
373 | constraint pkStudent primary key (username)
374 | );
375 |
--------------------------------------------------------------------------------
/Supplements/Database Supplements/createsampletables_mysql.txt:
--------------------------------------------------------------------------------
1 | /* Save and run the entire script in one file */
2 | /* Student information database tables
3 | Run this from the root */
4 |
5 | drop database javabook;
6 | create database javabook;
7 | create user 'scott'@'localhost' identified by 'tiger';
8 | grant select, insert, update, delete, create, create view, drop,
9 | execute, references on javabook.* to 'scott'@'localhost';
10 |
11 | use javabook;
12 |
13 | drop table if exists Student;
14 | drop table if exists Course;
15 | drop table if exists College;
16 | drop table if exists Subject;
17 | drop table if exists Department;
18 | drop table if exists Enrollment;
19 | drop table if exists TaughtBy;
20 | drop table if exists Faculty;
21 |
22 | create table Department (
23 | deptId char(4) not null,
24 | name varchar(25) unique, /* works in MYSQL */
25 | chairId varchar(9),
26 | collegeId varchar(4),
27 | constraint pkDepartment primary key (deptId));
28 |
29 | create table Enrollment (
30 | ssn char(9) not null,
31 | courseId char(5) not null,
32 | dateRegistered date,
33 | grade char(1),
34 | constraint pkEnrollment primary key (ssn, courseId));
35 |
36 | create table TaughtBy (
37 | courseId char(5),
38 | ssn char(9));
39 |
40 | create table Faculty(
41 | ssn char(9) not null,
42 | firstName varchar(25),
43 | mi char(1),
44 | lastName varchar(25),
45 | phone char(10),
46 | email varchar(50),
47 | office varchar(15),
48 | startTime date,
49 | rank varchar(20),
50 | salary double,
51 | deptId char(4),
52 | constraint pkFaculty primary key (ssn));
53 |
54 | create table Subject (
55 | subjectId char(4) not null,
56 | name varchar(25),
57 | startTime date,
58 | deptId char(4),
59 | constraint pkSubject primary key (subjectId));
60 |
61 | create table Student (
62 | ssn char(9) not null,
63 | firstName varchar(25),
64 | mi char(1),
65 | lastName varchar(25),
66 | phone char(11),
67 | birthDate date,
68 | street varchar(100),
69 | zipCode char(5),
70 | deptId char(4),
71 | constraint pkStudent primary key (ssn),
72 | constraint fkDeptId foreign key (deptId)
73 | references Department(deptId));
74 |
75 | create table Course(
76 | courseId char(5),
77 | subjectId char(4) not null,
78 | courseNumber integer,
79 | title varchar(50) not null,
80 | numOfCredits integer,
81 | constraint pkCourse primary key (courseId),
82 | constraint fkSubjectId foreign key (subjectId)
83 | references Subject(subjectId));
84 |
85 | create table College(
86 | collegeId char(4) not null,
87 | name varchar(25) not null,
88 | since date,
89 | deanId char(9),
90 | constraint pkCollege primary key (collegeId),
91 | constraint fkDeanId foreign key (deanId)
92 | references Faculty(ssn));
93 |
94 | insert into Department values (
95 | 'CS', 'Computer Science', '111221115', 'SC');
96 | insert into Department values (
97 | 'MATH', 'Mathematics', '111221116', 'SC');
98 | insert into Department values (
99 | 'CHEM', 'Chemistry', '111225555', 'SC');
100 | insert into Department values (
101 | 'EDUC', 'Education', '333114444', 'EDUC');
102 | insert into Department values (
103 | 'ACCT', 'Accounting', '333115555', 'BUSS');
104 | insert into Department values (
105 | 'BIOL', 'Biology', '111225555', 'SC');
106 |
107 |
108 | insert into Enrollment values (
109 | '444111110', '11111', now(), 'A');
110 | /* In MS Access, replace now() by date() */
111 | insert into Enrollment values (
112 | '444111110', '11112', now(), 'B');
113 | insert into Enrollment values (
114 | '444111110', '11113', now(), 'C');
115 | insert into Enrollment values (
116 | '444111111', '11111', now(), 'D');
117 | insert into Enrollment values (
118 | '444111111', '11112', now(), 'F');
119 | insert into Enrollment values (
120 | '444111111', '11113', now(), 'A');
121 | insert into Enrollment values (
122 | '444111112', '11111', now(), null);
123 | insert into Enrollment values (
124 | '444111112', '11112', now(), null);
125 | insert into Enrollment values (
126 | '444111112', '11114', now(), 'B');
127 | insert into Enrollment values (
128 | '444111112', '11115', now(), 'C');
129 | insert into Enrollment values (
130 | '444111112', '11116', now(), null);
131 | insert into Enrollment values (
132 | '444111113', '11111', now(), null);
133 | insert into Enrollment values (
134 | '444111113', '11113', now(), null);
135 | insert into Enrollment values (
136 | '444111114', '11115', now(), null);
137 | insert into Enrollment values (
138 | '444111115', '11115', now(), null);
139 | insert into Enrollment values (
140 | '444111115', '11116', now(), null);
141 | insert into Enrollment values (
142 | '444111116', '11111', now(), null);
143 | insert into Enrollment values (
144 | '444111117', '11111', now(), null);
145 | insert into Enrollment values (
146 | '444111118', '11111', now(), null);
147 | insert into Enrollment values (
148 | '444111118', '11112', now(), null);
149 | insert into Enrollment values (
150 | '444111118', '11113', now(), null);
151 |
152 | insert into TaughtBy values (
153 | '11111', '111221111');
154 | insert into TaughtBy values (
155 | '11112', '111221111');
156 | insert into TaughtBy values (
157 | '11113', '111221111');
158 | insert into TaughtBy values (
159 | '11114', '111221115');
160 | insert into TaughtBy values (
161 | '11115', '111221110');
162 | insert into TaughtBy values (
163 | '11116', '111221115');
164 | insert into TaughtBy values (
165 | '11117', '111221116');
166 | insert into TaughtBy values (
167 | '11118', '111221112');
168 |
169 | insert into Subject values (
170 | 'CSCI', 'Computer Science', '1980-08-01', 'CS');
171 | insert into Subject values (
172 | 'ITEC', 'Information Technology', '2002-01-01', 'CS');
173 | insert into Subject values (
174 | 'MATH', 'Mathematical Science', '1935-08-01', 'MATH');
175 | insert into Subject values (
176 | 'STAT', 'Statistics', '1980-08-01', 'MATH');
177 | insert into Subject values (
178 | 'EDUC', 'Education', '1980-08-01', 'EDUC');
179 |
180 | insert into Faculty values (
181 | 111221110, 'Patty', 'R', 'Smith', '9129215555',
182 | 'patty@yahoo.com', 'SC129', '1976-10-11',
183 | 'Full Professor', 60000, 'MATH');
184 | insert into Faculty values (
185 | 111221111, 'George', 'P', 'Franklin', '9129212525',
186 | 'george@yahoo.com', 'SC130', '1986-10-12',
187 | 'Associate Professor', 65000, 'CS');
188 | insert into Faculty values (
189 | 111221112, 'Jean', 'D', 'Yao', '9129215556',
190 | 'jean@yahoo.com', 'SC131', '1976-08-13',
191 | 'Full Professor', 65000, 'MATH');
192 | insert into Faculty values (
193 | '111221113', 'Frank', 'E', 'Goldman', '9129215557',
194 | 'frank@yahoo.com', 'SC132', '1996-01-14',
195 | 'Assistant Professor', 60000, 'ACCT');
196 | insert into Faculty values (
197 | '111221114', 'Steve', 'T', 'Templeton', '9129215558',
198 | 'steve@yahoo.com', 'UH132', '1998-01-01',
199 | 'Assistant Professor', 60000, 'ACCT');
200 | insert into Faculty values (
201 | '111221115', 'Alex', 'T', 'Bedat', '9129215559',
202 | 'alex@yahoo.com', 'SC133', '2000-01-01',
203 | 'Full Professor', 95000, 'CS');
204 | insert into Faculty values (
205 | '111221116', 'Judy', 'T', 'Woo', '9129215560',
206 | 'judy@yahoo.com', 'SC133', '2000-01-01',
207 | 'Full Professor', 55000, 'MATH');
208 | insert into Faculty values (
209 | '111221117', 'Joe', 'R', 'Chang', '9129215561',
210 | 'joe@yahoo.com', 'ED133', '2000-01-01',
211 | 'Full Professor', 55000, 'EDUC');
212 | insert into Faculty values (
213 | '111221118', 'Francis', 'R', 'Chin', '9129215562',
214 | 'joe@yahoo.com', 'ED133', '1989-01-01',
215 | 'Full Professor', 75000, 'BIOL');
216 | insert into Faculty values (
217 | '111221119', 'Ray', 'R', 'Smith', '9129215563',
218 | 'ray@yahoo.com', 'SC133', '1994-01-01',
219 | 'Full Professor', 85000, 'CHEM');
220 |
221 | insert into Student values (
222 | '444111110', 'Jacob', 'R', 'Smith', null,
223 | '1985-04-09', '99 Kingston Street', '31435', 'BIOL');
224 | insert into Student values (
225 | '444111111', 'John', 'K', 'Stevenson', '9129219434',
226 | null, '100 Main Street', '31411', 'BIOL');
227 | insert into Student values (
228 | '444111112', 'George', 'R', 'Heintz', '9129213454',
229 | '1974-10-10', '1200 Abercorn Street', '31419', 'CS');
230 | insert into Student values (
231 | '444111113', 'Frank', 'E', 'Jones', '9125919434',
232 | '1970-09-09', '100 Main Street', '31411', 'BIOL');
233 | insert into Student values (
234 | '444111114', 'Jean', 'K', 'Smith', '9129219434',
235 | '1970-02-09', '100 Main Street', '31411', 'CHEM');
236 | insert into Student values (
237 | '444111115', 'Josh', 'R', 'Woo', '7075989434',
238 | '1970-02-09', '555 Franklin Street', '31411', 'CHEM');
239 | insert into Student values (
240 | '444111116', 'Josh', 'R', 'Smith', '9129219434',
241 | '1973-02-09', '100 Main Street', '31411', 'BIOL');
242 | insert into Student values (
243 | '444111117', 'Joy', 'P', 'Kennedy', '9129229434',
244 | '1974-03-19', '103 Bay Street', '31412', 'CS');
245 | insert into Student values (
246 | '444111118', 'Toni', 'R', 'Peterson', '9129229434',
247 | '1964-04-29', '103 Bay Street', '31412', 'MATH');
248 | insert into Student values (
249 | '444111119', 'Patrick', 'R', 'Stoneman', null,
250 | '1969-04-29', '101 Washington Street', '31435', 'MATH');
251 | insert into Student values (
252 | '444111120', 'Rick', 'R', 'Carter', null,
253 | '1986-04-09', '19 West Ford Street', '31411', 'BIOL');
254 |
255 | insert into Course values (
256 | '11111', 'CSCI', '1301', 'Intro to Java I', 4);
257 | insert into Course values (
258 | '11112', 'CSCI', '1302', 'Intro to Java II', 3);
259 | insert into Course values (
260 | '11113', 'CSCI', '4720', 'Database Systems', 3);
261 | insert into Course values (
262 | '11114', 'CSCI', '4750', 'Rapid Java Application', 3);
263 | insert into Course values (
264 | '11115', 'MATH', '2750', 'Calculus I', 5);
265 | insert into Course values (
266 | '11116', 'MATH', '3750', 'Calculus II', 5);
267 | insert into Course values (
268 | '11117', 'EDUC', '1111', 'Reading', 3);
269 | insert into Course values (
270 | '11118', 'ITEC', '1344', 'Database Administration', 3);
271 |
272 | insert into College values (
273 | 'SC', 'Science', '1994-01-01','111221110');
274 | insert into College values (
275 | 'NURS', 'Nursing', '1994-01-01',null);
276 | insert into College values (
277 | 'EDUC', 'Education', '1994-01-01', '111221117');
278 | insert into College values (
279 | 'BUSS', 'Business', '1994-01-01', '111221114');
280 |
281 | /* For exercise 34.7 in intro6e */
282 | drop table if exists csci4990;
283 | drop table if exists csci1301;
284 | drop table if exists csci1302;
285 |
286 | create table csci4990 (
287 | ssn char(11),
288 | name varchar(25),
289 | score float);
290 |
291 | insert into csci4990 values (
292 | '111-22-3333', 'John F Smith', 85.4);
293 |
294 | create table csci1301 (
295 | ssn char(11),
296 | name varchar(25),
297 | score float);
298 |
299 | insert into csci1301 values (
300 | '111-22-3333', 'John F Smith', 75.4);
301 |
302 | create table csci1302 (
303 | ssn char(11),
304 | name varchar(25),
305 | score float);
306 |
307 | insert into csci1302 values (
308 | '111-22-3333', 'John F Smith', 55.4);
309 |
310 | /* For exercise 34.8 in intro6e */
311 | drop table if exists Account;
312 | drop table if exists Address;
313 |
314 | create table Account (
315 | username varchar(20),
316 | password varchar(25),
317 | name varchar(20));
318 |
319 | insert into Account values (
320 | 'javaman', 'namavaj', 'John F. Smith');
321 |
322 | /* For the Database Programming Chapter in intro7e */
323 | create table Staff (
324 | id char(9) not null,
325 | lastName varchar(15),
326 | firstName varchar(15),
327 | mi char(1),
328 | address varchar(20),
329 | city varchar(20),
330 | state char(2),
331 | telephone char(10),
332 | email varchar(40),
333 | primary key (id)
334 | );
335 |
336 | create table Quiz(
337 | questionId int,
338 | question varchar(4000),
339 | choicea varchar(1000),
340 | choiceb varchar(1000),
341 | choicec varchar(1000),
342 | choiced varchar(1000),
343 | answer varchar(5));
344 |
345 | /* For the example in the Web chapters in intro7e */
346 | create table Address (
347 | firstname varchar(25),
348 | mi char(1),
349 | lastname varchar(25),
350 | street varchar(40),
351 | city varchar(20),
352 | state varchar(2),
353 | zip varchar(5),
354 | telephone varchar(10),
355 | email varchar(30),
356 | primary key (firstname, mi, lastname)
357 | );
358 |
359 | /* For the example in the advanced database chatper in intro7e. Use MySQL version 5*/
360 | drop table if exists Person;
361 | drop table if exists StateCapital;
362 |
363 | create table Person (
364 | firstName varchar(20),
365 | mi char(1),
366 | lastName varchar(20)
367 | );
368 |
369 | /* For the example in the advanced database chatper. Use MySQL version 5*/
370 | create table StateCapital (
371 | state varchar(40),
372 | capital varchar(40)
373 | );
374 |
375 | insert into StateCapital values (
376 | 'Georgia', 'Atlanta');
377 | insert into StateCapital values (
378 | 'Texas', 'Austin');
379 | insert into StateCapital values (
380 | 'Indiana', 'Indianapolis');
381 | insert into StateCapital values (
382 | 'Illlinois', 'Springfield');
383 | insert into StateCapital values (
384 | 'California', 'Sacramento');
385 | /*
386 | insert into StateCapital values (
387 | 'Alabama', 'Montgomery');
388 | insert into StateCapital values (
389 | 'Alaska', 'Juneau');
390 | insert into StateCapital values (
391 | 'Arizona', 'Phoenix');
392 | */
393 |
394 | drop table Country;
395 |
396 | create table Country(name varchar(30), flag blob,
397 | description varchar(255));
398 |
399 | create table Student1 (
400 | username varchar(50) not null,
401 | password varchar(50) not null,
402 | fullname varchar(200) not null,
403 | constraint pkStudent primary key (username)
404 | );
405 |
406 | insert into Student1 values ('abc1', 'abc', 'John F Smith');
407 | insert into Student1 values ('abc2', 'abc', 'Peter R Smith');
408 | insert into Student1 values ('abc3', 'abc', 'Jane Paul');
409 | insert into Student1 values ('abc4', 'abc', 'George King');
410 |
411 | create table Student2 (
412 | username varchar(50) not null,
413 | password varchar(50) not null,
414 | firstname varchar(100),
415 | lastname varchar(100),
416 | constraint pkStudent primary key (username)
417 | );
418 |
419 |
420 | /* For the exercise in the advanced database chatper. Use MySQL version 5*/
421 | create table Temp(num1 double, num2 double, num3 double);
422 |
423 | /* For the callable statement example. Use MySQL version 5 */
424 | drop function if exists studentFound;
425 |
426 | delimiter //
427 |
428 | create function studentFound(first VARCHAR(20), last VARCHAR(20))
429 | returns int
430 | begin
431 | declare result int;
432 |
433 | select count(*) into result
434 | from Student
435 | where Student.firstName = first and
436 | Student.lastName = last;
437 |
438 | return result;
439 | end;
440 | //
441 |
442 | delimiter ; /* Please note that there is a space between delimiter and ; */
443 |
444 |
445 | /* For the example in the RMI and Web service */
446 | drop table if exists Scores;
447 | create table Scores (name varchar(20),
448 | score double, permission double);
449 |
450 | insert into Scores values ('John', 90.5, 1);
451 | insert into Scores values ('Michael', 100, 1);
452 | insert into Scores values ('Michelle', 100, 0);
453 |
454 | commit;
--------------------------------------------------------------------------------
/Supplements/Database Supplements/createsampletables_oracle.txt:
--------------------------------------------------------------------------------
1 | /* Student information database tables */
2 | drop table Enrollment cascade constraints;
3 | drop table Student cascade constraints;
4 | drop table Subject cascade constraints;
5 | drop table Department;
6 | drop table TaughtBy;
7 | drop table Course;
8 | drop table College;
9 | drop table Faculty;
10 |
11 | create table Department (
12 | deptId char(4),
13 | name varchar2(25),
14 | chairId varchar2(9),
15 | collegeId varchar2(4),
16 | constraint pkDepartment primary key (deptId));
17 |
18 | create table Enrollment (
19 | ssn char(9) not null,
20 | courseId char(5) not null,
21 | dateRegistered date,
22 | grade char(1),
23 | constraint pkEnrollment primary key (ssn, courseId));
24 |
25 | create table TaughtBy (
26 | courseId char(5),
27 | ssn char(9));
28 |
29 | create table Faculty(
30 | ssn char(9),
31 | firstName varchar2(25),
32 | mi char(1),
33 | lastName varchar(25),
34 | phone char(10),
35 | email varchar2(50),
36 | office varchar2(15),
37 | startTime date,
38 | rank varchar2(20),
39 | salary number(10, 2),
40 | deptId char(4),
41 | constraint pkFaculty primary key (ssn));
42 |
43 | create table Subject (
44 | subjectId char(4),
45 | name varchar2(25) unique,
46 | startTime date,
47 | deptId char(4),
48 | constraint pkSubject primary key (subjectId));
49 |
50 | create table Course(
51 | courseId char(5),
52 | subjectId char(4) not null,
53 | courseNumber number(4),
54 | title varchar2(50) not null,
55 | numOfCredits number(1)
56 | constraint ckNumOfCredits check (numOfCredits >= 1),
57 | constraint pkCourse primary key (courseId),
58 | constraint fkSubjectId foreign key (subjectId)
59 | references Subject(subjectId));
60 |
61 | create table Student (
62 | ssn char(9) constraint pkStudent primary key,
63 | firstName varchar2(25),
64 | mi char(1),
65 | lastName varchar2(25),
66 | phone char(11),
67 | birthDate date,
68 | street varchar2(100),
69 | zipCode char(5),
70 | deptId char(4),
71 | constraint fkDeptId foreign key (deptId)
72 | references Department(deptId));
73 |
74 | create table College(
75 | collegeId varchar2(4) not null,
76 | name varchar(25) not null,
77 | since date,
78 | deanId char(9),
79 | constraint pkCollege primary key (collegeId),
80 | constraint fkDeanId foreign key (deanId)
81 | references Faculty(ssn));
82 |
83 | insert into Department values (
84 | 'CS', 'Computer Science', '111221115', 'SC');
85 | insert into Department values (
86 | 'MATH', 'Mathematics', '111221116', 'SC');
87 | insert into Department values (
88 | 'CHEM', 'Chemistry', '111225555', 'SC');
89 | insert into Department values (
90 | 'EDUC', 'Education', '333114444', 'EDUC');
91 | insert into Department values (
92 | 'ACCT', 'Accounting', '333115555', 'BUSS');
93 | insert into Department values (
94 | 'BIOL', 'Biology', '333116666', 'SC');
95 |
96 | /*
97 | insert into Department values (
98 | 'BIOL', 'Biology', '111225555', 'SC'); */
99 |
100 | insert into Enrollment values (
101 | '444111110', '11111', sysdate, 'A');
102 | --In MS Access, replace sysdate by date()
103 | insert into Enrollment values (
104 | '444111110', '11112', sysdate, 'B');
105 | insert into Enrollment values (
106 | '444111110', '11113', sysdate, 'C');
107 | insert into Enrollment values (
108 | '444111111', '11111', sysdate, 'D');
109 | insert into Enrollment values (
110 | '444111111', '11112', sysdate, 'F');
111 | insert into Enrollment values (
112 | '444111111', '11113', sysdate, 'A');
113 | insert into Enrollment values (
114 | '444111112', '11114', sysdate, 'B');
115 | insert into Enrollment values (
116 | '444111112', '11115', sysdate, 'C');
117 | insert into Enrollment values (
118 | '444111112', '11116', sysdate, null);
119 | insert into Enrollment values (
120 | '444111113', '11111', sysdate, null);
121 | insert into Enrollment values (
122 | '444111113', '11113', sysdate, null);
123 | insert into Enrollment values (
124 | '444111114', '11115', sysdate, null);
125 | insert into Enrollment values (
126 | '444111115', '11115', sysdate, null);
127 | insert into Enrollment values (
128 | '444111115', '11116', sysdate, null);
129 | insert into Enrollment values (
130 | '444111116', '11111', sysdate, null);
131 | insert into Enrollment values (
132 | '444111117', '11111', sysdate, null);
133 | insert into Enrollment values (
134 | '444111118', '11111', sysdate, null);
135 | insert into Enrollment values (
136 | '444111118', '11112', sysdate, null);
137 | insert into Enrollment values (
138 | '444111118', '11113', sysdate, null);
139 |
140 | insert into TaughtBy values (
141 | '11111', '111221111');
142 | insert into TaughtBy values (
143 | '11112', '111221111');
144 | insert into TaughtBy values (
145 | '11113', '111221111');
146 | insert into TaughtBy values (
147 | '11114', '111221115');
148 | insert into TaughtBy values (
149 | '11115', '111221110');
150 | insert into TaughtBy values (
151 | '11116', '111221115');
152 | insert into TaughtBy values (
153 | '11117', '111221116');
154 | insert into TaughtBy values (
155 | '11118', '111221112');
156 |
157 | insert into Subject values (
158 | 'CSCI', 'Computer Science', '1-AUG-1980', 'CS');
159 | insert into Subject values (
160 | 'ITEC', 'Information Technology', '1-JAN-2002', 'CS');
161 | insert into Subject values (
162 | 'MATH', 'Mathematical Science', '1-AUG-1935', 'MATH');
163 | insert into Subject values (
164 | 'STAT', 'Statistics', '1-AUG-1980', 'MATH');
165 | insert into Subject values (
166 | 'EDUC', 'Education', '1-AUG-1980', 'EDUC');
167 |
168 | insert into Faculty values (
169 | 111221110, 'Patty', 'R', 'Smith', '9129215555',
170 | 'patty@yahoo.com', 'SC129', '11-OCT-76',
171 | 'Full Professor', 60000, 'MATH');
172 | insert into Faculty values (
173 | 111221111, 'George', 'P', 'Franklin', '9129212525',
174 | 'george@yahoo.com', 'SC130', '12-OCT-86',
175 | 'Associate Professor', 65000, 'CS');
176 | insert into Faculty values (
177 | 111221112, 'Jean', 'D', 'Yao', '9129215556',
178 | 'jean@yahoo.com', 'SC131', '13-AUG-76',
179 | 'Full Professor', 65000, 'MATH');
180 | insert into Faculty values (
181 | '111221113', 'Frank', 'E', 'Goldman', '9129215557',
182 | 'frank@yahoo.com', 'SC132', '14-JAN-96',
183 | 'Assistant Professor', 60000, 'BUSS');
184 | insert into Faculty values (
185 | '111221114', 'Steve', 'T', 'Templeton', '9129215558',
186 | 'steve@yahoo.com', 'UH132', '01-JAN-98',
187 | 'Assistant Professor', 60000, 'BUSS');
188 | insert into Faculty values (
189 | '111221115', 'Alex', 'T', 'Bedat', '9129215559',
190 | 'alex@yahoo.com', 'SC133', '01-JAN-00',
191 | 'Full Professor', 95000, 'CS');
192 | insert into Faculty values (
193 | '111221116', 'Judy', 'T', 'Woo', '9129215560',
194 | 'judy@yahoo.com', 'SC133', '01-JAN-00',
195 | 'Full Professor', 55000, 'MATH');
196 | insert into Faculty values (
197 | '111221117', 'Joe', 'R', 'Chang', '9129215561',
198 | 'joe@yahoo.com', 'ED133', '01-JAN-00',
199 | 'Full Professor', 55000, 'EDUC');
200 | insert into Faculty values (
201 | '111221118', 'Francis', 'R', 'Chin', '9129215562',
202 | 'joe@yahoo.com', 'ED133', '01-JAN-89',
203 | 'Full Professor', 75000, 'BIOL');
204 | insert into Faculty values (
205 | '111221119', 'Ray', 'R', 'Smith', '9129215563',
206 | 'ray@yahoo.com', 'SC133', '01-JAN-94',
207 | 'Full Professor', 85000, 'CHEM');
208 |
209 | insert into Student values (
210 | '444111110', 'Jacob', 'R', 'Smith', null,
211 | '9-APR-1985', '99 Kingston Street', '31435', 'BIOL');
212 | insert into Student values (
213 | '444111111', 'John', 'K', 'Stevenson', '9129219434',
214 | null, '100 Main Street', '31411', 'BIOL');
215 | insert into Student values (
216 | '444111112', 'George', 'R', 'Heintz', '9129213454',
217 | '10-OCT-1974', '1200 Abercorn Street', '31419', 'CS');
218 | insert into Student values (
219 | '444111113', 'Frank', 'E', 'Jones', '9125919434',
220 | '9-SEP-1970', '100 Main Street', '31411', 'BIOL');
221 | insert into Student values (
222 | '444111114', 'Jean', 'K', 'Smith', '9129219434',
223 | '9-FEB-1970', '100 Main Street', '31411', 'CHEM');
224 | insert into Student values (
225 | '444111115', 'Josh', 'R', 'Woo', '7075989434',
226 | '9-FEB-1970', '555 Franklin Street', '31411', 'CHEM');
227 | insert into Student values (
228 | '444111116', 'Josh', 'R', 'Smith', '9129219434',
229 | '9-FEB-1973', '100 Main Street', '31411', 'BIOL');
230 | insert into Student values (
231 | '444111117', 'Joy', 'P', 'Kennedy', '9129229434',
232 | '19-MAR-1974', '103 Bay Street', '31412', 'CS');
233 | insert into Student values (
234 | '444111118', 'Toni', 'R', 'Peterson', '9129229434',
235 | '29-APR-1964', '103 Bay Street', '31412', 'MATH');
236 | insert into Student values (
237 | '444111119', 'Patrick', 'R', 'Stoneman', null,
238 | '29-APR-1969', '101 Washington Street', '31435', 'MATH');
239 | insert into Student values (
240 | '444111120', 'Rick', 'R', 'Carter', null,
241 | '9-APR-1986', '19 West Ford Street', '31411', 'BIOL');
242 |
243 | insert into Course values (
244 | '11111', 'CSCI', '1301', 'Intro to Java I', 4);
245 | insert into Course values (
246 | '11112', 'CSCI', '1302', 'Intro to Java II', 3);
247 | insert into Course values (
248 | '11113', 'CSCI', '4720', 'Database Systems', 3);
249 | insert into Course values (
250 | '11114', 'CSCI', '4750', 'Rapid Java Application', 3);
251 | insert into Course values (
252 | '11115', 'MATH', '2750', 'Calculus I', 3);
253 | insert into Course values (
254 | '11116', 'MATH', '3750', 'Calculus II', 3);
255 | insert into Course values (
256 | '11117', 'EDUC', '1111', 'Reading', 3);
257 | insert into Course values (
258 | '11118', 'ITEC', '1344', 'Database Administration', 3);
259 |
260 | insert into College values (
261 | 'SC', 'Science', '01-JAN-94','111221110');
262 | insert into College values (
263 | 'NURS', 'Nursing', '01-JAN-94',null);
264 | insert into College values (
265 | 'EDUC', 'Education', '01-JAN-94', '111221117');
266 | insert into College values (
267 | 'BUSS', 'Business', '01-JAN-94', '111221114');
268 |
269 | /* For exercise 34.7 in intro6e */
270 | drop table csci4990;
271 | drop table csci1301;
272 | drop table csci1302;
273 | drop table csci3720;
274 |
275 | create table csci4990 (
276 | ssn char(11),
277 | name varchar2(25),
278 | score number);
279 |
280 | insert into csci4990 values (
281 | '111-22-3333', 'John F Smith', 85.4);
282 |
283 | create table csci1301 (
284 | ssn char(11),
285 | name varchar2(25),
286 | score number);
287 |
288 | insert into csci1301 values (
289 | '111-22-3333', 'John F Smith', 75.4);
290 | insert into csci1301 values (
291 | '111-22-4444', 'Corey Stack', 45.4);
292 | insert into csci1301 values (
293 | '111-22-5555', 'Jane Smith', 45.9);
294 | insert into csci1301 values (
295 | '111-22-6666', 'Jean White', 85.9);
296 |
297 | create table csci1302 (
298 | ssn char(11),
299 | name varchar2(25),
300 | score number);
301 |
302 | insert into csci1302 values (
303 | '111-22-3333', 'John F Smith', 55.4);
304 | insert into csci1302 values (
305 | '111-22-4444', 'Corey Stack', 95.4);
306 | insert into csci1302 values (
307 | '111-22-5555', 'Jane Smith', 75.0);
308 |
309 | create table csci3720 (
310 | ssn char(11),
311 | name varchar2(25),
312 | score number);
313 |
314 | insert into csci3720 values (
315 | '111-22-3333', 'John F Smith', 85.4);
316 | insert into csci3720 values (
317 | '111-22-4444', 'Corey Stack', 75.4);
318 | insert into csci3720 values (
319 | '111-22-5555', 'Jane Smith', 95.0);
320 |
321 | drop table Account;
322 |
323 | create table Account (
324 | username varchar(20),
325 | password varchar2(25),
326 | name varchar(20));
327 |
328 | insert into Account values (
329 | 'javaman', 'namavaj', 'John F. Smith');
330 |
331 | /* For the example in ch34 in intro6e */
332 | drop table Address;
333 |
334 | create table Address (
335 | firstname varchar(25),
336 | mi char(1),
337 | lastname varchar(25),
338 | street varchar(40),
339 | city varchar(20),
340 | state varchar(2),
341 | zip varchar(5),
342 | telephone varchar(10),
343 | email varchar(30)
344 | );
345 |
346 | drop table Country;
347 |
348 | create table Country(name varchar(30), flag blob,
349 | description varchar(255));
350 |
351 | create table Student1 (
352 | username varchar(50) not null,
353 | password varchar(50) not null,
354 | fullname varchar(200) not null,
355 | constraint pkStudent primary key (username)
356 | );
357 |
358 | insert into Student1 values ('abc1', 'abc', 'John Smith');
359 | insert into Student1 values ('abc2', 'abc', 'Peter Smith');
360 | insert into Student1 values ('abc3', 'abc', 'Jane Paul');
361 |
362 | create table Student2 (
363 | username varchar(50) not null,
364 | password varchar(50) not null,
365 | firstname varchar(100),
366 | lastname varchar(100),
367 | constraint pkStudent primary key (username)
368 | );
369 |
370 | commit;
--------------------------------------------------------------------------------
/Supplements/Glossary.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Glossary.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/.DS_Store
--------------------------------------------------------------------------------
/Supplements/Java Supplements/AdditionalBooleanOperarators.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/AdditionalBooleanOperarators.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/Assertion.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/Assertion.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/CommandLineArgument.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/CommandLineArgument.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/DatagramSocket.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/DatagramSocket.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/Designpatterns.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/Designpatterns.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/EnumeratedType.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/EnumeratedType.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/FormatingString.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/FormatingString.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/GridBagLayout.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/GridBagLayout.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/HidingDataFieldsAndStaticMethods.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/HidingDataFieldsAndStaticMethods.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/ImmutableObjects.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/ImmutableObjects.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/InitializationBlock.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/InitializationBlock.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/InternalFrame.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/InternalFrame.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/JEditorPane.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/JEditorPane.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/JNI_Example.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/JNI_Example.zip
--------------------------------------------------------------------------------
/Supplements/Java Supplements/JavaCharacteristics.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/JavaCharacteristics.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/JavaWebStart.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/JavaWebStart.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/LiangAppFv3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/LiangAppFv3.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/Logger.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/Logger.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/ObjectClass.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/ObjectClass.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/OpearatorEvaluation.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/OpearatorEvaluation.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/OverlayLayout.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/OverlayLayout.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/Overriding.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/Overriding.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/Package.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/Package.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/PackageArchive.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/PackageArchive.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/PluggableLookAndFeel.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/PluggableLookAndFeel.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/Profiler.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/Profiler.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/RegularExpression.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/RegularExpression.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/SignedApplet.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/SignedApplet.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/SpringLayout.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/SpringLayout.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/StatementLabel.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/StatementLabel.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/StringTokenizer.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/StringTokenizer.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/SwingWorkerJProgressBar.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/SwingWorkerJProgressBar.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/TextIOReaderWriter.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/TextIOReaderWriter.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/UMLNotation.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/UMLNotation.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/UnitTest.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/UnitTest.pdf
--------------------------------------------------------------------------------
/Supplements/Java Supplements/javadoccomments.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Java Supplements/javadoccomments.pdf
--------------------------------------------------------------------------------
/Supplements/Supplement1cJDKonDOS.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Supplement1cJDKonDOS.pdf
--------------------------------------------------------------------------------
/Supplements/Supplement1fUsingPackage.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Supplement1fUsingPackage.pdf
--------------------------------------------------------------------------------
/Supplements/Web Programming Supplements/CSS.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Web Programming Supplements/CSS.pdf
--------------------------------------------------------------------------------
/Supplements/Web Programming Supplements/HTML.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Web Programming Supplements/HTML.pdf
--------------------------------------------------------------------------------
/Supplements/Web Programming Supplements/JavaXML.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Web Programming Supplements/JavaXML.pdf
--------------------------------------------------------------------------------
/Supplements/Web Programming Supplements/M41.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Web Programming Supplements/M41.pdf
--------------------------------------------------------------------------------
/Supplements/Web Programming Supplements/Tomcat5.5.9.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Web Programming Supplements/Tomcat5.5.9.pdf
--------------------------------------------------------------------------------
/Supplements/Web Programming Supplements/XML.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aJetHorn/Intro-to-Java-Programming-9E/1d0da3b8bec72097c0aa7c502e34787de572afa0/Supplements/Web Programming Supplements/XML.pdf
--------------------------------------------------------------------------------