├── .gitignore ├── docker └── builds │ └── jdk │ ├── bin │ └── entrypoint.sh │ ├── etc │ └── env-example │ └── Dockerfile ├── src └── main │ └── java │ └── com │ └── guide │ ├── c10 │ ├── RWData.java │ ├── CopyFile.java │ ├── ShowFile.java │ ├── CompFiles.java │ ├── CompFiles2.java │ ├── CompFiles3.java │ ├── ShowFile2.java │ ├── ShowFile3.java │ ├── RandomAccessDemo.java │ ├── README.md │ ├── WriteDemo.java │ └── ReadBytes.java │ ├── c7 │ ├── X2.java │ ├── Y.java │ ├── A.java │ ├── X.java │ ├── Sup.java │ ├── C2.java │ ├── Sub1.java │ ├── Sub2.java │ ├── OrderOfConstruction.java │ ├── README.md │ ├── B2.java │ ├── UseSuper.java │ ├── A2.java │ ├── Override.java │ ├── Y2.java │ ├── Overload.java │ ├── TwoDShape.java │ ├── B4.java │ ├── FinalD.java │ ├── A3.java │ ├── Rectangle.java │ ├── Triangle.java │ ├── Triangle2.java │ ├── A4.java │ ├── B3.java │ ├── IncompatibleRef.java │ ├── B.java │ ├── Truck.java │ ├── ColorTriangle.java │ ├── DynDispDemo.java │ ├── Triangle4.java │ ├── Triangle3.java │ ├── SupSubRef.java │ ├── Shapes3.java │ ├── Shapes4.java │ ├── ErrorMsg.java │ ├── SelfTest2.java │ ├── AbsShape.java │ ├── Shapes7.java │ ├── DynShapes.java │ ├── Rectangle8.java │ ├── Rectangle9.java │ ├── Shapes6.java │ ├── TwoDShape2.java │ ├── TwoDShape4.java │ ├── Triangle6.java │ ├── Shapes.java │ ├── Shapes2.java │ ├── SelfTest2Demo.java │ ├── Triangle5.java │ ├── TruckDemo.java │ ├── Shapes5.java │ ├── Triangle7.java │ ├── Triangle8.java │ ├── TwoDShape5.java │ ├── TwoDShape6.java │ ├── Triangle9.java │ ├── TwoDShape7.java │ └── Vehicle.java │ ├── c8 │ ├── A.java │ ├── B.java │ ├── IConst.java │ ├── README.md │ ├── Series.java │ ├── ICharQ.java │ ├── IFExtend.java │ ├── qpack │ │ ├── ICharQ.java │ │ ├── FixedQueue.java │ │ ├── DynQueue.java │ │ └── CircularQueue.java │ ├── MyIFImp.java │ ├── MyIFImp2.java │ ├── MyIF.java │ ├── MyClass.java │ ├── IConstD.java │ ├── IVehicle.java │ ├── ByTwos.java │ ├── ByThrees.java │ ├── MyIF2.java │ ├── SeriesDemo2.java │ ├── Series2.java │ ├── bookpack │ │ ├── Book.java │ │ ├── Book2.java │ │ ├── Book3.java │ │ └── BookDemo.java │ ├── DefaultMethodDemo.java │ ├── ByTwos2.java │ ├── SeriesDemo.java │ ├── bookpackext │ │ ├── UseBook2.java │ │ ├── UseBook.java │ │ ├── ProtectDemo.java │ │ └── ExtBook.java │ ├── FixedQueue.java │ ├── Series3.java │ ├── DynQueue.java │ └── CircularQueue.java │ ├── c5 │ ├── SelfTest2.java │ ├── SelfTest1.java │ ├── ArrayErr.java │ ├── NoZeroDiv2.java │ ├── SubStr.java │ ├── ShowBits.java │ ├── NoZeroDiv.java │ ├── ArrayDemo.java │ ├── CLDemo.java │ ├── SelfTest3.java │ ├── LowCase.java │ ├── TwoD.java │ ├── StringDemo.java │ ├── UpCase.java │ ├── MinMax2.java │ ├── ForEach.java │ ├── NoChange.java │ ├── SelfTest13.java │ ├── Search.java │ ├── NotDemo.java │ ├── ACopy.java │ ├── StringArrays.java │ ├── ForEach2.java │ ├── Squares.java │ ├── StringSwitch.java │ ├── ShowBitsDemo.java │ ├── MinMax.java │ ├── ShowBits53.java │ ├── Queue.java │ ├── ShiftDemo.java │ ├── Encode.java │ ├── Phone.java │ ├── Bubble.java │ ├── LengthDemo.java │ ├── SelfTest4.java │ ├── Ragged.java │ ├── AssignARef.java │ └── SelfTest7.java │ ├── c2 │ ├── SelfTest4.java │ ├── StrDemo.java │ ├── DynInit.java │ ├── LtoD.java │ ├── UseCast.java │ ├── PromDemo.java │ ├── Inches.java │ ├── Sound.java │ ├── VarInitDemo.java │ ├── ScopeDemo.java │ ├── Hypot.java │ ├── CharArithDemo.java │ ├── SelfTest10.java │ ├── SoundExt.java │ ├── ModDemo.java │ ├── BoolDemo.java │ ├── LtoD2.java │ ├── NestVar.java │ ├── SideEffects.java │ ├── SCops.java │ ├── CastDemo.java │ ├── RelLogOps.java │ ├── LogicalOpTable.java │ └── LogicalOpTable2.java │ ├── c4 │ ├── Factor.java │ ├── MyClass.java │ ├── ChkNum.java │ ├── ConsDemo.java │ ├── ParmConsDemo.java │ ├── IsFact.java │ ├── ParmDemo.java │ ├── Pwr.java │ ├── DemoPwr.java │ ├── Vehicle.java │ ├── VehicleDemo.java │ ├── VehConsDemo.java │ ├── HelpClassDemo.java │ ├── AddMeth.java │ ├── RetMeth.java │ ├── CompFuel.java │ └── TwoVehicles.java │ ├── c9 │ ├── README.md │ ├── QueueEmptyException.java │ ├── FinallyDemo.java │ ├── ICharQ.java │ ├── QueueFullException.java │ ├── NotHandled.java │ ├── RethrowDemo.java │ ├── NonIntResultException.java │ ├── ExcTest2.java │ ├── ExcDemo2.java │ ├── ExcTest.java │ ├── ThrowDemo.java │ ├── UseThrowableMethods.java │ ├── ExcDemo3.java │ ├── ThrowsDemo.java │ ├── ExcTypeMismatch.java │ ├── ExcDemo1.java │ ├── ExcDemo5.java │ ├── MultiCatch.java │ ├── ExcDemo4.java │ ├── Rethrow.java │ ├── NestTrys.java │ ├── FixedQueue.java │ ├── CustomExceptDemo.java │ ├── QExcDemo.java │ └── UseFinally.java │ ├── c6 │ ├── README.md │ ├── StaticMeth.java │ ├── SumIt.java │ ├── NestedClassDemo.java │ ├── Test.java │ ├── ErrMsg.java │ ├── Err.java │ ├── Test2.java │ ├── SelfTest4.java │ ├── SumDemo.java │ ├── StaticDemo.java │ ├── SDemo3.java │ ├── PassObRef.java │ ├── CallByValue.java │ ├── ErrInfo.java │ ├── StaticBlock.java │ ├── Summation.java │ ├── SDemo2.java │ ├── MyClass.java │ ├── Overload2.java │ ├── SelfTest13.java │ ├── StaticError.java │ ├── ErrorMsg.java │ ├── SelfTest6.java │ ├── PassOb.java │ ├── AccessDemo.java │ ├── ErrorInfo.java │ ├── OverloadConsDemo.java │ ├── Factorial.java │ ├── VarArgs4.java │ ├── TypeConv.java │ ├── Block.java │ ├── VarArgs2.java │ ├── MyClass2.java │ ├── OverloadDemo.java │ ├── QSDemo.java │ ├── VarArgs.java │ ├── Overload.java │ ├── SelfTest3.java │ ├── Recursion.java │ ├── Queue.java │ ├── Stack.java │ ├── Quicksort.java │ ├── FSDemo.java │ ├── FailSoftArray.java │ ├── Outer.java │ ├── SDemo.java │ ├── LocalClassDemo.java │ ├── QDemo2.java │ ├── VarArgs3.java │ └── Queue2.java │ ├── c1 │ ├── Example.java │ ├── GalToLit.java │ ├── BlockDemo.java │ ├── Example2.java │ ├── SelfTest9.java │ ├── Example3.java │ ├── GalToLitTable.java │ ├── SelfTest10.java │ └── IfDemo.java │ └── c3 │ ├── DecrFor.java │ ├── Comma.java │ ├── SelfTest4.java │ ├── Empty3.java │ ├── ContDemo.java │ ├── Empty.java │ ├── WhileDemo.java │ ├── BreakErr.java │ ├── DWDemo.java │ ├── ForTest.java │ ├── Empty2.java │ ├── SelfTest9.java │ ├── SelfTest8.java │ ├── KbIn.java │ ├── Break2.java │ ├── BreakDemo.java │ ├── ContToLabel.java │ ├── SelfTest7.java │ ├── Power.java │ ├── FindFact.java │ ├── Guess.java │ ├── Guess2.java │ ├── ForVar.java │ ├── SqrRoot.java │ ├── Break3.java │ ├── Ladder.java │ ├── SelfTest1.java │ ├── Break5.java │ ├── Guess3.java │ ├── Break6.java │ ├── NoBreak.java │ ├── Break4.java │ ├── SwitchDemo.java │ ├── SelfTest10.java │ ├── Guess4.java │ └── Help.java ├── java-beginners-guide.jpg └── docker-compose.yml /.gitignore: -------------------------------------------------------------------------------- 1 | *.idea 2 | *.iml 3 | .env 4 | jvm-sources 5 | .m2 6 | target -------------------------------------------------------------------------------- /docker/builds/jdk/bin/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | 4 | exec "$@" -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/RWData.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | public class RWData { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/CopyFile.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | public class CopyFile { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/ShowFile.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | public class ShowFile { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/CompFiles.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | public class CompFiles { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/CompFiles2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | public class CompFiles2 { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/CompFiles3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | public class CompFiles3 { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/ShowFile2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | public class ShowFile2 { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/ShowFile3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | public class ShowFile3 { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/RandomAccessDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | public class RandomAccessDemo { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/X2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | class X2 { 4 | int a; 5 | 6 | X2(int i) { a = i; } 7 | } -------------------------------------------------------------------------------- /java-beginners-guide.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pavel-rossinsky/java-a-beginners-guide-herbert-schildt/HEAD/java-beginners-guide.jpg -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Y.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Y { 4 | int a; 5 | 6 | Y(int i) { a = i; } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/A.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public interface A { 4 | void meth1(); 5 | void meth2(); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/A.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Using super to overcome name hiding. 4 | public class A { 5 | int i; 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/X.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // This will not compile. 4 | public class X { 5 | int a; 6 | 7 | X(int i ) { a = i; } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Sup.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Sup { 4 | void who() { 5 | System.out.println("who() in Sup"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/C2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class C2 extends B2 { 4 | C2() { 5 | System.out.println("Constructing C."); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/B.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // B now includes meth1() and meth2() - it adds meth3(). 4 | public interface B extends A { 5 | void meth3(); 6 | } 7 | -------------------------------------------------------------------------------- /docker/builds/jdk/etc/env-example: -------------------------------------------------------------------------------- 1 | # Your local path to the source files (no trailing slash at the end) 2 | CODEBASE=/your/local/path 3 | # Set to 1 if you want to debug java code 4 | DEBUG=0 -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Sub1.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Sub1 extends Sup { 4 | void who() { 5 | System.out.println("who() in Sub1"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Sub2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Sub2 extends Sup { 4 | void who() { 5 | System.out.println("who() is Sub2"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/IConst.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public interface IConst { 4 | int MIN = 0; 5 | int MAX = 10; 6 | String ERRORMSG = "Boundary Error"; 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/SelfTest2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | public class SelfTest2 { 4 | public static void main(String[] args) { 5 | int[] def1 = { 1, 2, 3, 4, 5 }; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/SelfTest4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | public class SelfTest4 { 4 | public static void main(String[] args) { 5 | System.out.println("One\nTwo\nThree"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/OrderOfConstruction.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class OrderOfConstruction { 4 | public static void main(String[] args) { 5 | C2 c = new C2(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 10: Using I/O 2 | 3 | See the list of classes, key skills, and concepts under:\ 4 | https://github.com/pavel-rossinsky/java-a-beginners-guide-herbert-schildt/issues/18 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 7: Inheritance 2 | 3 | See the list of classes, key skills, and concepts under:\ 4 | https://github.com/pavel-rossinsky/java-a-beginners-guide-herbert-schildt/issues/15 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/Factor.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class Factor { 4 | boolean isFactor(int a, int b) { 5 | if ((b % a) == 0) return true; 6 | else return false; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/B2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Create a subclass by extending class A. 4 | public class B2 extends A2 { 5 | B2() { 6 | System.out.println("Constructing B."); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 9: Exception Handling 2 | 3 | See the list of classes, key skills, and concepts under:\ 4 | https://github.com/pavel-rossinsky/java-a-beginners-guide-herbert-schildt/issues/17 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 8: Packages and Interfaces 2 | 3 | See the list of classes, key skills, and concepts under:\ 4 | https://github.com/pavel-rossinsky/java-a-beginners-guide-herbert-schildt/issues/16 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/UseSuper.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class UseSuper { 4 | public static void main(String[] args) { 5 | B subOb = new B(1, 2); 6 | 7 | subOb.show(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 6: A Closer Look at Methods and Classes 2 | 3 | See the list of classes, key skills, and concepts under:\ 4 | https://github.com/pavel-rossinsky/java-a-beginners-guide-herbert-schildt/issues/14 5 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/Series.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public interface Series { 4 | int getNext(); // return next number in series 5 | void reset(); // restart 6 | void setStart(int x); // set starting value 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c1/Example.java: -------------------------------------------------------------------------------- 1 | package com.guide.c1; 2 | 3 | class Example { 4 | public static void main(String[] args) { 5 | System.out.println(Example.class); 6 | System.out.println("Java drives the Web"); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/A2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Demonstrate when constructors are executed 4 | 5 | // Create a super class. 6 | public class A2 { 7 | A2() { 8 | System.out.println("Constructing A."); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Override.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Override { 4 | public static void main(String[] args) { 5 | B3 subOb = new B3(1, 2, 3); 6 | 7 | subOb.show(); // this calls show in B 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/QueueEmptyException.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // An exception for queue-empty errors. 4 | public class QueueEmptyException extends Exception { 5 | public String toString() { 6 | return "\nQueue is empty."; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/MyClass.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class MyClass { 4 | int x; 5 | 6 | // A simple constructor 7 | MyClass() { 8 | x = 10; 9 | } 10 | 11 | MyClass(int i) { 12 | x = i; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/ICharQ.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // A character queue interface. 4 | public interface ICharQ { 5 | // Put a character into the queue. 6 | void put(char ch); 7 | 8 | // Get a character from the queue. 9 | char get(); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/IFExtend.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public class IFExtend { 4 | public static void main(String[] args) { 5 | MyClass ob = new MyClass(); 6 | 7 | ob.meth1(); 8 | ob.meth2(); 9 | ob.meth3(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/StaticMeth.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Use a static method. 4 | public class StaticMeth { 5 | static int val = 1024; // a static variable 6 | 7 | // a static method 8 | static int valDiv2() { 9 | return val/2; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/qpack/ICharQ.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.qpack; 2 | 3 | // A character queue interface. 4 | public interface ICharQ { 5 | // Put a character into the queue. 6 | void put(char ch); 7 | 8 | // Get a character from the queue. 9 | char get(); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/SumIt.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class SumIt { 4 | int sum(int ... n) { 5 | int result = 0; 6 | 7 | for (int i = 0; i < n.length; i++) { 8 | result += n[i]; 9 | } 10 | return result; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/ChkNum.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | // A simple example that uses a parameter. 4 | public class ChkNum { 5 | // return true if x is even 6 | boolean isEven(int x) { 7 | if ((x%2) == 0) return true; 8 | else return false; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/ConsDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class ConsDemo { 4 | public static void main(String[] args) { 5 | MyClass t1 = new MyClass(); 6 | MyClass t2 = new MyClass(); 7 | 8 | System.out.println(t1.x + " " + t2.x); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/NestedClassDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class NestedClassDemo { 4 | public static void main(String[] args) { 5 | int[] x = { 3, 2, 1, 5, 6, 9, 7, 8 }; 6 | Outer outOb = new Outer(x); 7 | 8 | outOb.analyze(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Test.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Primitive types are passed by value. 4 | public class Test { 5 | // This method causes no change to the arguments used in the call. 6 | void noChange(int i, int j) { 7 | i = i + j; 8 | j = -j; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/ParmConsDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class ParmConsDemo { 4 | public static void main(String[] args) { 5 | MyClass t1 = new MyClass(10); 6 | MyClass t2 = new MyClass(88); 7 | 8 | System.out.println(t1.x + " " + t2.x); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/FinallyDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | public class FinallyDemo { 4 | public static void main(String[] args) { 5 | for (int i = 0; i < 3; i++) { 6 | UseFinally.genException(i); 7 | System.out.println(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/SelfTest1.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Two ways to declare one-dimensional array of 12 doubles. 4 | public class SelfTest1 { 5 | public static void main(String[] args) { 6 | double[] def1 = new double[12]; 7 | double def2[] = new double[12]; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/ErrMsg.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class ErrMsg { 4 | public static void main(String[] args) { 5 | ErrorMsg err = new ErrorMsg(); 6 | 7 | System.out.println(err.getErrorMsg(2)); 8 | System.out.println(err.getErrorMsg(19)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Y2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | class Y2 extends X2 { 4 | public static void main(String[] args) { 5 | System.out.println("Hello, I'm Y2 class!"); 6 | } 7 | int b; 8 | 9 | Y2(int i, int j) { 10 | super(j); 11 | b = i; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Overload.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Overload { 4 | public static void main(String[] args) { 5 | B4 subOb = new B4(1, 2, 3); 6 | subOb.show("This is k: "); // this calls show() in B 7 | subOb.show(); // this calls show() in A 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/WriteDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | // Demonstrate System.out.write(). 4 | public class WriteDemo { 5 | public static void main(String[] args) { 6 | int b; 7 | 8 | b = 'X'; 9 | System.out.write(b); 10 | System.out.write('\n'); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/DecrFor.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // A negatively running for loop. 4 | public class DecrFor { 5 | public static void main(String[] args) { 6 | int x; 7 | 8 | for (x = 100; x > -100; x -= 5) { 9 | System.out.println(x); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/TwoDShape.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A class for two-dimensional hierarchy. 4 | public class TwoDShape { 5 | double width; 6 | double height; 7 | 8 | void showDim() { 9 | System.out.println("Width and height are " + width + " and " + height); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Err.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Return a programmer-defined object. 4 | public class Err { 5 | String msg; // error message 6 | int severity; // code indicating severity of error 7 | 8 | Err(String m, int s) { 9 | msg = m; 10 | severity = s; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/B4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class B4 extends A4 { 4 | int k; 5 | 6 | B4(int a, int b, int c) { 7 | super(a, b); 8 | k = c; 9 | } 10 | 11 | // overload show() 12 | void show(String msg) { 13 | System.out.println(msg + k); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/FinalD.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class FinalD { 4 | public static void main(String[] args) { 5 | ErrorMsg err = new ErrorMsg(); 6 | 7 | System.out.println(err.getErrorMsg(err.OUTERR)); 8 | System.out.println(err.getErrorMsg(err.DISKERR)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/MyIFImp.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // Implement MyIF. 4 | public class MyIFImp implements MyIF { 5 | // Only getUserID() defined by MyIF needs to be implemented. 6 | // getAdminID() can be allowed to default. 7 | public int getUserID() { 8 | return 100; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ICharQ.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // A character queue interface. 4 | public interface ICharQ { 5 | // Put a character into the queue. 6 | void put(char ch) throws QueueFullException; 7 | 8 | // Get a character from the queue. 9 | char get() throws QueueEmptyException; 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/A3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Method overriding. 4 | public class A3 { 5 | int i, j; 6 | A3(int a, int b) { 7 | i = a; 8 | j = b; 9 | } 10 | 11 | // display i and j 12 | void show() { 13 | System.out.println("i and j: " + i + " " + j); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/MyIFImp2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // Here, implementations for both getUserID() and getAdminID() are provided. 4 | public class MyIFImp2 implements MyIF { 5 | public int getUserID() { 6 | return 100; 7 | } 8 | 9 | public int getAdminID() { 10 | return 42; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/StrDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Demonstrate escape sequences in strings 4 | public class StrDemo { 5 | public static void main(String[] args) { 6 | System.out.println("First line\nSecond line"); 7 | System.out.println("A\tB\tC"); 8 | System.out.println("D\tE\tF"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Comma.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Use commas in a for statement. 4 | public class Comma { 5 | public static void main(String[] args) { 6 | int i, j; 7 | 8 | for (i = 0, j = 10; i < j; i++, j--) { 9 | System.out.println("i and j: " + i + " " + j); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/IsFact.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class IsFact { 4 | public static void main(String[] args) { 5 | Factor x = new Factor(); 6 | 7 | if (x.isFactor(2, 20)) System.out.println("2 is factor"); 8 | if (x.isFactor(3, 20)) System.out.println("this won't be displayed"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/ArrayErr.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Demonstrate an array overrun. 4 | public class ArrayErr { 5 | public static void main(String[] args) { 6 | int[] sample = new int[10]; 7 | int i; 8 | 9 | for (i = 0; i < 100; i++) { 10 | sample[i] = i; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Test2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Objects are passed through their references. 4 | public class Test2 { 5 | int a, b; 6 | 7 | Test2(int i, int j) { 8 | a = i; 9 | b = j; 10 | } 11 | 12 | void change(Test2 ob) { 13 | ob.a = ob.a + ob.b; 14 | ob.b = -ob.b; 15 | } 16 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/SelfTest4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | /** 4 | * Show the for statement for a loop that count from 1000 to 0 by -2. 5 | */ 6 | public class SelfTest4 { 7 | public static void main(String[] args) { 8 | for (int j = 1000; j >= 0; j -= 2) { 9 | System.out.println(j); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for rectangles. 4 | public class Rectangle extends TwoDShape { 5 | boolean isSquare() { 6 | if (width == height) return true; 7 | return false; 8 | } 9 | 10 | double area() { 11 | return width * height; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/SelfTest4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Show the swap() method. 4 | public class SelfTest4 { 5 | int a; 6 | 7 | SelfTest4(int i) { 8 | a = i; 9 | } 10 | 11 | public void swap(SelfTest4 t1, SelfTest4 t2) { 12 | int b = t1.a; 13 | t1.a = t2.a; 14 | t2.a = t1.a; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/SumDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class SumDemo { 4 | public static void main(String[] args) { 5 | Summation s1 = new Summation(5); 6 | Summation s2 = new Summation(s1); 7 | 8 | System.out.println("s1.sum: " + s1.sum); 9 | System.out.println("s2.sum: " + s2.sum); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c1/GalToLit.java: -------------------------------------------------------------------------------- 1 | package com.guide.c1; 2 | 3 | class GalToLit { 4 | public static void main(String[] args) { 5 | double gallons; 6 | double liters; 7 | 8 | gallons = 10; 9 | 10 | liters = gallons * 3.7854; 11 | 12 | System.out.println(gallons + " gallons is " + liters + " liters."); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Triangle.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for triangles. 4 | public class Triangle extends TwoDShape { 5 | String style; 6 | 7 | double area() { 8 | return width * height / 2; 9 | } 10 | 11 | void showStyle() { 12 | System.out.println("Triangle is " + style); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/DynInit.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | public class DynInit { 4 | public static void main(String[] args) { 5 | double radius = 4, height = 5; 6 | 7 | // dynamically initialize volume 8 | double volume = 3.1416 * radius * radius * height; 9 | 10 | System.out.println("Volume is " + volume); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/StaticDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Use a static variable. 4 | public class StaticDemo { 5 | int x; // a normal instance variable 6 | static int y; // a static variable 7 | 8 | // Return the sum of the instance variable x 9 | // and the static variable y. 10 | int sum() { 11 | return x + y; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Triangle2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for triangles. 4 | public class Triangle2 extends TwoDShape2 { 5 | String style; 6 | 7 | double area() { 8 | return getWidth() * getHeight() / 2; 9 | } 10 | 11 | void showStyle() { 12 | System.out.println("Triangle is " + style); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/NoZeroDiv2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Prevent a division by zero using the ?. 4 | public class NoZeroDiv2 { 5 | public static void main(String[] args) { 6 | for (int i = -5; i < 6; i++) { 7 | if (i != 0 ? true : false) 8 | System.out.println("100 / " + i + " is " + 100 / i); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/SDemo3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class SDemo3 { 4 | public static void main(String[] args) { 5 | StaticBlock ob = new StaticBlock("Inside Constructor"); 6 | 7 | System.out.println("Square root of 2 is " + StaticBlock.rootOf2); 8 | System.out.println("Square root of 3 is " + StaticBlock.rootOf3); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Empty3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // The body of a loop can be empty. 4 | public class Empty3 { 5 | public static void main(String[] args) { 6 | int i; 7 | int sum = 0; 8 | 9 | // sum the numbers through 5 10 | for (i = 1; i <= 5; sum += i++); 11 | 12 | System.out.println("Sum is " + sum); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/A4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Methods with differing signatures are overloaded and not overridden. 4 | public class A4 { 5 | int i, j; 6 | 7 | A4(int a, int b) { 8 | i = a; 9 | j = b; 10 | } 11 | 12 | // display i and j 13 | void show() { 14 | System.out.println("i and j: " + i + " " + j); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/QueueFullException.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // An exception for queue-full errors. 4 | public class QueueFullException extends Exception { 5 | int size; 6 | 7 | QueueFullException(int s) { 8 | size = s; 9 | } 10 | 11 | public String toString() { 12 | return "\nQueue is full. Maximum size is " + size; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/B3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class B3 extends A3 { 4 | int k; 5 | 6 | B3(int a, int b, int c) { 7 | super(a, b); 8 | k = c; 9 | } 10 | 11 | // display k - this overriders show() in A 12 | void show() { 13 | super.show(); // this calls A's show() 14 | System.out.println("k: " + k); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/NotHandled.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Let JVM handle the error. 4 | public class NotHandled { 5 | public static void main(String[] args) { 6 | int[] nums = new int[4]; 7 | 8 | System.out.println("Before exception is generated."); 9 | 10 | // generate an index out-of-bounds exception 11 | nums[7] = 10; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/ContDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Use continue. 4 | public class ContDemo { 5 | public static void main(String[] args) { 6 | int i; 7 | 8 | // print even numbers between 0 and 100 9 | for (i = 1; i <= 100; i++) { 10 | if ((i % 2) != 0) continue; 11 | System.out.println(i); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/ParmDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class ParmDemo { 4 | public static void main(String[] args) { 5 | ChkNum e = new ChkNum(); 6 | 7 | if (e.isEven(10)) System.out.println("10 is even."); 8 | 9 | if (e.isEven(9)) System.out.println("9 is even."); 10 | 11 | if (e.isEven(8)) System.out.println("8 is even."); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/SubStr.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Use substring(). 4 | public class SubStr { 5 | public static void main(String[] args) { 6 | String orgstr = "Java makes the Web move."; 7 | 8 | String substr = orgstr.substring(5, 18); 9 | 10 | System.out.println("orgstr: " + orgstr); 11 | System.out.println("substr: " + substr); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c1/BlockDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c1; 2 | 3 | class BlockDemo { 4 | public static void main(String[] args) { 5 | double i, j, d; 6 | 7 | i = 5; 8 | j = 10; 9 | 10 | if (i != 0) { 11 | System.out.println("i does not equal zero"); 12 | d = j / i; 13 | System.out.println("j / i is " + d); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Empty.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Parts of the for can be empty. 4 | public class Empty { 5 | public static void main(String[] args) { 6 | int i; 7 | 8 | for (i = 0; i < 10; ) { // the iteration expression is missing. 9 | System.out.println("Pass #" + i); 10 | i++; // increment loop control var 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/WhileDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Demonstrate the while loop. 4 | public class WhileDemo { 5 | public static void main(String[] args) { 6 | char ch; 7 | 8 | // print the alphabet using a while loop. 9 | ch = 'a'; 10 | while (ch <= 'z') { 11 | System.out.print(ch); 12 | ch++; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/IncompatibleRef.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Compilation error: incompatible types 4 | public class IncompatibleRef { 5 | public static void main(String[] args) { 6 | X x = new X(10); 7 | X x2; 8 | Y y = new Y(15); 9 | 10 | x2 = x; // OK, both of the same type 11 | 12 | // x2 = y; // Error, not of the same type 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/MyIF.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public interface MyIF { 4 | // This is a "normal" interface method declaration. 5 | // It does NOT define a default implementation. 6 | int getUserID(); 7 | 8 | // This is a default method. Notice that it provides 9 | // a default implementation. 10 | default int getAdminID() { 11 | return 1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/RethrowDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | public class RethrowDemo { 4 | public static void main(String[] args) { 5 | try { 6 | Rethrow.getException(); 7 | } catch (ArrayIndexOutOfBoundsException exc) { 8 | // recatch exception 9 | System.out.println("Fatal error - program terminated."); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/PassObRef.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class PassObRef { 4 | public static void main(String[] args) { 5 | Test2 ob = new Test2(15, 20); 6 | 7 | System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b); 8 | 9 | ob.change(ob); 10 | 11 | System.out.println("ob.a and ob.b after call: " + ob.a + " " + ob.b); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/LtoD.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Demonstrate automatic conversion from long to double. 4 | public class LtoD { 5 | public static void main(String[] args) { 6 | long L; 7 | double D; 8 | 9 | L = 100123285L; 10 | D = L; // automatic conversion from long to double. 11 | 12 | System.out.println("L and D: " + L + " " + D); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c1/Example2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c1; 2 | 3 | class Example2 { 4 | public static void main(String[] args) { 5 | int var1; 6 | int var2; 7 | 8 | var1 = 1024; 9 | 10 | System.out.println("var 1 contains " + var1); 11 | 12 | var2 = var1 / 2; 13 | 14 | System.out.print("var 2 contains var1 / 2: "); 15 | System.out.println(var2); 16 | } 17 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/Pwr.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class Pwr { 4 | double b; 5 | int e; 6 | double val; 7 | 8 | Pwr(double base, int exp) { 9 | b = base; 10 | e = exp; 11 | 12 | val = 1; 13 | if (exp == 0) return; 14 | for ( ; exp > 0; exp--) val = val * base; 15 | } 16 | 17 | double get_pwr() { 18 | return val; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/CallByValue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class CallByValue { 4 | public static void main(String[] args) { 5 | Test ob = new Test(); 6 | 7 | int a = 15, b = 20; 8 | 9 | System.out.println("a and b before call: " + a + " " + b); 10 | 11 | ob.noChange(a, b); 12 | 13 | System.out.println("a and b after call: " + a + " " + b); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/NonIntResultException.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Use a custom exception. 4 | public class NonIntResultException extends Exception { 5 | int n; 6 | int d; 7 | 8 | NonIntResultException(int i, int j) { 9 | n = i; 10 | d = j; 11 | } 12 | 13 | public String toString() { 14 | return "Result of " + n + " / " + d + " in non-integer."; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c1/SelfTest9.java: -------------------------------------------------------------------------------- 1 | package com.guide.c1; 2 | 3 | /** 4 | * Calculates effective weight on the Moon of a man of 80kg. 5 | */ 6 | class SelfTest9 { 7 | public static void main(String[] args) { 8 | double gravityCoefficient = 0.17; 9 | 10 | double effectiveWeight = 80 * gravityCoefficient; 11 | 12 | System.out.println("Effective weight on the Moon: " + effectiveWeight); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/UseCast.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Using a cast. 4 | public class UseCast { 5 | public static void main(String[] args) { 6 | int i; 7 | 8 | for (i = 0; i < 5; i++) { 9 | System.out.println(i + " / 3: " + i / 3); 10 | System.out.println(i + " / 3 with fractions: " + (double)i / 3); 11 | System.out.println(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/ShowBits.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Display the bits within a byte 4 | public class ShowBits { 5 | public static void main(String[] args) { 6 | int t; 7 | byte val; 8 | 9 | val = 123; 10 | for (t = 128; t > 0; t = t/2) { 11 | if ((val & t) != 0) System.out.print("1 "); 12 | else System.out.print("0 "); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/PromDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // A promotion surprise. 4 | public class PromDemo { 5 | public static void main(String[] args) { 6 | byte b; 7 | int i; 8 | 9 | b = 10; 10 | i = b * b; // OK, no cast needed 11 | 12 | b = 10; 13 | b = (byte) (b * b); // cast needed!! 14 | 15 | System.out.println("i and b: " + i + " " + b); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/BreakErr.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | public class BreakErr { 4 | public static void main(String[] args) { 5 | one: for (int i = 0; i < 3; i++) { 6 | System.out.println("Pass " + i + ": "); 7 | } 8 | 9 | for (int j = 0; j < 100; j++) { 10 | // if (j == 10) break one; // WRONG 11 | System.out.println(j + " "); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/Inches.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | /** 4 | * Compute the number of cubic inches 5 | * in one cubic mile. 6 | */ 7 | public class Inches { 8 | public static void main(String[] args) { 9 | long ci; 10 | long im; 11 | 12 | im = 5280 * 12; 13 | 14 | ci = im * im * im; 15 | 16 | System.out.println("There are " + ci + " cubic inches in cubic mile."); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/Sound.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | /** 4 | * Try This 2-1 5 | * Compute the distance to a lightning 6 | * strike whose sound takes 7.2 seconds 7 | * to reach you. 8 | */ 9 | public class Sound { 10 | public static void main(String[] args) { 11 | double dist; 12 | 13 | dist = 7.2 * 1100; 14 | 15 | System.out.println("The lightning is " + dist + " feet away"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/VarInitDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Demonstrate lifetime of a variable. 4 | public class VarInitDemo { 5 | public static void main(String[] args) { 6 | int x; 7 | 8 | for (x = 0; x < 3; x++) { 9 | int y = -1; 10 | System.out.println("y is: " + y); 11 | y = 100; 12 | System.out.println("y is now: " + y); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/DWDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Demonstrate the do-while loop. 4 | public class DWDemo { 5 | public static void main(String[] args) 6 | throws java.io.IOException { 7 | 8 | char ch; 9 | 10 | do { 11 | System.out.println("Press a key followed by ENTER: "); 12 | ch = (char) System.in.read(); // get a char 13 | } while (ch != 'q'); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/ForTest.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Loop until an S is typed. 4 | public class ForTest { 5 | public static void main(String[] args) 6 | throws java.io.IOException { 7 | 8 | int i; 9 | 10 | System.out.println("Press S to stop."); 11 | 12 | for (i = 0; (char) System.in.read() != 'S'; i++) { 13 | System.out.println("Pass #" + i); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/NoZeroDiv.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Prevent a division by zero using the ?. 4 | public class NoZeroDiv { 5 | public static void main(String[] args) { 6 | int result; 7 | 8 | for (int i = -5; i < 6; i++) { 9 | result = i != 0 ? 100 / i : 0; 10 | if (i != 0) 11 | System.out.println("100 / " + i + " is" + result); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/ErrInfo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class ErrInfo { 4 | public static void main(String[] args) { 5 | ErrorInfo err = new ErrorInfo(); 6 | Err e; 7 | 8 | e = err.getErrorInfo(2); 9 | System.out.println(e.msg + " severity: " + e.severity); 10 | 11 | e = err.getErrorInfo(19); 12 | System.out.println(e.msg + " severity: " + e.severity); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/StaticBlock.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Use a static block 4 | public class StaticBlock { 5 | static double rootOf2; 6 | static double rootOf3; 7 | 8 | static { 9 | System.out.println("Inside static block."); 10 | rootOf2 = Math.sqrt(2.0); 11 | rootOf3 = Math.sqrt(3.0); 12 | } 13 | 14 | StaticBlock(String msg) { 15 | System.out.println(msg); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/B.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Create a subclass by extending class A. 4 | public class B extends A { 5 | int i; // this i hides the i in A 6 | 7 | B(int a, int b) { 8 | super.i = a; // i in A 9 | i = b; // i in B 10 | } 11 | 12 | void show() { 13 | System.out.println("i in superclass: " + super.i); 14 | System.out.println("i in subclass: " + i); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Summation.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Initialize one object with another. 4 | public class Summation { 5 | int sum; 6 | 7 | // Construct from an int. 8 | Summation(int num) { 9 | sum = 0; 10 | for (int i = 1; i <= num; i++) 11 | sum += i; 12 | } 13 | 14 | // Construct from another object. 15 | Summation(Summation ob) { 16 | sum = ob.sum; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/MyClass.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // This class must implement all of A and B 4 | public class MyClass implements B { 5 | public void meth1() { 6 | System.out.println("Implement meth1()."); 7 | } 8 | 9 | public void meth2() { 10 | System.out.println("Implement meth2()."); 11 | } 12 | 13 | public void meth3() { 14 | System.out.println("Implement meth3()."); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/ArrayDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Demonstrate a one-dimensional array 4 | public class ArrayDemo { 5 | public static void main(String[] args) { 6 | int[] sample = new int[10]; 7 | int i; 8 | 9 | for (i = 0; i < 10; i++) 10 | sample[i] = i; 11 | 12 | for (i = 0; i < 10; i++) 13 | System.out.println("This is sample[" + i + "]: " + sample[i]); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/CLDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Display all command-line information. 4 | public class CLDemo { 5 | public static void main(String[] args) { 6 | System.out.println("There are " + args.length + " command-line arguments."); 7 | 8 | System.out.println("They are: "); 9 | for (int i = 0; i < args.length; i++) 10 | System.out.println("arg[" + i + "]: " + args[i]); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/ScopeDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Demonstrate block scope. 4 | public class ScopeDemo { 5 | public static void main(String[] args) { 6 | int x; 7 | 8 | x = 10; 9 | 10 | if (x == 10) { 11 | int y = 20; 12 | 13 | System.out.println("x and y: " + x + " " + y); 14 | x = y * 2; 15 | } 16 | 17 | System.out.println("x is " + x); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Empty2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Move more out of the for loop. 4 | public class Empty2 { 5 | public static void main(String[] args) { 6 | int i; 7 | 8 | i = 0; // move initialization out of the loop. 9 | for (; i < 10; ) { // the iteration expression is missing. 10 | System.out.println("Pass #" + i); 11 | i++; // increment loop control var 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/SelfTest9.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | /** 4 | * Write a program that uses a for loop to generate and display the progression 5 | * 1, 2, 4, 8, 16, 32, and so on. 6 | */ 7 | public class SelfTest9 { 8 | public static void main(String[] args) { 9 | System.out.println("Progression: "); 10 | for (int i = 0; i <= 10; ) { 11 | System.out.println((int)Math.pow(2, i++)); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/SelfTest3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | public class SelfTest3 { 4 | public static void main(String[] args) { 5 | double[] x = { 1.2, 1.3, 3.3, 3.5, 3.1, 7.55, 3.1416, 1.167, 6.33, 0.7 }; 6 | double avg = 0.0; 7 | 8 | for (double v : x) { 9 | avg += v; 10 | } 11 | avg /= x.length; 12 | 13 | System.out.println("Average of the x array is: " + avg); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/IConstD.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public class IConstD implements IConst { 4 | public static void main(String[] args) { 5 | int[] nums = new int[MAX]; 6 | 7 | for (int i = MIN; i < 11; i++) { 8 | if (i >= MAX) System.out.println(ERRORMSG); 9 | else { 10 | nums[i] = i; 11 | System.out.print(nums[i] + " "); 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/SelfTest8.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | /** 4 | * What does the following fragment print? 5 | * 6 | * 0 1 7 | * 2 3 8 | * 4 5 9 | * 6 7 10 | * 8 9 11 | */ 12 | public class SelfTest8 { 13 | public static void main(String[] args) { 14 | for (int i = 0; i < 10; i++) { 15 | System.out.print(i + " "); 16 | if ((i%2) == 0) continue; 17 | System.out.println(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ExcTest2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Using the Throwable methods. 4 | public class ExcTest2 { 5 | // Generate an exception. 6 | static void genException() { 7 | int[] nums = new int[4]; 8 | 9 | System.out.println("Before exception is generated."); 10 | 11 | // generate an index out-of-bounds exception 12 | nums[7] = 10; 13 | System.out.println("this won't be displayed"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/SDemo2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class SDemo2 { 4 | public static void main(String[] args) { 5 | System.out.println("val is " + StaticMeth.val); 6 | System.out.println("StaticMeth.valDiv2(): " + StaticMeth.valDiv2()); 7 | 8 | StaticMeth.val = 4; 9 | 10 | System.out.println("val is " + StaticMeth.val); 11 | System.out.println("StaticMeth.valDiv2(): " + StaticMeth.valDiv2()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Truck.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Truck extends Vehicle { 4 | private int cargocap; // cargo capacity in pounds 5 | 6 | // This is a constructor for Truck. 7 | Truck(int p, int f, int m, int c) { 8 | super(p, f, m); 9 | 10 | cargocap = c; 11 | } 12 | 13 | int getCargo() { 14 | return cargocap; 15 | } 16 | 17 | void putCargo(int c) { 18 | cargocap = c; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/Hypot.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | /** 4 | * Use the Pythagorean theorem to find 5 | * the length of the hypotenuse 6 | * given the lengths of the two opposing 7 | * sides. 8 | */ 9 | public class Hypot { 10 | public static void main(String[] args) { 11 | double x, y, z; 12 | x = 3; 13 | y = 4; 14 | 15 | z = Math.sqrt(x*x + y*y); 16 | 17 | System.out.println("Hypotenuse is " + z); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/IVehicle.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public interface IVehicle { 4 | // Return the range. 5 | int range(); 6 | 7 | // Compute fuel needed for a given distance. 8 | double fuelneeded(int miles); 9 | 10 | // Access methods for instance variables. 11 | int getPassengers(); 12 | void setPassengers(int p); 13 | int getFuelcap(); 14 | void setFuelcap(int f); 15 | int getMpg(); 16 | void setMpg(int m); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/ColorTriangle.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Extend Triangle. 4 | public class ColorTriangle extends Triangle6 { 5 | private String color; 6 | 7 | ColorTriangle(String c, String s, double w, double h) { 8 | super(s, w, h); 9 | 10 | color = c; 11 | } 12 | 13 | String getColor() { 14 | return color; 15 | } 16 | 17 | void showColor() { 18 | System.out.println("Color is " + color); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/KbIn.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | /** 4 | * Read a character from the keyboard. 5 | */ 6 | public class KbIn { 7 | public static void main(String[] args) 8 | throws java.io.IOException { 9 | 10 | char ch; 11 | 12 | System.out.print("Press a key followed by ENTER: "); 13 | 14 | ch = (char) System.in.read(); // Read a character from the keyboard 15 | 16 | System.out.println("Your key is: " + ch); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/MyClass.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Public vs private access. 4 | public class MyClass { 5 | private int alpha; 6 | public int beta; 7 | int gamma; 8 | 9 | /** 10 | * Methods to access alpha. It is OK for a member of a class 11 | * to access a private member of the same class. 12 | */ 13 | void setAlpha(int a) { 14 | alpha = a; 15 | } 16 | 17 | int getAlpha() { 18 | return alpha; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Overload2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | /** 4 | * Automatic type conversions can affect 5 | * overloaded method resolution. 6 | */ 7 | public class Overload2 { 8 | void f(byte x) { 9 | System.out.println("Inside f(byte): " + x); 10 | } 11 | 12 | void f(int x) { 13 | System.out.println("Inside f(int): " + x); 14 | } 15 | 16 | void f(double x) { 17 | System.out.println("Inside f(double): " + x); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/SelfTest13.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Demonstrate a varargs method called sum() that sums the int values passed to it. 4 | public class SelfTest13 { 5 | public static void main(String[] args) { 6 | SumIt siObj = new SumIt(); 7 | 8 | int total = siObj.sum(1, 2, 3); 9 | System.out.println("Sum is " + total); 10 | 11 | total = siObj.sum(1, 2, 3, 4, 5); 12 | System.out.println("Sum is " + total); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ExcDemo2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | public class ExcDemo2 { 4 | public static void main(String[] args) { 5 | int[] nums = new int[4]; 6 | 7 | try { 8 | ExcTest.genException(); 9 | } catch (ArrayIndexOutOfBoundsException exc) { 10 | // catch the exception. 11 | System.out.println("Index out-of-bounds!"); 12 | } 13 | System.out.println("After catch statement."); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/DynDispDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class DynDispDemo { 4 | public static void main(String[] args) { 5 | Sup superOb = new Sup(); 6 | Sub1 subOb1 = new Sub1(); 7 | Sub2 subOb2 = new Sub2(); 8 | 9 | Sup supRef; 10 | 11 | supRef = superOb; 12 | supRef.who(); 13 | 14 | supRef = subOb1; 15 | supRef.who(); 16 | 17 | supRef = subOb2; 18 | supRef.who(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/ByTwos.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public class ByTwos implements Series { 4 | int start; 5 | int val; 6 | 7 | ByTwos() { 8 | start = 0; 9 | val = 0; 10 | } 11 | 12 | public int getNext() { 13 | val += 2; 14 | return val; 15 | } 16 | 17 | public void reset() { 18 | val = start; 19 | } 20 | 21 | public void setStart(int x) { 22 | start = x; 23 | val = x; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/LowCase.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Lowercase letters. 4 | public class LowCase { 5 | public static void main(String[] args) { 6 | char ch; 7 | 8 | for (int i = 0; i < 10; i++) { 9 | ch = (char) ('A' + i); 10 | System.out.print(ch); 11 | 12 | // This statement turns on the 6th bit. 13 | ch = (char) ((int) ch | 32); 14 | 15 | System.out.print(ch + " "); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/StaticError.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class StaticError { 4 | int denom = 3; // a normal instance variable 5 | static int val = 1024; // a static variable 6 | 7 | /** 8 | * Error! Can't access a non-static variable 9 | * from within a static method. 10 | */ 11 | // static int valDivDenom() { 12 | // return val/denom; // won't compile (non-static variable denom cannot be referenced from a static context) 13 | // } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Triangle4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for triangles. 4 | public class Triangle4 extends TwoDShape4 { 5 | private String style; 6 | 7 | Triangle4(String s, double w, double h) { 8 | super(w, h); 9 | 10 | style = s; 11 | } 12 | 13 | double area() { 14 | return getWidth() * getHeight() / 2; 15 | } 16 | 17 | void showStyle() { 18 | System.out.println("Triangle is " + style); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/ByThrees.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public class ByThrees implements Series { 4 | int start; 5 | int val; 6 | 7 | ByThrees() { 8 | start = 0; 9 | val = 0; 10 | } 11 | 12 | public int getNext() { 13 | val += 3; 14 | return val; 15 | } 16 | 17 | public void reset() { 18 | val = start; 19 | } 20 | 21 | public void setStart(int x) { 22 | start = x; 23 | val = x; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/CharArithDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | /** 4 | * Character variables can be handled like integers 5 | */ 6 | public class CharArithDemo { 7 | public static void main(String[] args) { 8 | char ch; 9 | 10 | ch = 'X'; 11 | System.out.println("ch contains " + ch); 12 | 13 | ch++; 14 | 15 | System.out.println("ch is now " + ch); 16 | 17 | ch = 90; 18 | 19 | System.out.println("ch is now " + ch); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ExcTest.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // An exception can be generated by one method and caught by another. 4 | public class ExcTest { 5 | // Generate an exception. 6 | static void genException() { 7 | int[] nums = new int[4]; 8 | 9 | System.out.println("Before exception is generated."); 10 | 11 | // generate an index out-of-bounds exception 12 | nums[7] = 10; 13 | System.out.println("this won't be displayed"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Break2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Read input until a q us received. 4 | public class Break2 { 5 | public static void main(String[] args) 6 | throws java.io.IOException { 7 | 8 | char ch; 9 | 10 | for ( ; ; ) { 11 | ch = (char) System.in.read(); // get a char 12 | System.out.println("How often do you read?!"); 13 | 14 | if (ch == 'q') break; 15 | } 16 | System.out.println("You pressed q!"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/BreakDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Using break to exit a loop. 4 | public class BreakDemo { 5 | public static void main(String[] args) { 6 | int num; 7 | 8 | num = 100; 9 | 10 | // loop while i-squared is less than num 11 | for (int i = 0; i < num; i++) { 12 | if (i*i >= num) break; // terminate loop if i*i >= 100 13 | System.out.print(i + " "); 14 | } 15 | System.out.println("Loop complete."); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/TwoD.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Demonstrate a two-dimensional array 4 | public class TwoD { 5 | public static void main(String[] args) { 6 | int t, i; 7 | int[][] table = new int[3][4]; 8 | 9 | for (t = 0; t < 3; t++) { 10 | for (i = 0; i < 4; i++) { 11 | table[t][i] = (t*4) + i + 1; 12 | System.out.println(table[t][i] + " "); 13 | } 14 | System.out.println(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/ErrorMsg.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Return a String object. 4 | public class ErrorMsg { 5 | String[] msgs = { 6 | "Output Error", 7 | "InputError", 8 | "Disk Full", 9 | "Index Out-Of-Bounds" 10 | }; 11 | 12 | // Return the error message. 13 | String getErrorMsg(int i) { 14 | if (i >= 0 && i < msgs.length) 15 | return msgs[i]; 16 | else 17 | return "Invalid Error Code"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/SelfTest6.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Write a recursive method that displays a string backwards. 4 | public class SelfTest6 { 5 | public static void main(String[] args) { 6 | String s1 = "Hello Java World!"; 7 | 8 | readStrBackward(s1, 0); 9 | } 10 | 11 | public static void readStrBackward(String s, int pos) { 12 | if (pos != s.length() - 1) readStrBackward(s, pos + 1); 13 | 14 | System.out.println("read: " + s.charAt(pos)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/ContToLabel.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Use continue with a label. 4 | public class ContToLabel { 5 | public static void main(String[] args) { 6 | outerloop: 7 | for (int i = 1; i < 10; i++) 8 | { 9 | System.out.println("\nOuter loop pass " + i + ", Inner loop: "); 10 | for (int j = 1; j < 10; j++) { 11 | if (j == 5) continue outerloop; 12 | System.out.print(j); 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/SelfTest7.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | /** 4 | * In the following fragment, after the break statement executes, what is displayed? 5 | */ 6 | public class SelfTest7 { 7 | public static void main(String[] args) { 8 | int x = 5; 9 | for (int i = 0; i < 10; i++) { 10 | while (true) { 11 | if (i < x) break; 12 | } 13 | System.out.println("after while"); 14 | } 15 | System.out.println("after for"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/StringDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Introduce String. 4 | public class StringDemo { 5 | public static void main(String[] args) { 6 | // declare strings in various ways 7 | String str1 = new String("Java strings are objects."); 8 | String str2 = "They are constructed various ways."; 9 | String str3 = new String(str2); 10 | 11 | System.out.println(str1); 12 | System.out.println(str2); 13 | System.out.println(str3); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/UpCase.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Uppercase letters. 4 | public class UpCase { 5 | public static void main(String[] args) { 6 | char ch; 7 | 8 | for (int i = 0; i < 10; i++) { 9 | ch = (char) ('a' + i); 10 | System.out.print(ch); 11 | 12 | // This statement turns off the 6th bit. 13 | ch = (char) ((int) (ch) & 65503); // ch is now uppercase 14 | 15 | System.out.print(ch + " "); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ThrowDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Manually throw an exception 4 | public class ThrowDemo { 5 | public static void main(String[] args) { 6 | try { 7 | System.out.println("Before throw."); 8 | throw new ArithmeticException(); 9 | } catch (ArithmeticException exc) { 10 | // catch the exception 11 | System.out.println("Exception caught."); 12 | } 13 | System.out.println("After try/catch block."); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/SelfTest10.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Find all of the prime numbers between 2 and 100. 4 | public class SelfTest10 { 5 | public static void main(String[] args) { 6 | mainLoop: for (int i = 2, j = 100; i <= j; i++) { 7 | int d = i; 8 | 9 | while (--d > 1) { 10 | if (i % d == 0) { 11 | continue mainLoop; 12 | } 13 | } 14 | 15 | System.out.print(i + " "); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/SoundExt.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | /** 4 | * Try This 2-1 5 | * Compute the distance to a large object 6 | * such a rock or wall, by timing the echo. 7 | */ 8 | public class SoundExt { 9 | public static void main(String[] args) { 10 | double echoTime = 5; 11 | short speedOfSound = 1100; // feet per second through air 12 | 13 | double dist = (echoTime * speedOfSound) / 2; 14 | 15 | System.out.println("The big object is " + dist + " feet away"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Triangle3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for triangles. 4 | public class Triangle3 extends TwoDShape2 { 5 | private String style; 6 | 7 | Triangle3(String s, double w, double h) { 8 | setWidth(w); 9 | setHeight(h); 10 | 11 | style = s; 12 | } 13 | 14 | double area() { 15 | return getWidth() * getHeight() / 2; 16 | } 17 | 18 | void showStyle() { 19 | System.out.println("Triangle is " + style); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/MyIF2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public interface MyIF2 { 4 | // this is a "normal" interface method declaration. 5 | // It does NOT define a default implementation. 6 | int getUserID(); 7 | 8 | // This is a default method. Notice that it provides 9 | // a default implementation. 10 | default int getAdminID() { 11 | return 1; 12 | } 13 | 14 | // This is a static interface method. 15 | static int getUniversalID() { 16 | return 0; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Power.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Compute integer powers of 2. 4 | public class Power { 5 | public static void main(String[] args) { 6 | int e; 7 | int result; 8 | 9 | for (int i = 0; i < 10; i++) { 10 | result = 1; 11 | e = i; 12 | while (e > 0) { 13 | result *= 2; 14 | e--; 15 | } 16 | 17 | System.out.println("2 to the " + i + " power is " + result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/MinMax2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | public class MinMax2 { 4 | public static void main(String[] args) { 5 | int[] nums = {99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49}; 6 | 7 | int min, max; 8 | 9 | min = max = nums[0]; 10 | 11 | for (int i = 1; i < 10; i++) { 12 | if (nums[i] < min) min = nums[i]; 13 | if (nums[i] > max) max = nums[i]; 14 | } 15 | 16 | System.out.println("min and max: " + min + " " + max); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c10/ReadBytes.java: -------------------------------------------------------------------------------- 1 | package com.guide.c10; 2 | 3 | import java.io.*; 4 | 5 | // Read an array of bytes from the keyboard. 6 | public class ReadBytes { 7 | public static void main(String[] args) throws IOException { 8 | byte[] data = new byte[10]; 9 | 10 | System.out.println("Enter some characters."); 11 | System.in.read(data); 12 | System.out.println("You entered: "); 13 | for (int i = 0; i < data.length; i++) 14 | System.out.print((char) data[i]); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/FindFact.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | /** 4 | * Use nested loops to find factors of numbers 5 | * between 2 and 100. 6 | */ 7 | public class FindFact { 8 | public static void main(String[] args) { 9 | 10 | for (int i = 2; i <= 100; i++) { 11 | System.out.print("Factors of " + i + ": "); 12 | for (int j = 2; j < i; j++) { 13 | if ((i%j) == 0) System.out.print(j + " "); 14 | } 15 | System.out.println(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/DemoPwr.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class DemoPwr { 4 | public static void main(String[] args) { 5 | Pwr x = new Pwr(4.0, 2); 6 | Pwr y = new Pwr(2.5, 1); 7 | Pwr z = new Pwr(5.7, 0); 8 | 9 | System.out.println(x.b + " raised to the " + x.e + " power is " + x.get_pwr()); 10 | System.out.println(y.b + " raised to the " + y.e + " power is " + y.get_pwr()); 11 | System.out.println(z.b + " raised to the " + z.e + " power is " + z.get_pwr()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/PassOb.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class PassOb { 4 | public static void main(String[] args) { 5 | Block ob1 = new Block(10, 2, 5); 6 | Block ob2 = new Block(10, 2, 5); 7 | Block ob3 = new Block(4, 5, 5); 8 | 9 | System.out.println("ob1 same dimensions as ob2: " + ob1.sameBlock(ob2)); 10 | System.out.println("ob1 same dimensions as ob3: " + ob1.sameBlock(ob3)); 11 | System.out.println("ob1 same volume as ob3: " + ob1.sameVolume(ob2)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Guess.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Guess the letter game. 4 | public class Guess { 5 | public static void main(String[] args) 6 | throws java.io.IOException { 7 | 8 | char ch, answer = 'K'; 9 | 10 | System.out.println("I'm thinking of a letter between A and Z."); 11 | System.out.print("Can you guess it: "); 12 | 13 | ch = (char) System.in.read(); // read a char from tge keyboard 14 | 15 | if (ch == answer) System.out.println("*** Right ***"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/AccessDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class AccessDemo { 4 | public static void main(String[] args) { 5 | MyClass ob = new MyClass(); 6 | 7 | // Access to alpha is allowed only through its accessor methods. 8 | ob.setAlpha(-99); 9 | System.out.println("ob.alpha is " + ob.getAlpha()); 10 | 11 | // ob.alpha = 10; // compilation error 12 | 13 | // There are OK because beta and gamma are public 14 | ob.beta = 88; 15 | ob.gamma = 99; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/ErrorInfo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Return a programmer-defined object. 4 | public class ErrorInfo { 5 | String[] msgs = { 6 | "Output Error", 7 | "InputError", 8 | "Disk Full", 9 | "Index Out-Of-Bounds" 10 | }; 11 | int[] howbad = { 3, 3, 2, 4 }; 12 | 13 | Err getErrorInfo(int i) { 14 | if (i >= 0 && i < msgs.length) 15 | return new Err(msgs[i], howbad[i]); 16 | else 17 | return new Err("Invalid Error Code", 0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/OverloadConsDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class OverloadConsDemo { 4 | public static void main(String[] args) { 5 | MyClass2 t1 = new MyClass2(); 6 | MyClass2 t2 = new MyClass2(88); 7 | MyClass2 t3 = new MyClass2(17.23); 8 | MyClass2 t4 = new MyClass2(2, 4); 9 | 10 | System.out.println("t1.x: " + t1.x); 11 | System.out.println("t2.x: " + t2.x); 12 | System.out.println("t3.x: " + t3.x); 13 | System.out.println("t4.x: " + t4.x); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/SeriesDemo2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public class SeriesDemo2 { 4 | public static void main(String[] args) { 5 | ByTwos2 twoOb = new ByTwos2(); 6 | ByThrees threeOb = new ByThrees(); 7 | Series ob; 8 | 9 | for (int i = 0; i < 5; i++) { 10 | ob = twoOb; 11 | System.out.println("Next ByTwos2 value is " + ob.getNext()); 12 | 13 | ob = threeOb; 14 | System.out.println("Next ByThrees value is " + ob.getNext()); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Factorial.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // A simple example of recursion. 4 | public class Factorial { 5 | // This is a recursive function. 6 | int factR(int n) { 7 | int result; 8 | if (n == 1) return 1; 9 | result = factR(n-1) * n; 10 | return result; 11 | } 12 | 13 | // This is an iterative equivalent. 14 | int factI(int n) { 15 | int t, result; 16 | 17 | result = 1; 18 | for (t = 1; t <= n; t++) result *= t; 19 | return result; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/Series2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // An enhanced version of Series that includes a default method called getNextArray(). 4 | public interface Series2 { 5 | int getNext(); // return next number in series 6 | 7 | default int[] getNextArray(int n) { 8 | int[] vals = new int[n]; 9 | 10 | for (int i = 0; i < n; i++) { 11 | vals[i] = getNext(); 12 | } 13 | 14 | return vals; 15 | }; 16 | 17 | void reset(); // restart 18 | void setStart(int x); // set starting value 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/bookpack/Book.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.bookpack; 2 | 3 | // A short package demonstration. 4 | public class Book { 5 | private String title; 6 | private String author; 7 | private int pubDate; 8 | 9 | Book(String t, String a, int d) { 10 | title = t; 11 | author = a; 12 | pubDate = d; 13 | } 14 | 15 | void show() { 16 | System.out.println(title); 17 | System.out.println(author); 18 | System.out.println(pubDate); 19 | System.out.println(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/ForEach.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Use a for-each style for loop. 4 | public class ForEach { 5 | public static void main(String[] args) { 6 | int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 7 | int sum = 0; 8 | 9 | // Use for-each style for to display and sum the values. 10 | for (int x : nums) { // a for-each style for loop 11 | System.out.println("Value is: " + x); 12 | sum += x; 13 | } 14 | 15 | System.out.println("Summation: " + sum); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/bookpack/Book2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.bookpack; 2 | 3 | // A short package demonstration. 4 | public class Book2 { 5 | private String title; 6 | private String author; 7 | private int pubDate; 8 | 9 | public Book2(String t, String a, int d) { 10 | title = t; 11 | author = a; 12 | pubDate = d; 13 | } 14 | 15 | public void show() { 16 | System.out.println(title); 17 | System.out.println(author); 18 | System.out.println(pubDate); 19 | System.out.println(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/bookpack/Book3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.bookpack; 2 | 3 | // A short package demonstration. 4 | public class Book3 { 5 | protected String title; 6 | protected String author; 7 | protected int pubDate; 8 | 9 | public Book3(String t, String a, int d) { 10 | title = t; 11 | author = a; 12 | pubDate = d; 13 | } 14 | 15 | public void show() { 16 | System.out.println(title); 17 | System.out.println(author); 18 | System.out.println(pubDate); 19 | System.out.println(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c1/Example3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c1; 2 | 3 | class Example3 { 4 | public static void main(String[] args) { 5 | int var; 6 | double x; 7 | 8 | var = 10; 9 | x = 10.0; 10 | 11 | System.out.println("Original value of var: " + var); 12 | System.out.println("Original value of x: " + x); 13 | System.out.println(); 14 | 15 | var = var / 4; 16 | x = x / 4; 17 | 18 | System.out.println("var after division: " + var); 19 | System.out.println("x after division: " + x); 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/DefaultMethodDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // Use the default method. 4 | public class DefaultMethodDemo { 5 | public static void main(String[] args) { 6 | MyIFImp obj = new MyIFImp(); 7 | 8 | // Can call getUserID(), because it is explicitly 9 | // implemented by MyIFImp: 10 | System.out.println("User ID is " + obj.getUserID()); 11 | 12 | // Can also call get AdminID(), because of default 13 | // implementation 14 | System.out.println("Administrator ID is " + obj.getAdminID()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/ModDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | /** 4 | * Demonstrate the % operator. 5 | */ 6 | public class ModDemo { 7 | public static void main(String[] args) { 8 | int iresult, irem; 9 | double dresult, drem; 10 | 11 | iresult = 10 / 3; 12 | irem = 10 % 3; 13 | 14 | dresult = 10.0 / 3.0; 15 | drem = 10.0 % 3.0; 16 | 17 | System.out.println("Result and remainder of 10 / 3: " + iresult + " " + irem); 18 | System.out.println("Result and remainder of 10.0 / 3.0: " + dresult + " " + drem); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/NoChange.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // The for-each loop is essentially read-only. 4 | public class NoChange { 5 | public static void main(String[] args) { 6 | int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 7 | int sum = 0; 8 | 9 | for (int x : nums) { 10 | System.out.print(x + " "); 11 | x = x * 10; // no effect on nums 12 | } 13 | 14 | System.out.println(); 15 | 16 | for (int x : nums) 17 | System.out.print(x + " "); 18 | 19 | System.out.println(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/BoolDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | /** 4 | * Demonstrate boolean values. 5 | */ 6 | public class BoolDemo { 7 | public static void main(String[] args) { 8 | boolean b; 9 | 10 | b = false; 11 | System.out.println("b is " + b); 12 | b = true; 13 | System.out.println("b is " + b); 14 | 15 | if (b) System.out.println("This is executed"); 16 | 17 | b = false; 18 | 19 | if (b) System.out.println("This is not executed"); 20 | 21 | System.out.println("10 > 9 is " + (10 > 9)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/SelfTest13.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Rewrite the MinMax class shown earlier so that it uses a for-each style for loop. 4 | public class SelfTest13 { 5 | public static void main(String[] args) { 6 | int[] nums = {99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49}; 7 | 8 | int min, max; 9 | 10 | min = max = nums[0]; 11 | 12 | for (int x : nums) { 13 | if (x < min) min = x; 14 | if (x > max) max = x; 15 | } 16 | 17 | System.out.println("min and max: " + min + " " + max); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/SupSubRef.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | class SupSubRef { 4 | public static void main(String[] args) { 5 | X2 x = new X2(10); 6 | X2 x2; 7 | Y2 y = new Y2(5, 6); 8 | 9 | x2 = x; // OK, both of same type 10 | System.out.println("x2.a: " + x2.a); 11 | 12 | x2 = y; // still Ok because Y is derived from X 13 | System.out.println("x2.a: " + x2.a); 14 | 15 | // X references know only about X members 16 | x2.a = 19; // OK 17 | // x2.b = 27; // Error, X doesn't have a b member 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/VarArgs4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // This program contains an error and will not compile! 4 | // Compilation error: reference to vaTest is ambiguous 5 | public class VarArgs4 { 6 | // Use an int vararg parameter. 7 | static void vaTest(int ... v) { 8 | 9 | } 10 | 11 | // Use a boolean vararg parameter. 12 | static void vaTest(boolean ... v) { 13 | 14 | } 15 | 16 | public static void main(String[] args) { 17 | vaTest(1, 2, 3); // OK 18 | vaTest(true, false, false); // OK 19 | 20 | // vaTest(); // Ambiguous! 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/UseThrowableMethods.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | public class UseThrowableMethods { 4 | public static void main(String[] args) { 5 | try { 6 | ExcTest2.genException(); 7 | } catch (ArrayIndexOutOfBoundsException exc) { 8 | // catch the exception 9 | System.out.println("Standard message is: "); 10 | System.out.println(exc); 11 | System.out.println("\nStack trace: "); 12 | 13 | exc.printStackTrace(); 14 | } 15 | System.out.println("After catch statement."); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Guess2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Guess the letter game. 4 | public class Guess2 { 5 | public static void main(String[] args) 6 | throws java.io.IOException { 7 | 8 | char ch, answer = 'K'; 9 | 10 | System.out.println("I'm thinking of a letter between A and Z."); 11 | System.out.print("Can you guess it: "); 12 | 13 | ch = (char) System.in.read(); // read a char from tge keyboard 14 | 15 | if (ch == answer) System.out.println("*** Right ***"); 16 | else System.out.println("...Sorry, you're wrong."); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/TypeConv.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class TypeConv { 4 | public static void main(String[] args) { 5 | Overload2 ob = new Overload2(); 6 | 7 | int i = 10; 8 | double d = 10.1; 9 | 10 | byte b = 99; 11 | short s = 10; 12 | float f = 11.5F; 13 | 14 | ob.f(i); // calls ob.f(int) 15 | ob.f(d); // calls ob.f(double) 16 | 17 | ob.f(b); // calls ob.f(int) - type conversion 18 | ob.f(s); // calls ob.f(int) - type conversion 19 | ob.f(f); // calls ob.f(double) - type conversion 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Block.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Objects can be passed to methods. 4 | public class Block { 5 | int a, b, c; 6 | int volume; 7 | 8 | Block(int i, int j, int k) { 9 | a = i; 10 | b = j; 11 | c = k; 12 | volume = a * b * c; 13 | } 14 | 15 | // Return true if ob defines same block. 16 | boolean sameBlock(Block ob) { 17 | return ob.a == a && ob.b == b && ob.c == c; 18 | } 19 | 20 | // Return true if ob has same volume. 21 | boolean sameVolume(Block ob) { 22 | return ob.volume == volume; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c1/GalToLitTable.java: -------------------------------------------------------------------------------- 1 | package com.guide.c1; 2 | 3 | class GalToLitTable { 4 | public static void main(String[] args) { 5 | double gallons, liters; 6 | int counter; 7 | 8 | counter = 0; 9 | 10 | for (gallons = 1; gallons <= 100; gallons++) { 11 | liters = gallons * 3.7854; 12 | 13 | System.out.println(gallons + " gallons is " + liters + " liters."); 14 | 15 | counter++; 16 | 17 | if (counter == 10) { 18 | System.out.println(); 19 | counter = 0; 20 | } 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/ForVar.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Declare loop control variable inside the for. 4 | public class ForVar { 5 | public static void main(String[] args) { 6 | int sum = 0; 7 | int fact = 1; 8 | 9 | // compute the factorial of the numbers through 5 10 | for (int i = 1; i <= 5; i++) { 11 | sum += i; // i is known throughout the loop 12 | fact *= i; 13 | } 14 | 15 | // but, i is not known here 16 | 17 | System.out.println("Sum is " + sum); 18 | System.out.println("Factorial is " + fact); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/Search.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Search an array using for-each style for. 4 | public class Search { 5 | public static void main(String[] args) { 6 | int[] nums = { 6, 8, 3, 7, 5, 6, 1, 4 }; 7 | int val = 5; 8 | boolean found = false; 9 | 10 | // Use for-each style for to search nums for val. 11 | for (int x : nums) { 12 | if (x == val) { 13 | found = true; 14 | break; 15 | } 16 | } 17 | 18 | if (found) 19 | System.out.println("Value found!"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ExcDemo3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | public class ExcDemo3 { 4 | public static void main(String[] args) { 5 | int[] numer = { 4, 8, 16, 32, 64, 128 }; 6 | int[] denom = { 2, 0, 4, 4, 0, 8 }; 7 | 8 | for (int i = 0; i < numer.length; i++) { 9 | try { 10 | System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); 11 | } catch (ArithmeticException exc) { 12 | // catch the exception. 13 | System.out.println("Can't divide by Zero!"); 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/SqrRoot.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Show square roots of 1 to 99 and the rounding error. 4 | public class SqrRoot { 5 | public static void main(String[] args) { 6 | double num, sroot, rerr; 7 | 8 | for (num = 1.0; num < 100.0; num++) { 9 | sroot = Math.sqrt(num); 10 | System.out.println("Square root of " + num + " is " + sroot); 11 | 12 | // compute rounding error 13 | rerr = num - (sroot * sroot); 14 | System.out.println("Rounding errors is " + rerr); 15 | System.out.println(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/LtoD2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | /** 4 | * Demonstrate automatic conversion from long to double. 5 | * 6 | * Compilation failure 7 | * [ERROR] /var/www/java-beginners-guide/src/main/java/com/guide/c2/LtoD2.java:[10,13] incompatible types: possible lossy conversion from double to long 8 | */ 9 | public class LtoD2 { 10 | public static void main(String[] args) { 11 | long L; 12 | double D; 13 | 14 | D = 100123285.0; 15 | // L = D; // Illegal, no automatic conversion from double to long. 16 | 17 | // System.out.println("L and D: " + L + " " + D); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/VarArgs2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Use varargs with standard arguments. 4 | public class VarArgs2 { 5 | static void vaTest(String msg, int ... v) { 6 | System.out.println(msg + v.length); 7 | System.out.println("Contents: "); 8 | 9 | for (int i = 0; i < v.length; i++) 10 | System.out.println(" arg " + i + ": " + v[i]); 11 | 12 | System.out.println(); 13 | } 14 | 15 | public static void main(String[] args) { 16 | vaTest("One vararg: ", 10); 17 | vaTest("Three varargs: ", 1, 2, 3); 18 | vaTest("No varargs: "); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Shapes3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Shapes3 { 4 | public static void main(String[] args) { 5 | Triangle3 t1 = new Triangle3("filled", 4.0, 4.0); 6 | Triangle3 t2 = new Triangle3("outlined", 8.0, 12.0); 7 | 8 | System.out.println("Info for t1: "); 9 | t1.showStyle(); 10 | t1.showDim(); 11 | System.out.println("Area is " + t1.area()); 12 | 13 | System.out.println(); 14 | 15 | System.out.println("Info for t2: "); 16 | t2.showStyle(); 17 | t2.showDim(); 18 | System.out.println("Area is " + t2.area()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Shapes4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Shapes4 { 4 | public static void main(String[] args) { 5 | Triangle4 t1 = new Triangle4("filled", 4.0, 4.0); 6 | Triangle4 t2 = new Triangle4("outlined", 8.0, 12.0); 7 | 8 | System.out.println("Info for t1: "); 9 | t1.showStyle(); 10 | t1.showDim(); 11 | System.out.println("Area is " + t1.area()); 12 | 13 | System.out.println(); 14 | 15 | System.out.println("Info for t2: "); 16 | t2.showStyle(); 17 | t2.showDim(); 18 | System.out.println("Area is " + t2.area()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c1/SelfTest10.java: -------------------------------------------------------------------------------- 1 | package com.guide.c1; 2 | 3 | /** 4 | * Prints a conversion table of inches to meters. 5 | */ 6 | class SelfTest10 { 7 | public static void main(String[] args) { 8 | double meters; 9 | int counter = 0; 10 | 11 | for (int inches = 1; inches <= 144; inches++) { 12 | meters = inches / 39.37; 13 | 14 | System.out.println(inches + " inches is " + meters + " meters."); 15 | 16 | counter++; 17 | 18 | if (counter == 12) { 19 | System.out.println(); 20 | counter = 0; 21 | } 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/NestVar.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | /** 4 | * This program attempts to declare a variable 5 | * in an inner scope with the same name as one 6 | * defined in an outer scope. 7 | */ 8 | public class NestVar { 9 | public static void main(String[] args) { 10 | int count; 11 | 12 | for (count = 0; count < 10; count += 1) { 13 | System.out.println("This is count: " + count); 14 | 15 | //int count; // illegal 16 | for (count = 0; count < 2; count++) { 17 | System.out.println("This program is in error!"); 18 | } 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/Vehicle.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class Vehicle { 4 | int passengers; // number of passengers 5 | int fuelcap; // fuel capacity in gallons 6 | int mpg; // fuel consumption in miles per gallon 7 | 8 | Vehicle() {} 9 | 10 | Vehicle(int p, int f, int m) { 11 | passengers = p; 12 | fuelcap = f; 13 | mpg = m; 14 | } 15 | 16 | int range() { 17 | return fuelcap * mpg; 18 | } 19 | 20 | /** 21 | * Compute fuel needed for a given distance. 22 | */ 23 | double fuelneeded(int miles) { 24 | return (double) miles / mpg; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/MyClass2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Demonstrate an overloaded constructor. 4 | public class MyClass2 { 5 | int x; 6 | 7 | MyClass2() { 8 | System.out.println("Inside MyClass2()."); 9 | x = 0; 10 | } 11 | 12 | MyClass2(int i) { 13 | System.out.println("Inside MyClass2(int)."); 14 | x = i; 15 | } 16 | 17 | MyClass2(double d) { 18 | System.out.println("Inside MyClass2(double)."); 19 | x = (int) d; 20 | } 21 | 22 | MyClass2(int i, int j) { 23 | System.out.println("Inside MyClass2(int, int)."); 24 | x = i * j; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/VehicleDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | // This class declares an object of type Vehicle. 4 | public class VehicleDemo { 5 | public static void main(String[] args) { 6 | Vehicle minivan = new Vehicle(); 7 | int range; 8 | 9 | // assign values to fields in minivan 10 | minivan.passengers = 7; 11 | minivan.fuelcap = 16; 12 | minivan.mpg = 21; 13 | 14 | // compute the range assuming a full tank of gas 15 | range = minivan.fuelcap * minivan.mpg; 16 | System.out.println("Minivan can carry " + minivan.passengers + " with a range of " + range); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/NotDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Demonstrate the bitwise NOT. 4 | public class NotDemo { 5 | public static void main(String[] args) { 6 | byte b = -34; 7 | 8 | for (int t = 128; t > 0; t = t/2) { 9 | if ((b & t) != 0) System.out.print("1 "); 10 | else System.out.print("0 "); 11 | } 12 | System.out.println(); 13 | 14 | // reverse all bits 15 | b = (byte) ~b; 16 | 17 | for (int t = 128; t > 0; t = t/2) { 18 | if ((b & t) != 0) System.out.print("1 "); 19 | else System.out.print("0 "); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/ErrorMsg.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Return a String object 4 | public class ErrorMsg { 5 | // Error codes. 6 | final int OUTERR = 0; 7 | final int INERR = 1; 8 | final int DISKERR = 2; 9 | final int INDEXERR = 3; 10 | 11 | String[] msgs = { 12 | "Output Error", 13 | "Input Error", 14 | "Disk Full", 15 | "Index Out-Of-Bounds" 16 | }; 17 | 18 | // Return the error message. 19 | String getErrorMsg(int i) { 20 | if (i >= 0 && i < msgs.length) 21 | return msgs[i]; 22 | else 23 | return "Invalid Error Code"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/VehConsDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class VehConsDemo { 4 | public static void main(String[] args) { 5 | Vehicle minivan = new Vehicle(7, 16, 21); 6 | Vehicle sportscar = new Vehicle(2, 14, 12); 7 | double gallons; 8 | int dist = 252; 9 | 10 | gallons = minivan.fuelneeded(dist); 11 | 12 | System.out.println("To go " + dist + " miles minivan needs " + gallons + " gallons of fuel."); 13 | 14 | gallons = sportscar.fuelneeded(dist); 15 | 16 | System.out.println("To go " + dist + " miles sportscar needs " + gallons + " gallons of fuel."); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/ACopy.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Use length variable to help copy an array. 4 | public class ACopy { 5 | public static void main(String[] args) { 6 | int i; 7 | int[] nums1 = new int[10]; 8 | int[] nums2 = new int[10]; 9 | 10 | for (i = 0; i < nums1.length; i++) 11 | nums1[i] = i; 12 | 13 | // copy nums1 to nums2 14 | if (nums2.length >= nums1.length) 15 | for (i = 0; i < nums1.length; i++) 16 | nums2[i] = nums1[i]; 17 | 18 | for (i = 0; i < nums2.length; i++) 19 | System.out.print(nums2[i] + " "); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/SelfTest2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Create a subclass of TwoDShape to represent a Circle. 4 | public class SelfTest2 extends TwoDShape9 { 5 | // A default constructor. 6 | SelfTest2() { 7 | super(); 8 | } 9 | 10 | // Construct Circle 11 | SelfTest2(double x) { 12 | super(x, "circle"); // call superclass constructor 13 | } 14 | 15 | // Construct an object from an object. 16 | SelfTest2(SelfTest2 ob) { 17 | super(ob); // pass object to TwoDShape constructor 18 | } 19 | 20 | double area() { 21 | return 3.14156 * Math.pow(getWidth() / 2, 2); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/StringArrays.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Demonstrate String arrays. 4 | public class StringArrays { 5 | public static void main(String[] args) { 6 | String[] strs = { "This", "is", "a", "test." }; 7 | 8 | System.out.println("Original array: "); 9 | for (String s : strs) 10 | System.out.print(s + " "); 11 | System.out.println("\n"); 12 | 13 | // change a string 14 | strs[1] = "was"; 15 | strs[3] = "test, too!"; 16 | 17 | System.out.println("Modified array: "); 18 | for (String s : strs) 19 | System.out.print(s + " "); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/OverloadDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class OverloadDemo { 4 | public static void main(String[] args) { 5 | Overload ob = new Overload(); 6 | int resI; 7 | double resD; 8 | 9 | // call all versions of ovlDemo() 10 | ob.ovlDemo(); 11 | System.out.println(); 12 | 13 | ob.ovlDemo(2); 14 | System.out.println(); 15 | 16 | resI = ob.ovlDemo(4, 6); 17 | System.out.println("Result of ob.ovlDemo(4, 6): " + resI); 18 | 19 | resD = ob.ovlDemo(1.1, 2.32); 20 | System.out.println("Result of ob.ovlDemo(1.1, 2.32): " + resD); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/AbsShape.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class AbsShape { 4 | public static void main(String[] args) { 5 | TwoDShape9[] shapes = new TwoDShape9[4]; 6 | 7 | shapes[0] = new Triangle9("outlined", 8.0, 12.0); 8 | shapes[1] = new Rectangle9(10); 9 | shapes[2] = new Rectangle9(10, 4); 10 | shapes[3] = new Triangle9(7.0); 11 | 12 | for (int i = 0; i < shapes.length; i++) { 13 | System.out.println("object is " + shapes[i].getName()); 14 | System.out.println("Area is " + shapes[i].area()); 15 | 16 | System.out.println(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ThrowsDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Use throws. 4 | public class ThrowsDemo { 5 | public static char prompt(String str) throws java.io.IOException { 6 | 7 | System.out.println(str + ": "); 8 | return (char) System.in.read(); 9 | } 10 | 11 | public static void main(String[] args) { 12 | char ch; 13 | 14 | try { 15 | ch = prompt("Enter a letter"); 16 | } catch (java.io.IOException exc) { 17 | System.out.println("I/O exception occurred."); 18 | ch = 'X'; 19 | } 20 | 21 | System.out.println("You pressed " + ch); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Shapes7.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Shapes7 { 4 | public static void main(String[] args) { 5 | Triangle7 t1 = new Triangle7("outlined", 8.0, 12.0); 6 | 7 | // make a copy of t1 8 | Triangle7 t2 = new Triangle7(t1); 9 | 10 | System.out.println("Info for t1: "); 11 | t1.showStyle(); 12 | t1.showDim(); 13 | System.out.println("Area is " + t1.area()); 14 | 15 | System.out.println(); 16 | 17 | System.out.println("Info for t2: "); 18 | t2.showStyle(); 19 | t2.showDim(); 20 | System.out.println("Area is " + t2.area()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/QSDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class QSDemo { 4 | public static void main(String[] args) { 5 | char[] a = { 'd', 'x' , 'a', 'r', 'p', 'j', 'i' }; 6 | int i; 7 | 8 | System.out.print("Original array: "); 9 | for (i = 0; i < a.length; i++) 10 | System.out.print(a[i]); 11 | 12 | System.out.println(); 13 | 14 | // now, sort the array 15 | Quicksort.qsort(a); 16 | 17 | System.out.print("Sorted array: "); 18 | for (i=0; i < a.length; i++) { 19 | System.out.print(a[i]); 20 | } 21 | 22 | System.out.println(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/bookpack/BookDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.bookpack; 2 | 3 | public class BookDemo { 4 | public static void main(String[] args) { 5 | Book[] books = new Book[5]; 6 | 7 | books[0] = new Book("Java: A Beginner's Guide", "Schildt", 2018); 8 | books[1] = new Book("Java: The Complete Reference", "Schildt", 2018); 9 | books[2] = new Book("Introducing JavaFX 8 Programming", "Schildt", 2015); 10 | books[3] = new Book("Red Storm Rising", "Clancy", 1986); 11 | books[4] = new Book("On the Road", "Kerouac", 1955); 12 | 13 | for (Book x: books) { 14 | x.show(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Break3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Using break with nested loops. 4 | public class Break3 { 5 | public static void main(String[] args) { 6 | 7 | for (int i = 0; i < 3; i++) { 8 | System.out.println("Outer loop count: " + i); 9 | System.out.print(" Inner loop count: "); 10 | 11 | int t = 0; 12 | while (t < 100) { 13 | if (t == 10) break; // terminate loop if t is 10 14 | System.out.print(t + " "); 15 | t++; 16 | } 17 | System.out.println(); 18 | } 19 | System.out.println("Loops complete."); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/ByTwos2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public class ByTwos2 implements Series { 4 | int start; 5 | int val; 6 | int prev; 7 | 8 | ByTwos2() { 9 | start = 0; 10 | val = 0; 11 | prev = -2; 12 | } 13 | 14 | public int getNext() { 15 | prev = val; 16 | val += 2; 17 | return val; 18 | } 19 | 20 | public void reset() { 21 | val = start; 22 | prev = start - 2; 23 | } 24 | 25 | public void setStart(int x) { 26 | start = x; 27 | val = x; 28 | prev = x - 2; 29 | } 30 | 31 | int getPrevious() { 32 | return prev; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/SideEffects.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Side effects can be important. 4 | public class SideEffects { 5 | public static void main(String[] args) { 6 | int i; 7 | 8 | i = 0; 9 | 10 | // Here, i is still incremented even though the if statement fails. 11 | if (false & (++i < 100)) 12 | System.out.print("if statement executed: " + i); 13 | 14 | // In this case, i is not incremented because the short-circuit operator skips the increment. 15 | if (false && (++i < 100)) 16 | System.out.print("this won't be executed"); 17 | System.out.print("if statement executed: " + i); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Ladder.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Demonstrate an if-else-if ladder. 4 | public class Ladder { 5 | public static void main(String[] args) { 6 | int x; 7 | 8 | for (x = 0; x < 6; x++) { 9 | if (x == 1) 10 | System.out.println("x is one"); 11 | else if (x == 2) 12 | System.out.println("x is two"); 13 | else if (x == 3) 14 | System.out.println("x is three"); 15 | else if (x == 4) 16 | System.out.println("x is four"); 17 | else 18 | System.out.println("x is not between 1 and 4"); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/ForEach2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Use for-each style for on a two-dimensional array. 4 | public class ForEach2 { 5 | public static void main(String[] args) { 6 | int sum = 0; 7 | int[][] nums = new int[3][5]; 8 | 9 | // give nums some values 10 | for (int i = 0; i < 3; i++) 11 | for (int j = 0; j < 5; j++) 12 | nums[i][j] = (i+1)*(j+1); 13 | 14 | for (int[] x : nums) { 15 | for (int y : x) { 16 | System.out.println("Value is: " + y); 17 | sum += y; 18 | } 19 | } 20 | System.out.println("Summation: " + sum); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ExcTypeMismatch.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // This won't work 4 | public class ExcTypeMismatch { 5 | public static void main(String[] args) { 6 | int[] nums = new int[4]; 7 | 8 | try { 9 | System.out.println("Before exception is generated."); 10 | 11 | // generate an index out-of-bounds exception 12 | nums[7] = 10; 13 | System.out.println("this won't be displayed"); 14 | } catch (ArithmeticException exc) { 15 | // catch the exception 16 | System.out.println("Index out-of-bounds!"); 17 | } 18 | System.out.println("After catch statement"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/SelfTest1.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | /** 4 | * Write a program that reads characters from the keyboard until a period is received. 5 | * Have the program count the number of spaces. Report the total at the end of the program. 6 | */ 7 | public class SelfTest1 { 8 | public static void main(String[] args) 9 | throws java.io.IOException { 10 | 11 | char c; 12 | int spaces = 0; 13 | 14 | do { 15 | c = (char) System.in.read(); 16 | 17 | if (c == ' ') { 18 | spaces++; 19 | } 20 | } while (c != '.'); 21 | 22 | System.out.println("The number of spaces: " + spaces); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Break5.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Another example of using break with a label. 4 | public class Break5 { 5 | public static void main(String[] args) { 6 | done: 7 | for (int i = 0; i < 10; i++) { 8 | for (int j = 0; j < 10; j++) { 9 | for (int k = 0; k < 10; k++) { 10 | System.out.println(k + " "); 11 | if (k == 5) break done; // jump to done 12 | } 13 | System.out.println("After k loop"); // won't execute 14 | } 15 | System.out.println("After j loop"); // won't execute 16 | } 17 | System.out.println("After i loop"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ExcDemo1.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Demonstrate exception handling. 4 | public class ExcDemo1 { 5 | public static void main(String[] args) { 6 | int[] nums = new int[4]; 7 | 8 | try { 9 | System.out.println("Before exception is generated."); 10 | 11 | // Generate an index out-of-bounds exception. 12 | nums[7] = 10; 13 | System.out.println("this won't be displayed"); 14 | } catch (ArrayIndexOutOfBoundsException exc) { 15 | // catch the exception. 16 | System.out.println("Index out-of-bounds!"); 17 | } 18 | System.out.println("After catch statement."); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/SCops.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Demonstrate the short-circuit operators. 4 | public class SCops { 5 | public static void main(String[] args) { 6 | int n, d, q; 7 | 8 | n = 10; 9 | d = 2; 10 | if (d != 0 && (n % d) == 0) 11 | System.out.println(d + " is a factor of " + n); 12 | 13 | d = 0; 14 | 15 | // Since d is zero, the second operand is not evaluated. 16 | if (d != 0 && (n % d) == 0) 17 | System.out.println(d + " is a factor of " + n); 18 | 19 | // cause division by zero 20 | if (d != 0 & (n % d) == 0) 21 | System.out.println(d + " is a factor of " + n); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/VarArgs.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Demonstrate variable-length arguments. 4 | public class VarArgs { 5 | static void vaTest(int... v) { 6 | System.out.println("Number of args: " + v.length); 7 | System.out.println("Contents: "); 8 | 9 | for (int i = 0; i < v.length; i++) { 10 | System.out.print(" arg " + i + ": " + v[i]); 11 | System.out.println(); 12 | } 13 | } 14 | 15 | public static void main(String[] args) { 16 | // Notice how vaTest() can be called with a variable number of arguments. 17 | vaTest(10); // 1 arg 18 | vaTest(1, 2, 3); // 3 args 19 | vaTest(); // no args 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/DynShapes.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class DynShapes { 4 | public static void main(String[] args) { 5 | TwoDShape8[] shapes = new TwoDShape8[5]; 6 | 7 | shapes[0] = new Triangle8("outlined", 8.0, 12.0); 8 | shapes[1] = new Rectangle8(10); 9 | shapes[2] = new Rectangle8(10, 4); 10 | shapes[3] = new Triangle8(7.0); 11 | shapes[4] = new TwoDShape8(10, 20, "generic"); 12 | 13 | for (int i = 0; i < shapes.length; i++) { 14 | System.out.println("Object is " + shapes[i].getName()); 15 | System.out.println("Area is " + shapes[i].area()); 16 | System.out.println(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Rectangle8.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for rectangles. 4 | public class Rectangle8 extends TwoDShape8 { 5 | Rectangle8() { 6 | super(); 7 | } 8 | 9 | // Constructor for Rectangle. 10 | Rectangle8(double w, double h) { 11 | super(w, h, "rectangle"); 12 | } 13 | 14 | // Construct a square. 15 | Rectangle8(double x) { 16 | super(x, "rectangle"); 17 | } 18 | 19 | boolean isSquare() { 20 | if (getWidth() == getHeight()) return true; 21 | return false; 22 | } 23 | 24 | // Override area for Rectangle. 25 | double area() { 26 | return getWidth() * getHeight(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Rectangle9.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for rectangles. 4 | public class Rectangle9 extends TwoDShape9 { 5 | Rectangle9() { 6 | super(); 7 | } 8 | 9 | // Constructor for Rectangle. 10 | Rectangle9(double w, double h) { 11 | super(w, h, "rectangle"); 12 | } 13 | 14 | // Construct a square. 15 | Rectangle9(double x) { 16 | super(x, "rectangle"); 17 | } 18 | 19 | boolean isSquare() { 20 | if (getWidth() == getHeight()) return true; 21 | return false; 22 | } 23 | 24 | // Override area for Rectangle. 25 | double area() { 26 | return getWidth() * getHeight(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/Squares.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Initialize a two-dimensional array. 4 | public class Squares { 5 | public static void main(String[] args) { 6 | int[][] sqrs = { 7 | { 1, 1 }, 8 | { 2, 4 }, 9 | { 3, 9 }, 10 | { 4, 16 }, 11 | { 5, 25 }, 12 | { 6, 36 }, 13 | { 7, 49 }, 14 | { 8, 64 }, 15 | { 9, 81 }, 16 | { 10, 100 } 17 | }; 18 | int i, j; 19 | 20 | for (i = 0; i < 10; i++) { 21 | for (j = 0; j < 2; j++) 22 | System.out.print(sqrs[i][j] + " "); 23 | System.out.println(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/SeriesDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | public class SeriesDemo { 4 | public static void main(String[] args) { 5 | ByTwos ob = new ByTwos(); 6 | 7 | for (int i = 0; i < 5; i++) { 8 | System.out.println("Next value is " + ob.getNext()); 9 | } 10 | 11 | System.out.println("\nResetting"); 12 | ob.reset(); 13 | for (int i = 0; i < 5; i++) 14 | System.out.println("Next value is " + ob.getNext()); 15 | 16 | System.out.println("\nStarting at 100"); 17 | ob.setStart(100); 18 | for (int i = 0; i < 5; i++) { 19 | System.out.println("Next value is " + ob.getNext()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Shapes6.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Shapes6 { 4 | public static void main(String[] args) { 5 | ColorTriangle t1 = new ColorTriangle("Blue", "outlined", 8.0, 12.0); 6 | ColorTriangle t2 = new ColorTriangle("Red", "filled", 2.0, 2.0); 7 | 8 | System.out.println("Info for t1: "); 9 | t1.showStyle(); 10 | t1.showDim(); 11 | t1.showColor(); 12 | System.out.println("Area is " + t1.area()); 13 | 14 | System.out.println(); 15 | 16 | System.out.println("Info for t2: "); 17 | t2.showStyle(); 18 | t2.showDim(); 19 | t2.showColor(); 20 | System.out.println("Area is " + t2.area()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/TwoDShape2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Use accessor methods to set and get private members. 4 | 5 | // A class for two-dimensional objects. 6 | public class TwoDShape2 { 7 | private double width; 8 | private double height; 9 | 10 | // Accessor methods for with and height. 11 | double getWidth() { 12 | return width; 13 | } 14 | 15 | double getHeight() { 16 | return height; 17 | } 18 | 19 | void setWidth(double w) { 20 | width = w; 21 | } 22 | 23 | void setHeight(double h) { 24 | height = h; 25 | } 26 | 27 | void showDim() { 28 | System.out.println("Width and height are " + width + " and " + height); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/TwoDShape4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Add constructors to the class. 4 | public class TwoDShape4 { 5 | private double width; 6 | private double height; 7 | 8 | TwoDShape4(double w, double h) { 9 | width = w; 10 | height = h; 11 | } 12 | 13 | // Accessor methods for with and height. 14 | double getWidth() { 15 | return width; 16 | } 17 | double getHeight() { 18 | return height; 19 | } 20 | void setWidth(double w) { 21 | width = w; 22 | } 23 | void setHeight(double h) { 24 | height = h; 25 | } 26 | 27 | void showDim() { 28 | System.out.println("Width and height are " + width + " and " + height); 29 | } 30 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/StringSwitch.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Use a string to control a switch statement. 4 | public class StringSwitch { 5 | public static void main(String[] args) { 6 | String command = "cancel"; 7 | 8 | switch (command) { 9 | case "connect": 10 | System.out.println("Connecting"); 11 | break; 12 | case "cancel": 13 | System.out.println("Canceling"); 14 | break; 15 | case "disconnect": 16 | System.out.println("Disconnecting"); 17 | break; 18 | default: 19 | System.out.println("Command Error!"); 20 | break; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/ShowBitsDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Demonstrate ShowBits53. 4 | public class ShowBitsDemo { 5 | public static void main(String[] args) { 6 | ShowBits53 b = new ShowBits53(8); 7 | ShowBits53 i = new ShowBits53(32); 8 | ShowBits53 li = new ShowBits53(64); 9 | 10 | System.out.println("123 in binary: "); 11 | b.show(123); 12 | 13 | System.out.println("\n87987 in binary: "); 14 | i.show(87987); 15 | 16 | System.out.println("\n237658768 in binary: "); 17 | li.show(237658768); 18 | 19 | // you can also show low-order bits of any integer 20 | System.out.println("\nLow order 8 bits of 87987 in binary: "); 21 | b.show(87987); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Guess3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Guess the letter game, 3rd version. 4 | public class Guess3 { 5 | public static void main(String[] args) 6 | throws java.io.IOException { 7 | 8 | char ch, answer = 'K'; 9 | 10 | System.out.println("I'm thinking of a letter between A and Z."); 11 | System.out.print("Can you guess it: "); 12 | 13 | ch = (char) System.in.read(); // read a char from tge keyboard 14 | 15 | if (ch == answer) System.out.println("*** Right ***"); 16 | else { 17 | System.out.print("...Sorry, you're "); 18 | 19 | if (ch < answer) System.out.println("too low"); 20 | else System.out.println("too high"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/HelpClassDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class HelpClassDemo { 4 | public static void main(String[] args) 5 | throws java.io.IOException { 6 | char choice, ignore; 7 | 8 | Help hlpobj = new Help(); 9 | 10 | for (;;) { 11 | do { 12 | hlpobj.showMenu(); 13 | 14 | choice = (char) System.in.read(); 15 | 16 | do { 17 | ignore = (char) System.in.read(); 18 | } while (ignore != '\n'); 19 | 20 | } while (!hlpobj.isValid(choice)); 21 | 22 | if (choice == 'q') break; 23 | 24 | System.out.println("\n"); 25 | 26 | hlpobj.helpOn(choice); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Triangle6.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Triangle6 extends TwoDShape6 { 4 | private String style; 5 | 6 | // A default constructor. 7 | Triangle6() { 8 | super(); 9 | style = "none"; 10 | } 11 | 12 | // Constructor 13 | Triangle6(String s, double w, double h) { 14 | super(w, h); 15 | 16 | style = s; 17 | } 18 | 19 | // one argument constructor. 20 | Triangle6(double x) { 21 | super(x); // call superclass constructor 22 | 23 | style = "filled"; 24 | } 25 | 26 | double area() { 27 | return getWidth() * getHeight() / 2; 28 | } 29 | 30 | void showStyle() { 31 | System.out.println("Triangle is " + style); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/bookpackext/UseBook2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.bookpackext; 2 | 3 | import com.guide.c8.bookpack.*; 4 | 5 | // Use the Book2 class from bookpack. 6 | public class UseBook2 { 7 | public static void main(String[] args) { 8 | Book3[] books = new Book3[5]; 9 | 10 | books[0] = new Book3("Java: A Beginner's Guide", "Schildt", 2018); 11 | books[1] = new Book3("Java: The Complete Reference", "Schildt", 2018); 12 | books[2] = new Book3("Introducing JavaFX 8 Programming", "Schildt", 2015); 13 | books[3] = new Book3("Red Storm Rising", "Clancy", 1986); 14 | books[4] = new Book3("On the Road", "Kerouac", 1955); 15 | 16 | for (Book3 x: books) { 17 | x.show(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Overload.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Demonstrate method overloading. 4 | public class Overload { 5 | void ovlDemo() { 6 | System.out.println("No Parameters"); 7 | } 8 | 9 | // Overload ovlDemo for one integer parameter. 10 | void ovlDemo(int a) { 11 | System.out.println("One parameter: " + a); 12 | } 13 | 14 | // Overload ovlDemo for two integer parameters. 15 | int ovlDemo(int a, int b) { 16 | System.out.println("Two parameters: " + a + " " + b); 17 | return a + b; 18 | } 19 | 20 | // Overload ovlDemo for two double parameters. 21 | double ovlDemo(double a, double b) { 22 | System.out.println("Two double parameters: " + a + " " + b); 23 | return a + b; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/SelfTest3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Test a Stack 4 | public class SelfTest3 { 5 | public static void main(String[] args) { 6 | Stack myStack = new Stack(11); 7 | 8 | String greet = "Hello Java World!"; 9 | for (int i = 0; i < greet.length(); i++) { 10 | myStack.put(greet.charAt(i)); 11 | } 12 | 13 | char c; 14 | do { 15 | c = myStack.pop(); 16 | } while (c != 0); 17 | 18 | Stack myStack2 = new Stack(4); 19 | myStack2.put('H'); 20 | myStack2.put('i'); 21 | myStack2.put('!'); 22 | myStack2.pop(); 23 | myStack2.put('?'); 24 | 25 | do { 26 | c = myStack2.pop(); 27 | } while (c != 0); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.7" 2 | 3 | services: 4 | jdk: 5 | build: 6 | context: ./docker/builds/jdk 7 | dockerfile: Dockerfile 8 | volumes: 9 | - target:/var/www/java-beginners-guide 10 | - jvm-sources:/usr/lib/jvm 11 | - maven-repository:/root/.m2 12 | ports: 13 | - 5005:5005 14 | environment: 15 | - DEBUG=${DEBUG} 16 | volumes: 17 | target: 18 | driver: local 19 | driver_opts: 20 | type: none 21 | device: ${CODEBASE} 22 | o: bind 23 | jvm-sources: 24 | driver: local 25 | driver_opts: 26 | type: none 27 | device: ${CODEBASE}/jvm-sources 28 | o: bind 29 | maven-repository: 30 | driver: local 31 | driver_opts: 32 | type: none 33 | device: ${CODEBASE}/.m2 34 | o: bind -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Shapes.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Shapes { 4 | public static void main(String[] args) { 5 | Triangle t1 = new Triangle(); 6 | Triangle t2 = new Triangle(); 7 | 8 | t1.width = 4.0; 9 | t1.height = 4.0; 10 | t1.style = "filled"; 11 | 12 | t2.width = 8.0; 13 | t2.height = 12.0; 14 | t2.style = "outlined"; 15 | 16 | System.out.println("Info for t1: "); 17 | t1.showStyle(); 18 | t1.showDim(); 19 | System.out.println("Area is " + t1.area()); 20 | 21 | System.out.println(); 22 | 23 | System.out.println("Info for t2: "); 24 | t2.showStyle(); 25 | t2.showDim(); 26 | System.out.println("Area is " + t2.area()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/AddMeth.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class AddMeth { 4 | public static void main(String[] args) { 5 | Vehicle minivan = new Vehicle(); 6 | Vehicle sportscar = new Vehicle(); 7 | 8 | // assign values to fields in minivan 9 | minivan.passengers = 7; 10 | minivan.fuelcap = 16; 11 | minivan.mpg = 21; 12 | 13 | // assign values to fields in sportscar 14 | sportscar.passengers = 2; 15 | sportscar.fuelcap = 14; 16 | sportscar.mpg = 12; 17 | 18 | System.out.println("Minivan can carry " + minivan.passengers + " with a range of " + minivan.range()); 19 | 20 | System.out.println("Sportscar can carry " + sportscar.passengers + " with a range of " + sportscar.range()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ExcDemo5.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | public class ExcDemo5 { 4 | public static void main(String[] args) { 5 | // Here, numer is longer that denom. 6 | int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 }; 7 | int[] denom = { 2, 0, 4, 4, 0, 8 }; 8 | 9 | for (int i = 0; i < numer.length; i++) { 10 | try { 11 | System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); 12 | } catch (ArrayIndexOutOfBoundsException exc) { 13 | // catch the exception 14 | System.out.println("No matching element found."); 15 | } catch (Throwable exc) { 16 | System.out.println("Some exceptions occurred."); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Recursion.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class Recursion { 4 | public static void main(String[] args) { 5 | Factorial f = new Factorial(); 6 | 7 | System.out.println("Factorials using recursive method."); 8 | System.out.println("Factorial of 3 is " + f.factR(3)); 9 | System.out.println("Factorial of 4 is " + f.factR(4)); 10 | System.out.println("Factorial of 5 is " + f.factR(5)); 11 | System.out.println(); 12 | 13 | System.out.println("Factorial using iterative method."); 14 | System.out.println("Factorial of 3 is " + f.factI(3)); 15 | System.out.println("Factorial of 4 is " + f.factI(4)); 16 | System.out.println("Factorial of 5 is " + f.factI(5)); 17 | System.out.println(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Shapes2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Shapes2 { 4 | public static void main(String[] args) { 5 | Triangle2 t1 = new Triangle2(); 6 | Triangle2 t2 = new Triangle2(); 7 | 8 | t1.setWidth(4.0); 9 | t1.setHeight(4.0); 10 | t1.style = "filled"; 11 | 12 | t2.setWidth(8.0); 13 | t2.setHeight(12.0); 14 | t2.style = "outlined"; 15 | 16 | System.out.println("Info for t1: "); 17 | t1.showStyle(); 18 | t1.showDim(); 19 | System.out.println("Area is " + t1.area()); 20 | 21 | System.out.println(); 22 | 23 | System.out.println("Info for t2: "); 24 | t2.showStyle(); 25 | t2.showDim(); 26 | System.out.println("Area is " + t2.area()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/MinMax.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Find the minimum and maximum value in an array 4 | public class MinMax { 5 | public static void main(String[] args) { 6 | int[] nums = new int[10]; 7 | int min, max; 8 | 9 | nums[0] = 99; 10 | nums[1] = -10; 11 | nums[2] = 100123; 12 | nums[3] = 18; 13 | nums[4] = -978; 14 | nums[5] = 5623; 15 | nums[6] = 463; 16 | nums[7] = -9; 17 | nums[8] = 287; 18 | nums[9] = 49; 19 | 20 | min = max = nums[0]; 21 | 22 | for (int i = 1; i < 10; i++) { 23 | if (nums[i] < min) min = nums[i]; 24 | if (nums[i] > max) max = nums[i]; 25 | } 26 | 27 | System.out.println("min and max: " + min + " " + max); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/SelfTest2Demo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class SelfTest2Demo { 4 | public static void main(String[] args) { 5 | SelfTest2 circle1 = new SelfTest2(); 6 | System.out.println("Area of circle1: " + circle1.area()); 7 | System.out.println("Name of circle1: " + circle1.getName()); 8 | System.out.println(); 9 | 10 | SelfTest2 circle2 = new SelfTest2(11); 11 | System.out.println("Area of circle2: " + circle2.area()); 12 | System.out.println("Name of circle2: " + circle2.getName()); 13 | System.out.println(); 14 | 15 | SelfTest2 circle3 = new SelfTest2(circle2); 16 | System.out.println("Area of circle3: " + circle3.area()); 17 | System.out.println("Name of circle3: " + circle3.getName()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Triangle5.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for triangles. 4 | public class Triangle5 extends TwoDShape5 { 5 | private String style; 6 | 7 | // A default constructor. 8 | Triangle5() { 9 | super(); 10 | style = "none"; 11 | } 12 | 13 | // Constructor 14 | Triangle5(String s, double w, double h) { 15 | super(w, h); 16 | 17 | style = s; 18 | } 19 | 20 | // one argument constructor. 21 | Triangle5(double x) { 22 | super(x); // call superclass constructor 23 | 24 | style = "filled"; 25 | } 26 | 27 | double area() { 28 | return getWidth() * getHeight() / 2; 29 | } 30 | 31 | void showStyle() { 32 | System.out.println("Triangle is " + style); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Break6.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Where you put a label is important. 4 | public class Break6 { 5 | public static void main(String[] args) { 6 | int x = 0, y = 0; 7 | 8 | // label before for statement 9 | stop1: for (x = 0; x < 5; x++) { 10 | for (y = 0; y < 5; y++) { 11 | if (y == 2) break stop1; 12 | System.out.println("x and y: " + x + " " + y); 13 | } 14 | } 15 | 16 | System.out.println(); 17 | 18 | // label immediately before { 19 | for (x = 0; x < 5; x++) 20 | stop2: { 21 | for (y = 0; y < 5; y++) { 22 | if (y == 2) break stop2; 23 | System.out.println("x and y: " + x + " " + y); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/NoBreak.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Demonstrate the switch without break statements. 4 | public class NoBreak { 5 | public static void main(String[] args) { 6 | int i; 7 | 8 | for (i = 0; i < 5; i++) { 9 | switch (i) { 10 | case 0: 11 | System.out.println("i is less than one"); 12 | case 1: 13 | System.out.println("i is less than two"); 14 | case 2: 15 | System.out.println("i is less than three"); 16 | case 3: 17 | System.out.println("i is less than four"); 18 | case 4: 19 | System.out.println("i is less than five"); 20 | } 21 | System.out.println(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/MultiCatch.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Use the multi-catch feature. 4 | public class MultiCatch { 5 | public static void main(String[] args) { 6 | int a = 88, b = 0; 7 | int result; 8 | char[] chrs = { 'A', 'B', 'C' }; 9 | 10 | for (int i = 0; i < 2; i++) { 11 | try { 12 | if (i == 0) { 13 | result = a / b; // generate an ArithmeticException 14 | } else { 15 | chrs[5] = 'X'; // generate an ArrayIndexOutOfBoundsException 16 | } 17 | } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) { 18 | System.out.println("Exception caught: " + e); 19 | } 20 | } 21 | 22 | System.out.println("After multi-catch"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/ShowBits53.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | /** 4 | * Try This 5-3 5 | * A class that displays the binary representation of a value. 6 | */ 7 | public class ShowBits53 { 8 | int numbits; 9 | 10 | ShowBits53(int n) { 11 | numbits = n; 12 | } 13 | 14 | void show(long val) { 15 | long mask = 1; 16 | 17 | // left-shift a 1 into the proper position 18 | mask <<= numbits-1; 19 | 20 | int spacer = 0; 21 | for (; mask != 0; mask >>>= 1) { 22 | if ((val & mask) != 0) System.out.print("1"); 23 | else System.out.print("0"); 24 | spacer++; 25 | if (spacer % 8 == 0) { 26 | System.out.print(" "); 27 | spacer = 0; 28 | } 29 | } 30 | System.out.println(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/TruckDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class TruckDemo { 4 | public static void main(String[] args) { 5 | // construct some trucks 6 | Truck semi = new Truck(2, 200, 7, 44000); 7 | Truck pickup = new Truck(3, 28, 15, 2000); 8 | double gallons; 9 | int dist = 252; 10 | 11 | gallons = semi.fuelneeded(dist); 12 | 13 | System.out.println("Semi can carry " + semi.getCargo() + " pounds."); 14 | System.out.println("To go " + dist + " miles semi needs " + gallons + " gallons of fuel.\n"); 15 | 16 | gallons = pickup.fuelneeded(dist); 17 | 18 | System.out.println("Pickup can carry " + pickup.getCargo() + " pounds."); 19 | System.out.println("To go " + dist + " miles pickup needs " + gallons + " gallons of fuel.\n"); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c1/IfDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c1; 2 | 3 | class IfDemo { 4 | public static void main(String[] args) { 5 | int a, b, c; 6 | 7 | a = 2; 8 | b = 3; 9 | 10 | if (a < b) System.out.println("a is less than b"); 11 | 12 | if (a == b) System.out.println("you won't see this"); 13 | 14 | System.out.println(); 15 | 16 | c = a - b; 17 | 18 | System.out.println("c contains -1 (" + c + ")"); 19 | 20 | if (c >= 0) System.out.println("c is non-negative"); 21 | if (c < 0) System.out.println("c is negative"); 22 | 23 | System.out.println(); 24 | 25 | c = b - a; 26 | 27 | System.out.println("c contains 1 (" + c + ")"); 28 | 29 | if (c >= 0) System.out.println("c is non-negative"); 30 | if (c < 0) System.out.println("c is negative"); 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/ExcDemo4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Use multiple catch statements. 4 | public class ExcDemo4 { 5 | public static void main(String[] args) { 6 | // Here, numer is longer that denom. 7 | int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 }; 8 | int[] denom = { 2, 0, 4, 4, 0, 8 }; 9 | 10 | for (int i = 0; i < numer.length; i++) { 11 | try { 12 | System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); 13 | } catch (ArithmeticException exc) { 14 | // catch the exception 15 | System.out.println("Can't divide by Zero!"); 16 | } catch (ArrayIndexOutOfBoundsException exc) { 17 | // catch the exception 18 | System.out.println("No matching element found."); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/bookpackext/UseBook.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.bookpackext; 2 | 3 | // Use the Book2 class from bookpack. 4 | public class UseBook { 5 | public static void main(String[] args) { 6 | com.guide.c8.bookpack.Book2[] books = new com.guide.c8.bookpack.Book2[5]; 7 | 8 | books[0] = new com.guide.c8.bookpack.Book2("Java: A Beginner's Guide", "Schildt", 2018); 9 | books[1] = new com.guide.c8.bookpack.Book2("Java: The Complete Reference", "Schildt", 2018); 10 | books[2] = new com.guide.c8.bookpack.Book2("Introducing JavaFX 8 Programming", "Schildt", 2015); 11 | books[3] = new com.guide.c8.bookpack.Book2("Red Storm Rising", "Clancy", 1986); 12 | books[4] = new com.guide.c8.bookpack.Book2("On the Road", "Kerouac", 1955); 13 | 14 | for (com.guide.c8.bookpack.Book2 x: books) { 15 | x.show(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Queue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // An improved queue class for characters. 4 | public class Queue { 5 | private char[] q; // this array holds the queue 6 | private int putloc, getloc; // the put and get indices 7 | 8 | Queue(int size) { 9 | q = new char[size]; // allocate memory for queue 10 | putloc = getloc = 0; 11 | } 12 | 13 | // put a character into the queue 14 | void put(char ch) { 15 | if (putloc == q.length) { 16 | System.out.println(" - Queue is full."); 17 | return; 18 | } 19 | 20 | q[putloc++] = ch; 21 | } 22 | 23 | // get a character from the queue 24 | char get() { 25 | if (getloc == putloc) { 26 | System.out.println(" - Queue is empty."); 27 | return (char) 0; 28 | } 29 | 30 | return q[getloc++]; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Shapes5.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | public class Shapes5 { 4 | public static void main(String[] args) { 5 | Triangle5 t1 = new Triangle5(); 6 | Triangle5 t2 = new Triangle5("outlined", 8.0, 12.0); 7 | Triangle5 t3 = new Triangle5(4.0); 8 | 9 | t1 = t2; 10 | 11 | System.out.println("Info for t1: "); 12 | t1.showStyle(); 13 | t1.showDim(); 14 | System.out.println("Area is " + t1.area()); 15 | 16 | System.out.println(); 17 | 18 | System.out.println("Info for t2: "); 19 | t2.showStyle(); 20 | t2.showDim(); 21 | System.out.println("Area is " + t2.area()); 22 | 23 | System.out.println(); 24 | 25 | System.out.println("Info for t3: "); 26 | t3.showStyle(); 27 | t3.showDim(); 28 | System.out.println("Area is " + t3.area()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/RetMeth.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class RetMeth { 4 | public static void main(String[] args) { 5 | Vehicle minivan = new Vehicle(); 6 | Vehicle sportscar = new Vehicle(); 7 | 8 | int range1, range2; 9 | 10 | // assign values to fields in minivan 11 | minivan.passengers = 7; 12 | minivan.fuelcap = 16; 13 | minivan.mpg = 21; 14 | 15 | // assign values to fields in sportscar 16 | sportscar.passengers = 2; 17 | sportscar.fuelcap = 14; 18 | sportscar.mpg = 12; 19 | 20 | range1 = minivan.range(); 21 | System.out.println("Minivan can carry " + minivan.passengers + " with a range of " + range1 + " miles"); 22 | 23 | range2 = sportscar.range(); 24 | System.out.println("Sportscar can carry " + sportscar.passengers + " with a range of " + range2 + " miles"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/Queue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | /** 4 | * Try This 5-2 5 | * 6 | * A queue class for characters. 7 | */ 8 | public class Queue { 9 | char[] q; // this array holds the queue 10 | int putloc, getloc; // the put and get indices 11 | 12 | Queue(int size) { 13 | q = new char[size]; // allocate memory for queue 14 | putloc = getloc = 0; 15 | } 16 | 17 | // put a character into the queue 18 | void put(char ch) { 19 | if (putloc == q.length) { 20 | System.out.println(" - Queue is full."); 21 | return; 22 | } 23 | 24 | q[putloc++] = ch; 25 | } 26 | 27 | // get a character from the queue 28 | char get() { 29 | if (getloc == putloc) { 30 | System.out.println(" - Queue is empty."); 31 | return (char) 0; 32 | } 33 | 34 | return q[getloc++]; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/CastDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Demonstrate casting. 4 | public class CastDemo { 5 | public static void main(String[] args) { 6 | double x, y; 7 | byte b; 8 | int i; 9 | char ch; 10 | 11 | x = 10.0; 12 | y = 3.0; 13 | 14 | i = (int) (x / y); // cast double to int. Truncate will occur in this conversion. 15 | System.out.println("Integer outcome of x / y: " + i); 16 | 17 | i = 100; 18 | b = (byte) i; // No loss of info here. A byte can hold the value 100. 19 | System.out.println("Value of b: " + b); 20 | 21 | i = 257; 22 | b = (byte) i; // Information loss this time. A byte cannot hold the value 257. 23 | System.out.println("Value of b: " + b); 24 | 25 | b = 88; // ASCII code for X 26 | ch = (char) b; 27 | System.out.println("ch: " + ch); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Stack.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Create a Stack 4 | public class Stack { 5 | private char[] q; // this array holds the stack 6 | private int loc; // the put and pop index 7 | 8 | // Construct a Stack 9 | Stack(int size) { 10 | q = new char[size]; // allocate memory for stack 11 | loc = size; 12 | 13 | } 14 | 15 | void put(char ch) { 16 | if (loc == 0) { 17 | System.out.println(" - Stack is full."); 18 | return; 19 | } 20 | 21 | System.out.println(" - put " + ch); 22 | 23 | q[--loc] = ch; 24 | } 25 | 26 | char pop() { 27 | if (loc == q.length) { 28 | System.out.println(" - Stack is empty."); 29 | return (char) 0; 30 | } 31 | 32 | char ch = q[loc++]; 33 | 34 | System.out.println(" - pop " + ch); 35 | 36 | return ch; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/Rethrow.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Rethrow an exception. 4 | public class Rethrow { 5 | public static void getException() { 6 | // Here, numer is longer that denom. 7 | int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 }; 8 | int[] denom = { 2, 0, 4, 4, 0, 8 }; 9 | 10 | for (int i = 0; i < numer.length; i++) { 11 | try { 12 | System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); 13 | } catch (ArithmeticException exc) { 14 | // catch the exception 15 | System.out.println("Can't divide by Zero!"); 16 | } catch (ArrayIndexOutOfBoundsException exc) { 17 | // catch the exception 18 | System.out.println("No matching element found."); 19 | throw exc; // rethrow the exception 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/CompFuel.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | public class CompFuel { 4 | public static void main(String[] args) { 5 | Vehicle minivan = new Vehicle(); 6 | Vehicle sportscar = new Vehicle(); 7 | double gallons; 8 | int dist = 252; 9 | 10 | // assign values to fields in minivan 11 | minivan.passengers = 7; 12 | minivan.fuelcap = 16; 13 | minivan.mpg = 21; 14 | 15 | // assign values to fields in sportscar 16 | sportscar.passengers = 2; 17 | sportscar.fuelcap = 14; 18 | sportscar.mpg = 12; 19 | 20 | gallons = minivan.fuelneeded(dist); 21 | System.out.println("To go " + dist + " miles minivan needs " + gallons + " gallons of fuel."); 22 | 23 | gallons = sportscar.fuelneeded(dist); 24 | System.out.println("To go " + dist + " miles sportscar needs " + gallons + " gallons of fuel."); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/ShiftDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Demonstrate the shift << and >> operators. 4 | public class ShiftDemo { 5 | public static void main(String[] args) { 6 | int val = 1; 7 | 8 | for (int i = 0; i < 8; i++) { 9 | for (int t = 128; t > 0; t = t/2) { 10 | if ((val & t) != 0) System.out.print("1 "); 11 | else System.out.print("0 "); 12 | } 13 | System.out.println(); 14 | val = val << 1; // left shift 15 | } 16 | System.out.println(); 17 | 18 | val = 128; 19 | 20 | for (int i = 0; i < 8; i++) { 21 | for (int t = 128; t > 0; t = t/2) { 22 | if ((val & t) != 0) System.out.print("1 "); 23 | else System.out.print("0 "); 24 | } 25 | System.out.println(); 26 | val = val >> 1; // right shift 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Break4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Using break with a label. 4 | public class Break4 { 5 | public static void main(String[] args) { 6 | int i; 7 | 8 | for (i = 1; i < 4; i++) { 9 | one: { 10 | two: { 11 | three: { 12 | System.out.println("\ni is " + i); 13 | if (i == 1) break one; 14 | if (i == 2) break two; 15 | if (i == 3) break three; 16 | 17 | // this is never reached 18 | System.out.println("Won't print"); 19 | } 20 | System.out.println("After block three."); 21 | } 22 | System.out.println("After block two."); 23 | } 24 | System.out.println("After block one."); 25 | } 26 | 27 | System.out.println("After for."); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/Encode.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Use XOR to encode and decode a message 4 | public class Encode { 5 | public static void main(String[] args) { 6 | String msg = "This is a test"; 7 | String encmsg = ""; 8 | String decmsg = ""; 9 | int key = 88; 10 | 11 | System.out.print("Original message: "); 12 | System.out.println(msg); 13 | 14 | // encode the message 15 | for (int i = 0; i < msg.length(); i++) { 16 | encmsg = encmsg + (char) (msg.charAt(i) ^ key); 17 | } 18 | 19 | System.out.print("Encoded message: "); 20 | System.out.println(encmsg); 21 | 22 | // decode the message 23 | for (int i = 0; i < msg.length(); i++) { 24 | decmsg = decmsg + (char) (encmsg.charAt(i) ^ key); 25 | } 26 | 27 | System.out.print("Decoded message: "); 28 | System.out.println(decmsg); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/Phone.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // A simple automated telephone directory. 4 | public class Phone { 5 | public static void main(String[] args) { 6 | String[][] numbers = { 7 | { "Tom", "555-3322" }, 8 | { "Mary", "555-8976" }, 9 | { "Jon", "555-1037" }, 10 | { "Rachel", "555-1400" } 11 | }; 12 | 13 | int i; 14 | 15 | if (args.length != 1) { 16 | System.out.println("Usage: java Phone "); 17 | } else { 18 | for (i = 0; i < numbers.length; i++) { 19 | if (numbers[i][0].equals(args[0])) { 20 | System.out.println(numbers[i][0] + ": " + numbers[i][1]); 21 | break; 22 | } 23 | } 24 | if (i == numbers.length) { 25 | System.out.println("Name not found."); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/RelLogOps.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Demonstrate the relational and logical operators. 4 | public class RelLogOps { 5 | public static void main(String[] args) { 6 | int i, j; 7 | boolean b1, b2; 8 | 9 | i = 10; 10 | j = 11; 11 | if (i < j) System.out.println("i < j"); 12 | if (i <= j) System.out.println("i <= j"); 13 | if (i != j) System.out.println("i != j"); 14 | if (i == j) System.out.println("this won't execute"); 15 | if (i >= j) System.out.println("this won't execute"); 16 | if (i > j) System.out.println("this won't execute"); 17 | 18 | b1 = true; 19 | b2 = false; 20 | 21 | if (b1 & b2) System.out.println("this won't execute"); 22 | if (!(b1 & b2)) System.out.println("!(b1 & b2) is true"); 23 | if (b1 | b2) System.out.println("b1 | b2 is true"); 24 | if (b1 ^ b2) System.out.println("b1 ^ b2 is true"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Quicksort.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Try This 6-3: A simple version of the Quicksort. 4 | public class Quicksort { 5 | // Set up a call to the actual Quicksort method. 6 | static void qsort(char[] items) { 7 | qs(items, 0, items.length-1); 8 | } 9 | 10 | private static void qs(char[] items, int left, int right) { 11 | int i, j; 12 | char x, y; 13 | 14 | i = left; 15 | j = right; 16 | x = items[(left+right)/2]; 17 | 18 | do { 19 | while ((items[i] < x) && (i < right)) i++; 20 | while ((x < items[j]) && (j > left)) j--; 21 | 22 | if (i <= j) { 23 | y = items[i]; 24 | items[i] = items[j]; 25 | items[j] = y; 26 | i++; j--; 27 | } 28 | } while (i <= j); 29 | 30 | if (left < j) qs(items, left, j); 31 | if (i < right) qs(items, i, right); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Triangle7.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for triangles. 4 | public class Triangle7 extends TwoDShape7 { 5 | private String style; 6 | 7 | // A default constructor. 8 | Triangle7() { 9 | super(); 10 | style = "none"; 11 | } 12 | 13 | // Constructor 14 | Triangle7(String s, double w, double h) { 15 | super(w, h); 16 | 17 | style = s; 18 | } 19 | 20 | // one argument constructor. 21 | Triangle7(double x) { 22 | super(x); // call superclass constructor 23 | 24 | style = "filled"; 25 | } 26 | 27 | // Construct an object from on object. 28 | Triangle7(Triangle7 ob) { 29 | super(ob); // pass object to TwoDShape constructor 30 | style = ob.style; 31 | } 32 | 33 | double area() { 34 | return getWidth() * getHeight() / 2; 35 | } 36 | 37 | void showStyle() { 38 | System.out.println("Triangle is " + style); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/FixedQueue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // A fixed-size queue class for characters. 4 | public class FixedQueue implements ICharQ { 5 | private char[] q; // this array holds the queue 6 | private int putloc, getloc; // the put and get indices 7 | 8 | // Construct an empty queue given its size. 9 | public FixedQueue(int size) { 10 | q = new char[size]; // allocate memory for queue 11 | putloc = getloc = 0; 12 | } 13 | 14 | // Put a character into the queue. 15 | public void put(char ch) { 16 | if (putloc == q.length) { 17 | System.out.println(" - Queue is full."); 18 | return; 19 | } 20 | 21 | q[putloc++] = ch; 22 | } 23 | 24 | // Get a character from the queue. 25 | public char get() { 26 | if (getloc == putloc) { 27 | System.out.println(" - Queue is empty."); 28 | return (char) 0; 29 | } 30 | 31 | return q[getloc++]; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/NestTrys.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Use a nested try block. 4 | public class NestTrys { 5 | public static void main(String[] args) { 6 | // Here, numer is longer that denom. 7 | int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 }; 8 | int[] denom = { 2, 0, 4, 4, 0, 8 }; 9 | 10 | try { 11 | for (int i = 0; i < numer.length; i++) { 12 | try { 13 | System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); 14 | } catch (ArithmeticException exc) { 15 | // catch the exception 16 | System.out.println("Can't divide by Zero!"); 17 | } 18 | } 19 | } catch (ArrayIndexOutOfBoundsException exc) { 20 | // catch the exception 21 | System.out.println("No matching element found."); 22 | System.out.println("Fatal error - program terminated."); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/FixedQueue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // A fixed-size queue class for characters that uses exceptions. 4 | public class FixedQueue implements ICharQ { 5 | private char[] q; // this array holds the queue 6 | private int putloc, getloc; // the put and get indices 7 | 8 | // Construct an empty queue given its size. 9 | public FixedQueue(int size) { 10 | q = new char[size]; // allocate memory for queue 11 | putloc = getloc = 0; 12 | } 13 | 14 | // Put a character into the queue. 15 | public void put(char ch) throws QueueFullException { 16 | if (putloc == q.length) { 17 | throw new QueueFullException(q.length); 18 | } 19 | 20 | q[putloc++] = ch; 21 | } 22 | 23 | // Get a character from the queue. 24 | public char get() throws QueueEmptyException { 25 | if (getloc == putloc) { 26 | throw new QueueEmptyException(); 27 | } 28 | 29 | return q[getloc++]; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/SwitchDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Demonstrate the switch. 4 | public class SwitchDemo { 5 | public static void main(String[] args) { 6 | int i; 7 | 8 | for (i = 0; i < 10; i++) { 9 | switch (i) { 10 | case 0: 11 | System.out.println("i is zero"); 12 | break; 13 | case 1: 14 | System.out.println("i is one"); 15 | break; 16 | case 2: 17 | System.out.println("i is two"); 18 | break; 19 | case 3: 20 | System.out.println("i is three"); 21 | break; 22 | case 4: 23 | System.out.println("i is four"); 24 | break; 25 | default: 26 | System.out.println("i is five or more"); 27 | break; 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Triangle8.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for triangles. 4 | public class Triangle8 extends TwoDShape8 { 5 | private String style; 6 | 7 | // A default constructor. 8 | Triangle8() { 9 | super(); 10 | style = "none"; 11 | } 12 | 13 | // Constructor 14 | Triangle8(String s, double w, double h) { 15 | super(w, h, "triangle"); 16 | 17 | style = s; 18 | } 19 | 20 | // one argument constructor. 21 | Triangle8(double x) { 22 | super(x, "triangle"); // call superclass constructor 23 | 24 | style = "filled"; 25 | } 26 | 27 | // Construct an object from on object. 28 | Triangle8(Triangle8 ob) { 29 | super(ob); // pass object to TwoDShape constructor 30 | style = ob.style; 31 | } 32 | 33 | double area() { 34 | return getWidth() * getHeight() / 2; 35 | } 36 | 37 | void showStyle() { 38 | System.out.println("Triangle is " + style); 39 | } 40 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/TwoDShape5.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Add constructors to the class. 4 | public class TwoDShape5 { 5 | private double width; 6 | private double height; 7 | 8 | // A default constructor. 9 | TwoDShape5() { 10 | width = height = 0.0; 11 | } 12 | 13 | // Parameterized constructor. 14 | TwoDShape5(double w, double h) { 15 | width = w; 16 | height = h; 17 | } 18 | 19 | // Construct object with equal with and height 20 | TwoDShape5(double x) { 21 | width = height = x; 22 | } 23 | 24 | // Accessor methods for with and height. 25 | double getWidth() { 26 | return width; 27 | } 28 | double getHeight() { 29 | return height; 30 | } 31 | void setWidth(double w) { 32 | width = w; 33 | } 34 | void setHeight(double h) { 35 | height = h; 36 | } 37 | 38 | void showDim() { 39 | System.out.println("Width and height are " + width + " and " + height); 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/TwoDShape6.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A multilevel hierarchy. 4 | public class TwoDShape6 { 5 | private double width; 6 | private double height; 7 | 8 | // A default constructor. 9 | TwoDShape6() { 10 | width = height = 0.0; 11 | } 12 | 13 | // Parameterized constructor. 14 | TwoDShape6(double w, double h) { 15 | width = w; 16 | height = h; 17 | } 18 | 19 | // Construct object with equal with and height 20 | TwoDShape6(double x) { 21 | width = height = x; 22 | } 23 | 24 | // Accessor methods for with and height. 25 | double getWidth() { 26 | return width; 27 | } 28 | double getHeight() { 29 | return height; 30 | } 31 | void setWidth(double w) { 32 | width = w; 33 | } 34 | void setHeight(double h) { 35 | height = h; 36 | } 37 | 38 | void showDim() { 39 | System.out.println("Width and height are " + width + " and " + height); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/qpack/FixedQueue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.qpack; 2 | 3 | // A fixed-size queue class for characters. 4 | public class FixedQueue implements ICharQ { 5 | private char[] q; // this array holds the queue 6 | private int putloc, getloc; // the put and get indices 7 | 8 | // Construct an empty queue given its size. 9 | public FixedQueue(int size) { 10 | q = new char[size]; // allocate memory for queue 11 | putloc = getloc = 0; 12 | } 13 | 14 | // Put a character into the queue. 15 | public void put(char ch) { 16 | if (putloc == q.length) { 17 | System.out.println(" - Queue is full."); 18 | return; 19 | } 20 | 21 | q[putloc++] = ch; 22 | } 23 | 24 | // Get a character from the queue. 25 | public char get() { 26 | if (getloc == putloc) { 27 | System.out.println(" - Queue is empty."); 28 | return (char) 0; 29 | } 30 | 31 | return q[getloc++]; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Triangle9.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A subclass of TwoDShape for triangles. 4 | public class Triangle9 extends TwoDShape9 { 5 | private String style; 6 | 7 | // A default constructor. 8 | Triangle9() { 9 | super(); 10 | style = "none"; 11 | } 12 | 13 | // Constructor 14 | Triangle9(String s, double w, double h) { 15 | super(w, h, "triangle"); 16 | 17 | style = s; 18 | } 19 | 20 | // One argument constructor. 21 | Triangle9(double x) { 22 | super(x, "triangle"); // call superclass constructor 23 | 24 | style = "filled"; 25 | } 26 | 27 | // Construct an object from on object. 28 | Triangle9(Triangle9 ob) { 29 | super(ob); // pass object to TwoDShape constructor 30 | style = ob.style; 31 | } 32 | 33 | double area() { 34 | return getWidth() * getHeight() / 2; 35 | } 36 | 37 | void showStyle() { 38 | System.out.println("Triangle is " + style); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /docker/builds/jdk/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:9-jdk 2 | 3 | ARG MAVEN_VERSION=3.6.3 4 | ARG USER_HOME_DIR="/root" 5 | ARG SHA=c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0 6 | ARG BASE_URL=https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries 7 | 8 | RUN mkdir -p /usr/local/maven /usr/local/maven/ref \ 9 | && curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \ 10 | && echo "${SHA} /tmp/apache-maven.tar.gz" | sha512sum -c - \ 11 | && tar -xzf /tmp/apache-maven.tar.gz -C /usr/local/maven --strip-components=1 \ 12 | && rm -f /tmp/apache-maven.tar.gz \ 13 | && ln -s /usr/local/maven/bin/mvn /usr/bin/mvn 14 | 15 | ENV MAVEN_HOME /usr/local/maven 16 | ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2" 17 | 18 | COPY bin/entrypoint.sh /usr/local/bin/entrypoint.sh 19 | RUN chmod +x /usr/local/bin/entrypoint.sh 20 | 21 | WORKDIR /var/www/java-beginners-guide 22 | 23 | ENTRYPOINT ["entrypoint.sh"] 24 | 25 | CMD ["mvn", "-version"] -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/SelfTest10.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | /** 4 | * Write a program that reads characters from the keyboard and coverts all lowercase letters to uppercase, 5 | * and all uppercase letters to lowercase, displaying the result. 6 | * Have the program stop when the user enters a period. 7 | * At the end, have the program display the number of case changes that have taken place. 8 | */ 9 | public class SelfTest10 { 10 | public static void main(String[] args) 11 | throws java.io.IOException { 12 | char c; 13 | int changes = 0; 14 | 15 | do { 16 | c = (char) System.in.read(); 17 | 18 | if (c >= 'a' && c <= 'z') { 19 | c -= 32; 20 | changes++; 21 | } else if (c >= 'A' && c <= 'Z') { 22 | c += 32; 23 | changes++; 24 | } 25 | 26 | System.out.print(c); 27 | } while (c != '.'); 28 | 29 | System.out.print("Total changes: " + changes); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/CustomExceptDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | public class CustomExceptDemo { 4 | public static void main(String[] args) { 5 | // Here, numer contains some odd values 6 | int[] numer = { 4, 8, 15, 32, 64, 127, 256, 512 }; 7 | int[] denom = { 2, 0, 4, 4, 0, 8 }; 8 | 9 | for (int i = 0; i < numer.length; i++) { 10 | try { 11 | if ((numer[i]%2) != 0) { 12 | throw new NonIntResultException(numer[i], denom[i]); 13 | } 14 | 15 | System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i] / denom[i]); 16 | } catch (ArithmeticException exc) { 17 | System.out.println("Can't divide by Zero!"); 18 | } catch (ArrayIndexOutOfBoundsException exc) { 19 | System.out.println("No matching element found."); 20 | } catch (NonIntResultException exc) { 21 | System.out.println(exc); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/QExcDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | public class QExcDemo { 4 | public static void main(String[] args) { 5 | FixedQueue q = new FixedQueue(10); 6 | char ch; 7 | int i; 8 | 9 | try { 10 | // overrun the queue 11 | for (i = 0; i < 11; i++) { 12 | System.out.print("Attempting to store: " + (char) ('A' + i)); 13 | q.put((char) ('A' + i)); 14 | System.out.println(" - OK"); 15 | } 16 | System.out.println(); 17 | } catch (QueueFullException exc) { 18 | System.out.println(exc); 19 | } 20 | System.out.println(); 21 | 22 | try { 23 | for (i = 0; i < 11; i++) { 24 | System.out.print("Getting next char: "); 25 | ch = q.get(); 26 | System.out.println(ch); 27 | } 28 | } catch (QueueEmptyException exc) { 29 | System.out.println(exc); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c4/TwoVehicles.java: -------------------------------------------------------------------------------- 1 | package com.guide.c4; 2 | 3 | // This program creates two vehicle objects. 4 | public class TwoVehicles { 5 | public static void main(String[] args) { 6 | Vehicle minivan = new Vehicle(); 7 | Vehicle sportscar = new Vehicle(); 8 | 9 | int range1, range2; 10 | 11 | // assign values to fields in minivan 12 | minivan.passengers = 7; 13 | minivan.fuelcap = 16; 14 | minivan.mpg = 21; 15 | 16 | // assign values to fields in sportscar 17 | sportscar.passengers = 2; 18 | sportscar.fuelcap = 14; 19 | sportscar.mpg = 12; 20 | 21 | // compute the ranges assuming a full tank of gas 22 | range1 = minivan.fuelcap * minivan.mpg; 23 | range2 = sportscar.fuelcap * sportscar.mpg; 24 | 25 | System.out.println("Minivan can carry " + minivan.passengers + " with a range of " + range1); 26 | 27 | System.out.println("Sportscar can carry " + sportscar.passengers + " with a range of " + range2); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/bookpackext/ProtectDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.bookpackext; 2 | 3 | public class ProtectDemo { 4 | public static void main(String[] args) { 5 | ExtBook[] books = new ExtBook[5]; 6 | 7 | books[0] = new ExtBook("Java: A Beginner's Guide", "Schildt", 2018, "Oracle Press"); 8 | books[1] = new ExtBook("Java: The Complete Reference", "Schildt", 2018, "Oracle Press"); 9 | books[2] = new ExtBook("Introducing JavaFX 8 Programming", "Schildt", 2015, "Oracle Press"); 10 | books[3] = new ExtBook("Red Storm Rising", "Clancy", 1986, "Putnam"); 11 | books[4] = new ExtBook("On the Road", "Kerouac", 1955, "Viking"); 12 | 13 | for (ExtBook x: books) { 14 | x.show(); 15 | } 16 | 17 | // Find books by author 18 | System.out.println("Showing all books by Schildt."); 19 | for (int i = 0; i < books.length; i++) { 20 | if (books[i].getAuthor() == "Schildt") 21 | System.out.println(books[i].getTitle()); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Guess4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | // Guess the letter game, 4th version. 4 | public class Guess4 { 5 | public static void main(String[] args) 6 | throws java.io.IOException { 7 | 8 | char ch, ignore, answer = 'K'; 9 | 10 | do { 11 | System.out.println("I'm thinking of a letter between A and Z."); 12 | System.out.print("Can you guess it: "); 13 | 14 | // read a character 15 | ch = (char) System.in.read(); 16 | 17 | // discard any other characters in the input buffer 18 | do { 19 | ignore = (char) System.in.read(); 20 | } while (ignore != '\n'); 21 | 22 | if (ch == answer) System.out.println("** Right **"); 23 | else { 24 | System.out.println("...Sorry, you're "); 25 | if (ch < answer) System.out.println("too low"); 26 | else System.out.println("too high"); 27 | System.out.println("Try again!\n"); 28 | } 29 | } while (answer != ch); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c9/UseFinally.java: -------------------------------------------------------------------------------- 1 | package com.guide.c9; 2 | 3 | // Use finally. 4 | public class UseFinally { 5 | public static void genException(int what) { 6 | int t; 7 | int[] nums = new int[2]; 8 | 9 | System.out.println("Receiving " + what); 10 | try { 11 | switch (what) { 12 | case 0: 13 | t = 10 / what; // generate div-by-zero error 14 | break; 15 | case 1: 16 | nums[4] = 4; // generate array index error 17 | break; 18 | case 2: 19 | return; // return from try block 20 | } 21 | } catch (ArithmeticException exc) { 22 | // catch the exception 23 | System.out.println("Can't divide by Zero!"); 24 | return; // return from catch 25 | } catch (ArrayIndexOutOfBoundsException exc) { 26 | System.out.println("No matching elements found."); 27 | } finally { 28 | System.out.println("Leaving try."); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/LogicalOpTable.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Try this 2-2: a truth table for the logical operators. 4 | public class LogicalOpTable { 5 | public static void main(String[] args) { 6 | boolean p, q; 7 | 8 | System.out.println("P\tQ\tAND\tOR\tXOR\tNOT"); 9 | 10 | p = true; q = true; 11 | System.out.print(p + "\t" + q + "\t"); 12 | System.out.print((p&q) + "\t" + (p|q) + "\t"); 13 | System.out.println((p^q) + "\t" + (!p)); 14 | 15 | p = true; q = false; 16 | System.out.print(p + "\t" + q + "\t"); 17 | System.out.print((p&q) + "\t" + (p|q) + "\t"); 18 | System.out.println((p^q) + "\t" + (!p)); 19 | 20 | p = false; q = true; 21 | System.out.print(p + "\t" + q + "\t"); 22 | System.out.print((p&q) + "\t" + (p|q) + "\t"); 23 | System.out.println((p^q) + "\t" + (!p)); 24 | 25 | p = false; q = false; 26 | System.out.print(p + "\t" + q + "\t"); 27 | System.out.print((p&q) + "\t" + (p|q) + "\t"); 28 | System.out.println((p^q) + "\t" + (!p)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/bookpackext/ExtBook.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.bookpackext; 2 | 3 | public class ExtBook extends com.guide.c8.bookpack.Book3 { 4 | private String publisher; 5 | 6 | public ExtBook(String t, String a, int d, String p) { 7 | super(t, a, d); 8 | publisher = p; 9 | } 10 | 11 | public void show() { 12 | super.show(); 13 | System.out.println(publisher); 14 | System.out.println(); 15 | } 16 | 17 | public String getPublisher() { 18 | return publisher; 19 | } 20 | 21 | public void setPublisher(String p) { 22 | publisher = p; 23 | } 24 | 25 | public String getTitle() { 26 | return title; 27 | } 28 | 29 | public void setTitle(String t) { 30 | title = t; 31 | } 32 | 33 | public String getAuthor() { 34 | return author; 35 | } 36 | 37 | public void setAuthor(String a) { 38 | author = a; 39 | } 40 | 41 | public int getPubDate() { 42 | return pubDate; 43 | } 44 | 45 | public void setPubDate(int d) { 46 | pubDate = d; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/Bubble.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | /** 4 | * Try This 5-1 5 | * 6 | * Demonstrate the Bubble sort. 7 | */ 8 | public class Bubble { 9 | public static void main(String[] args) { 10 | int[] nums = {99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49}; 11 | 12 | int a, b, t; 13 | int size; 14 | 15 | size = 10; // number of elements to sort 16 | 17 | System.out.print("Original array is:"); 18 | for (int i = 0; i < size; i++) 19 | System.out.print(" " + nums[i]); 20 | System.out.println(); 21 | 22 | for (a = 1; a < size; a++) 23 | for (b = size - 1; b >= a; b--) { 24 | if (nums[b - 1] > nums[b]) { 25 | t = nums[b - 1]; 26 | nums[b - 1] = nums[b]; 27 | nums[b] = t; 28 | } 29 | } 30 | 31 | // display sorted array 32 | System.out.print("Sorted array is:"); 33 | for (int i = 0; i < size; i++) 34 | System.out.print(" " + nums[i]); 35 | System.out.println(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/FSDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Demonstrate the fail-soft array. 4 | public class FSDemo { 5 | public static void main(String[] args) { 6 | FailSoftArray fs = new FailSoftArray(5, -1); 7 | int x; 8 | 9 | // show quiet failures 10 | System.out.println("Fail quietly."); 11 | for (int i = 0; i < fs.length * 2; i++) 12 | fs.put(i, i*10); 13 | 14 | for (int i = 0; i < fs.length * 2; i++){ 15 | x = fs.get(i); 16 | if (x != -1) System.out.print(x + " "); 17 | } 18 | System.out.println(); 19 | 20 | // now, handle failures 21 | System.out.println("\nFail with error reports."); 22 | for (int i = 0; i < fs.length * 2; i++) 23 | if (!fs.put(i, i*10)) 24 | System.out.println("Index " + i + " out-of-bounds"); 25 | 26 | for (int i = 0; i < fs.length * 2; i++) { 27 | x = fs.get(i); 28 | if (x != -1) System.out.print(x + " "); 29 | else System.out.println("Index " + i + " out-of-bounds"); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/Series3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // A further enhanced version of Series that includes two default methods that use a private method called getArray(); 4 | public interface Series3 { 5 | int getNext(); // return next number in series 6 | 7 | // Return an array that contains the next n elements 8 | // in the series beyond the current element. 9 | default int[] getNextArray(int n) { 10 | return getArray(n); 11 | } 12 | 13 | // Return an array that contains the next n elements 14 | // in the series, after skipping elements. 15 | default int[] skipAndGetNextArray(int skip, int n) { 16 | // Skip the specified number of elements. 17 | getArray(skip); 18 | 19 | return getArray(n); 20 | } 21 | 22 | // A private method that returns an array containing the next n elements. 23 | private int[] getArray(int n) { 24 | int[] vals = new int[n]; 25 | 26 | for (int i = 0; i < n; i++) vals[i] = getNext(); 27 | return vals; 28 | } 29 | 30 | void reset(); 31 | void setStart(int x); // set starting value 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/DynQueue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // A dynamic queue. 4 | public class DynQueue implements ICharQ { 5 | private char[] q; // this array holds the queue 6 | private int putloc, getloc; // the put and get indices 7 | 8 | // Construct an empty queue given its size. 9 | public DynQueue(int size) { 10 | q = new char[size]; // allocate memory for queue 11 | putloc = getloc = 0; 12 | } 13 | 14 | // Put a character into the queue. 15 | public void put(char ch) { 16 | if (putloc == q.length) { 17 | // increase queue size 18 | char[] t = new char[q.length * 2]; 19 | 20 | // copy elements into new queue 21 | for (int i = 0; i < q.length; i++) 22 | t[i] = q[i]; 23 | 24 | q = t; 25 | } 26 | 27 | q[putloc++] = ch; 28 | } 29 | 30 | // Get a character from the queue. 31 | public char get() { 32 | if (getloc == putloc) { 33 | System.out.println(" - Queue is empty."); 34 | return (char) 0; 35 | } 36 | 37 | return q[getloc++]; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/FailSoftArray.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Implements a "fail-soft" array which prevents runtime errors. 4 | public class FailSoftArray { 5 | private int a[]; // reference to array 6 | private int errval; // value to return if get() fails 7 | public int length; // length is public 8 | 9 | /** 10 | * Construct array given uts size and the value to return if get() fails. 11 | */ 12 | public FailSoftArray(int size, int errv) { 13 | a = new int[size]; 14 | errval = errv; 15 | length = size; 16 | } 17 | 18 | // Return value at given index. 19 | public int get(int index) { 20 | if (indexOK(index)) return a[index]; 21 | return errval; 22 | } 23 | 24 | // Put a value at an index. Return false on failure. 25 | public boolean put(int index, int val) { 26 | if (indexOK(index)) { 27 | a[index] = val; 28 | return true; 29 | } 30 | return false; 31 | } 32 | 33 | private boolean indexOK(int index) { 34 | if (index >= 0 & index < length) return true; 35 | return false; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/qpack/DynQueue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.qpack; 2 | 3 | // A dynamic queue. 4 | public class DynQueue implements ICharQ { 5 | private char[] q; // this array holds the queue 6 | private int putloc, getloc; // the put and get indices 7 | 8 | // Construct an empty queue given its size. 9 | public DynQueue(int size) { 10 | q = new char[size]; // allocate memory for queue 11 | putloc = getloc = 0; 12 | } 13 | 14 | // Put a character into the queue. 15 | public void put(char ch) { 16 | if (putloc == q.length) { 17 | // increase queue size 18 | char[] t = new char[q.length * 2]; 19 | 20 | // copy elements into new queue 21 | for (int i = 0; i < q.length; i++) 22 | t[i] = q[i]; 23 | 24 | q = t; 25 | } 26 | 27 | q[putloc++] = ch; 28 | } 29 | 30 | // Get a character from the queue. 31 | public char get() { 32 | if (getloc == putloc) { 33 | System.out.println(" - Queue is empty."); 34 | return (char) 0; 35 | } 36 | 37 | return q[getloc++]; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Outer.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Use an inner class. 4 | public class Outer { 5 | int[] nums; 6 | 7 | Outer(int[] n) { 8 | nums = n; 9 | } 10 | 11 | void analyze() { 12 | Inner inOb = new Inner(); 13 | 14 | System.out.println("Minimum: " + inOb.min()); 15 | System.out.println("Maximum: " + inOb.max()); 16 | System.out.println("Average: " + inOb.avg()); 17 | } 18 | 19 | // This is an inner class 20 | class Inner { 21 | int min() { 22 | int m = nums[0]; 23 | 24 | for (int i = 1; i < nums.length; i++) 25 | if (nums[i] < m) m = nums[i]; 26 | 27 | return m; 28 | } 29 | 30 | int max() { 31 | int m = nums[0]; 32 | 33 | for (int i = 1; i < nums.length; i++) 34 | if (nums[i] > m) m = nums[i]; 35 | 36 | return m; 37 | } 38 | 39 | int avg() { 40 | int a = 0; 41 | 42 | for (int i = 0; i < nums.length; i++) 43 | a += nums[i]; 44 | 45 | return a / nums.length; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/SDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class SDemo { 4 | public static void main(String[] args) { 5 | StaticDemo ob1 = new StaticDemo(); 6 | StaticDemo ob2 = new StaticDemo(); 7 | 8 | // Each object has its own copy of an instance variable. 9 | ob1.x = 10; 10 | ob2.x = 20; 11 | 12 | System.out.println("Of course, ob1.x and ob2.x are independent"); 13 | System.out.println("ob1.x: " + ob1.x + "\nob2.x: " + ob2.x); 14 | System.out.println(); 15 | 16 | // Each object share one copy of a static variable. 17 | System.out.println("The static variable y is shared."); 18 | StaticDemo.y = 19; 19 | System.out.println("Set StaticDemo.y to 19."); 20 | 21 | System.out.println("ob1.sum(): " + ob1.sum()); 22 | System.out.println("ob2.sum(): " + ob2.sum()); 23 | System.out.println(); 24 | 25 | StaticDemo.y = 100; 26 | System.out.println("Change StaticDemo.y to 100"); 27 | 28 | System.out.println("ob1.sum(): " + ob1.sum()); 29 | System.out.println("ob2.sum(): " + ob2.sum()); 30 | System.out.println(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/TwoDShape7.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // A multilevel hierarchy. 4 | public class TwoDShape7 { 5 | private double width; 6 | private double height; 7 | 8 | // A default constructor. 9 | TwoDShape7() { 10 | width = height = 0.0; 11 | } 12 | 13 | // Parameterized constructor. 14 | TwoDShape7(double w, double h) { 15 | width = w; 16 | height = h; 17 | } 18 | 19 | // Construct object with equal with and height 20 | TwoDShape7(double x) { 21 | width = height = x; 22 | } 23 | 24 | // Construct an object from an object. 25 | TwoDShape7(TwoDShape7 ob) { 26 | width = ob.width; 27 | height = ob.height; 28 | } 29 | 30 | 31 | // Accessor methods for with and height. 32 | double getWidth() { 33 | return width; 34 | } 35 | 36 | double getHeight() { 37 | return height; 38 | } 39 | void setWidth(double w) { 40 | width = w; 41 | } 42 | 43 | void setHeight(double h) { 44 | height = h; 45 | } 46 | 47 | void showDim() { 48 | System.out.println("Width and height are " + width + " and " + height); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/LengthDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Use the length array member. 4 | public class LengthDemo { 5 | public static void main(String[] args) { 6 | int[] list = new int[10]; 7 | int[] nums = {1, 2, 3}; 8 | int[][] table = { 9 | {1, 2, 3}, 10 | {4, 5}, 11 | {6, 7, 8, 9} 12 | }; 13 | 14 | System.out.println("length of list is " + list.length); 15 | System.out.println("length of nums is " + nums.length); 16 | System.out.println("length of table is " + table.length); 17 | System.out.println("length of table[0] is " + table[0].length); 18 | System.out.println("length of table[1] is " + table[1].length); 19 | System.out.println("length of table[2] is " + table[2].length); 20 | 21 | // use length to initialize list 22 | for (int i = 0; i < list.length; i++) { 23 | list[i] = i * i; 24 | } 25 | 26 | System.out.print("Here is list: "); 27 | // now use length to display list 28 | for (int i = 0; i < list.length; i++) 29 | System.out.print(list[i] + " "); 30 | System.out.println(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c7/Vehicle.java: -------------------------------------------------------------------------------- 1 | package com.guide.c7; 2 | 3 | // Try This 7-1 4 | // 5 | // Build a subclass of Vehicle for trucks. 6 | public class Vehicle { 7 | private int passengers; // number of passengers 8 | private int fuelcap; // fuel capacity in gallons 9 | private int mpg; // fuel consumption in miles per gallon 10 | 11 | Vehicle(int p, int f, int m) { 12 | passengers = p; 13 | fuelcap = f; 14 | mpg = m; 15 | } 16 | 17 | int range() { 18 | return fuelcap * mpg; 19 | } 20 | 21 | /** 22 | * Compute fuel needed for a given distance. 23 | */ 24 | double fuelneeded(int miles) { 25 | return (double) miles / mpg; 26 | } 27 | 28 | // Accessor methods for instance variables. 29 | int getPassengers() { 30 | return passengers; 31 | } 32 | 33 | void setPassengers(int p) { 34 | passengers = p; 35 | } 36 | 37 | int getFuelcap() { 38 | return fuelcap; 39 | } 40 | 41 | void setFuelcap(int f) { 42 | fuelcap = f; 43 | } 44 | 45 | int getMpg() { 46 | return mpg; 47 | } 48 | 49 | void setMpg(int m) { 50 | mpg = m; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c2/LogicalOpTable2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c2; 2 | 3 | // Try this 2-2: a truth table for the logical operators. 4 | // The program uses and displays 1's and 0's rather that true and false. 5 | public class LogicalOpTable2 { 6 | public static void main(String[] args) { 7 | int p, q; 8 | 9 | System.out.println("P\tQ\tAND\tOR\tXOR\tNOT"); 10 | 11 | p = 1; q = 1; 12 | System.out.print(p + "\t" + q + "\t"); 13 | System.out.print((p&q) + "\t" + (p|q) + "\t"); 14 | System.out.println((p^q) + "\t" + (p == 1 ? 0 : 1)); 15 | 16 | p = 1; q = 0; 17 | System.out.print(p + "\t" + q + "\t"); 18 | System.out.print((p&q) + "\t" + (p|q) + "\t"); 19 | System.out.println((p^q) + "\t" + (p == 1 ? 0 : 1)); 20 | 21 | p = 0; q = 1; 22 | System.out.print(p + "\t" + q + "\t"); 23 | System.out.print((p&q) + "\t" + (p|q) + "\t"); 24 | System.out.println((p^q) + "\t" + (p == 1 ? 0 : 1)); 25 | 26 | p = 0; q = 0; 27 | System.out.print(p + "\t" + q + "\t"); 28 | System.out.print((p&q) + "\t" + (p|q) + "\t"); 29 | System.out.println((p^q) + "\t" + (p == 1 ? 0 : 1)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/LocalClassDemo.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Use ShowBits as a local class. 4 | public class LocalClassDemo { 5 | public static void main(String[] args) { 6 | // An inner class version of ShowBits. 7 | class ShowBits53 { 8 | int numbits; 9 | 10 | ShowBits53(int n) { 11 | numbits = n; 12 | } 13 | 14 | void show(long val) { 15 | long mask = 1; 16 | 17 | // left-shift a 1 into the proper position 18 | mask <<= numbits-1; 19 | 20 | int spacer = 0; 21 | for (; mask != 0; mask >>>= 1) { 22 | if ((val & mask) != 0) System.out.print("1"); 23 | else System.out.print("0"); 24 | spacer++; 25 | if (spacer % 8 == 0) { 26 | System.out.print(" "); 27 | spacer = 0; 28 | } 29 | } 30 | System.out.println(); 31 | } 32 | } 33 | 34 | for (byte b = 0; b < 10; b++) { 35 | ShowBits53 byteval = new ShowBits53(8); 36 | System.out.print(b + " in binary: "); 37 | byteval.show(b); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/CircularQueue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8; 2 | 3 | // A circular queue. 4 | public class CircularQueue implements ICharQ { 5 | private char[] q; // this array hold the queue 6 | private int putloc, getloc; // the put and get indices 7 | 8 | // Construct an empty queue given its size. 9 | public CircularQueue(int size) { 10 | q = new char[size + 1]; 11 | putloc = getloc = 0; 12 | } 13 | 14 | // Put a character into the queue. 15 | public void put(char ch) { 16 | /* Queue is full if either putloc is one less than getloc, or if 17 | putloc is at the end of the array and getloc is at the beginning. */ 18 | if (putloc + 1 == getloc || ((putloc == q.length - 1) && (getloc == 0))) { 19 | System.out.println(" - Queue is full."); 20 | return; 21 | } 22 | 23 | q[putloc++] = ch; 24 | if (putloc == q.length) putloc = 0; // loop back 25 | } 26 | 27 | // Get a character from the queue. 28 | public char get() { 29 | if (getloc == putloc) { 30 | System.out.println(" - Queue is empty."); 31 | return (char) 0; 32 | } 33 | 34 | char ch = q[getloc++]; 35 | if (getloc == q.length) getloc = 0; // loop back 36 | return ch; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/QDemo2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Demonstrate the Queue2 class. 4 | public class QDemo2 { 5 | public static void main(String[] args) { 6 | // construct 10 element empty queue 7 | Queue2 q1 = new Queue2(10); 8 | 9 | char[] name = {'T', 'o', 'm'}; 10 | // construct queue from array 11 | Queue2 q2 = new Queue2(name); 12 | 13 | char ch; 14 | int i; 15 | 16 | // put some characters into q1 17 | for (i = 0; i < 10; i++) 18 | q1.put((char) ('A' + i)); 19 | 20 | // construct queue from another queue 21 | Queue2 q3 = new Queue2(q1); 22 | 23 | // Show the queues. 24 | System.out.print("Contents of q1: "); 25 | for (i = 0; i < 10; i++) { 26 | ch = q1.get(); 27 | System.out.print(ch); 28 | } 29 | 30 | System.out.println("\n"); 31 | 32 | System.out.print("Contents of q2: "); 33 | for (i = 0; i < 3; i++) { 34 | ch = q2.get(); 35 | System.out.print(ch); 36 | } 37 | 38 | System.out.println("\n"); 39 | 40 | System.out.print("Contents of q3: "); 41 | for (i = 0; i < 10; i++) { 42 | ch = q3.get(); 43 | System.out.print(ch); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c8/qpack/CircularQueue.java: -------------------------------------------------------------------------------- 1 | package com.guide.c8.qpack; 2 | 3 | // A circular queue. 4 | public class CircularQueue implements ICharQ { 5 | private char[] q; // this array hold the queue 6 | private int putloc, getloc; // the put and get indices 7 | 8 | // Construct an empty queue given its size. 9 | public CircularQueue(int size) { 10 | q = new char[size + 1]; 11 | putloc = getloc = 0; 12 | } 13 | 14 | // Put a character into the queue. 15 | public void put(char ch) { 16 | /* Queue is full if either putloc is one less than getloc, or if 17 | putloc is at the end of the array and getloc is at the beginning. */ 18 | if (putloc + 1 == getloc || ((putloc == q.length - 1) && (getloc == 0))) { 19 | System.out.println(" - Queue is full."); 20 | return; 21 | } 22 | 23 | q[putloc++] = ch; 24 | if (putloc == q.length) putloc = 0; // loop back 25 | } 26 | 27 | // Get a character from the queue. 28 | public char get() { 29 | if (getloc == putloc) { 30 | System.out.println(" - Queue is empty."); 31 | return (char) 0; 32 | } 33 | 34 | char ch = q[getloc++]; 35 | if (getloc == q.length) getloc = 0; // loop back 36 | return ch; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/SelfTest4.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | /** 4 | * * Demonstrate the Bubble sort for strings. 5 | */ 6 | public class SelfTest4 { 7 | public static void main(String[] args) { 8 | String[] numstrs = {"99", "-10", "100123", "18", "-978", "5623", "463", "-9", "287", "49"}; 9 | 10 | int a, b; 11 | String t; 12 | 13 | System.out.print("Original array is: "); 14 | for (int i = 0; i < numstrs.length; i++) 15 | System.out.print(" " + numstrs[i]); 16 | System.out.println(); 17 | 18 | for (a = 1; a < numstrs.length; a++) { 19 | for (b = numstrs.length - 1; b >= a; b--) { 20 | //System.out.println("Comparing [b] to [b - 1], result: " + numstrs[b] + " : " + numstrs[b - 1] + ", " + numstrs[b].compareTo(numstrs[b - 1])); 21 | if (numstrs[b].compareTo(numstrs[b - 1]) < 0) { 22 | t = numstrs[b - 1]; 23 | numstrs[b - 1] = numstrs[b]; 24 | numstrs[b] = t; 25 | } 26 | } 27 | } 28 | 29 | // display sorted array 30 | System.out.print("Sorted array is: "); 31 | for (int i = 0; i < numstrs.length; i++) 32 | System.out.print(" " + numstrs[i]); 33 | System.out.println(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/VarArgs3.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | // Varargs and overloading. 4 | public class VarArgs3 { 5 | static void vaTest(int ... v) { 6 | System.out.println("vaTest(int ... v): " + "Number of args: " + v.length); 7 | System.out.println("Contents: "); 8 | 9 | for (int i = 0; i < v.length; i++) 10 | System.out.println(" arg " + i + ": " + v[i]); 11 | 12 | System.out.println(); 13 | } 14 | 15 | static void vaTest(boolean ... v) { 16 | System.out.println("vaTest(boolean ... v): " + "Number of args: " + v.length); 17 | System.out.println("Contents: "); 18 | 19 | for (int i = 0; i < v.length; i++) 20 | System.out.println(" arg " + i + ": " + v[i]); 21 | 22 | System.out.println(); 23 | } 24 | 25 | static void vaTest(String msg, int ... v) { 26 | System.out.println("vaTest(String msg, int ... v) : " + "Number of args: " + msg + v.length); 27 | System.out.println("Contents: "); 28 | 29 | for (int i = 0; i < v.length; i++) 30 | System.out.println(" arg " + i + ": " + v[i]); 31 | 32 | System.out.println(); 33 | } 34 | 35 | public static void main(String[] args) { 36 | vaTest(1, 2, 3); 37 | vaTest("Testing: ", 10, 20); 38 | vaTest(true, false, false); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c3/Help.java: -------------------------------------------------------------------------------- 1 | package com.guide.c3; 2 | 3 | /** 4 | * Try this 3-1 5 | * 6 | * A simple help system. 7 | */ 8 | public class Help { 9 | public static void main(String[] args) 10 | throws java.io.IOException { 11 | char choice; 12 | 13 | System.out.println("Help on:"); 14 | System.out.println(" 1. if"); 15 | System.out.println(" 2. switch"); 16 | System.out.print("Choose one: "); 17 | choice = (char) System.in.read(); 18 | 19 | System.out.println("\n"); 20 | 21 | switch (choice) { 22 | case '1': 23 | System.out.println("The if:\n"); 24 | System.out.println("if(condition) statement;"); 25 | System.out.println("else statement;"); 26 | break; 27 | case '2': 28 | System.out.println("The switch:"); 29 | System.out.println("switch(expression) {"); 30 | System.out.println(" case constant:"); 31 | System.out.println(" statement sequence"); 32 | System.out.println(" break;"); 33 | System.out.println(" // ..."); 34 | System.out.println("}"); 35 | break; 36 | default: 37 | System.out.print("Selection not found."); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c6/Queue2.java: -------------------------------------------------------------------------------- 1 | package com.guide.c6; 2 | 3 | public class Queue2 { 4 | private char[] q; // this array holds the queue 5 | private int putloc, getloc; // the put and get indices 6 | 7 | Queue2(int size) { 8 | q = new char[size]; // allocate memory for queue 9 | putloc = getloc = 0; 10 | } 11 | 12 | Queue2(Queue2 ob) { 13 | putloc = ob.putloc; 14 | getloc = ob.getloc; 15 | q = new char[ob.q.length]; 16 | 17 | // copy elements 18 | for (int i = getloc; i < putloc; i++) { 19 | q[i] = ob.q[i]; 20 | } 21 | } 22 | 23 | // Construct a Queue with initial values. 24 | Queue2(char[] a) { 25 | putloc = 0; 26 | getloc = 0; 27 | q = new char[a.length]; 28 | 29 | for (int i = 0; i < a.length; i++) put(a[i]); 30 | } 31 | 32 | // put a character into the queue 33 | void put(char ch) { 34 | if (putloc == q.length) { 35 | System.out.println(" - Queue is full."); 36 | return; 37 | } 38 | 39 | q[putloc++] = ch; 40 | } 41 | 42 | // get a character from the queue 43 | char get() { 44 | if (getloc == putloc) { 45 | System.out.println(" - Queue is empty."); 46 | return (char) 0; 47 | } 48 | 49 | return q[getloc++]; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/Ragged.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Manually allocate differing size second dimensions. 4 | public class Ragged { 5 | public static void main(String[] args) { 6 | int[][] riders = new int[7][]; 7 | riders[0] = new int[10]; 8 | riders[1] = new int[10]; 9 | riders[2] = new int[10]; 10 | riders[3] = new int[10]; 11 | riders[4] = new int[10]; 12 | riders[5] = new int[2]; 13 | riders[6] = new int[2]; 14 | 15 | int i, j; 16 | 17 | // fabricate some fake data 18 | for (i = 0; i < 5; i++) 19 | for (j = 0; j < 10; j++) 20 | riders[i][j] = i + j + 10; 21 | 22 | for (i = 5; i < 7; i++) 23 | for (j = 0; j < 2; j++) 24 | riders[i][j] = i + j + 10; 25 | 26 | System.out.println("Riders per trip during the week:"); 27 | for (i = 0; i < 5; i++) { 28 | for (j = 0; j < 10; j++) 29 | System.out.println(riders[i][j] + " "); 30 | System.out.println(); 31 | } 32 | System.out.println(); 33 | 34 | System.out.println("Riders per trip on the weekend:"); 35 | for (i = 5; i < 7; i++) { 36 | for (j = 0; j < 2; j++) 37 | System.out.print(riders[i][j] + " "); 38 | System.out.println(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/AssignARef.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | import javax.sound.midi.SysexMessage; 4 | 5 | // Assigning array reference variables. 6 | public class AssignARef { 7 | public static void main(String[] args) { 8 | int i; 9 | 10 | int[] nums1 = new int[10]; 11 | int[] nums2 = new int[10]; 12 | 13 | for (i = 0; i < 10; i++) 14 | nums1[i] = i; 15 | 16 | for (i = 0; i < 10; i++) 17 | nums2[i] = -i; 18 | 19 | System.out.print("Here is nums1: "); 20 | for (i = 0; i < 10; i++) 21 | System.out.print(nums1[i] + " "); 22 | System.out.println(); 23 | 24 | System.out.print("Here is nums2: "); 25 | for (i = 0; i < 10; i++) 26 | System.out.print(nums2[i] + " "); 27 | System.out.println(); 28 | 29 | nums2 = nums1; // now nums2 ferers to nums1 30 | 31 | System.out.print("Here is nums2 after assignment: "); 32 | for (i = 0; i < 10; i++) 33 | System.out.print(nums2[i] + " "); 34 | System.out.println(); 35 | 36 | // now operate on nums1 array through nums2 37 | nums2[3] = 99; 38 | 39 | System.out.print("Here is nums1 after change through nums2: "); 40 | for (i = 0; i < 10; i++) 41 | System.out.print(nums1[i] + " "); 42 | System.out.println(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/guide/c5/SelfTest7.java: -------------------------------------------------------------------------------- 1 | package com.guide.c5; 2 | 3 | // Use XOR and an eight-character string to encode and decode a message. 4 | public class SelfTest7 { 5 | public static void main(String[] args) { 6 | String msg = "This is a test written by Pavel Rossinsky"; 7 | String encmsg = ""; 8 | String decmsg = ""; 9 | String key = "asdhzqta"; 10 | 11 | System.out.print("Original message: "); 12 | System.out.println(msg); 13 | System.out.println("its length: " + msg.length()); 14 | 15 | // encode the message 16 | for (int i = 0, j = 0; i < msg.length(); i++, j++) { 17 | if (j == key.length()) { 18 | j = 0; 19 | } 20 | // System.out.print("Key charAt: " + j + " " + key.charAt(j)); 21 | // System.out.print(" " + msg.charAt(i)); 22 | encmsg += ((char) (msg.charAt(i) ^ key.charAt(j))); 23 | } 24 | 25 | System.out.print("Encoded message: "); 26 | System.out.println(encmsg); 27 | 28 | // decode the message 29 | for (int i = 0, j = 0; i < encmsg.length(); i++, j++) { 30 | if (j == key.length()) { 31 | j = 0; 32 | } 33 | decmsg += ((char) (encmsg.charAt(i) ^ key.charAt(j))); 34 | } 35 | 36 | System.out.print("Decoded message: "); 37 | System.out.println(decmsg); 38 | } 39 | } 40 | --------------------------------------------------------------------------------