├── Chapter 1 └── Listing 1.0 ├── Chapter 2 └── Listing 2.0.java ├── Chapter 3 ├── Listing 3.0.java ├── Listing 3.1.java ├── Listing 3.2.java ├── Listing 3.3.java ├── Listing 3.4.java ├── Listing 3.5.java └── Listing 3.6.java ├── Chapter 4 ├── Listing 4.0.java ├── Listing 4.1.java ├── Listing 4.2.java ├── Listing 4.3.java ├── Listing 4.4.java └── Listing 4.5.java ├── Chapter 5 ├── Listing 5.0.java ├── Listing 5.1.java ├── Listing 5.10.java ├── Listing 5.11.java ├── Listing 5.12.java ├── Listing 5.13.java ├── Listing 5.14.java ├── Listing 5.2.java ├── Listing 5.3.java ├── Listing 5.4.java ├── Listing 5.5.java ├── Listing 5.6.java ├── Listing 5.7.java ├── Listing 5.8.java └── Listing 5.9.java ├── Chapter 6 ├── Listing 6.0.java ├── Listing 6.1.java ├── Listing 6.10.java ├── Listing 6.11.java ├── Listing 6.12.java ├── Listing 6.13.java ├── Listing 6.14.java ├── Listing 6.15.java ├── Listing 6.16.java ├── Listing 6.2.java ├── Listing 6.3.java ├── Listing 6.4.java ├── Listing 6.5.java ├── Listing 6.6.java ├── Listing 6.7.java ├── Listing 6.8.java └── Listing 6.9.java ├── Chapter 7 ├── Listing 7.0.java ├── Listing 7.1.java ├── Listing 7.10.java ├── Listing 7.11.java ├── Listing 7.12.java ├── Listing 7.13.java ├── Listing 7.14.java ├── Listing 7.15.java ├── Listing 7.2.java ├── Listing 7.3.java ├── Listing 7.4.java ├── Listing 7.5.java ├── Listing 7.6.java ├── Listing 7.7.java ├── Listing 7.8.java └── Listing 7.9.java ├── Chapter 8 ├── Listing 8.0.java ├── Listing 8.1.java ├── Listing 8.10.java ├── Listing 8.2.java ├── Listing 8.3.java ├── Listing 8.4.java ├── Listing 8.5.java ├── Listing 8.6.java ├── Listing 8.7.java ├── Listing 8.8.java └── Listing 8.9.java ├── Chapter 9 ├── Listing 9.0.java └── Listing 9.1.java ├── README.md └── cover.png /Chapter 1/Listing 1.0: -------------------------------------------------------------------------------- 1 | ; Assembly Language: Low Level Language 2 | mov rax, a ; Read the value of a 3 | add rax, b ; Add the value of b to a 4 | mov a, rax ; Write the result to a 5 | 6 | // Java: High Level Language 7 | a = a + b; -------------------------------------------------------------------------------- /Chapter 2/Listing 2.0.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | } 4 | } -------------------------------------------------------------------------------- /Chapter 3/Listing 3.0.java: -------------------------------------------------------------------------------- 1 | 1: public class MainClass { // Definition of MainClass 2 | 2: public static void main(String[] args) { // main method entry point 3 | 3: System.out.println("This is a test application!"); 4 | 4: } // Close brace for main method 5 | 5: } // Close brace for MainClass -------------------------------------------------------------------------------- /Chapter 3/Listing 3.1.java: -------------------------------------------------------------------------------- 1 | c.setRadius(100); 2 | c . setRadius 3 | ( 100 4 | ) 5 | ; -------------------------------------------------------------------------------- /Chapter 3/Listing 3.2.java: -------------------------------------------------------------------------------- 1 | // This is a single line comment! 2 | System.out.println("Press a key to continue..."); // Single line comment! 3 | 4 | /* 5 | This is a multi-line comment. It extends from the opening symbol of a slash and 6 | star to the closing symbol, a star followed by a slash! 7 | */ -------------------------------------------------------------------------------- /Chapter 3/Listing 3.3.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] arguments) { 3 | System.out.println("This is a test application!"); 4 | } 5 | } -------------------------------------------------------------------------------- /Chapter 3/Listing 3.4.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void mainMethod(String[] args) { 3 | System.out.println("This is a test application!"); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /Chapter 3/Listing 3.5.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | System.out.println("Hello World!"); 4 | } 5 | } -------------------------------------------------------------------------------- /Chapter 3/Listing 3.6.java: -------------------------------------------------------------------------------- 1 | public class MyClass { 2 | public static void main(String[] args) { 3 | System.out.println("This is a test application!"); 4 | } 5 | } -------------------------------------------------------------------------------- /Chapter 4/Listing 4.0.java: -------------------------------------------------------------------------------- 1 | 1: import java.util.Scanner; 2 | 2: public class MainClass { 3 | 3: public static void main(String[] args) { 4 | 4: Scanner scanner = new Scanner(System.in); 5 | 5: System.out.println("What is your name?"); 6 | 6: String str = scanner.nextLine(); 7 | 7: System.out.println("Nice to meet you, " + str + "!"); 8 | 8: } 9 | 9: } -------------------------------------------------------------------------------- /Chapter 4/Listing 4.1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class MainClass { 3 | public static void main(String[] args) { 4 | // Declare all variables 5 | String userName; 6 | String userPassword; 7 | Scanner scanner = new Scanner(System.in); 8 | 9 | // Request the user's name 10 | System.out.print("What is your name? "); 11 | userName = scanner.nextLine(); 12 | 13 | // Request the user's password 14 | System.out.print("Ok, and what is your password? "); 15 | userPassword = scanner.nextLine(); 16 | 17 | // Print out the info we read in a message 18 | System.out.println("Whoa! That was too easy! You're crazy, " + 19 | userName + ", you just told a complete stranger " + 20 | "that your password is " + userPassword + "!"); 21 | 22 | // Close the scanner to avoid memory leaks 23 | if(scanner != null) 24 | scanner.close(); 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter 4/Listing 4.2.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | 3 | // Method to return lowest prime factor of the integer x 4 | private static int GetLowestFactor(int x) { 5 | int sqrt = (int)Math.sqrt(x); 6 | if(x % 2 == 0) { 7 | return 2; 8 | } 9 | for(int q = 3; q <= sqrt; q+=2) { 10 | if(x % q == 0) { 11 | return q; 12 | } 13 | } 14 | return x; 15 | } 16 | 17 | // Determines if x is prime by seeing if it is equal to 18 | // it's own lowest factor 19 | private static Boolean IsPrime(int x) { 20 | return GetLowestFactor(x) == x; 21 | } 22 | 23 | // This program lists the prime numbers in the range 1 to 1000 24 | public static void main(String[] args) { 25 | // Declare local variables 26 | int i = 2, count = 0, countPerLine = 10; 27 | // Show a description of the program 28 | System.out.println("Prime numbers from 1 to 1000 are: "); 29 | // Loop which finds the primes 30 | while(i <= 1000) { 31 | // If i is prime... 32 | if(IsPrime(i)) { 33 | // Add it to the list... 34 | System.out.print(i + ", "); 35 | // Increment the count 36 | count++; 37 | // If there's countPerLine items on this line, 38 | // print a new line. 39 | if(count % countPerLine == 0) { 40 | System.out.println(); 41 | } 42 | } 43 | // Increment i to the next number 44 | i++; 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Chapter 4/Listing 4.3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class MainClass { 3 | public static void main(String[] args) { 4 | Scanner scanner = new Scanner(System.in); 5 | System.out.println("What is your age?"); 6 | String age = scanner.nextLine(); 7 | System.out.println("You are " + age + " years old!"); 8 | } 9 | } -------------------------------------------------------------------------------- /Chapter 4/Listing 4.4.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class MainClass { 3 | public static void main(String[] args) { 4 | // Declare all variables 5 | String userName; 6 | String userPassword; 7 | String website; 8 | Scanner scanner = new Scanner(System.in); 9 | 10 | // Request the user's name 11 | System.out.print("What is your name? "); 12 | userName = scanner.nextLine(); 13 | 14 | // Request the user's password 15 | System.out.print("Ok, and what is your password? "); 16 | userPassword = scanner.nextLine(); 17 | System.out.print("And what website is this the password for? "); 18 | website = scanner.nextLine(); 19 | 20 | // Print out a message: 21 | System.out.println("Ok, gotta go. I have to quickly check " + 22 | website + "."); 23 | 24 | // Close the scanner to avoid memory leaks 25 | if(scanner != null) 26 | scanner.close(); 27 | } 28 | } -------------------------------------------------------------------------------- /Chapter 4/Listing 4.5.java: -------------------------------------------------------------------------------- 1 | int i = 2, count = 0, countPerLine = 2; 2 | 3 | // Show a description of the program 4 | System.out.println("Prime numbers from 1 to 50 are as follows: "); 5 | // Loop which finds the primes 6 | while(i <= 50) { 7 | // If i is prime... 8 | if(IsPrime(i)) { -------------------------------------------------------------------------------- /Chapter 5/Listing 5.0.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | int i = 100; // Declare and initialize an int 4 | int b = 10, c = 5;// Declare and initialize two variables 5 | boolean mb = false;// Declare and define a boolean 6 | 7 | char myLetter; // Declare a character 8 | myLetter = 'h'; // Define/set the character to be 'h' 9 | byte hp, bp, sp; // Declare 3 byte variables 10 | hp = bp = sp = 0; // Set all 3 bytes to 0 11 | short ss = 189; // Declare and define a short integer 12 | long p = ss; // Declare and define a long with the value of ss 13 | double myDouble = 100.0;// Declare and define a double 14 | float f = 0.1f; // Declare and define a float 15 | } 16 | } -------------------------------------------------------------------------------- /Chapter 5/Listing 5.1.java: -------------------------------------------------------------------------------- 1 | String description = "Strings can " + 2 | "be defined on multiple " + 3 | "lines with the + operator!"; -------------------------------------------------------------------------------- /Chapter 5/Listing 5.10.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | int[] myArray = new int[4]; 4 | myArray[0] = 300; 5 | myArray[1] = 400; 6 | myArray[2] = 500; 7 | myArray[3] = 600; 8 | System.out.println("Element 0 is: " + myArray[0]); 9 | System.out.println("Element 3 is: " + myArray[10-7]); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter 5/Listing 5.11.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | int[] myArray = { 4 | 300, 5 | 400, 6 | 500, 7 | 600 8 | }; 9 | System.out.println("Element 0 is: " + myArray[0]); 10 | System.out.println("Element 3 is: " + myArray[10-123]); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter 5/Listing 5.12.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | float[][] array2d = new float[7][5]; 4 | array2d[0][0] = 50.5f; 5 | array2d[4][4] = 60.5f; 6 | array2d[3][1] = 10.3f; 7 | System.out.println("Element (3, 1): " + array2d[3][1]); 8 | System.out.println("Element (4, 0): " + array2d[4][0]); 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter 5/Listing 5.13.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | 4 | float[][] array2d = new float[7][5]; 5 | 6 | for(int y = 0; y < 5; y++) { 7 | for(int x = 0; x < 7; x++) { 8 | array2d[x][y] = x * y; 9 | } 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter 5/Listing 5.14.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | // Declare 4D array 4 | int[][][][] arr4D = new int[3][4][5][6]; 5 | 6 | // Set all the elements to 1729 7 | for(int a = 0; a < 3; a++) { 8 | for(int b = 0; b < 4; b++) { 9 | for(int c = 0; c < 5; c++) { 10 | for(int d = 0; d < 6; d++) { 11 | arr4D[a][b][c][d] = 1729; 12 | } 13 | } 14 | } 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Chapter 5/Listing 5.2.java: -------------------------------------------------------------------------------- 1 | String str = "100"; 2 | int j = 10 + str; -------------------------------------------------------------------------------- /Chapter 5/Listing 5.3.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | // Set str to the string "7" 4 | String str = "7"; 5 | 6 | // Set j to the integer 10 + "7" parsed to int 7 | int j = 10 + Integer.parseInt(str); 8 | 9 | // Set c to the character '7' 10 | char c = '7'; 11 | 12 | // Set i to 10 + 7, notice we subtract the 13 | // character '0' from '7' to get the int 7 14 | // from the character '7' instead of parsing 15 | 16 | int i = 10 + c - '0'; 17 | 18 | // Both j and i are set to 17 19 | System.out.println("j: " + j); 20 | System.out.println("i: " + i); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter 5/Listing 5.4.java: -------------------------------------------------------------------------------- 1 | int idNumber = 26771; 2 | String desc = "The ID number is " + idNumber + "."; -------------------------------------------------------------------------------- /Chapter 5/Listing 5.5.java: -------------------------------------------------------------------------------- 1 | int i = (int) 7.8; // Cast the double 7.8 to an integer, store in i 2 | double d = (double) 89.7f;// Cast the float 89.7f to a double, store in d 3 | short q = (short) (i * d);// Cast the double i*d to a short and store in q -------------------------------------------------------------------------------- /Chapter 5/Listing 5.6.java: -------------------------------------------------------------------------------- 1 | int i = 100; 2 | int j = -239; 3 | short p = 99; 4 | byte q = -55; 5 | long l = 6787612356765l; 6 | 7 | int h = 0x5DD2; // Hexadecimal 8 | int w = 067; // Octal 9 | int bin = 0b10011001; // Binary literal, Java SE 7 and above -------------------------------------------------------------------------------- /Chapter 5/Listing 5.7.java: -------------------------------------------------------------------------------- 1 | float y = 99.0f; 2 | float q = -3.4512f; 3 | double h = 99.0; 4 | double g = -3.4512; 5 | double p = 2.4e3; 6 | double u = 2.4e-3; -------------------------------------------------------------------------------- /Chapter 5/Listing 5.8.java: -------------------------------------------------------------------------------- 1 | boolean b1 = true; 2 | boolean b2 = false; 3 | char m = 'h'; 4 | char g = 65; // 65 is 'A' in Unicode! 5 | char c = 0b1000001; // 65 in binary, this is also the character 'A' -------------------------------------------------------------------------------- /Chapter 5/Listing 5.9.java: -------------------------------------------------------------------------------- 1 | String m = "This is a string literal"; 2 | String j = "If your string literal is too long, you can " + 3 | "use the + operator and continue on the next line!"; -------------------------------------------------------------------------------- /Chapter 6/Listing 6.0.java: -------------------------------------------------------------------------------- 1 | int j = 10; 2 | int b = ++j; // j increment to 11 3 | // b is set to 11 4 | 5 | int p = 10; 6 | int g = p++; // b is set to 10 7 | // p increments to 11 -------------------------------------------------------------------------------- /Chapter 6/Listing 6.1.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | int j = 190; // j gets 190 4 | j++; // j gets 191 5 | j = j - 89; // j gets 102 6 | int b = 78 * j; // b gets 7956 7 | int c = b + (9*j) / 12; // c gets 8032 8 | b--; // b gets 7955 9 | c = c % b + (j / 2); // c gets 128 10 | c = --b; // c and b get 7954 11 | int h = (j + b) % 210; // h gets 76 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.10.java: -------------------------------------------------------------------------------- 1 | String s1 = "This is"; 2 | String s2 = " a string"; 3 | String s3 = "!"; 4 | String concatenated = s1 + s2 + s3; 5 | String str1 = "Concatenating integers is fine! " + 278; 6 | String str2 = "And floats/doubles too!" + 45.678; 7 | System.out.println(concatenated); 8 | System.out.println(str1); 9 | System.out.println(str2); -------------------------------------------------------------------------------- /Chapter 6/Listing 6.11.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | int a = 25, b = 60; 4 | int larger = (a > b)? a : b; 5 | System.out.println(larger); 6 | } 7 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.12.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | int a = 25, b = 60; 4 | 5 | int larger; 6 | 7 | if(a > b) 8 | larger = a; 9 | else 10 | larger = b; 11 | 12 | System.out.println(larger); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.13.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | MyClass someInstance = new MyClass(); 4 | if(someInstance instanceof MyClass) 5 | System.out.println("someInstance is of MyClass!"); 6 | else 7 | System.out.println("someInstance is not of MyClass..."); 8 | } 9 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.14.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | int eggsTotal = 792671; 4 | int cartons = eggsTotal / 12; 5 | int eggsLeftOver = eggsTotal % 12; 6 | System.out.println("The farmer has " + cartons + " cartons of eggs, " 7 | + "and " + eggsLeftOver + " eggs left over for breakfast."); 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.15.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | int result = 4; 4 | result = result - 2; 5 | result = result * 12; 6 | result++; 7 | result = result + result; 8 | result = result / 7; 9 | result = result * result; 10 | result = result * result * result; 11 | result--; 12 | // To work out if a number evenly divides another, use mod: 13 | int mod2 = result % 2; 14 | int mod3 = result % 3; 15 | int mod17 = result % 17; 16 | int mod19 = result % 19; 17 | int mod43 = result % 43; 18 | System.out.println("The final result is " + result); 19 | System.out.println("Division by 2 leaves: " + mod2 + " remainder."); 20 | System.out.println("Division by 3 leaves: " + mod3 + " remainder."); 21 | System.out.println("Division by 17 leaves: " + mod17 + " remainder."); 22 | System.out.println("Division by 19 leaves: " + mod19 + " remainder."); 23 | System.out.println("Division by 43 leaves: " + mod43 + " remainder."); 24 | } 25 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.16.java: -------------------------------------------------------------------------------- 1 | // The first example shows computing the result without simplifying 2 | // the expression: 3 | int result1 = (((((((4-2)*12)+1)*2)/7)*(((((4-2)*12)+1)*2)/7))* 4 | ((((((4-2)*12)+1)*2)/7)*(((((4-2)*12)+1)*2)/7))* 5 | ((((((4-2)*12)+1)*2)/7)*(((((4-2)*12)+1)*2)/7)))-1; 6 | System.out.println("Final result is " + result1); 7 | 8 | // The second example shows that all we're really doing is raising 9 | // 7 to the power of 6 and subtracting 1: 10 | int result2 = 7*7*7*7*7*7-1; 11 | System.out.println("Final result is " + result2); -------------------------------------------------------------------------------- /Chapter 6/Listing 6.2.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | 4 | // Causing overflow of a byte: 5 | byte myByte = 127; 6 | myByte++; // Results in -128 7 | System.out.println("Byte says 127+1=" + myByte); 8 | myByte = -128; 9 | myByte--; // Results in 127 10 | System.out.println("Byte says -128-1=" + myByte); 11 | 12 | // Causing overflow of a short 13 | short myShort = 32767; // This is the maximum for short! 14 | System.out.println("Short is " + myShort + " before ++."); 15 | myShort++; 16 | System.out.println("Short is " + myShort + " after ++."); 17 | myShort--; 18 | System.out.println("Short is " + myShort + " after --."); 19 | } 20 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.3.java: -------------------------------------------------------------------------------- 1 | int a = 24/5; // Results in 4 2 | int b = -24/5; // Results in -4 -------------------------------------------------------------------------------- /Chapter 6/Listing 6.4.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | int numerator = 24; 4 | int denominator = 5; 5 | 6 | int wholeResult = numerator / denominator; 7 | int fractionalResult = numerator % denominator; 8 | 9 | System.out.println("The result is " + wholeResult + " and " + 10 | fractionalResult + "/" + denominator); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.5.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | float j = 901.0f/13.0f; 4 | float q = (53.0f / 13.0f)*17.0f; 5 | 6 | System.out.println("J: " + j + " Q:" + q); 7 | 8 | if(j != q) 9 | System.out.println("The two are not equal!"); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.6.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | float j = 901.0f/13.0f; 4 | float q = (53.0f / 13.0f)*17.0f; 5 | 6 | System.out.println("J: " + j + " Q:" + q); 7 | 8 | if(Math.abs(j - q) > 0.001f) 9 | System.out.println("The two are not equal!"); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter 6/Listing 6.7.java: -------------------------------------------------------------------------------- 1 | Var1 -= 7; // Means Var1 = Var1 – 7; 2 | Var1 ^= 7; // Means Var1 = Var1 ^ 7; 3 | Var1 /= 7; // Means Var1 = Var1 / 7; -------------------------------------------------------------------------------- /Chapter 6/Listing 6.8.java: -------------------------------------------------------------------------------- 1 | c = a & b; // c will be set to 10000010 2 | c = a | b; // c will be set to 11101111 3 | c = a ^ b; // c will be set to 01101101 -------------------------------------------------------------------------------- /Chapter 6/Listing 6.9.java: -------------------------------------------------------------------------------- 1 | int var1 = 100, var2 = 30; 2 | boolean ans1 = var1 == var2; // ans1 becomes false, 100 does not equal 30 3 | boolean ans2 = var1 > var2; // ans2 becomes true, 100 is greater than 30 4 | boolean ans3 = var2 <= var1; // ans3 becomes true, 30 is less than or equal to 100 5 | boolean ans4 = var2 != var1; // ans4 becomes true, 30 is not equal 100 6 | 7 | int v1 = 100, v2 = 30, v3 = 200; 8 | boolean ans1 = (v1 < v2) || (v3 > v1); // Using logical OR -------------------------------------------------------------------------------- /Chapter 7/Listing 7.0.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class MainClass { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | int height = 0; 7 | System.out.print("How tall are you (in cm)? "); 8 | height = Integer.parseInt(scanner.nextLine()); 9 | 10 | if(height < 100) { 11 | System.out.println("Wow! Are you a quokka?"); 12 | } 13 | 14 | if(height > 300) 15 | System.out.println("Yeah right? Are you a giraffe?"); 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.1.java: -------------------------------------------------------------------------------- 1 | int password = 25; 2 | String userName = "Sam"; 3 | if(password == 10 || userName == "Sam") { 4 | System.out.println("That is the correct password or User Name!"); 5 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.10.java: -------------------------------------------------------------------------------- 1 | if(i == 0) // Do something 2 | else if(i == 1) // Do somethingElse 3 | else if(i == 2) // Do somethingElse 4 | else // Do the default 5 | 6 | switch (i ) { 7 | case 0: { Do something; break; } 8 | case 1: { Do somethingElse; break; } 9 | case 2: { Do SomethingElse; break; } 10 | default: { Do the default; break; } 11 | }; -------------------------------------------------------------------------------- /Chapter 7/Listing 7.11.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class MainClass { 4 | public static void main(String[] args){ 5 | Scanner s = new Scanner(System.in); 6 | // Parameters for the operation 7 | double parameterA = 0.0, parameterB = 0.0; 8 | 9 | // Index of the operation 10 | int operation = 0; 11 | 12 | System.out.println("Welcome to Calculator"); 13 | do { 14 | 15 | // Ask the user for two numbers and an operator: 16 | System.out.print("Type a number: "); 17 | parameterA = Double.parseDouble(s.nextLine()); 18 | System.out.print("Type another number: "); 19 | parameterB = Double.parseDouble(s.nextLine()); 20 | System.out.println("Select an option: "); 21 | System.out.println( 22 | "1. Add\n2. Subtract\n3. Multiply\n4. Divide\n0. Quit"); 23 | System.out.print(": "); 24 | 25 | operation = Integer.parseInt(s.nextLine()); 26 | 27 | // Perform the selected operation using switch 28 | switch(operation){ 29 | case 1: { // If the user selected 1, add the parameters 30 | System.out.println(parameterA + "+" + parameterB 31 | + "=" + (parameterA + parameterB)); 32 | break; 33 | } 34 | case 2: { // If 2, subtract the parameters 35 | System.out.println(parameterA + "-" + parameterB 36 | + "=" + (parameterA - parameterB)); 37 | break; 38 | } 39 | case 3: { // If 3, multiply the parameters 40 | System.out.println(parameterA + "*" + parameterB 41 | + "=" + (parameterA * parameterB)); 42 | break; 43 | } 44 | case 4: { // If 4, divide the parameters 45 | System.out.println(parameterA + "/" + parameterB 46 | + "=" + (parameterA / parameterB)); 47 | break; 48 | } 49 | case 0: { // If 0, do nothing, they are quitting 50 | break; 51 | } 52 | default: { // Default option prints an error message 53 | System.out.println( 54 | "That wasn't one of the options."); 55 | } 56 | } 57 | } while(operation != 0); 58 | 59 | // The user has quit 60 | System.out.println("Bye, bye!"); 61 | if(s != null) 62 | s.close(); 63 | } 64 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.12.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.InputMismatchException; 3 | 4 | public class MainClass { 5 | public static void main(String[] args) { 6 | float numerator = 0.0f; 7 | float denominator = 0.0f; 8 | Scanner s = new Scanner(System.in); 9 | 10 | // Try to execute some code block: 11 | try { 12 | System.out.print("Enter a numerator: "); 13 | numerator = s.nextFloat(); 14 | System.out.print("Enter a denomintor: "); 15 | denominator = s.nextFloat(); 16 | if(denominator == 0.0f) 17 | throw new IllegalArgumentException(); 18 | System.out.println(numerator + " divided by " + denominator + 19 | " is equal to " + (numerator / denominator)); 20 | } 21 | 22 | // If an InputMismatchException is thrown 23 | catch (InputMismatchException e) { 24 | System.out.println("Your input was not a float."); 25 | } 26 | 27 | // If an IllegalArgumentException is thrown 28 | catch (IllegalArgumentException e) { 29 | System.out.println 30 | ("The result of division by Zero is not defined."); 31 | } 32 | 33 | // If any other type of exception is thrown 34 | catch (Exception e) { 35 | System.out.println 36 | ("An exception was thrown: " + e.getMessage()); 37 | } 38 | 39 | // After we perform try and catch: 40 | finally { 41 | if(s != null) 42 | s.close(); 43 | } 44 | System.out.println("Thank you for your time."); 45 | } 46 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.13.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.InputMismatchException; 3 | 4 | public class MainClass { 5 | public static void main(String[] args) 6 | throws InputMismatchException, IllegalArgumentException { 7 | float numerator = 0.0f; 8 | float denominator = 0.0f; 9 | Scanner s = new Scanner(System.in); 10 | System.out.print("Enter a numerator: "); 11 | numerator = s.nextFloat(); 12 | System.out.print("Enter a denomintor: "); 13 | denominator = s.nextFloat(); 14 | if(denominator == 0.0f) 15 | throw new IllegalArgumentException(); 16 | System.out.println(numerator + " divided by " + denominator + 17 | " is equal to " + (numerator / denominator)); 18 | 19 | System.out.println("Thank you for your time."); 20 | } 21 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.14.java: -------------------------------------------------------------------------------- 1 | // Import all classes from java.util 2 | import java.util.*; 3 | 4 | public class MainClass { 5 | public static void main(String[] args) { 6 | 7 | // Declare a pseudo-random number generator called r 8 | Random r = new Random(); 9 | 10 | 11 | // Declare a new Scanner called scanner 12 | Scanner scanner = new Scanner(System.in); 13 | 14 | // Select a random number from 1, 2 or 3 15 | int doorWithCar = r.nextInt(3) + 1; 16 | System.out.println("There are three doors before you, " + 17 | "behind two doors, you will find a goat, but " + 18 | "behind the third, there is a brand new car!\n"); 19 | 20 | // Declare a variable to store the door the user selects 21 | int selectedDoor = 0; 22 | 23 | // Loop until they guess the right door: 24 | while (selectedDoor != doorWithCar) { 25 | System.out.println("Which door would you like to open?"); 26 | 27 | // Read a door from the user 28 | selectedDoor = scanner.nextInt(); 29 | if (selectedDoor < 1 || selectedDoor > 3) { 30 | System.out.println("The doors are 1, 2 or 3..."); 31 | selectedDoor = 0; 32 | } else if (selectedDoor == doorWithCar) { 33 | System.out.println("Yes, you won a car!!!!"); 34 | } else { 35 | System.out.println("Nope, that's a goat..."); 36 | selectedDoor = 0; 37 | } 38 | } 39 | 40 | if(scanner != null) 41 | scanner.close(); 42 | } 43 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.15.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | // Use a for loop to count from 1 to 100 4 | for(int i = 1; i <= 100; i++) { 5 | // If the number is divisible by 3 and 5, print Fizz!Buzz! 6 | if(i % 15 == 0) System.out.print("Fizz!Buzz!"); 7 | // Otherwise, if the number is divisible by 3, print Fizz! 8 | else if(i % 3 == 0) System.out.print("Fizz! "); 9 | // Otherwise, if the number is divisible by 5, print Buzz! 10 | else if(i % 5 == 0) System.out.print("Buzz!" ); 11 | // Otherwise, just print the number itself 12 | else System.out.print(i + " "); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.2.java: -------------------------------------------------------------------------------- 1 | int password = 25; 2 | String userName = "Sam"; 3 | if(password == 10 && userName == "Sam") { 4 | System.out.println("That is the correct password AND User Name!"); 5 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.3.java: -------------------------------------------------------------------------------- 1 | int age = 190, price = 14; 2 | if(age > 50 && age < 100) { 3 | if(price > 20) { 4 | System.out.println("Age: " + age + " Price: " + price); 5 | } 6 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.4.java: -------------------------------------------------------------------------------- 1 | int i = 25; 2 | if(i % 2 == 0) { 3 | System.out.println("i is even"); 4 | } 5 | else if(i % 3 == 0) { 6 | System.out.println("i is odd, but divisible by 3"); 7 | } 8 | else if(i % 5 == 0) { 9 | System.out.println("i is divisible by 5"); 10 | } 11 | else { 12 | System.out.println("i is not divisible by 2, 3 or 5"); 13 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.5.java: -------------------------------------------------------------------------------- 1 | for(int j = 0; j < 10; j++) { 2 | System.out.println(j); 3 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.6.java: -------------------------------------------------------------------------------- 1 | for(int j = 10; j > 0; j--) { 2 | System.out.println(j); 3 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.7.java: -------------------------------------------------------------------------------- 1 | for(int outerCounter = 0; outerCounter < 5; outerCounter++) { 2 | for(int innerCounter = 0; innerCounter < 3; innerCounter++) { 3 | System.out.println("Outer Counter: " + outerCounter + 4 | " Inner Counter: " + innerCounter); 5 | } 6 | } -------------------------------------------------------------------------------- /Chapter 7/Listing 7.8.java: -------------------------------------------------------------------------------- 1 | while(condition) { 2 | // Body of the loop 3 | } 4 | 5 | do { 6 | // Body of the loop 7 | } while(condition); -------------------------------------------------------------------------------- /Chapter 7/Listing 7.9.java: -------------------------------------------------------------------------------- 1 | int counter = 0; 2 | 3 | while(counter < 10) { 4 | System.out.println("Counter in while: " + counter); 5 | counter++; 6 | } 7 | 8 | do { 9 | System.out.println("Counter in do ... while: " + counter); 10 | counter++; 11 | } while(counter < 10); -------------------------------------------------------------------------------- /Chapter 8/Listing 8.0.java: -------------------------------------------------------------------------------- 1 | public class Circle { 2 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.1.java: -------------------------------------------------------------------------------- 1 | public class Circle { 2 | }public class Circle { 3 | public float radius; 4 | private String name; 5 | protected float lineWidth; 6 | private static int circleCount; 7 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.10.java: -------------------------------------------------------------------------------- 1 | public class Ant extends Insect { 2 | public Ant() { 3 | super(); 4 | wings = false; 5 | } 6 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.2.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | Circle c = new Circle(); 4 | c.radius = 100.0f; 5 | System.out.println("Circle radius is " + c.radius); 6 | } 7 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.3.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | Circle c = new Circle(); 4 | Circle b = new Circle(); 5 | c.radius = 100.0f; 6 | b.radius = 900.0f; 7 | System.out.println("Radius of c: " + c.radius); // Prints 100 8 | System.out.println("Radius of b: " + b.radius); // Prints 900 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.4.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | // Declare array of 100 Circles objects 4 | Circle[] circleArray = new Circle[100]; 5 | 6 | // Use a loop to call new and set radiuses 7 | for(int i = 0; i < 100; i++) { 8 | circleArray[i] = new Circle(); 9 | circleArray[i].radius = (float)i; 10 | } 11 | // Print out a radius as an example 12 | System.out.println 13 | ("circleArray[20] Radius: " + circleArray[20].radius); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.5.java: -------------------------------------------------------------------------------- 1 | public class Circle { 2 | private float radius; 3 | private String name; 4 | private float lineWidth; 5 | private static int circleCount; 6 | 7 | // Constructors: 8 | 9 | // Default Constructor 10 | public Circle() { 11 | this.radius = 0.0f; 12 | this.name = "No name"; 13 | this.lineWidth = 0.0f; 14 | circleCount++; 15 | } 16 | // Two argument constructor 17 | public Circle(String name, float radius) { 18 | this.radius = radius; 19 | this.name = name; 20 | this.lineWidth = 0.0f; 21 | circleCount++; 22 | } 23 | 24 | // Methods 25 | public void setRadius(float radius) { 26 | this.radius = radius; 27 | } 28 | public float getRadius() { 29 | return radius; 30 | } 31 | public void print(){ 32 | System.out.println("Circle: " + name + " Rad: " + radius); 33 | } 34 | protected void setLineWidth(float newWidth) { 35 | this.lineWidth = newWidth; 36 | } 37 | public static void zeroCount() { 38 | circleCount = 0; 39 | } 40 | public static int getCount() { 41 | return circleCount; 42 | } 43 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.6.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | // Create a circle with the default constructor 4 | Circle c = new Circle(); 5 | 6 | // Create a circle with the (String, radius) constructor 7 | Circle b = new Circle("Dennis", 55.0f); 8 | 9 | // See how many circles we've counted with getCount 10 | System.out.println("Number of circles created: " + 11 | Circle.getCount()); 12 | b.print(); // Call the print method of b 13 | 14 | c.setRadius(27.0f);// Call setRadius to set c.radius to 27.0f 15 | c.setLineWidth(100.6f); // set c.lineWidth to 100.6f 16 | System.out.println("Radius of C: " + c.getRadius()); 17 | 18 | Circle.zeroCount(); // Calling a static method 19 | System.out.println( 20 | "Number of circles created: " + Circle.getCount()); 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.7.java: -------------------------------------------------------------------------------- 1 | public class Animal { 2 | public boolean voluntaryMotion; 3 | public boolean requiresFood; 4 | 5 | public Animal(){ 6 | voluntaryMotion = true; 7 | requiresFood = true; 8 | } 9 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.8.java: -------------------------------------------------------------------------------- 1 | public class Insect extends Animal { 2 | 3 | public boolean antennas; 4 | public String skeleton; 5 | public int numberOfLegs; 6 | public boolean wings; 7 | 8 | public Insect(){ 9 | super(); 10 | antennas = true; 11 | skeleton = "Exosketon"; 12 | numberOfLegs = 6; 13 | wings = true; 14 | } 15 | 16 | public void Print() { 17 | System.out.println("Antennas: " + antennas + 18 | " Skeleton: " + skeleton + 19 | " Number of Legs: " + numberOfLegs + 20 | " Wings: " + wings + 21 | " Voluntary Motion: " + voluntaryMotion + 22 | " Requires Food: " + requiresFood); 23 | } 24 | } -------------------------------------------------------------------------------- /Chapter 8/Listing 8.9.java: -------------------------------------------------------------------------------- 1 | public class MainClass { 2 | public static void main(String[] args) { 3 | // Create an instance of Insect called i 4 | Insect i = new Insect(); 5 | // Change the insect class's members: 6 | i.antennas = false; 7 | i.numberOfLegs = 100; 8 | // Call the Insect's Print method: 9 | i.Print(); 10 | // Change the parent class's requiresFood member: 11 | i.requiresFood = false; 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter 9/Listing 9.0.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class MainClass { 4 | public static void main(String[] args) { 5 | 6 | // Declare local variables 7 | Scanner scanner = new Scanner(System.in); // For reading input 8 | String playerInput = ""; // Variable for reading player input 9 | int sticks = 37; // Current number of sticks left 10 | 11 | // Print some instructions: 12 | System.out.println("Remove 1, 2 or 3 sticks from a pile."); 13 | System.out.println("The player who takes the final stick"); 14 | System.out.println("is the winner!"); 15 | 16 | // Main game loop: 17 | while(true) { 18 | // Tell the user how many sticks are left, and print a prompt 19 | System.out.println("There are " + sticks + " left. "); 20 | System.out.print("How many would you like to take? "); 21 | 22 | // Read the user's input 23 | playerInput = scanner.nextLine(); 24 | 25 | // Subtract the number of sticks the player chose: 26 | if(playerInput.equals("1")) 27 | sticks--; 28 | else if(playerInput.equals("2")) 29 | sticks-=2; 30 | else if(playerInput.equals("3")) 31 | sticks-=3; 32 | 33 | // If they did not select 1, 2 or 3, ask them to select again: 34 | else { 35 | System.out.println 36 | ("You can only take 1, 2, or 3 sticks...\n"); 37 | continue; 38 | } 39 | // Did the player win? 40 | if(sticks == 0) { 41 | System.out.println("You took the last stick, you win!"); 42 | break; 43 | } 44 | // If the player did not win, it's the computer's turn: 45 | if(sticks % 4 != 0) { 46 | // Print the player's and the computer's moves 47 | System.out.println 48 | ("Ok, you take " + playerInput + ", I'll take " + 49 | (sticks % 4) + "."); 50 | 51 | // Subtract the computer's move from the pile: 52 | sticks -= (sticks % 4); 53 | } 54 | else { 55 | // We have lost unless the player makes a mistake! 56 | // Select a random number of sticks to subtract: 57 | 58 | int take = (int)(Math.random() * 3.0) + 1; 59 | 60 | // Print out the player's and computer's moves 61 | System.out.println 62 | ("Ok, you take " + playerInput + 63 | ", I'll take " + take + "."); 64 | 65 | // Subtract the computer's move from the pile: 66 | sticks-=take; 67 | } 68 | 69 | // If the computer just took the last stick, the player loses! 70 | if(sticks == 0) { 71 | System.out.println("I took the last stick, I win!"); 72 | break; 73 | } 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /Chapter 9/Listing 9.1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class MainClass { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | String userInput = ""; // User's input string 7 | int x; // The value of userInput converted to int 8 | int iterations = 0;// Iterations to reach 1 9 | int largest = 0; // Largest value reached in computation 10 | while(true) { 11 | System.out.println("Enter a number, or 'Q' to quit: "); 12 | userInput = scanner.nextLine().toLowerCase(); 13 | // Allow the user to quit: 14 | if(userInput.equals("q")) 15 | break; 16 | x = Integer.parseInt(userInput); 17 | 18 | largest = 0; 19 | for(iterations = 0; ; iterations++) { 20 | 21 | // If x is even, halve it: 22 | if(x % 2 == 0) 23 | x /= 2; 24 | 25 | // Otherwise, x is odd, mul by 3 and increment 26 | else 27 | x = (3 * x) + 1; 28 | 29 | // Make sure we do not overflow the int: 30 | if(x <= 0) { 31 | System.out.println( 32 | "Overflow detected, use long for more range..."); 33 | break; 34 | } 35 | 36 | // If x is > than largest, record new record: 37 | if(largest < x) 38 | largest = x; 39 | 40 | // If we find 1, we're done: 41 | if(x == 1) 42 | break; 43 | } 44 | System.out.println("Reached 1 after " + iterations + 45 | " iterations. Largest point reached: " + largest + "."); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java Succinctly Part 1 2 | 3 | This is the companion repo for [*Java Succinctly Part 1*](https://www.syncfusion.com/ebooks/Java_Succinctly_Part_1) by Chris Rose. Published by Syncfusion. 4 | 5 | [![cover](https://github.com/SyncfusionSuccinctlyE-Books/Java-Succinctly-Part-1/blob/master/cover.png)](https://www.syncfusion.com/ebooks/Java_Succinctly_Part_1) 6 | 7 | ## Looking for more _Succinctly_ titles? 8 | 9 | Check out the entire library of more than 130 _Succinctly_ e-books at [https://www.syncfusion.com/ebooks](https://www.syncfusion.com/ebooks). 10 | -------------------------------------------------------------------------------- /cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SyncfusionSuccinctlyE-Books/Java-Succinctly-Part-1/77ffd45d967f6b738d64c5b7ad79338535dddb28/cover.png --------------------------------------------------------------------------------