├── org └── javaturk │ └── oop │ ├── ch08 │ ├── packaging │ │ ├── packageX │ │ │ ├── ClassX.java │ │ │ └── ClassZ.java │ │ ├── packageA │ │ │ └── ClassA.java │ │ └── packageB │ │ │ └── ClassB.java │ ├── access │ │ ├── classAccess │ │ │ ├── ClassB.java │ │ │ ├── x │ │ │ │ ├── ClassZZ.java │ │ │ │ └── ClassXX.java │ │ │ └── ClassA.java │ │ └── memberAccess │ │ │ ├── p │ │ │ ├── ClassP.java │ │ │ └── ClassA.java │ │ │ └── ClassA.java │ ├── enums │ │ ├── Day.java │ │ ├── Days.java │ │ └── EnumTest.java │ ├── encapsulation │ │ ├── elevator │ │ │ ├── DataOnlyElevator.java │ │ │ ├── DataOnlyElevatorTest.java │ │ │ ├── EncapsulatedElevatorTest.java │ │ │ ├── UnEncapsulatedElevatorTest.java │ │ │ ├── UnEncapsulatedElevator.java │ │ │ └── EncapsulatedElevator.java │ │ └── calendar │ │ │ ├── Date.java │ │ │ ├── CalendarTest.java │ │ │ └── Calendar.java │ └── Bulp.java │ ├── ch09 │ ├── enums │ │ ├── Day.java │ │ ├── Days.java │ │ └── EnumTest.java │ ├── inheritance │ │ ├── ParentClass1.java │ │ ├── otherPackage │ │ │ └── ParentClass2.java │ │ ├── SuperExample.java │ │ └── InheritanceExample.java │ ├── shadowing │ │ ├── StaticOverridingExample.java │ │ ├── Parent.java │ │ ├── Child.java │ │ └── ShadowingExample.java │ ├── composition │ │ ├── CarTest.java │ │ ├── Wheel.java │ │ ├── Transmission.java │ │ ├── Engine.java │ │ ├── Door.java │ │ └── Car.java │ ├── factories │ │ └── factory1 │ │ │ ├── Director.java │ │ │ ├── Manager.java │ │ │ ├── Test.java │ │ │ └── Employee.java │ ├── factory │ │ ├── Director.java │ │ ├── Test.java │ │ ├── Manager.java │ │ └── Employee.java │ ├── initialization │ │ └── InitializationOrder.java │ ├── CompositionExample.java │ └── ObjectClass │ │ ├── EqualityExample.java │ │ └── ToStringExample.java │ ├── ch07 │ ├── university │ │ ├── Department.java │ │ ├── Student.java │ │ ├── Professor.java │ │ ├── Course.java │ │ └── Test.java │ ├── Deneme.java │ ├── thisKeyword │ │ ├── ThisDemo.java │ │ ├── bank │ │ │ ├── Bank.java │ │ │ ├── Account.java │ │ │ └── Customer.java │ │ ├── ThisTree.java │ │ └── Tree.java │ ├── car │ │ ├── CarAttributeOnlyComposite.java │ │ ├── Person.java │ │ ├── PersonAttributeOnlyComposite.java │ │ ├── Car.java │ │ ├── Test.java │ │ ├── CarAttributeOnly.java │ │ └── TestAttributeOnlyComposite.java │ ├── memory │ │ ├── StackDemo.java │ │ ├── Person.java │ │ ├── Car.java │ │ ├── HeapDemo.java │ │ ├── HeapDemo2.java │ │ └── SieveOfEratosthenes.java │ ├── tree │ │ ├── TreeWithNoConstructor.java │ │ ├── TreeWithDefaultConstructor.java │ │ ├── TreesWithoutReferences.java │ │ ├── TreeWithThis.java │ │ ├── TreeWithArgumentConstructor.java │ │ └── TreeWithOverloadedConstructors.java │ ├── ParameterPassing.java │ ├── StaticDemo.java │ ├── ObjectPassing.java │ ├── Tree.java │ ├── PrimitiveOverloading.java │ ├── init │ │ ├── InitializersDemo.java │ │ ├── InitializationOrder.java │ │ └── InitializerBlocks.java │ ├── InstanceVariablesInitialValues.java │ ├── Calculator.java │ ├── CalculatorOverloaded.java │ └── FinalCar.java │ ├── ch05 │ ├── ListCharacters.java │ ├── ListCharactersWithIf.java │ ├── DoWhileDemo.java │ ├── WhileDemo.java │ ├── ContinueDemo.java │ ├── BreakDemo.java │ ├── TernaryOperatorDemo.java │ ├── BreakAndContinue.java │ ├── StringInSwitchDemo.java │ ├── ReturnDemo.java │ ├── ForDemo.java │ ├── StatementProblem.java │ ├── BreakAndContinueWithLabelDemo.java │ ├── MonteCarloPI.java │ ├── IfElseDemo.java │ ├── SwitchDemo.java │ ├── AgeClassifier.java │ ├── ReversingANumber.java │ ├── UnreachableCode.java │ └── FizzBuzz.java │ ├── ch03 │ ├── unicode │ │ ├── UnicodeApplet.html │ │ ├── UnicodeApplet.java │ │ ├── BlockCanvas.java │ │ ├── UnicodeCharacters.java │ │ └── CharacterBlock.java │ ├── FinalVariableDemo.java │ ├── OctalAndHexadecimalDemo.java │ ├── ValuesOfExpressions.java │ ├── StringDemo.java │ ├── TurkceKarakterlerDemo.java │ ├── ScopeDemo.java │ ├── EscapesDemo.java │ ├── BlocksDemo.java │ ├── WideningConversionDemo.java │ ├── VariablesDemo.java │ ├── Underscores.java │ ├── NarrowingConversionDemo.java │ ├── NumericLiteralsWithUnderscores_7.java │ ├── UnicodeSelamDemo.java │ ├── EnvironmentDemo.java │ └── PrimitivesDemo.java │ ├── ch01 │ ├── Selam.java │ └── SelamTest.java │ ├── ch04 │ ├── Conversions.java │ ├── BitwiseLogicalOperators.java │ ├── ShiftOperators.java │ ├── ArithmeticConversion.java │ ├── RelationalOperators.java │ ├── OperatorPrecedence.java │ ├── UnaryOperators.java │ └── ConditionalOperators.java │ ├── ch02 │ ├── Car.java │ └── CarTest.java │ └── ch06 │ ├── ArrayDemo.java │ ├── MultiDimArray.java │ └── ArraysDemo.java └── README.md /org/javaturk/oop/ch08/packaging/packageX/ClassX.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.packaging.packageX; 2 | 3 | public class ClassX {} 4 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/access/classAccess/ClassB.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.access.classAccess; 2 | 3 | class ClassB { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/access/classAccess/x/ClassZZ.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.access.classAccess.x; 2 | 3 | class ClassZZ { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/packaging/packageX/ClassZ.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.packaging.packageX; 2 | 3 | public class ClassZ { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/access/classAccess/x/ClassXX.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.access.classAccess.x; 2 | 3 | public class ClassXX { 4 | 5 | ClassZZ z; 6 | } 7 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/enums/Day.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.enums; 2 | 3 | public enum Day { 4 | SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 5 | THURSDAY, FRIDAY, SATURDAY 6 | } 7 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/enums/Day.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.enums; 2 | 3 | public enum Day { 4 | SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 5 | THURSDAY, FRIDAY, SATURDAY 6 | } 7 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/university/Department.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.university; 2 | 3 | public class Department { 4 | String name; 5 | Professor head; 6 | Course[] courses; 7 | } 8 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/Deneme.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07; 2 | 3 | public class Deneme { 4 | 5 | 6 | public static void main(String[] args) { 7 | int i =8; 8 | 9 | System.out.println(i); 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/access/memberAccess/p/ClassP.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.access.memberAccess.p; 2 | 3 | public class ClassP { 4 | public int x = 10; 5 | int y = 20; 6 | protected int w = 30; 7 | private int z = 40; 8 | } 9 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/thisKeyword/ThisDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.thisKeyword; 2 | 3 | public class ThisDemo { 4 | int i = 0; 5 | int k = 5; 6 | 7 | {this.k = this.i;} 8 | 9 | int j = this.k; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/university/Student.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.university; 2 | 3 | public class Student { 4 | String no; 5 | String name; 6 | String dob; 7 | Department department; 8 | Professor advisor; 9 | Course[] coursesTaken; 10 | } 11 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/access/classAccess/ClassA.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.access.classAccess; 2 | 3 | import org.javaturk.oop.ch08.access.classAccess.x.ClassXX; 4 | 5 | public class ClassA { 6 | ClassB b = new ClassB(); 7 | 8 | ClassXX x; 9 | 10 | // ClassZZ z; 11 | } 12 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/car/CarAttributeOnlyComposite.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.car; 2 | 3 | 4 | public class CarAttributeOnlyComposite { 5 | String make; 6 | String model; 7 | String year; 8 | int speed; 9 | int distance; 10 | 11 | PersonAttributeOnlyComposite owner; 12 | } 13 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/university/Professor.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.university; 2 | 3 | public class Professor { 4 | String no; 5 | String name; 6 | String dob; 7 | String rank; 8 | Department department; 9 | Student[] advisee; 10 | Course[] coursesGiven; 11 | } 12 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/inheritance/ParentClass1.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.inheritance; 2 | 3 | class ParentClass1 { 4 | public int x = 10; 5 | int y = 20; 6 | protected int t = 30; 7 | private int z = 40; 8 | 9 | public void f() { 10 | System.out.println("f in ParentClass1"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Programming examples for OOP with Java. 2 | 3 | This is a repository that has all of the examples of OOP with Java course. 4 | The presentations and other materials can be found at www.javaturk.org address. 5 | 6 | Please contact me at akin@javaturk.org if you need further help. 7 | 8 | Thanks. 9 | 10 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/packaging/packageA/ClassA.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.packaging.packageA; 2 | 3 | public class ClassA { 4 | 5 | org.javaturk.oop.ch08.packaging.packageX.ClassX x; 6 | 7 | public ClassA(org.javaturk.oop.ch08.packaging.packageX.ClassX x) { 8 | this.x = x; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/shadowing/StaticOverridingExample.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.shadowing; 2 | 3 | public class StaticOverridingExample { 4 | 5 | public static void main(String[] args) { 6 | Parent.g(); 7 | Child.g(); 8 | 9 | // Parent p = new Child(); 10 | // p.g(); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/shadowing/Parent.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.shadowing; 2 | 3 | public class Parent { 4 | int i = 5; 5 | 6 | public void f() { 7 | System.out.println("f() in Parent"); 8 | } 9 | 10 | public static void g(){ 11 | System.out.println("g() in Parent"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/composition/CarTest.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.composition; 2 | 3 | public class CarTest { 4 | 5 | public static void main(String[] args) { 6 | Car car = new Car("Mercedes Benz", "CLK 200", "2013", 2); 7 | car.start(); 8 | car.accelerate(100); 9 | car.go(1000); 10 | car.stop(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/car/Person.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.car; 2 | 3 | public class Person { 4 | String tckn; 5 | String firstName; 6 | String lastName; 7 | Car car; 8 | 9 | public String getInfo(){ 10 | return "Person Info: " + tckn + " " + firstName + " " + lastName + " and has a car: " + car.getInfo(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/university/Course.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.university; 2 | 3 | import java.util.Date; 4 | 5 | public class Course { 6 | String code; 7 | String name; 8 | Department department; 9 | Professor teacher; 10 | Student[] students; 11 | String[] days; 12 | Date time; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/car/PersonAttributeOnlyComposite.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.car; 2 | 3 | public class PersonAttributeOnlyComposite { 4 | String tckn; 5 | String firstName; 6 | String lastName; 7 | 8 | CarAttributeOnlyComposite car; 9 | 10 | // public void taxiTut(CarAttributeOnlyComposite car) { 11 | // 12 | // } 13 | } 14 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/composition/Wheel.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.composition; 2 | 3 | class Wheel { 4 | private String name; 5 | 6 | public Wheel(String name) { 7 | this.name = name; 8 | System.out.println("A wheel is created: " + name); 9 | } 10 | 11 | public void rotate(){ 12 | System.out.println("Wheel is rotating."); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/inheritance/otherPackage/ParentClass2.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.inheritance.otherPackage; 2 | 3 | public class ParentClass2{ 4 | public int x = 10; 5 | int y = 20; 6 | protected int t = 30; 7 | private int z = 40; 8 | 9 | public void f() { 10 | System.out.println("f in ParentClass2"); 11 | } 12 | } -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/ListCharacters.java: -------------------------------------------------------------------------------- 1 | /* Ch 5 2 | * Class.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | public class ListCharacters { 8 | public static void main(String[] args) { 9 | 10 | for(char c = 0; c < 256; c++) 11 | System.out.println("value: " + (int)c + " character: " + c); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/ListCharactersWithIf.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch05; 2 | 3 | public class ListCharactersWithIf { 4 | 5 | public static void main(String[] args) { 6 | 7 | for (char c = 0; c < 256; c++) { 8 | System.out.print("\t" + (int) c + " : " + c); 9 | if (c % 10 == 0 & c != 0) 10 | System.out.print("\n"); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/access/memberAccess/p/ClassA.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.access.memberAccess.p; 2 | 3 | public class ClassA { 4 | public static void main(String[] args) { 5 | ClassP p = new ClassP(); 6 | 7 | System.out.println(p.x); 8 | System.out.println(p.y); 9 | System.out.println(p.w); 10 | // System.out.println(p.z); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/unicode/UnicodeApplet.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Unicode Applet 5 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/DoWhileDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 5 2 | * DoWhileLoop.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | public class DoWhileDemo { 8 | public static void main(String[] args) { 9 | 10 | double r = 0; 11 | 12 | do { 13 | r = Math.random(); 14 | System.out.println(r); 15 | }while(r < 0.8d); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/WhileDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 5 2 | * WhileLoop.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | public class WhileDemo { 8 | public static void main(String[] args) { 9 | 10 | double r = Math.random(); 11 | 12 | while(r < 0.8d) { 13 | r = Math.random(); 14 | System.out.println(r); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch01/Selam.java: -------------------------------------------------------------------------------- 1 | 2 | package org.javaturk.oop.ch01; 3 | 4 | /** 5 | * @author Akin Kaldiroglu 6 | *

7 | * For more info please @see http://www.javaturk.org 8 | *

9 | */ 10 | public class Selam { 11 | 12 | public String selamSoyle(String isim) { 13 | return "Selam " + isim + " :)"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch04/Conversions.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch04; 2 | 3 | public class Conversions { 4 | 5 | 6 | public static void main(String[] args) { 7 | // Unary conversions 8 | byte b = 3; 9 | // b = +b; // Unary conversion 10 | // b = -b; // Unary conversion 11 | // b = ~b; // Unary conversion 12 | 13 | // Binary conversions 14 | // b >> b; 15 | 16 | 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/ContinueDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 5 2 | * Continue.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | public class ContinueDemo { 8 | 9 | public static void main(String args[]) { 10 | for (int i = 1; i <= 5; ++i) { 11 | if (i == 4) 12 | continue; // Skip the rest for this iteration 13 | System.out.println(i + "\t" + Math.sqrt(i)); 14 | } // end of for loop 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/access/memberAccess/ClassA.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.access.memberAccess; 2 | 3 | import org.javaturk.oop.ch08.access.memberAccess.p.ClassP; 4 | 5 | public class ClassA { 6 | 7 | public static void main(String[] args) { 8 | ClassP p = new ClassP(); 9 | System.out.println(p.x); 10 | // System.out.println(p.y); 11 | // System.out.println(p.w); 12 | // System.out.println(p.z); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/enums/Days.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.enums; 2 | 3 | public class Days { 4 | public static void main(String[] args) { 5 | String nameOfDay = Day.FRIDAY.name(); 6 | System.out.println(nameOfDay); 7 | 8 | int ordinal = Day.TUESDAY.ordinal(); 9 | System.out.println(ordinal); 10 | 11 | Day[] days = Day.values(); 12 | for(Day day: days) 13 | System.out.println(day); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/enums/Days.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.enums; 2 | 3 | public class Days { 4 | public static void main(String[] args) { 5 | String nameOfDay = Day.FRIDAY.name(); 6 | System.out.println(nameOfDay); 7 | 8 | int ordinal = Day.TUESDAY.ordinal(); 9 | System.out.println(ordinal); 10 | 11 | Day[] days = Day.values(); 12 | for(Day day: days) 13 | System.out.println(day); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/shadowing/Child.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.shadowing; 2 | 3 | public class Child extends Parent { 4 | int i = 6; 5 | 6 | public void f() { 7 | System.out.println("f() in Child"); 8 | } 9 | 10 | public static void g(){ 11 | System.out.println("g() in Child"); 12 | } 13 | 14 | public void printInfo() { 15 | System.out.print("\nInfo: "); 16 | System.out.println(i); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/FinalVariableDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | public class FinalVariableDemo { 4 | 5 | public static void main(String[] args) { 6 | 7 | final int i = 8; 8 | //System.out.println(i); 9 | 10 | // Illegal 11 | // i = 7; 12 | // i = 9; 13 | 14 | // Following is not illegal 15 | final long l; 16 | 17 | l = 7; 18 | System.out.println(l); 19 | // l = 6; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/shadowing/ShadowingExample.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.shadowing; 2 | 3 | public class ShadowingExample { 4 | 5 | public static void main(String[] args) { 6 | Parent p = new Parent(); 7 | System.out.println(p.i); 8 | p.f(); 9 | 10 | Child c = new Child(); 11 | System.out.println(c.i); 12 | c.f(); 13 | c.printInfo(); 14 | 15 | // Parent p1 = new Child(); 16 | // System.out.println(p1.i); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/composition/Transmission.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.composition; 2 | 3 | public class Transmission { 4 | 5 | public Transmission() { 6 | System.out.println("A Transmission is created"); 7 | } 8 | 9 | public void forward(){ 10 | System.out.println("Transmission is is set for forward."); 11 | } 12 | 13 | public void backward(){ 14 | System.out.println("Transmission is is set for backward."); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/BreakDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 5 2 | * Breakdemo.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | public class BreakDemo { 8 | 9 | public static void main(String args[]) { 10 | for (int i = 1; i <= 5; ++i) { 11 | if (i == 4) 12 | break; // Terminate loop. Control to *** 13 | System.out.println(i + "\t" + Math.sqrt(i)); 14 | } // end of for loop 15 | System.out.println("I'm here."); 16 | } // *** 17 | } 18 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/factories/factory1/Director.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.factories.factory1; 2 | 3 | public class Director extends Manager { 4 | protected double bonus; 5 | 6 | public Director(int no, String name, int year, String workingDepartment, String managingDepartment, double bonus) { 7 | super(no, name, year, workingDepartment, managingDepartment); 8 | // System.out.println("in Director"); 9 | this.bonus = bonus; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/OctalAndHexadecimalDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | public class OctalAndHexadecimalDemo { 4 | 5 | 6 | public static void main(String[] args) { 7 | 8 | int sayi1 = 0132; 9 | int sayi2 = 0x5a; 10 | 11 | System.out.println(sayi1 + " " + sayi2); 12 | 13 | int sayi3 = sayi1 + sayi2; 14 | int sayi4 = sayi1 * sayi2; 15 | 16 | System.out.println(sayi3); 17 | System.out.println(sayi4); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/factories/factory1/Manager.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.factories.factory1; 2 | 3 | public class Manager extends Employee{ 4 | protected String departmentManaged; 5 | 6 | public static final int MANAGEMENT_PAYMENT = 3000; 7 | 8 | public Manager(int no, String name, int year, String workingDepartment, String departmentManaged){ 9 | super(no, name, year, workingDepartment); 10 | this.departmentManaged = departmentManaged; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch01/SelamTest.java: -------------------------------------------------------------------------------- 1 | 2 | package org.javaturk.oop.ch01; 3 | 4 | /** 5 | * @author Akin Kaldiroglu 6 | *

7 | * For more info please @see http://www.javaturk.org 8 | *

9 | */ 10 | 11 | public class SelamTest { 12 | 13 | public static void main(String[] args) { 14 | Selam selam = new Selam(); 15 | String cevap = selam.selamSoyle("Ali"); 16 | System.out.println(cevap); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/memory/StackDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.memory; 2 | 3 | public class StackDemo { 4 | 5 | static int callCount; 6 | 7 | public static void main(String[] args) { 8 | recursiveCall(); 9 | } 10 | 11 | public static void recursiveCall(){ 12 | // int i = 1024; 13 | String s = "Naber abi ansilsinf fsdkjfdskfsdkfhdsjfhdsfjghdgdfgfghdsghfja"; 14 | callCount++; 15 | if(callCount%100 == 0) 16 | System.out.println(callCount); 17 | recursiveCall(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/thisKeyword/bank/Bank.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.thisKeyword.bank; 2 | 3 | public class Bank { 4 | 5 | 6 | public static void main(String[] args) { 7 | Customer c1 = new Customer(1, "Fatma Sen"); 8 | Account acc1 = new Account(1, 1000.0, c1); 9 | 10 | // How to get rid of this line. Because programmers keep forgeting it 11 | // causing NullPointerException 12 | // c1.account = acc1; 13 | 14 | // How to make following call safe? 15 | c1.printInfo(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/ValuesOfExpressions.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | public class ValuesOfExpressions { 4 | 5 | public static void main(String[] args) { 6 | int k = 2147483647; 7 | 8 | int i = k + 1; 9 | System.out.println(i); 10 | 11 | // Not an unreachable code :) 12 | int a = 10; 13 | int m; 14 | if(a < 11) 15 | m = 7; 16 | 17 | System.out.println(); 18 | 19 | // System.out.println(m); 20 | 21 | // else 22 | // m = 9; 23 | // 24 | // System.out.println(m); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/factory/Director.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.factory; 2 | 3 | public class Director extends Manager { 4 | protected double bonus; 5 | 6 | public Director(int no, String name, int year, String workingDepartment, String managingDepartment, double bonus) { 7 | super(no, name, year, workingDepartment, managingDepartment); 8 | // System.out.println("in Director"); 9 | this.bonus = bonus; 10 | } 11 | 12 | public void printInfo(){ 13 | System.out.println("\nDirector Info"); 14 | super.printInfo(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/TernaryOperatorDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch05; 2 | 3 | public class TernaryOperatorDemo { 4 | 5 | public static void main(String[] args) { 6 | double random = Math.random(); 7 | 8 | String para = random > 0.5 ? "Tura" : "Yazı"; 9 | System.out.println(para); 10 | 11 | if(random > 0.5) 12 | para = "Tura"; 13 | else 14 | para = "Yazı"; 15 | 16 | 17 | int x = 5; 18 | int y = 2; 19 | 20 | int bigger = x > y ? x : y; 21 | 22 | System.out.println(bigger); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/packaging/packageB/ClassB.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.packaging.packageB; 2 | 3 | import org.javaturk.oop.ch08.packaging.packageX.ClassX; 4 | import org.javaturk.oop.ch08.packaging.packageX.ClassZ; 5 | 6 | // ya da 7 | 8 | import org.javaturk.oop.ch08.packaging.packageX.*; 9 | 10 | public class ClassB { 11 | ClassX x; 12 | ClassZ z; 13 | 14 | public ClassB(ClassX x) { 15 | this.x = x; 16 | } 17 | 18 | public ClassB(ClassX x, ClassZ z) { 19 | super(); 20 | this.x = x; 21 | this.z = z; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/composition/Engine.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.composition; 2 | 3 | class Engine { 4 | private int volume; 5 | private int horsePower; 6 | 7 | 8 | public Engine(int volume, int horsePower) { 9 | this.volume = volume; 10 | this.horsePower = horsePower; 11 | System.out.println("A engine is created. Volume is " + volume + " cc and horse power is " + horsePower); 12 | } 13 | 14 | public void start(){ 15 | System.out.println("Engine started."); 16 | } 17 | 18 | public void stop(){ 19 | System.out.println("Engine stopped."); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/tree/TreeWithNoConstructor.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.tree; 2 | 3 | public class TreeWithNoConstructor { 4 | private String type; 5 | private float height; 6 | 7 | public void printInfo(){ 8 | System.out.println("\nTreeWithNoConstructor Info:"); 9 | System.out.println("Type: " + type); 10 | System.out.println("Height: " + height); 11 | } 12 | 13 | public static void main(String[] args) { 14 | TreeWithNoConstructor tree = new TreeWithNoConstructor(); 15 | tree.printInfo(); 16 | tree.type = "Oak"; 17 | tree.height = 8.74f; 18 | tree.printInfo(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/car/Car.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.car; 2 | 3 | public class Car { 4 | String make; 5 | String model; 6 | String year; 7 | int speed; 8 | int distance; 9 | Person owner; 10 | 11 | public void go(int newDistance){ 12 | distance += newDistance; 13 | } 14 | 15 | public void accelerate(int newSpeed){ 16 | speed = newSpeed; 17 | } 18 | 19 | public void stop(){ 20 | speed = 0; 21 | } 22 | 23 | public String getInfo(){ 24 | return "Car Info: " + year + " " + make + " " + model + ". Distance: " + distance + " km. and traveling at " + speed + " kmph."; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/encapsulation/elevator/DataOnlyElevator.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.encapsulation.elevator; 2 | 3 | public class DataOnlyElevator { 4 | public byte currentFloor; 5 | public boolean doorOpen; 6 | 7 | public final int TOP_FLOOR = 7; 8 | public final int BOTTOM_FLOOR = -1; 9 | 10 | public void printInfo() { 11 | String doorStatus = null; 12 | if (doorOpen) 13 | doorStatus = "open"; 14 | else 15 | doorStatus = "closed"; 16 | 17 | System.out.println("Floor: " + currentFloor + " Door: " + doorStatus); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/thisKeyword/bank/Account.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.thisKeyword.bank; 2 | 3 | public class Account { 4 | int no; 5 | double balance; 6 | Customer owner; 7 | 8 | public Account(int no, double balance, Customer owner) { 9 | this.no = no; 10 | this.balance = balance; 11 | this.owner = owner; 12 | 13 | // Call Customer's setAccount method here to make sure that Customer know her account. 14 | // owner.setAccount(this); 15 | 16 | // printInfo(); 17 | } 18 | 19 | public void printInfo() { 20 | System.out.println("Account [no=" + no + ", balance=" + balance + "]"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/ParameterPassing.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07; 2 | 3 | public class ParameterPassing { 4 | 5 | public void f(A aa) { 6 | aa.j++; 7 | } 8 | 9 | public void f(int k) { 10 | k++; 11 | } 12 | 13 | public static void main(String[] args) { 14 | ParameterPassing o = new ParameterPassing(); 15 | 16 | // Passing a primitive 17 | int i = 5; 18 | o.f(i); 19 | System.out.println(i); 20 | 21 | // Passing a reference 22 | A a = new A(); 23 | System.out.println(a.j); 24 | o.f(a); 25 | System.out.println(a.j); 26 | o.f(a); 27 | } 28 | } 29 | 30 | class A { 31 | int j = 5; 32 | } 33 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch04/BitwiseLogicalOperators.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch04; 2 | 3 | public class BitwiseLogicalOperators { 4 | 5 | public static void main(String[] args) { 6 | int i = 4; 7 | int j = 8; 8 | 9 | int k = i & j; 10 | System.out.println(k); 11 | 12 | k = i | j; 13 | System.out.println(k); 14 | 15 | k = i ^ j; 16 | System.out.println(k); 17 | 18 | long u = 6; 19 | long v = 2; 20 | long w = u & v; 21 | System.out.println(w); 22 | 23 | byte m = 6; 24 | byte n = 2; 25 | byte l = (byte) (m & n); 26 | System.out.println(l); 27 | 28 | System.out.println(~l); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/factory/Test.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.factory; 2 | 3 | public class Test { 4 | 5 | public static void main(String[] args) { 6 | Employee e1 = new Employee(1, "Ali", 8, "Production"); 7 | e1.printInfo(); 8 | System.out.println("Maaşı: " + e1.calculateSalary()); 9 | 10 | Manager m1 = new Manager(2, "Fatma", 3, "Production", "Production"); 11 | m1.printInfo(); 12 | System.out.println("Maaşı: " + m1.calculateSalary()); 13 | 14 | Director d1 = new Director(4, "Mehmet", 20, "Management", "Management", 3000); 15 | d1.printInfo(); 16 | System.out.println("Maaşı: " + d1.calculateSalary()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/BreakAndContinue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BreakAndContinue.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | public class BreakAndContinue { 8 | public static void main(String[] args) { 9 | for (int i = 0; i < 100; i++) { 10 | if (i == 74) 11 | break; // Out of for loop 12 | if (i % 9 != 0) 13 | continue; // Next iteration 14 | System.out.println(i); 15 | } 16 | 17 | int i = 0; 18 | while (true) { 19 | i++; 20 | int j = i * 27; 21 | if (j == 1269) 22 | break; // Out of loop 23 | if (i % 10 != 0) 24 | continue; // Top of loop 25 | System.out.println(i); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/factories/factory1/Test.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.factories.factory1; 2 | 3 | public class Test { 4 | 5 | public static void main(String[] args) { 6 | Employee e1 = new Employee(1, "Ali", 8, "Production"); 7 | e1.printInfo(); 8 | System.out.println("Maaşı: " + e1.calculateSalary()); 9 | 10 | Manager m1 = new Manager(2, "Fatma", 3, "Production", "Production"); 11 | m1.printInfo(); 12 | System.out.println("Maaşı: " + m1.calculateSalary()); 13 | 14 | Director d1 = new Director(4, "Mehmet", 20, "Management", "Management", 3000); 15 | d1.printInfo(); 16 | System.out.println("Maaşı: " + d1.calculateSalary()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/memory/Person.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.memory; 2 | 3 | public class Person { 4 | String tckn; 5 | String firstName; 6 | String lastName; 7 | String address; 8 | String phone; 9 | String cellPhone; 10 | 11 | public Person(String tckn, String firstName, String lastName, String address, String phone, String cellPhone) { 12 | this.tckn = tckn; 13 | this.firstName = firstName; 14 | this.lastName = lastName; 15 | this.address = address; 16 | this.phone = phone; 17 | this.cellPhone = cellPhone; 18 | } 19 | 20 | 21 | public String getInfo(){ 22 | return "Person Info: " + tckn + " " + firstName + " " + lastName; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/StringDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | public class StringDemo { 4 | 5 | public static void main(String[] args) { 6 | 7 | String s1 = new String("http://www.JavaTurk.org"); 8 | String s2 = new String("http://java.sun.com"); 9 | 10 | System.out.println(s1 + " \t " + s2); 11 | 12 | String tmp = s1; 13 | s1 = s2; 14 | s2 = tmp; 15 | 16 | System.out.println(s1 + " \t " + s2); 17 | 18 | s2.concat(" naber?"); 19 | 20 | System.out.println(s1 + " \t " + s2); 21 | // 22 | // s1'i null yapınca sonraki iki satırda neler olur? 23 | s1 = null; 24 | System.out.println(s1); 25 | System.out.println(s1.charAt(0)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/factory/Manager.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.factory; 2 | 3 | public class Manager extends Employee{ 4 | protected String departmentManaged; 5 | 6 | public static final int MANAGEMENT_PAYMENT = 3000; 7 | 8 | public Manager(int no, String name, int year, String workingDepartment, String departmentManaged){ 9 | super(no, name, year, workingDepartment); 10 | this.departmentManaged = departmentManaged; 11 | } 12 | 13 | public double calculateSalary(){ 14 | return super.calculateSalary() + MANAGEMENT_PAYMENT; 15 | } 16 | 17 | public void printInfo(){ 18 | super.printInfo(); 19 | System.out.println("Managing Department: " + departmentManaged); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/StringInSwitchDemo.java: -------------------------------------------------------------------------------- 1 | 2 | package org.javaturk.oop.ch05; 3 | 4 | public class StringInSwitchDemo { 5 | 6 | public static void main(String[] args) { 7 | String dayInEnglish = args[0]; 8 | 9 | final String s = "sunday"; 10 | 11 | switch(dayInEnglish){ 12 | case "monday": System.out.println("Pazartesi"); break; 13 | case "tuesday": System.out.println("Sali"); break; 14 | case "wednesday": System.out.println("Çarsamba"); break; 15 | case "thursday": System.out.println("Persembe"); break; 16 | case "friday": System.out.println("Cuma"); break; 17 | case "saturday": System.out.println("Cumartesi"); break; 18 | case s: System.out.println("Pazar"); break; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/encapsulation/calendar/Date.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.encapsulation.calendar; 2 | 3 | public class Date { 4 | private int day; 5 | private String month; 6 | private int year; 7 | 8 | public int getDay() { 9 | return day; 10 | } 11 | public void setDay(int day) { 12 | this.day = day; 13 | } 14 | public String getMonth() { 15 | return month; 16 | } 17 | public void setMonth(String month) { 18 | this.month = month; 19 | } 20 | public int getYear() { 21 | return year; 22 | } 23 | public void setYear(int year) { 24 | this.year = year; 25 | } 26 | 27 | public String toString() { 28 | return "Date: " + day + " - " + month + " - " + year; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/thisKeyword/bank/Customer.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.thisKeyword.bank; 2 | 3 | public class Customer { 4 | int no; 5 | String name; 6 | Account account; 7 | 8 | public Customer(int no, String name, Account account) { 9 | this.no = no; 10 | this.name = name; 11 | this.account = account; 12 | } 13 | 14 | public Customer(int no, String name) { 15 | this.no = no; 16 | this.name = name; 17 | } 18 | 19 | public void printInfo(){ 20 | System.out.println("Customer [no=" + no + ", name=" + name +"]"); 21 | System.out.print("Account: "); 22 | account.printInfo(); 23 | } 24 | 25 | public void setAccount(Account account){ 26 | this.account = account; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/ReturnDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 5 2 | * ReturnExample.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | public class ReturnDemo { 8 | 9 | public static void main(String[] args) { 10 | System.out.println(max(10, 5)); 11 | System.out.println(max(5, 10)); 12 | System.out.println(max(5, 5)); 13 | } 14 | 15 | static int max(int value1, int value2) { 16 | if (value1 > value2) 17 | return +1; 18 | else if (value1 < value2) 19 | return -1; 20 | else 21 | return 0; // Match 22 | // Comment out following line 23 | // System.out.println("Metot sonu!"); 24 | } 25 | 26 | public void method1() { 27 | 28 | } 29 | 30 | public void method2() { 31 | return; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/composition/Door.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.composition; 2 | 3 | public class Door { 4 | private String name; 5 | private boolean closed; 6 | 7 | public Door(String name) { 8 | this.name = name; 9 | System.out.println(name + " door is created"); 10 | } 11 | 12 | public void open(){ 13 | System.out.println(name + " door is open."); 14 | } 15 | 16 | public void close(){ 17 | System.out.println(name + " door is closed."); 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public boolean isClosed() { 29 | return closed; 30 | } 31 | 32 | public void setClosed(boolean closed) { 33 | this.closed = closed; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/StaticDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07; 2 | 3 | public class StaticDemo { 4 | static int i = 10; 5 | 6 | public static void main(String args[]) { 7 | 8 | System.out.println(StaticDemo.i); 9 | 10 | StaticDemo.increment(); 11 | 12 | System.out.println(StaticDemo.i); 13 | 14 | StaticDemo st1 = new StaticDemo(); 15 | StaticDemo st2 = new StaticDemo(); 16 | 17 | // Unethical access :) 18 | System.out.println(st1.i); 19 | System.out.println(st2.i); 20 | 21 | // Unethical access :) 22 | st1.increment(); 23 | st2.increment(); 24 | 25 | System.out.println(st1.i); 26 | System.out.println(st2.i); 27 | System.out.println(StaticDemo.i); 28 | } 29 | 30 | static void increment() { 31 | i++; 32 | } 33 | } -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/ForDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 5 2 | * ForExample.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | public class ForDemo { 8 | 9 | public static void main(String[] args) { 10 | 11 | for (int i = 1, j = i + 10; (i < 5 & j > 2); i++, j = i * 2) { 12 | System.out.println("i= " + i + " j= " + j); 13 | } 14 | 15 | int t = 7; 16 | for (; t < 8; t += 2) { 17 | System.out.print(t + "*"); 18 | } 19 | System.out.println(t); 20 | 21 | // 22 | for (int k = 8; k < 8; k++) { 23 | System.out.println(k); 24 | } 25 | //Following for is ok but causes dead code. 26 | int i = 0; 27 | for (;;) { 28 | System.out.println(i); 29 | if (true) 30 | break; 31 | i++; // This is dead code. 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/ObjectPassing.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07; 2 | 3 | public class ObjectPassing { 4 | public static void main(String[] args) { 5 | ObjectPassing o = new ObjectPassing(); 6 | 7 | W w = new W(3, false); 8 | o.f(w); 9 | System.out.println("i of w is " + w.i + " and b of w is " + w.b); 10 | } 11 | 12 | public void f(W objectW) { 13 | objectW.i = 5; 14 | objectW.b = true; 15 | W ww = new W(8, true); 16 | objectW = ww; 17 | objectW.i = 12; 18 | objectW.b = false; 19 | System.out.println("i of objectW is " + objectW.i + " and b of objectW is " + objectW.b); 20 | } 21 | } 22 | 23 | class W { 24 | int i; 25 | boolean b; 26 | 27 | public W(int i, boolean b) { 28 | this.i = i; 29 | this.b = b; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/inheritance/SuperExample.java: -------------------------------------------------------------------------------- 1 | /* Ch 8 2 | * SuperExample.java 3 | */ 4 | 5 | package org.javaturk.oop.ch09.inheritance; 6 | 7 | public class SuperExample { 8 | public static void main(String args[]){ 9 | EE e = new EE(); 10 | System.out.println(e.x); 11 | e.print1(); 12 | 13 | FF f = new FF(); 14 | System.out.println(f.x); 15 | f.print1(); 16 | 17 | f.print2(); 18 | } 19 | } 20 | 21 | class EE{ 22 | int x = 10; 23 | public void print1(){ System.out.println("My name is EE");} 24 | } 25 | 26 | class FF extends EE{ 27 | int x = 20; 28 | public void print1(){ System.out.println("My name is FF");} 29 | public void print2(){ System.out.println(super.x); super.print1();} 30 | } 31 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/TurkceKarakterlerDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | /** 4 | * @author Akin Kaldiroglu 5 | *

6 | * For more info please @see http://www.javaturk.org 8 | *

9 | * This is an example that shows how a piece of Java code can be written 10 | * using Turkish letters. 11 | */ 12 | public class TurkceKarakterlerDemo { 13 | public static void main(String[] args) { 14 | int ı = 8; 15 | int ğ = 3; 16 | 17 | double ş = şunuHesaplaBakalım(ı, ğ); 18 | System.out.println(ş); 19 | } 20 | 21 | public static double şunuHesaplaBakalım(int ö, int ü) { 22 | int ı = ö + ü; 23 | double j = ö - ü; 24 | return ı * j; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/car/Test.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.car; 2 | 3 | public class Test { 4 | 5 | public static void main(String[] args) { 6 | Person person = new Person(); 7 | person.tckn = "1"; 8 | person.firstName = "Zeynep"; 9 | person.lastName = "Kaya"; 10 | // Following gives NullPointerException 11 | // String personInfo = person.getInfo(); 12 | // System.out.println(personInfo); 13 | 14 | Car car = new Car(); 15 | car.make = "Mercedes"; 16 | car.model = "E200"; 17 | car.year = "2011"; 18 | car.speed = 80; 19 | car.distance = 3; 20 | 21 | car.owner = person; 22 | person.car = car; 23 | 24 | car.speed = 100; 25 | car.go(10); 26 | System.out.println(car.getInfo()); 27 | System.out.println(person.getInfo()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/inheritance/InheritanceExample.java: -------------------------------------------------------------------------------- 1 | /* Ch 8 2 | * InheritanceExample.java 3 | */ 4 | 5 | package org.javaturk.oop.ch09.inheritance; 6 | 7 | public class InheritanceExample extends ParentClass1 { 8 | int u; 9 | 10 | public void g() { 11 | System.out.println("g in IE"); 12 | } 13 | 14 | public static void main(String args[]) { 15 | ParentClass1 pc = new ParentClass1(); 16 | pc.f(); 17 | pc.t = 6; 18 | 19 | InheritanceExample ie = new InheritanceExample(); 20 | ie.u = 7; 21 | ie.f(); 22 | ie.g(); 23 | System.out.println("x is " + ie.x); 24 | System.out.println("y is " + ie.y); // Only when they are in the same 25 | // package 26 | System.out.println("t is " + ie.t); 27 | // System.out.println("z is " + ie.z); // Does not compile 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/Tree.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07; 2 | 3 | public class Tree { 4 | int height; 5 | String type; 6 | 7 | public Tree() { 8 | System.out.println("Planting a seedling. Height: " + this.height); 9 | 10 | } 11 | 12 | public Tree(int height, String type) { 13 | this.height = height * 2; 14 | System.out.println("Creating a new Tree that is " + this.height + " meter tall"); 15 | 16 | } 17 | 18 | public static void main(String args[]) { 19 | Tree t = new Tree(); 20 | 21 | System.out.println("Height: " + t.height + "m."); 22 | 23 | for (int i = 0; i < 5; i++) { 24 | t = new Tree(i, "Cam"); 25 | System.out.println("Height: " + t.height + "m."); 26 | } 27 | 28 | System.out.println("Height: " + t.height + "m."); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/factory/Employee.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.factory; 2 | 3 | public class Employee{ 4 | protected int no; 5 | protected String name; 6 | protected int year; 7 | protected String department; 8 | 9 | public static final int BASE_SALARY = 500; 10 | 11 | public Employee(int no, String name, int year, String department){ 12 | this.no = no; 13 | this.name = name; 14 | this.year = year; 15 | this.department = department; 16 | } 17 | 18 | public double calculateSalary(){ 19 | return year * BASE_SALARY *1.05; 20 | } 21 | 22 | public void printInfo(){ 23 | System.out.println("\nNo: " + no); 24 | System.out.println("Name:" + name); 25 | System.out.println("Year: " + year); 26 | System.out.println("Department: " + department); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch02/Car.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch02; 2 | 3 | /** 4 | * @author Akin Kaldiroglu 5 | *

6 | * For more info please @see http://www.javaturk.org 7 | *

8 | */ 9 | public class Car { 10 | 11 | String make; 12 | String model; 13 | String year; 14 | int speed; 15 | int distance; 16 | 17 | public void go(int newDistance){ 18 | distance += newDistance; 19 | } 20 | 21 | public void accelerate(int newSpeed){ 22 | speed = newSpeed; 23 | } 24 | 25 | public void stop(){ 26 | speed = 0; 27 | } 28 | 29 | public String getInfo(){ 30 | return "Car Info: " + year + " " + make + " " + model + ". Distance: " + distance + " km. and traveling at " + speed + " kmph."; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/ScopeDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | public class ScopeDemo { 4 | // Instance variable 5 | private int i = 1; 6 | 7 | public static void main(String[] args) { 8 | int i = 2; 9 | // int ii; 10 | for (; i < 5; i++) { 11 | System.out.println(i); 12 | 13 | if (i < 3) { 14 | // Can't say this. 15 | // int i = 4; 16 | int ii = 3; 17 | f(ii); 18 | } else if (3 < i & i < 12) { 19 | int ii = 8; 20 | f(ii); 21 | 22 | } else { 23 | int ii = 17; 24 | f(ii); 25 | } 26 | } 27 | } 28 | 29 | // Empty statement 30 | ; 31 | 32 | public static void f(int i) { 33 | // Can't say this. 34 | // int i = 4; 35 | System.out.println(i); 36 | } 37 | 38 | public void ff() { 39 | System.out.println(i); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/EscapesDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | /** 4 | * @author Akin Kaldiroglu 5 | *

6 | * For more info please @see http://www.javaturk.org 7 | *

8 | * 9 | * This is an example that shows how the escape characters in Java are used. 10 | */ 11 | public class EscapesDemo { 12 | 13 | public static void main(String[] args) { 14 | 15 | System.out.println("Backspace: " + '\b'); 16 | System.out.println("Linefeed: " + '\n'); 17 | System.out.println("Form feed: " + '\f'); 18 | System.out.println("Carriage return: " + '\r'); 19 | System.out.println("Apostrophe-quote: " + '\''); 20 | System.out.println("Quotation mark: " + '\"'); 21 | System.out.println("Backslash: " + '\\'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/tree/TreeWithDefaultConstructor.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.tree; 2 | 3 | public class TreeWithDefaultConstructor { 4 | private String type; 5 | private float height; 6 | 7 | public TreeWithDefaultConstructor(){ 8 | type = "Pine"; 9 | height = 1.0f; 10 | } 11 | 12 | public void printInfo(){ 13 | System.out.println("\nTreeDefaultConstructor Info:"); 14 | System.out.println("Type: " + type); 15 | System.out.println("Height: " + height); 16 | } 17 | 18 | public static void main(String[] args) { 19 | TreeWithDefaultConstructor tree1 = new TreeWithDefaultConstructor(); 20 | // tree1.type = "Oak"; 21 | // tree1.height = 2; 22 | tree1.printInfo(); 23 | 24 | TreeWithDefaultConstructor tree2 = new TreeWithDefaultConstructor(); 25 | tree2.printInfo(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/factories/factory1/Employee.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.factories.factory1; 2 | 3 | public class Employee{ 4 | protected int no; 5 | protected String name; 6 | protected int year; 7 | protected String department; 8 | 9 | public static final int BASE_SALARY = 500; 10 | 11 | public Employee(int no, String name, int year, String department){ 12 | this.no = no; 13 | this.name = name; 14 | this.year = year; 15 | this.department = department; 16 | } 17 | 18 | public double calculateSalary(){ 19 | return year * BASE_SALARY *1.05; 20 | } 21 | 22 | public void printInfo(){ 23 | System.out.println("\nNo: " + no); 24 | System.out.println("Name:" + name); 25 | System.out.println("Year: " + year); 26 | System.out.println("Department: " + department); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/StatementProblem.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch05; 2 | 3 | public class StatementProblem { 4 | 5 | private int i = 1; 6 | 7 | public static void main(String[] args) { 8 | int i = 2; 9 | 10 | // int ii = 4; 11 | 12 | // if (i < 3) 13 | // int ii = 3; 14 | 15 | 16 | 17 | ; 18 | 19 | 20 | for (int j = 0; j < 10; j++) 21 | System.out.println(j); 22 | 23 | // for (int j = 0; j < 10; j++) 24 | // int k = j; 25 | // 26 | // 27 | // if(true) 28 | // int u = 7; 29 | // 30 | // for (int j = 0; j < 10; j++) 31 | // int k = j; 32 | // 33 | // if(true) 34 | // int z = 7; 35 | // 36 | // do 37 | // int r = 7; 38 | // while(true); 39 | } 40 | 41 | // public static void f(int i) { 42 | // if(i == 5) 43 | // int k = 8; 44 | // } 45 | } 46 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/BlocksDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | public class BlocksDemo { 4 | 5 | long l; 6 | 7 | // Declaration block 8 | { 9 | boolean b; 10 | } 11 | 12 | // Initialization block 13 | { 14 | l = 17L; 15 | } 16 | 17 | // Definition block 18 | { 19 | int i = 6; 20 | } 21 | 22 | // Method block 23 | public static void main(String[] args) { 24 | BlocksDemo bd = new BlocksDemo(); 25 | System.out.println(bd.l); 26 | 27 | { 28 | int t = 5; 29 | System.out.println(t); 30 | } 31 | 32 | } 33 | 34 | // Inner class block 35 | class InnerClass{} 36 | 37 | // Method block 38 | public void f(){ 39 | 40 | // Local class block 41 | new BlocksDemo(){ 42 | public void sop() { 43 | System.out.println(l); 44 | } 45 | }; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/BreakAndContinueWithLabelDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 5 2 | * BreakWithLabelDemo.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | public class BreakAndContinueWithLabelDemo { 8 | 9 | public static void main(String[] args) { 10 | int[][] arrayOfInts = { { 32, 87, 3, 589 }, { 12, 1076, 2000, 12 }, { 622, 127, 12, 955 } }; 11 | int searchfor = 12; 12 | 13 | int i = 0; 14 | int j = 0; 15 | boolean foundIt = false; 16 | 17 | search: for (; i < arrayOfInts.length; i++) { 18 | for (j = 0; j < arrayOfInts[i].length; j++) { 19 | 20 | if (arrayOfInts[i][j] == searchfor) { 21 | foundIt = true; 22 | System.out.println("Found " + searchfor + " at " + i + ", " + j); 23 | break search; 24 | } 25 | } 26 | } 27 | 28 | if (!foundIt) 29 | System.out.println(searchfor + "not in the array"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/encapsulation/calendar/CalendarTest.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.encapsulation.calendar; 2 | 3 | import java.util.Scanner; 4 | 5 | public class CalendarTest { 6 | 7 | public static void main(String[] args) { 8 | 9 | Scanner sc = new Scanner(System.in); 10 | 11 | Calendar calendar = new Calendar(); 12 | Date date = new Date(); 13 | 14 | 15 | while (true) { 16 | System.out.println("Please enter a date:"); 17 | System.out.print("Day as number: "); 18 | date.setDay(sc.nextInt()); 19 | System.out.print("Month as in \"January\": "); 20 | date.setMonth(sc.next()); 21 | System.out.print("Year: "); 22 | date.setYear(sc.nextInt()); 23 | Date nextDay = calendar.nextDay(date); 24 | System.out.println("Next day: " + nextDay.getDay() + " " + nextDay.getMonth() + " " + nextDay.getYear()); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/tree/TreesWithoutReferences.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.tree; 2 | 3 | public class TreesWithoutReferences { 4 | private String type; 5 | private float height; 6 | 7 | public TreesWithoutReferences(String newType, float newHeight) { 8 | System.out.println("in TreesWithoutReferences"); 9 | type = newType; 10 | height = newHeight; 11 | } 12 | 13 | public static void main(String[] args) { 14 | 15 | TreesWithoutReferences t = new TreesWithoutReferences("Pine", 5.0f); 16 | // t = null; 17 | 18 | System.out.println("Height: " + t.height + " m."); 19 | 20 | for (int i = 0; i < 5; i++) { 21 | t = new TreesWithoutReferences("Pine", i); 22 | System.out.println("Height: " + t.height + " m."); 23 | } 24 | 25 | System.out.println("Height: " + t.height + " m."); 26 | 27 | new TreesWithoutReferences("Pine", 5); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/thisKeyword/ThisTree.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.thisKeyword; 2 | 3 | public class ThisTree { 4 | 5 | private String type; 6 | private float height; 7 | 8 | public ThisTree(String type, float height) { 9 | this.type = type; 10 | this.height = height; 11 | } 12 | 13 | public void printInfo() { 14 | System.out.println("\nThisTree Info:"); 15 | System.out.println("Type: " + type); 16 | System.out.println("Height: " + height); 17 | } 18 | 19 | public static void main(String[] args) { 20 | ThisTree tree = new ThisTree("Cinar", 12.0f); 21 | tree.printInfo(); 22 | } 23 | 24 | public String getType() { 25 | return type; 26 | } 27 | 28 | public void setType(String type) { 29 | this.type = type; 30 | } 31 | 32 | public float getHeight() { 33 | return height; 34 | } 35 | 36 | public void setHeight(float height) { 37 | this.height = height; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/MonteCarloPI.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch05; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MonteCarloPI { 6 | public static void main(String[] args) { 7 | int dotsInCircle = 0; 8 | System.out.print("Number of points: "); 9 | Scanner in = new Scanner(System.in); 10 | int n = in.nextInt(); 11 | 12 | double start = System.currentTimeMillis(); 13 | for(int i = 0; i= 90) { 17 | grade = 'A'; 18 | } else if (score >= 80) { 19 | grade = 'B'; 20 | } else if (score >= 70) { 21 | grade = 'C'; 22 | } else if (score >= 60) { 23 | grade = 'D'; 24 | } else { // Comment out this else see what happens to grade variable 25 | grade = 'F'; 26 | } 27 | System.out.println("Your grade = " + grade); 28 | 29 | boolean i; 30 | 31 | if(i = 63 > 6) 32 | System.out.println("true"); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch04/ShiftOperators.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ShiftOperators.java 3 | * 4 | * This example shows the shift operators. 5 | */ 6 | 7 | package org.javaturk.oop.ch04; 8 | 9 | public class ShiftOperators { 10 | 11 | public static void main(String[] args) { 12 | int i = 512; 13 | i = i >> 1; 14 | System.out.println(i); 15 | 16 | i = i >> 3; 17 | System.out.println(i); 18 | 19 | i = i << 5; 20 | System.out.println(i); 21 | 22 | i = i >>> 1; 23 | System.out.println(i); 24 | 25 | i = -3; 26 | i = i << 3; 27 | System.out.println(i); 28 | 29 | i = i << 5; 30 | System.out.println(i); 31 | 32 | i = i >>> 8; 33 | System.out.println(i); 34 | 35 | long l1 = 9; 36 | l1 = l1 >> 2; 37 | System.out.println(l1); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/PrimitiveOverloading.java: -------------------------------------------------------------------------------- 1 | /* 2 | * PrimitiveOverloading.java 3 | */ 4 | 5 | package org.javaturk.oop.ch07; 6 | 7 | public class PrimitiveOverloading { 8 | static void f1(byte x) { prt("f1(byte)"); } 9 | static void f1(short x) { prt("f1(short)"); } 10 | static void f1(char x) { prt("f1(char)"); } 11 | static void f1(int x) { prt("f1(int)"); } 12 | static void f1(long x) { prt("f1(long)"); } 13 | static void f1(float x) { prt("f1(float)"); } 14 | static void f1(double x) { prt("f1(double)"); } 15 | 16 | static void prt(String s) { 17 | System.out.println(s); 18 | } 19 | 20 | public static void main(String[] args){ 21 | byte b = 3; f1(b); 22 | short s = 4; f1(s); 23 | char c = 'a'; f1(c); 24 | int i = 5; f1(i); 25 | long l = 5l; f1(l); 26 | float f = 5.0f; f1(f); 27 | double d = 6.0d; f1(d); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/WideningConversionDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 3 2 | * WideningConversionDemo.java 3 | */ 4 | 5 | package org.javaturk.oop.ch03; 6 | 7 | public class WideningConversionDemo { 8 | 9 | public static void main(String args[]){ 10 | byte b = 126; 11 | short s = 1000; 12 | char c = 65; 13 | int i = 2147483647; 14 | long l; 15 | float f = 3.14f; 16 | double d; 17 | /* 18 | c = b; // compile error: possible loss of precision 19 | c = s; // compile error: possible loss of precision 20 | i = d; // compile error: possible loss of precision*/ 21 | s = b; 22 | l = i; 23 | // Loss of precision 24 | f = i; 25 | System.out.println(f); 26 | 27 | l = i + 1; 28 | // Loss of precision 29 | f = l; 30 | System.out.println(f); 31 | d = f; 32 | i = c; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/initialization/InitializationOrder.java: -------------------------------------------------------------------------------- 1 | /* Ch 8 2 | * OrderOfConstructorCalls1.java 3 | */ 4 | 5 | package org.javaturk.oop.ch09.initialization; 6 | 7 | public class InitializationOrder extends B1 { 8 | private static ZZ zz = new ZZ(); 9 | private YY yy = new YY(); 10 | 11 | public InitializationOrder() { 12 | System.out.println("InitializationOrder constructor"); 13 | } 14 | 15 | public static void main(String[] args) { 16 | // InitializationOrder x = new InitializationOrder(); 17 | } 18 | } 19 | 20 | class A1 { 21 | private static YY yy = new YY(); 22 | A1() { 23 | System.out.println("A1 constructor"); 24 | } 25 | } 26 | 27 | class B1 extends A1 { 28 | private ZZ zz = new ZZ(); 29 | B1() { 30 | System.out.println("B1 constructor"); 31 | } 32 | } 33 | 34 | class YY { 35 | public YY() { 36 | System.out.println("in YY"); 37 | } 38 | } 39 | 40 | class ZZ { 41 | public ZZ() { 42 | System.out.println("in ZZ"); 43 | } 44 | } -------------------------------------------------------------------------------- /org/javaturk/oop/ch04/ArithmeticConversion.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch04; 2 | 3 | public class ArithmeticConversion { 4 | public static void main(String[] args) { 5 | byte b1 = 5; 6 | byte b2 = 8; 7 | // byte b3 = b1 * b2; // compile error: possible loss of precision 8 | byte b3 = (byte) (b1 * b2); // OK 9 | System.out.println(b3); 10 | 11 | int i = 12; 12 | double d = 8.524431; 13 | // int k = i * d; // compile error: possible loss of precision 14 | int k = (int) (i * d); 15 | System.out.println(k); 16 | 17 | short s1 = 187; 18 | short s2 = 8; 19 | // short s3 = s1 % s2; 20 | int s3 = s1 % s2; 21 | System.out.println(s3); 22 | 23 | byte b4 = 8; 24 | byte b5 = (byte) -b4; 25 | //-b4; 26 | 27 | // Compound assignments 28 | i = 5; 29 | 30 | i = i + 3; // i şimdi 8 31 | i += 3; // i şimdi 11 32 | System.out.println(i); 33 | 34 | i -= 4; // i şimdi 7; 35 | System.out.println(i); 36 | i *= 2; // i şimdi 14 37 | i /= 7; // i şimdi 2 38 | System.out.println(i); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/memory/Car.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.memory; 2 | 3 | public class Car { 4 | String country; 5 | String usage; 6 | String make; 7 | String model; 8 | String year; 9 | long speed; 10 | long distance; 11 | Person owner; 12 | 13 | 14 | 15 | 16 | 17 | public Car(String country, String usage, String make, String model, String year, long speed, long distance, 18 | Person owner) { 19 | this.country = country; 20 | this.usage = usage; 21 | this.make = make; 22 | this.model = model; 23 | this.year = year; 24 | this.speed = speed; 25 | this.distance = distance; 26 | this.owner = owner; 27 | } 28 | 29 | public void go(int newDistance){ 30 | distance += newDistance; 31 | } 32 | 33 | public void accelerate(int newSpeed){ 34 | speed = newSpeed; 35 | } 36 | 37 | public void stop(){ 38 | speed = 0; 39 | } 40 | 41 | public String getInfo(){ 42 | return "Car Info: " + year + " " + make + " " + model + ". Distance: " + distance + " km. and traveling at " + speed + " kmph."; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/CompositionExample.java: -------------------------------------------------------------------------------- 1 | 2 | package org.javaturk.oop.ch09; 3 | 4 | public class CompositionExample { 5 | public static void main(String args[]) { 6 | new Bicycle(); 7 | } 8 | } 9 | 10 | class Bicycle { 11 | private Wheel frontWheel; 12 | private Wheel rearWheel; 13 | private Seat seat; 14 | private Cadence cadence; 15 | 16 | public Bicycle() { 17 | frontWheel = new Wheel(); 18 | rearWheel = new Wheel(); 19 | seat = new Seat(); 20 | cadence = new Cadence(); 21 | System.out.println("A bicycle is created"); 22 | } 23 | 24 | public void go(){ 25 | if(cadence == null) 26 | cadence = new Cadence(); 27 | cadence.drive(); 28 | } 29 | } 30 | 31 | class Wheel { 32 | public Wheel() { 33 | System.out.println("A wheel is created"); 34 | } 35 | } 36 | 37 | class Seat { 38 | public Seat() { 39 | System.out.println("A seat is created"); 40 | } 41 | } 42 | 43 | class Cadence { 44 | public Cadence() { 45 | System.out.println("A cadence is created"); 46 | } 47 | 48 | public void drive(){ 49 | System.out.println("Cadence is driving!"); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/ObjectClass/EqualityExample.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.ObjectClass; 2 | 3 | public class EqualityExample { 4 | 5 | public static void main(String[] args) { 6 | 7 | Product p1 = new Product(112, "Kan epe", 169.0); 8 | Product p2 = new Product(11, "Kanepe", 170.0); 9 | // p1 = p2; 10 | // 11 | // if (p1 == p2) 12 | // System.out.println("The same"); 13 | // else 14 | // System.out.println("Different"); 15 | // // 16 | boolean b = p1.equals(p2); 17 | if (b) 18 | System.out.println("The same"); 19 | else 20 | System.out.println("Different"); 21 | } 22 | } 23 | 24 | class Product { 25 | int no; 26 | String name; 27 | double price; 28 | 29 | public Product(int no, String name, double price) { 30 | super(); 31 | this.no = no; 32 | this.name = name; 33 | this.price = price; 34 | new Object().equals(new Object()); 35 | } 36 | 37 | public boolean equals(Product p1){ 38 | boolean b = false; 39 | // Product p1 = (Product) o; 40 | if(no == p1.no) 41 | b = true; 42 | 43 | return b; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/init/InitializersDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.init; 2 | 3 | public class InitializersDemo { 4 | 5 | int minAge = 18; 6 | static double percentage = 5.8; 7 | boolean isOpen = isDoorOpen(); 8 | 9 | // No forward references! 10 | // int i = j; 11 | // AA a = new AA(j); 12 | // int j = 8; 13 | 14 | // But following is ok and it may cause a inconsistent state 15 | 16 | int i = getJ(); 17 | // { 18 | // System.out.println("In initialization block."); 19 | // System.out.println("i: " + i); 20 | // } 21 | int j = 8; 22 | 23 | // getD must also be static 24 | static double pi = getPi(); 25 | 26 | public static void main(String[] args) { 27 | InitializersDemo demo = new InitializersDemo(); 28 | System.out.println("i: " + demo.i); 29 | System.out.println("isOpen: " + demo.isOpen); 30 | } 31 | 32 | private static double getPi() { 33 | return Math.PI; 34 | } 35 | 36 | private boolean isDoorOpen() { 37 | return true; 38 | } 39 | 40 | public int getJ(){ 41 | return j; 42 | } 43 | } 44 | 45 | class AA { 46 | public AA(int i){} 47 | } 48 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch04/RelationalOperators.java: -------------------------------------------------------------------------------- 1 | /* Ch 4 2 | * BooleanOperators.java 3 | */ 4 | 5 | package org.javaturk.oop.ch04; 6 | 7 | import java.util.*; 8 | 9 | public class RelationalOperators { 10 | 11 | public static void main(String[] args) { 12 | 13 | Random rand = new Random(); 14 | int i = rand.nextInt() % 100; 15 | int j = rand.nextInt() % 100; 16 | 17 | prt("i = " + i); 18 | prt("j = " + j); 19 | prt("i > j is " + (i > j)); 20 | prt("i < j is " + (i < j)); 21 | prt("i >= j is " + (i >= j)); 22 | prt("i <= j is " + (i <= j)); 23 | prt("i == j is " + (i == j)); 24 | prt("i != j is " + (i != j)); 25 | 26 | boolean b1 = true; 27 | boolean b2 = false; 28 | if (b1 == b2) 29 | prt("The same"); 30 | else 31 | prt("Different"); 32 | 33 | if (b1) 34 | prt("true!"); 35 | 36 | String s1 = new String("String"); 37 | String s2 = new String("String"); 38 | // s1 = s2; 39 | if (s1 == s2) 40 | prt("The same"); 41 | else 42 | prt("Different"); 43 | 44 | } 45 | 46 | static void prt(String s) { 47 | System.out.println(s); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/InstanceVariablesInitialValues.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07; 2 | 3 | public class InstanceVariablesInitialValues { 4 | boolean t; 5 | char c; 6 | byte b; 7 | short s; 8 | int i; 9 | long l; 10 | float f; 11 | double d; 12 | String str; 13 | 14 | void print() { 15 | System.out.println( 16 | "Data type Initial value\n" + 17 | "boolean " + t + "\n" + 18 | "char [" + c + "] "+ (int)c +"\n"+ 19 | "byte " + b + "\n" + 20 | "short " + s + "\n" + 21 | "int " + i + "\n" + 22 | "long " + l + "\n" + 23 | "float " + f + "\n" + 24 | "double " + d + "\n" + 25 | "String " + str); 26 | } 27 | 28 | public static void main(String[] args) { 29 | // new InstanceVariablesInitialValues().print(); 30 | 31 | // int i; 32 | // System.out.println(i); 33 | 34 | InstanceVariablesInitialValues nesne = new InstanceVariablesInitialValues(); 35 | nesne.print(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/encapsulation/elevator/DataOnlyElevatorTest.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.encapsulation.elevator; 2 | 3 | /** 4 | * What is wrong with this approach? 5 | */ 6 | 7 | public class DataOnlyElevatorTest { 8 | 9 | public static void main(String[] args) { 10 | System.out.println("Creating DataOnlyElevator"); 11 | DataOnlyElevator elevator = new DataOnlyElevator(); 12 | 13 | elevator.printInfo(); 14 | // At floor 0 and waiting 15 | elevator.currentFloor = -124; 16 | elevator.doorOpen = false; 17 | elevator.printInfo(); 18 | 19 | // Loading for floor 5 20 | elevator.doorOpen = true; 21 | elevator.printInfo(); 22 | // elevator.doorOpen = false; 23 | elevator.currentFloor = 5; 24 | elevator.printInfo(); 25 | elevator.doorOpen = true; 26 | elevator.doorOpen = false; 27 | 28 | elevator.currentFloor = 0; 29 | elevator.printInfo(); 30 | 31 | elevator.currentFloor = 12; 32 | elevator.currentFloor = -4; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/VariablesDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | /** 4 | * @author Akin Kaldiroglu 5 | *

6 | * For more info please @see http://www.javaturk.org 8 | *

9 | * 10 | * This is an example that shows how variables of different types are 11 | * declared, initialized and defined. 12 | */ 13 | public class VariablesDemo { 14 | 15 | public static void main(String[] args) { 16 | 17 | // Declaring a variable 18 | int i; 19 | boolean b; 20 | String s1; 21 | // Do not use a variable before defining it! 22 | // System.out.println(i + " " + b + " " + s1); 23 | 24 | // Initializing a variable 25 | i = 8; 26 | b = false; 27 | s1 = "javaTurk.org"; 28 | 29 | // After they are defined and we can use them. 30 | System.out.println(i + " " + b + " " + s1); 31 | 32 | // Defining a variable 33 | int j = 18; 34 | float sin30 = 0.6f; 35 | String s2 = new String("Selam :)"); 36 | System.out.println(j + " " + sin30 + " " + s2); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/Underscores.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | public class Underscores { 4 | 5 | public static void main(String[] args) { 6 | 7 | int anInt = 10_000_000; 8 | System.out.println(anInt); 9 | 10 | int anotherInt = 10_00_00; 11 | System.out.println(anotherInt); 12 | 13 | int otherInt = 10__0_0____0_0; 14 | System.out.println(otherInt); 15 | 16 | int otherInt2 = 1________0; 17 | System.out.println(otherInt2); 18 | 19 | int otherInt3 = 0________0; 20 | System.out.println(otherInt3); 21 | 22 | long aLong = 2_147_483_648L; 23 | System.out.println(aLong); 24 | 25 | float aFloat = 3.14_15F; 26 | System.out.println(aFloat); 27 | 28 | double aDouble = 3.1_4_1_5; 29 | System.out.println(aDouble); 30 | 31 | 32 | // int otherInt2 = 10__0_0____0_0_; 33 | // int otherInt2 = _10__0_0____0_0; 34 | int otherInt6 = 10________0; 35 | int otherInt5 = 010________0; 36 | int otherInt4 = 10________0; 37 | 38 | int aBinary = 0b0001_1100__0011_0111__0010_1011__1010_0011; 39 | System.out.println(aBinary); 40 | 41 | System.out.println(Integer.parseInt("00011100001101110010101110100011", 2)); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/enums/EnumTest.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.enums; 2 | 3 | public class EnumTest { 4 | Day day; 5 | 6 | public EnumTest(Day day) { 7 | this.day = day; 8 | } 9 | 10 | public void tellItLikeItIs() { 11 | switch (day) { 12 | case MONDAY: 13 | System.out.println("Mondays are bad."); 14 | break; 15 | 16 | case FRIDAY: 17 | System.out.println("Fridays are better."); 18 | break; 19 | 20 | case SATURDAY: 21 | case SUNDAY: 22 | System.out.println("Weekends are best."); 23 | break; 24 | 25 | default: 26 | System.out.println("Midweek days are so-so."); 27 | break; 28 | } 29 | } 30 | 31 | public static void main(String[] args) { 32 | EnumTest firstDay = new EnumTest(Day.MONDAY); 33 | firstDay.tellItLikeItIs(); 34 | EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); 35 | thirdDay.tellItLikeItIs(); 36 | EnumTest fifthDay = new EnumTest(Day.FRIDAY); 37 | fifthDay.tellItLikeItIs(); 38 | EnumTest sixthDay = new EnumTest(Day.SATURDAY); 39 | sixthDay.tellItLikeItIs(); 40 | EnumTest seventhDay = new EnumTest(Day.SUNDAY); 41 | seventhDay.tellItLikeItIs(); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/enums/EnumTest.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.enums; 2 | 3 | public class EnumTest { 4 | Day day; 5 | 6 | public EnumTest(Day day) { 7 | this.day = day; 8 | } 9 | 10 | public void tellItLikeItIs() { 11 | switch (day) { 12 | case MONDAY: 13 | System.out.println("Mondays are bad."); 14 | break; 15 | 16 | case FRIDAY: 17 | System.out.println("Fridays are better."); 18 | break; 19 | 20 | case SATURDAY: 21 | case SUNDAY: 22 | System.out.println("Weekends are best."); 23 | break; 24 | 25 | default: 26 | System.out.println("Midweek days are so-so."); 27 | break; 28 | } 29 | } 30 | 31 | public static void main(String[] args) { 32 | EnumTest firstDay = new EnumTest(Day.MONDAY); 33 | firstDay.tellItLikeItIs(); 34 | EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); 35 | thirdDay.tellItLikeItIs(); 36 | EnumTest fifthDay = new EnumTest(Day.FRIDAY); 37 | fifthDay.tellItLikeItIs(); 38 | EnumTest sixthDay = new EnumTest(Day.SATURDAY); 39 | sixthDay.tellItLikeItIs(); 40 | EnumTest seventhDay = new EnumTest(Day.SUNDAY); 41 | seventhDay.tellItLikeItIs(); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/NarrowingConversionDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 3 2 | * NarrowingConversion.java 3 | */ 4 | 5 | package org.javaturk.oop.ch03; 6 | 7 | public class NarrowingConversionDemo { 8 | 9 | public static void main(String args[]) { 10 | byte b = 0; 11 | short s = 1000; 12 | char c; 13 | int i = 100; 14 | long l = 10000000000l; 15 | float f = 65.999999f; 16 | double d = 3.14353459485483d; 17 | 18 | // b = s; // compile error: possible loss of precision 19 | // c = i; // compile error: possible loss of precision 20 | // f = d; // compile error: possible loss of precision 21 | // System.out.println(f); 22 | 23 | b = (byte) s; 24 | System.out.println("s: " + s + "\t\tb: " + b); 25 | i = (int) l; 26 | System.out.println("l: " + l + "\t\ti: " + i); 27 | i = (int) f; 28 | System.out.println("f: " + f + "\t\ti: " + i); 29 | l = (long) f; 30 | System.out.println("f: " + f + "\t\tl: " + l); 31 | f = (float) d; 32 | System.out.println("d: " + d + "\t\tf: " + f); 33 | c = (char) i; 34 | System.out.println("i: " + i + "\t\tc: " + c); 35 | i = (int) d; 36 | System.out.println("i: " + i + "\t\td: " + d); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/NumericLiteralsWithUnderscores_7.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | public class NumericLiteralsWithUnderscores_7 { 4 | 5 | public static void main(String[] args) { 6 | 7 | // This is binary literal starting with 0b or 0B 8 | byte byte1 = 0b0000_1111; 9 | System.out.println(byte1); 10 | 11 | byte byte2 = 0B0000_1111; 12 | System.out.println(byte2); 13 | 14 | int b1 = 0b111001001001; 15 | System.out.println(b1); 16 | 17 | int b2 = 0B1110_0100_1001; 18 | System.out.println(b2); 19 | 20 | short aShort = (short)0b1010_0001_0100_0101; 21 | System.out.println(aShort); 22 | 23 | int anInt = 10_000_000; 24 | System.out.println(anInt); 25 | 26 | long aLong = 2_147_483_648L; 27 | System.out.println(aLong); 28 | 29 | int intBinary = 0b0001_1100__0011_0111__0010_1011__1010_0011; 30 | System.out.println(intBinary); 31 | 32 | System.out.println(Integer.parseInt("00011100001101110010101110100011", 2)); 33 | 34 | long longBinary = 0b1111_0001_1100__0011_0111__0010_1011__1010_0011L; 35 | System.out.println(longBinary); 36 | 37 | System.out.println(Long.parseLong("111100011100001101110010101110100011", 2)); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/encapsulation/elevator/EncapsulatedElevatorTest.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.encapsulation.elevator; 2 | 3 | public class EncapsulatedElevatorTest { 4 | 5 | public static void main(String[] args) { 6 | System.out.println("Creating EncapsulatedElevator"); 7 | 8 | EncapsulatedElevator elevator = new EncapsulatedElevator(); 9 | 10 | elevator.openDoor(); 11 | elevator.closeDoor(); 12 | elevator.openDoor(); 13 | elevator.goDown(); 14 | elevator.goUp(); 15 | elevator.goUp(); 16 | elevator.openDoor(); 17 | elevator.closeDoor(); 18 | elevator.goDown(); 19 | elevator.openDoor(); 20 | elevator.goDown(); 21 | elevator.openDoor(); 22 | elevator.goDown(); 23 | elevator.goDown(); 24 | 25 | int curFloor = elevator.getFloor(); 26 | 27 | if (curFloor != 5 && !elevator.getDoorStatus()) { 28 | elevator.setFloor(5); 29 | } 30 | 31 | elevator.setFloor(10); 32 | elevator.openDoor(); 33 | 34 | // elevator.currentFloor = 19; 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/ObjectClass/ToStringExample.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.ObjectClass; 2 | 3 | public class ToStringExample { 4 | 5 | public static void main(String[] args) { 6 | 7 | Car c1 = new Car(2012, "Mercedes", "CLK200", 450); 8 | System.out.println(c1); 9 | // System.out.println(c1.toString()); 10 | System.out.println(c1.hashCode()); 11 | System.out.println(c1.getClass().getName()); 12 | 13 | Car c2 = new Car(2006, "Skoda", "Fabia", 70); 14 | System.out.println(c2); 15 | } 16 | } 17 | 18 | class Car{ 19 | int year; 20 | String brand; 21 | String model; 22 | int power; 23 | 24 | 25 | public Car(int year, String brand, String model, int power) { 26 | super(); 27 | this.year = year; 28 | this.brand = brand; 29 | this.model = model; 30 | this.power = power; 31 | } 32 | 33 | public String toString() { 34 | String s = new String(); 35 | s += "Car Info\n"; 36 | s += "Year: " + year + "\n"; 37 | s += "Brand: " + brand + "\n"; 38 | s += "Model: " + model + "\n"; 39 | s += "Power: " + power + " HP \n"; 40 | 41 | return s; 42 | // return year + " " + brand + " " + model + " " + power; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch02/CarTest.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch02; 2 | 3 | /** 4 | * @author Akin Kaldiroglu 5 | *

6 | * For more info please @see http://www.javaturk.org 7 | *

8 | */ 9 | public class CarTest { 10 | 11 | public static void main(String[] args) { 12 | 13 | Car car1 = new Car(); 14 | car1.make = "Mercedes"; 15 | car1.model = "C200"; 16 | car1.year = "2012"; 17 | car1.distance = 0; 18 | car1.speed = 0; 19 | 20 | String infoCar1 = car1.getInfo(); 21 | System.out.println("1: " + infoCar1); 22 | 23 | car1.accelerate(80); 24 | car1.go(10); 25 | 26 | infoCar1 = car1.getInfo(); 27 | System.out.println("2: " + infoCar1); 28 | 29 | car1.accelerate(120); 30 | car1.go(20); 31 | 32 | infoCar1 = car1.getInfo(); 33 | System.out.println("3: " + infoCar1); 34 | 35 | 36 | Car car2 = new Car(); 37 | car2.make = "Ferrari"; 38 | car2.model = "Enzo"; 39 | car2.year = "2012"; 40 | car2.distance = 10; 41 | car2.speed = 240; 42 | String infoCar2 = car2.getInfo(); 43 | System.out.println("4: " + infoCar2); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/UnicodeSelamDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | /** 4 | * @author Akin Kaldiroglu 5 | *

6 | * For more info please @see http://www.javaturk.org 8 | *

9 | * This is an example that shows how the Unicode characters are used in 10 | * Java. It prints "Hello" in Japanese and Arabic. Notice the escape 11 | * characters in the code. 12 | */ 13 | public class UnicodeSelamDemo { 14 | 15 | public static void main(String[] args) { 16 | String selamInJapanese = "\u4eca\u65e5\u306f\u4e16\u754c"; 17 | System.out.println("Japonca da Selam Dünya " + selamInJapanese); 18 | // Notice \n 19 | System.out.print("Ya da\n"); 20 | 21 | char ch1 = '\u4eca'; 22 | char ch2 = '\u65e5'; 23 | char ch3 = '\u306f'; 24 | char ch4 = '\u4e16'; 25 | char ch5 = '\u754c'; 26 | 27 | System.out.println("Japonca da Selam Dünya: \t" + ch1 + ch2 + ch3 + ch4 + ch5); 28 | 29 | String selamInArabic = "\u0633\u0644\u0627\u0645"; 30 | // Notice \t 31 | System.out.println("Arapça Selam: \t" + selamInArabic); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/unicode/UnicodeApplet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UnicodeApplet.java 3 | */ 4 | 5 | package org.javaturk.oop.ch03.unicode; 6 | 7 | import java.awt.*; 8 | import java.awt.event.*; 9 | import java.applet.*; 10 | 11 | public class UnicodeApplet extends Applet implements ItemListener { 12 | List scripts = new List(CharacterBlock.getNumBlocks()); 13 | BlockCanvas theChart = new BlockCanvas(); 14 | 15 | public void init() { 16 | setLayout(new BorderLayout()); 17 | String[] names = CharacterBlock.getBlockNames(); 18 | for (int i = 0; i < names.length; i++) { 19 | scripts.add(names[i]); 20 | } 21 | scripts.addItemListener(this); 22 | scripts.select(0); 23 | this.add("West", scripts); 24 | this.add("Center", theChart); 25 | } 26 | 27 | public void itemStateChanged(ItemEvent ie) { 28 | if (ie.getStateChange() == ItemEvent.SELECTED) { 29 | String selected = scripts.getSelectedItem(); 30 | if (!selected.equals(theChart.getBlock().getName())) { 31 | theChart.setBlock(CharacterBlock.getBlock(selected)); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/encapsulation/elevator/UnEncapsulatedElevatorTest.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.encapsulation.elevator; 2 | 3 | public class UnEncapsulatedElevatorTest { 4 | 5 | public static void main(String[] args) { 6 | System.out.println("Creating UnEncapsulatedElevator"); 7 | UnEncapsulatedElevator elevator = new UnEncapsulatedElevator(); 8 | 9 | // At floor 0 and waiting 10 | elevator.setFloor(0); 11 | elevator.closeDoor(); 12 | elevator.printInfo(); 13 | 14 | // Loading for floor 5 but door is open 15 | elevator.openDoor(); 16 | elevator.setFloor(5); 17 | elevator.closeDoor(); 18 | elevator.setFloor(5); 19 | elevator.printInfo(); 20 | elevator.openDoor(); 21 | elevator.closeDoor(); 22 | 23 | elevator.setFloor(0); 24 | elevator.printInfo(); 25 | 26 | // How about these? 27 | elevator.setFloor(9); 28 | elevator.setFloor(-2); 29 | 30 | // And still 31 | elevator.doorOpen = false; 32 | elevator.currentFloor = 5; 33 | elevator.currentFloor = 12; 34 | elevator.currentFloor = -4; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/init/InitializationOrder.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.init; 2 | 3 | public class InitializationOrder { 4 | static A a = new A(); 5 | public InitializationOrder() { 6 | System.out.println("in InitializationOrder"); 7 | } 8 | public static void main(String args[]) { 9 | // System.out.println("in main"); 10 | InitializationOrder oi = new InitializationOrder(); 11 | // new InitializationOrder(); 12 | // new InitializationOrder(); 13 | // new InitializationOrder(); 14 | // B b = new B(); 15 | // b.f(); 16 | } 17 | C c = new C(); 18 | static int[] ints = new int[10]; 19 | static { // If static don't exists, it is not initialized as static 20 | System.out.println("Initializing the array"); 21 | for (int i = 0; i < ints.length; i++) 22 | ints[i] = i * 10; 23 | } 24 | } 25 | class A { 26 | C c = new C(); 27 | public A() { 28 | System.out.println("in A"); 29 | } 30 | } 31 | class B { 32 | public B() { 33 | System.out.println("in B"); 34 | } 35 | public void f() { 36 | System.out.println("in f()"); 37 | } 38 | } 39 | class C { 40 | static B b = new B(); 41 | public C() { 42 | System.out.println("in C"); 43 | } 44 | static void g() { 45 | System.out.println("in g()"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/unicode/BlockCanvas.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BlockCanvas.java 3 | */ 4 | 5 | package org.javaturk.oop.ch03.unicode; 6 | 7 | import java.awt.*; 8 | 9 | public class BlockCanvas extends Canvas { 10 | private CharacterBlock block = CharacterBlock.getBlock("Basic Latin"); 11 | 12 | private int charWidth = 20; 13 | private int charHeight = 15; 14 | private int hgap = 5; 15 | private int vgap = 0; 16 | 17 | public BlockCanvas() { 18 | setFont(new Font("Monospaced", Font.BOLD, 12)); 19 | } 20 | 21 | public void setBlock(CharacterBlock block) { 22 | this.block = block; 23 | this.repaint(); 24 | } 25 | 26 | public CharacterBlock getBlock() { 27 | return block; 28 | } 29 | 30 | public void paint(Graphics g) { 31 | char[] charsOnPage = block.getCharactersInBlock(); 32 | int charsPerRow = (getSize().width - 2*hgap)/charWidth; 33 | if (charsPerRow <= 0) charsPerRow = 1; 34 | 35 | for (int i = 0; i < charsOnPage.length; i++) { 36 | int x = i % charsPerRow; 37 | int y = i / charsPerRow + 1; 38 | g.drawChars(charsOnPage, i, 1, hgap + charWidth*x, vgap + charHeight*y); 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/tree/TreeWithThis.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.tree; 2 | 3 | public class TreeWithThis { 4 | 5 | private String type; 6 | private float height; 7 | 8 | // Can this constructor do its job by calling other constructors? 9 | public TreeWithThis(String type, float height) { 10 | System.out.println("\nin TreeWithThis(String, int)"); 11 | this.type = type; 12 | this.height = height; 13 | } 14 | 15 | public TreeWithThis(String newType) { 16 | // type = newType; 17 | // height = 1.0f; 18 | this(newType, 1.0f); 19 | System.out.println("\nin TreeWithThis(String)"); 20 | } 21 | 22 | public TreeWithThis(float newHeight) { 23 | // type = "Pine"; 24 | // height = newHeight; 25 | this("Pine", newHeight); 26 | System.out.println("\nin TreeWithThis(int)"); 27 | } 28 | 29 | public void printInfo(){ 30 | System.out.println("\nTreeWithThis Info:"); 31 | System.out.println("Type: " + type); 32 | System.out.println("Height: " + height); 33 | } 34 | 35 | public static void main(String[] args) { 36 | TreeWithThis tree1 = new TreeWithThis("Pine", 2.0f); 37 | tree1.printInfo(); 38 | 39 | TreeWithThis tree2 = new TreeWithThis("Oak"); 40 | tree2.printInfo(); 41 | 42 | TreeWithThis tree3 = new TreeWithThis(8.0f); 43 | tree3.printInfo(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/tree/TreeWithArgumentConstructor.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.tree; 2 | 3 | public class TreeWithArgumentConstructor { 4 | 5 | private String type; 6 | private float height; 7 | 8 | public TreeWithArgumentConstructor(String newType, float newHeight) { 9 | type = newType; 10 | height = newHeight; 11 | // System.out.println("TreeWithArgumentConstructor(String newType, float newHeight)"); 12 | } 13 | 14 | public TreeWithArgumentConstructor() { 15 | 16 | // this("Pine", 1.0f); 17 | 18 | type = "Pine"; 19 | height = 1.0f; 20 | 21 | System.out.println("Type: " + type); 22 | System.out.println("Height: " + height); 23 | // System.out.println("TreeWithArgumentConstructor"); 24 | } 25 | 26 | public void printInfo(){ 27 | System.out.println("\nTreeWithArgumentConstructor Info:"); 28 | System.out.println("Type: " + type); 29 | System.out.println("Height: " + height); 30 | } 31 | 32 | public static void main(String[] args) { 33 | TreeWithArgumentConstructor tree1 = new TreeWithArgumentConstructor("Pine", 2.0f); 34 | tree1.printInfo(); 35 | 36 | TreeWithArgumentConstructor tree2 = new TreeWithArgumentConstructor("Oak", 5.5f); 37 | tree2.printInfo(); 38 | 39 | TreeWithArgumentConstructor tree3 = new TreeWithArgumentConstructor(); 40 | tree3.printInfo(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/SwitchDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 5 2 | * SwitchDemo.java 3 | */ 4 | 5 | package org.javaturk.oop.ch05; 6 | 7 | import java.util.Scanner; 8 | 9 | public class SwitchDemo { 10 | 11 | public static void main(String[] args) { 12 | System.out.println("A number :"); 13 | Scanner stdin = new Scanner(System.in); 14 | int month = stdin.nextInt(); 15 | 16 | // final int i = 8; 17 | // final int i =8; 18 | 19 | switch (month) { 20 | case 1: System.out.println("January"); break; 21 | case 2: System.out.println("February"); break; 22 | case 3: System.out.println("March"); break; 23 | case 4: System.out.println("April"); break; 24 | case 5: System.out.println("May"); break; 25 | case 6: System.out.println("June"); break; 26 | case 7: System.out.println("July"); break; 27 | case 8: System.out.println("August"); break; 28 | case 9: System.out.println("September"); break; 29 | case 10: System.out.println("October"); break; 30 | case 11: System.out.println("November"); break; 31 | case 12: System.out.println("December"); break; 32 | default: System.out.println("Error! Please enter the month number between 1 and 12"); break; 33 | } 34 | } 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/Calculator.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07; 2 | 3 | public class Calculator { 4 | 5 | public void whoAreYou(){ 6 | System.out.println("I'm a Calculator :)"); 7 | } 8 | 9 | int add(int first, int second) { 10 | return first + second; 11 | } 12 | 13 | int subtract(int first, int second) { 14 | return first - second; 15 | } 16 | 17 | int multiply(int first, int second) { 18 | return first * second; 19 | } 20 | 21 | int multiplyByAdding(int first, int second) { 22 | int multiplication = 0; 23 | for(int i = 1; i <= second; i++) 24 | multiplication += first; 25 | System.out.println("Multiplication: " + multiplication); 26 | return multiplication; 27 | } 28 | 29 | double divide(int first, int second) { 30 | return (double)first / second; 31 | } 32 | 33 | public static void main(String[] args) { 34 | Calculator calculator = new Calculator(); 35 | calculator.whoAreYou(); 36 | 37 | int i = calculator.add(12, 27); 38 | System.out.println("12 + 27 = " + i); 39 | 40 | double d = calculator.divide(82, 9); 41 | System.out.println("82 / 9 = " + d); 42 | 43 | System.out.println("83 - 56 = " + calculator.subtract(83, 56)); 44 | System.out.println("8 * 7 = " + calculator.multiply(8, 7)); 45 | 46 | // Calling it for its side effect 47 | calculator.multiplyByAdding(22, 5); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch04/OperatorPrecedence.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch04; 2 | 3 | public class OperatorPrecedence { 4 | 5 | public static void main(String[] args) { 6 | 7 | int a = 6; 8 | int b = 5; 9 | int c = 10; 10 | 11 | float rs = a + ++b * c / a * b; 12 | System.out.println("rs :" + rs); 13 | 14 | rs = a + (++b) * ((c / a) * b); 15 | System.out.println("rs :" + rs); 16 | 17 | a += b += c; 18 | System.out.println("a: " + a); 19 | 20 | int u = 3; 21 | // System.out.println("u:" + ++u+u--); 22 | System.out.println(++u + u--); 23 | System.out.println("u:" + u); 24 | 25 | int x = 10, y = 7, z = 3; 26 | System.out.println("x*y%z: " + x * y % z); 27 | System.out.println("x*y%z: " + x * (y % z)); 28 | 29 | boolean b1 = false, b2 = true, b3 = true; 30 | boolean bool = b1 & b2 | b3; 31 | System.out.println("bool: " + bool); 32 | 33 | // The + operator is left associative 34 | System.out.println("Java" + 1 + 6); 35 | System.out.println(1 + 6 + "Java"); 36 | System.out.println("1" + "6" + "Java"); 37 | System.out.println('1' + '6' + "Java"); 38 | System.out.println("Java" + (1 + 6)); 39 | 40 | // Parameter evaluation 41 | String s = "Java"; 42 | print(s, s = "Javaci", s); 43 | } 44 | 45 | static void print(String a, String b, String c) { 46 | System.out.println(a + " " + b + " " + c); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/car/CarAttributeOnly.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.car; 2 | 3 | /** 4 | * That's a attribute-only car. It does not have any behavior, it has only attrributes. 5 | * @author akin 6 | * 7 | */ 8 | public class CarAttributeOnly { 9 | String make; 10 | String model; 11 | String year; 12 | int speed; 13 | int distance; 14 | 15 | public static void main(String[] args) { 16 | CarAttributeOnly myCar = new CarAttributeOnly(); 17 | myCar.make = "Mercedes"; 18 | myCar.model = "E200"; 19 | myCar.year = "2011"; 20 | myCar.speed = 80; 21 | myCar.distance = 30000; 22 | // 23 | 24 | System.out.println("My Car: " + myCar.year + " " + myCar.make + " " + myCar.model); 25 | 26 | CarAttributeOnly yourCar = new CarAttributeOnly(); 27 | yourCar.make = "Toyota"; 28 | yourCar.model = "Camry"; 29 | yourCar.year = "2011"; 30 | yourCar.speed = 0; 31 | yourCar.distance = 0; 32 | System.out.println("Your Car: " + yourCar.year + " " + yourCar.make + " " + yourCar.model); 33 | 34 | //Following shows the difference between a reference and an object. 35 | CarAttributeOnly tmpCar = myCar; 36 | myCar = yourCar; 37 | yourCar = tmpCar; 38 | System.out.println("My Car: " + myCar.year + " " + myCar.make + " " + myCar.model); 39 | System.out.println("Your Car: " + yourCar.year + " " + yourCar.make + " " + yourCar.model); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/university/Test.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.university; 2 | 3 | public class Test { 4 | 5 | public static void main(String[] args) { 6 | Department d1 = new Department(); 7 | 8 | Professor p1 = new Professor(); 9 | p1.name = "Ahmet Arslan"; 10 | 11 | Course c1 = new Course(); 12 | 13 | Student s1 = new Student(); 14 | s1.name = "Akin Kaldiroglu"; 15 | 16 | d1.name = "Software Engineering"; 17 | d1.head = p1; 18 | 19 | c1.name = "Int. to Software Engineering"; 20 | c1.department = d1; 21 | // Professor p2 = new Professor(); 22 | // p2.name = "Ahmet Aslan"; 23 | c1.teacher = p1; 24 | 25 | p1.coursesGiven = new Course[5]; 26 | p1.coursesGiven[0] = c1; 27 | p1.advisee = new Student[10]; 28 | p1.advisee[0] = s1; 29 | 30 | d1.courses = new Course[100]; 31 | d1.courses[0] = c1; 32 | 33 | 34 | s1.advisor = p1; 35 | s1.coursesTaken = new Course[10]; 36 | s1.coursesTaken[0] = c1; 37 | 38 | System.out.println("Name of student s1's first course is " + s1.coursesTaken[0].name); 39 | System.out.println("Name of student s1's first course's professor is " + s1.coursesTaken[0].teacher.name); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/AgeClassifier.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch05; 2 | 3 | import java.util.Scanner; 4 | 5 | public class AgeClassifier { 6 | 7 | private static Scanner in = new Scanner(System.in); 8 | private static int childCount; 9 | private static int youngCount; 10 | private static int middleAgeCount; 11 | private static int oldCount; 12 | 13 | public static void main(String[] args) { 14 | System.out.println("This program classifies people based on their ages."); 15 | System.out.println("Here are the ranges:"); 16 | System.out.println(" 1-12 : Child \n 13-30: Young \n 31-49: Middle age \n 60- : Old \n"); 17 | 18 | while (true) { 19 | System.out.println("Please enter the age:"); 20 | int age = in.nextInt(); 21 | if ((age <= 0) || (age > 120)) { 22 | System.out.println("Are you kidding me?"); 23 | break; 24 | } else if ((1 <= age) & (age <= 12)) 25 | childCount++; 26 | else if ((13 <= age) & (age <= 30)) 27 | youngCount++; 28 | else if ((31 <= age) & (age <= 49)) 29 | middleAgeCount++; 30 | else 31 | oldCount++; 32 | } 33 | System.out.println("Here is the classification:"); 34 | System.out.println("There are " + childCount + " children."); 35 | System.out.println("There are " + youngCount + " young people."); 36 | System.out.println("There are " + middleAgeCount + " middle age people."); 37 | System.out.println("There are " + oldCount + " old people."); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/tree/TreeWithOverloadedConstructors.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.tree; 2 | 3 | public class TreeWithOverloadedConstructors { 4 | private String type; 5 | private float height; 6 | 7 | public TreeWithOverloadedConstructors(String newType, float newHeight) { 8 | System.out.println("in TreeWithOverloadedConstructors(String, float)"); 9 | type = newType; 10 | height = newHeight; 11 | } 12 | 13 | public TreeWithOverloadedConstructors(String newType) { 14 | type = newType; 15 | height = 1.0f; 16 | } 17 | 18 | public TreeWithOverloadedConstructors(float newHeight) { 19 | type = "Pine"; 20 | height = newHeight; 21 | } 22 | 23 | public TreeWithOverloadedConstructors(){ 24 | // type = "Pine"; 25 | // height = 1.0f; 26 | this("Pine", 1.0f); 27 | } 28 | 29 | public void printInfo(){ 30 | System.out.println("\nTreeWithOverloadedConstructor Info:"); 31 | System.out.println("Type: " + type); 32 | System.out.println("Height: " + height); 33 | } 34 | 35 | public static void main(String[] args) { 36 | TreeWithOverloadedConstructors tree1 = new TreeWithOverloadedConstructors("Pine", 2.0f); 37 | tree1.printInfo(); 38 | 39 | TreeWithOverloadedConstructors tree2 = new TreeWithOverloadedConstructors("Oak"); 40 | tree2.printInfo(); 41 | 42 | TreeWithOverloadedConstructors tree3 = new TreeWithOverloadedConstructors(8.0f); 43 | tree3.printInfo(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/thisKeyword/Tree.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.thisKeyword; 2 | 3 | public class Tree { 4 | 5 | private String type; 6 | private float height; 7 | 8 | public Tree(String type, float height) { 9 | this.type = type; 10 | this.height = height; 11 | } 12 | 13 | public Tree copy() { 14 | Tree copyTree = new Tree(type, height); 15 | return copyTree; 16 | // return new Tree(type, height); 17 | } 18 | 19 | public Tree grow() { 20 | height++; 21 | return this; 22 | } 23 | 24 | // No need to use "this" here 25 | public void printInfo() { 26 | System.out.println("\nTree Info:"); 27 | this.printType(); 28 | this.printHeight(); 29 | } 30 | 31 | // No need to use "this" here 32 | public void printType() { 33 | System.out.println("Type: " + this.type); 34 | } 35 | 36 | // No need to use "this" here 37 | public void printHeight() { 38 | System.out.println("Height: " + this.height); 39 | } 40 | 41 | public static void main(String[] args) { 42 | Tree tree = new Tree("Pine", 15.0f); 43 | tree.printInfo(); 44 | Tree copyTree = tree.copy(); 45 | copyTree.printInfo(); 46 | 47 | 48 | tree.grow().grow().grow().printInfo(); 49 | 50 | Tree t = tree.grow().grow().grow().grow(); 51 | tree.printInfo(); 52 | 53 | 54 | if(tree == t) 55 | System.out.println("Aynı"); 56 | else 57 | System.out.println("Farklı"); 58 | 59 | if(copyTree == tree) 60 | System.out.println("Aynı"); 61 | else 62 | System.out.println("Farklı"); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/ReversingANumber.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch05; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReversingANumber { 6 | private static Scanner in; 7 | 8 | public static void main(String[] args) { 9 | System.out.println("This program reverses the number you enter."); 10 | System.out.println("Please enter a natural number that has digits between 2 and 9."); 11 | while (true) { 12 | int number = 0; 13 | String numberString = null; 14 | in = new Scanner(System.in); 15 | numberString = in.next(); 16 | if(numberString.equalsIgnoreCase("X")) 17 | break; 18 | try { 19 | number = Integer.parseInt(numberString); 20 | } catch (NumberFormatException e) { 21 | System.out.println("Please enter a number thas at least two digits and at most 9 digits:"); 22 | continue; 23 | } 24 | 25 | numberString = String.valueOf(number); 26 | int length = numberString.length(); 27 | if((length < 2) || (length > 9) || number < 0){ 28 | System.out.println("Please enter a positive number thas at least two digits and at most 9 digits:"); 29 | continue; 30 | } 31 | 32 | StringBuilder sb = new StringBuilder(length); 33 | for(int i = length-1; i >= 0; i--){ 34 | sb.append(numberString.charAt(i)); 35 | } 36 | String reversedNumberString = sb.toString(); 37 | System.out.println("Number you entered: " + numberString); 38 | System.out.println("Reverse of your number: " + reversedNumberString); 39 | System.out.println("\nPlease enter a natural number that has digits between 2 and 9."); 40 | } 41 | in.close(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/memory/HeapDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.memory; 2 | 3 | import java.util.Scanner; 4 | //http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html 5 | /** 6 | * Run with -Xnoclassgc -verbose:gc flags 7 | */ 8 | public class HeapDemo { 9 | static final int MB = 1024 * 1024; 10 | 11 | public static void main(String[] args) { 12 | System.out.println("At the beginning."); 13 | getMemory(0); 14 | 15 | System.out.println("\nEnter number of objects:"); 16 | long objectCount = new Scanner(System.in).nextLong(); 17 | 18 | for (long i = 1; i <= objectCount; i++) { 19 | Person person = new Person("TCKN"+i, "Akin"+i, "Kaldiroglu"+i, "Address"+i, "Phone"+i, "CellPhone"+i); 20 | new Car("Turkey"+i, "Personal car"+i, "Mercedes"+i, "CLK200"+i, "2012"+i, i, i, person); 21 | 22 | if(i%1000000 == 0) 23 | getMemory(i); 24 | } 25 | System.out.println("Done!"); 26 | } 27 | 28 | public static void getMemory(long i){ 29 | System.out.println("Object count: " + (2 * i)); 30 | long heapSize = Runtime.getRuntime().totalMemory(); 31 | System.out.println("\nHeap size: " + heapSize/MB + " MB"); 32 | 33 | long heapMaxSize = Runtime.getRuntime().maxMemory(); 34 | System.out.println("Max Heap size: " + heapMaxSize/MB + " MB"); 35 | 36 | long heapFreeSize = Runtime.getRuntime().freeMemory(); 37 | System.out.println("Free Heap size: " + heapFreeSize/MB + " MB"); 38 | 39 | long heapUsedSize = heapSize - heapFreeSize; 40 | System.out.println("Used Heap size: " + heapUsedSize/MB + " MB"); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/memory/HeapDemo2.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.memory; 2 | 3 | import java.util.*; 4 | //http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html 5 | /** 6 | * Run with -Xnoclassgc -verbose:gc flags 7 | */ 8 | public class HeapDemo2 { 9 | static final int MB = 1024 * 1024; 10 | 11 | private static List list = new ArrayList(); 12 | 13 | public static void main(String[] args) { 14 | System.out.println("At the beginning."); 15 | getMemory(0); 16 | 17 | System.out.println("\nEnter number of objects:"); 18 | long objectCount = new Scanner(System.in).nextLong(); 19 | 20 | for (long i = 1; i <= objectCount; i++) { 21 | Person person = new Person("TCKN"+i, "Akin"+i, "Kaldiroglu"+i, "Address"+i, "Phone"+i, "CellPhone"+i); 22 | Car car = new Car("Turkey"+i, "Personal car"+i, "Mercedes"+i, "CLK200"+i, "2012"+i, i, i, person); 23 | list.add(car); 24 | if(i%1000 == 0) 25 | getMemory(i); 26 | } 27 | System.out.println("Done!"); 28 | } 29 | 30 | public static void getMemory(long i){ 31 | System.out.println("Object count: " + (2 * i)); 32 | long heapSize = Runtime.getRuntime().totalMemory(); 33 | System.out.println("\nHeap size: " + heapSize/MB + " MB"); 34 | 35 | long heapMaxSize = Runtime.getRuntime().maxMemory(); 36 | System.out.println("Max Heap size: " + heapMaxSize/MB + " MB"); 37 | 38 | long heapFreeSize = Runtime.getRuntime().freeMemory(); 39 | System.out.println("Free Heap size: " + heapFreeSize/MB + " MB"); 40 | 41 | long heapUsedSize = heapSize - heapFreeSize; 42 | System.out.println("Used Heap size: " + heapUsedSize/MB + " MB"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/init/InitializerBlocks.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.init; 2 | 3 | public class InitializerBlocks { 4 | 5 | // Compiler error! Use block instead 6 | int i; 7 | { 8 | i = 8; 9 | } 10 | 11 | int j; 12 | boolean b1; 13 | 14 | { 15 | System.out.println("Instance initializer"); 16 | j = 8; 17 | b1 = true; 18 | } 19 | 20 | // Initialization by method a call 21 | boolean b2 = initializeBoolean(); // Can't initialize b1 this way using a 22 | // method call!s 23 | 24 | static double k; 25 | // k = 5.0; 26 | static char c; 27 | 28 | static { 29 | System.out.println("Static initializer"); 30 | k = 5.0; 31 | c = 'c'; 32 | } 33 | 34 | // Can't do this 35 | // static boolean[] b; 36 | // 37 | // static{ 38 | // b = {true, true, false}; 39 | // } 40 | 41 | static int[] ints = new int[10]; 42 | 43 | // If static don't exists, it is not initialized as static 44 | // Look what happens when you intentionally throw an exception in this 45 | // block. 46 | static { 47 | System.out.println("\nInitializing the array"); 48 | for (int i = 0; i < ints.length; i++) 49 | ints[i] = i * 10; 50 | } 51 | 52 | // No purpose of doing this because it is local to the block. 53 | { 54 | int m = 8; 55 | } 56 | 57 | 58 | public static void main(String[] args) { 59 | for (int i : ints) 60 | System.out.print(i + " "); 61 | 62 | System.out.println(); 63 | 64 | 65 | // 66 | // System.out.println("Array: "); 67 | // for(int i:ints) 68 | // System.out.print(i + " "); 69 | 70 | // new InitializerBlocks(); 71 | 72 | } 73 | 74 | public boolean initializeBoolean() { 75 | return true; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/unicode/UnicodeCharacters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UnicodeCharacters.java 3 | */ 4 | 5 | package org.javaturk.oop.ch03.unicode; 6 | 7 | import java.awt.*; 8 | import java.awt.event.*; 9 | import javax.swing.*; 10 | 11 | public class UnicodeCharacters extends JFrame implements ItemListener { 12 | private static final int width = 400; 13 | private static final int height = 500; 14 | private List scripts = new List(CharacterBlock.getNumBlocks()); 15 | private BlockCanvas theChart = new BlockCanvas(); 16 | 17 | public UnicodeCharacters() { 18 | setSize(width, height); 19 | setTitle("Unicode Characters"); 20 | 21 | Container root = getContentPane(); 22 | root.setLayout(new BorderLayout()); 23 | String[] names = CharacterBlock.getBlockNames(); 24 | for (int i = 0; i < names.length; i++) { 25 | scripts.add(names[i]); 26 | } 27 | scripts.addItemListener(this); 28 | scripts.select(0); 29 | root.add("West", scripts); 30 | root.add("Center", theChart); 31 | } 32 | 33 | public static void main(String[] args){ 34 | UnicodeCharacters frame = new UnicodeCharacters(); 35 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 36 | frame.setResizable(false); 37 | frame.show(); 38 | } 39 | 40 | public void itemStateChanged(ItemEvent ie) { 41 | if (ie.getStateChange() == ItemEvent.SELECTED) { 42 | String selected = scripts.getSelectedItem(); 43 | if (!selected.equals(theChart.getBlock().getName())) { 44 | theChart.setBlock(CharacterBlock.getBlock(selected)); 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/encapsulation/elevator/UnEncapsulatedElevator.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.encapsulation.elevator; 2 | 3 | /** 4 | * What is still wrong with this Elevator? 5 | */ 6 | 7 | public class UnEncapsulatedElevator { 8 | 9 | public int currentFloor; 10 | public boolean doorOpen; 11 | 12 | public final int TOP_FLOOR = 5; 13 | public final int BOTTOM_FLOOR = -1; 14 | 15 | 16 | public void openDoor() { 17 | System.out.println("Opening door."); 18 | doorOpen = true; 19 | System.out.println("Door is open."); 20 | } 21 | 22 | public void closeDoor() { 23 | System.out.println("Closing door."); 24 | doorOpen = false; 25 | System.out.println("Door is closed."); 26 | } 27 | 28 | public void goUp() { 29 | System.out.println("Going up one floor."); 30 | currentFloor++; 31 | System.out.println("Floor: " + currentFloor); 32 | } 33 | 34 | public void goDown() { 35 | System.out.println("Going down one floor."); 36 | currentFloor--; 37 | System.out.println("Floor: " + currentFloor); 38 | } 39 | 40 | public void setFloor(int newFloor) { 41 | currentFloor = newFloor; 42 | } 43 | 44 | public int getFloor() { 45 | return currentFloor; 46 | } 47 | 48 | public boolean checkDoorStatus() { 49 | return doorOpen; 50 | } 51 | 52 | public void printInfo() { 53 | String doorStatus = null; 54 | if (checkDoorStatus()) 55 | doorStatus = "open"; 56 | else 57 | doorStatus = "closed"; 58 | 59 | System.out.println("Floor: " + currentFloor + " Door: " + doorStatus); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/CalculatorOverloaded.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07; 2 | 3 | public class CalculatorOverloaded { 4 | 5 | int add(byte first, byte second) { 6 | System.out.println("Arguments are in byte"); 7 | return first + second; 8 | } 9 | 10 | int add(short first, short second) { 11 | System.out.println("Arguments are in short"); 12 | return first + second; 13 | } 14 | 15 | int add(char first, char second) { 16 | System.out.println("Arguments are in char"); 17 | return first + second; 18 | } 19 | 20 | int add(int first, int second) { 21 | System.out.println("Arguments are in int"); 22 | return first + second; 23 | } 24 | 25 | long add(long first, long second) { 26 | System.out.println("Arguments are in long"); 27 | return first + second; 28 | } 29 | 30 | long add(short first, long second) { 31 | System.out.println("Arguments are in short"); 32 | return first + second; 33 | } 34 | 35 | float add(float first, float second) { 36 | System.out.println("Arguments are in float"); 37 | return first + second; 38 | } 39 | 40 | double add(double first, double second) { 41 | System.out.println("Arguments are in double"); 42 | return first + second; 43 | } 44 | 45 | public static void main(String[] args) { 46 | 47 | CalculatorOverloaded calculator = new CalculatorOverloaded(); 48 | byte b1 = 18; 49 | byte b2 = -7; 50 | System.out.println(calculator.add(b1, b2)); 51 | 52 | double d1 = 3.1415; 53 | double d2 = 2.71828; 54 | System.out.println(calculator.add(d1, d2)); 55 | 56 | byte s = 33; 57 | long l1 = 4; 58 | System.out.println(calculator.add(s, l1)); 59 | 60 | double d3 = 3.1415; 61 | long l2 = 4; 62 | System.out.println(calculator.add(d1, l2)); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/FinalCar.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07; 2 | 3 | public class FinalCar { 4 | // If not initialized here it must be in constructor 5 | final String make; 6 | String model; 7 | String year; 8 | int speed; 9 | int distance; 10 | String owner; 11 | 12 | // Remove the initialization of variable make, following constructor will give 13 | // a compile-time error. 14 | public FinalCar(String model, String year, int speed, int distance) { 15 | this.make = "Mercedes"; 16 | this.model = model; 17 | this.year = year; 18 | this.speed = speed; 19 | this.distance = distance; 20 | } 21 | 22 | public FinalCar(String model, String year) { 23 | this.make = "Mercedes"; 24 | this.model = model; 25 | this.year = year; 26 | speed = 0; 27 | distance = 0; 28 | // make = "Murat124"; 29 | } 30 | 31 | public void speedUp(final int newSpeed){ 32 | // Can't do that 33 | // newSpeed = 75; 34 | speed = newSpeed; 35 | } 36 | 37 | public void setOwner(final String newOwner){ 38 | // Can't do that 39 | // newOwner = "Selim Soylu"; 40 | owner = newOwner; 41 | } 42 | 43 | public static void main(String[] args) { 44 | // FinalCar is a final reference 45 | final FinalCar finalCar; //= new FinalCar("CLK200", "2011", 0, 0); 46 | 47 | // Can do this only if finalCar is not defined above! 48 | finalCar = new FinalCar("CLK200", "2011", 0, 0); 49 | 50 | // Can't do this. 51 | // finalCar.make = " Murat 124"; 52 | 53 | // Can't do this 54 | // finalCar = new FinalCar("CLK200", "2011", 0, 0); 55 | finalCar.speed = 120; 56 | 57 | // Doable 58 | final int i; 59 | i = 6; 60 | // i = 9; 61 | 62 | // Can't do this 63 | // finalCar.make = "BMW"; 64 | 65 | finalCar.speedUp(100); 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/EnvironmentDemo.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch03; 2 | 3 | import java.io.*; 4 | import java.nio.charset.Charset; 5 | import java.util.*; 6 | 7 | /** 8 | * @author Akin Kaldiroglu 9 | *

10 | * For more info please @see http://www.javaturk.org 12 | *

13 | * This is an example that prints the default environment settings for 14 | * language and country. Optionally it lists all available character 15 | * sets in the system. 16 | */ 17 | public class EnvironmentDemo { 18 | public static void main(String[] args) throws IOException { 19 | InputStreamReader isr = new InputStreamReader(System.in); 20 | System.out.println("Encoding: " + isr.getEncoding()); 21 | 22 | Locale locale = Locale.getDefault(); 23 | System.out.println("Country: " + locale.getCountry()); 24 | System.out.println("Display language: " + locale.getDisplayLanguage()); 25 | System.out.println("Language: " + locale.getLanguage()); 26 | System.out.println("Display name: " + locale.getDisplayName()); 27 | System.out.println("ISO3 Language: " + locale.getISO3Language()); 28 | 29 | listCharSets(); 30 | } 31 | 32 | /** 33 | * Lists all available character sets in the system. 34 | */ 35 | public static void listCharSets() { 36 | SortedMap charsets = Charset.availableCharsets(); 37 | Set names = charsets.keySet(); 38 | for (Iterator e = names.iterator(); e.hasNext();) { 39 | String name = (String) e.next(); 40 | Charset charset = (Charset) charsets.get(name); 41 | System.out.println(charset); 42 | Set aliases = charset.aliases(); 43 | for (Iterator ee = aliases.iterator(); ee.hasNext();) { 44 | System.out.println(" " + ee.next()); 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch07/memory/SieveOfEratosthenes.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch07.memory; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class SieveOfEratosthenes { 7 | 8 | public static void main(String[] args) { 9 | SieveOfEratosthenes soe = new SieveOfEratosthenes(); 10 | System.out.println("Please enter a number to which to list prime numbers:"); 11 | Scanner sc = new Scanner(System.in); 12 | int number = sc.nextInt(); 13 | soe.listPrimes(number); 14 | } 15 | 16 | public void listPrimes(int upperBound) { 17 | System.out.println("Here are the prime numbers: "); 18 | 19 | // initially assume all integers are prime 20 | boolean[] isPrime = new boolean[upperBound + 1]; 21 | // for (int i = 2; i <= upperBound; i++) { 22 | // isPrime[i] = true; 23 | // } 24 | Arrays.fill(isPrime, true); 25 | 26 | int upperBoundSquareRoot = (int) Math.sqrt(upperBound); 27 | 28 | long start = System.currentTimeMillis(); 29 | 30 | // mark non-primes <= upperBound using Sieve of Eratosthenes 31 | for (int i = 2; i <= upperBoundSquareRoot; i++) { 32 | // if i is prime, then mark multiples of i as nonprime 33 | // suffices to consider mutiples i, i+1, ..., upperBound/i 34 | if (isPrime[i]) { 35 | for (int j = i; i * j <= upperBound; j++) { 36 | isPrime[i * j] = false; 37 | } 38 | } 39 | } 40 | 41 | long end = System.currentTimeMillis(); 42 | long miliSeconds = end-start; 43 | int seconds = (int) miliSeconds/1000; 44 | 45 | // count primes 46 | int primes = 0; 47 | for (int i = 2; i <= upperBound; i++) { 48 | if (isPrime[i]) { 49 | //System.out.println(i); 50 | primes++; 51 | } 52 | } 53 | System.out.println("There are " + primes + " prime numbers up to " + upperBound + "."); 54 | System.out.println("Time to find them: " + seconds + " s. or " + miliSeconds + " ms."); 55 | } 56 | } -------------------------------------------------------------------------------- /org/javaturk/oop/ch04/UnaryOperators.java: -------------------------------------------------------------------------------- 1 | /* Ch 4 2 | * UnaryPromotion.java 3 | */ 4 | 5 | package org.javaturk.oop.ch04; 6 | 7 | public class UnaryOperators { 8 | 9 | public static void main(String args[]) { 10 | unaryPromotion(); 11 | // autoIncDec(); 12 | // weirdBehavior(); 13 | } 14 | 15 | public static void weirdBehavior() { 16 | // For a good explanation: 17 | // http://skeletoncoder.blogspot.com/2006/09/java-tutorials-i-i.html and 18 | // http://forums.devx.com/archive/index.php/t-27360.html 19 | int i = 0; 20 | // i = i++ + 2; 21 | i = i++; 22 | System.out.println("i: " + i); 23 | 24 | // int j = i++; 25 | // System.out.println("i: " + i); 26 | // System.out.println("j: " + j); 27 | // 28 | // int k = i++; 29 | // System.out.println("i: " + i); 30 | // System.out.println("k: " + j); 31 | } 32 | 33 | public static void autoIncDec() { 34 | int i = 1; 35 | prt("i : \t" + i); 36 | prt("++i : \t" + ++i); // Pre-increment 37 | prt("i++ : \t" + i++); // Post-increment 38 | prt("i : \t" + i); 39 | prt("--i : \t" + --i); // Pre-decrement 40 | prt("i-- : \t" + i--); // Post-decrement 41 | prt("i : \t" + i); 42 | } 43 | 44 | public static void unaryPromotion() { 45 | byte b1 = 18; 46 | short s1 = 44; 47 | byte b2 = ++b1; // OK. No need to implicitly cast 48 | short s2 = --s1; // OK. No need to implicitly cast 49 | System.out.println("b2: " + b2); 50 | System.out.println("s2: " + s2); 51 | 52 | // -b1; // compile error: not a statement 53 | // byte b3 = -b1; // compile error: possible loss of precision 54 | byte b3 = (byte) -b1; 55 | // byte b4 = +b2; // compile error: possible loss of precision 56 | 57 | long l = 5; 58 | l = -l; 59 | System.out.println(l); 60 | 61 | boolean isTrue = true; 62 | isTrue = !isTrue; 63 | // !isTrue; // compile error: 64 | } 65 | 66 | static void prt(String s) { 67 | System.out.println(s); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch06/ArrayDemo.java: -------------------------------------------------------------------------------- 1 | /* Ch 7 2 | * ArrayExample.java 3 | */ 4 | 5 | package org.javaturk.oop.ch06; 6 | 7 | import java.util.Random; 8 | 9 | public class ArrayDemo { 10 | 11 | int[] intArray = new int[10]; 12 | Pizza[] pizzalar = new Pizza[3]; 13 | boolean[] flags = { true, false }; 14 | 15 | public ArrayDemo() { 16 | 17 | for (int i = 0; i < intArray.length; i++) { 18 | Random r = new Random(); 19 | int randomInt = r.nextInt(); 20 | int sayi = randomInt % 100; 21 | intArray[i] = sayi; 22 | } 23 | 24 | // foreach 25 | // for(int a:intArray){ 26 | // Random r = new Random(); 27 | // int randomInt = r.nextInt(); 28 | // int sayi = randomInt % 100; 29 | // a = sayi; 30 | // } 31 | 32 | pizzalar[0] = new Pizza("Peynirli"); 33 | pizzalar[1] = new Pizza("Sucuklu"); 34 | pizzalar[2] = new Pizza("Sebzeli"); 35 | } 36 | 37 | public static void main(String args[]) { 38 | (new ArrayDemo()).print(); 39 | } 40 | 41 | public void print() { 42 | System.out.println("Fifth element of intArray is " + intArray[5]); 43 | System.out.println("First element of flags is " + flags[0]); 44 | System.out.println("Name of the last element of pizzalar is " + pizzalar[2].name); 45 | 46 | for (int i = 0; i < intArray.length; i++) { 47 | System.out.print(intArray[i] + " "); 48 | } 49 | System.out.println(); 50 | 51 | // foreach 52 | for(int a:intArray) 53 | System.out.print(a + " "); 54 | 55 | System.out.println(); 56 | 57 | // long index can't be used 58 | // long l = 5; 59 | // Pizza pizza = pizzalar[l]; 60 | 61 | // Negative index causes ArrayIndexOutOfBoundsException 62 | // int i = -2; 63 | // Pizza pizza = pizzalar[i]; 64 | 65 | short s = 2; 66 | Pizza pizza = pizzalar[s]; 67 | System.out.println(pizza.name); 68 | } 69 | } 70 | 71 | class Pizza { 72 | String name; 73 | 74 | Pizza(String name) { 75 | this.name = name; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/UnreachableCode.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch05; 2 | 3 | import java.io.IOException; 4 | 5 | public class UnreachableCode { 6 | 7 | public static void main(String[] args) { 8 | 9 | // Not an unreachable code :) 10 | int a = 8; 11 | int k; 12 | if (a < 11) 13 | k = 7; 14 | else 15 | k = 9; 16 | 17 | System.out.println(k); 18 | 19 | // while (true) 20 | // System.out.println("Selam"); 21 | // int i = 5; // Unreachable code! 22 | 23 | // while (false) 24 | // System.out.println("Selam"); // Unreachable code! 25 | // int r = 5; 26 | 27 | // Unreachable code if break does not exist. 28 | // while (true) { 29 | // System.out.println("Selam"); 30 | //// break; 31 | // } 32 | // int i = 5; // Unreachable code without break. 33 | 34 | 35 | // That's not unreachable code 36 | if (false) { 37 | System.out.println("Selam"); 38 | } 39 | 40 | for (int m = 0; true; m++){ 41 | System.out.println(m); 42 | break; 43 | } 44 | System.out.println("Break!"); // Unreachable code without a break 45 | 46 | for (;;) { 47 | System.out.println("Selam"); 48 | // break; 49 | System.out.println("Bir selam daha."); // Unreachable code due to break 50 | } 51 | 52 | // for (;;) { 53 | // System.out.println("Selam"); 54 | // if (true) 55 | // break; 56 | // System.out.println("Bir selam daha."); // Dead code not unreachable code 57 | // } 58 | 59 | // try { 60 | // 61 | // } catch (IOException e) { // Unreachable code. Try to catch Exception 62 | // // or RuntimeException and see what happens 63 | // 64 | // } 65 | 66 | // return; 67 | // System.out.println("Son after return"); // Unreachable code! 68 | } 69 | 70 | public int f() { 71 | if (false) { 72 | System.out.println("In if"); 73 | return 8; 74 | } else { 75 | System.out.println("in Else"); 76 | return 0; 77 | } 78 | // System.out.println("Son"); // Unreachable code! 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch04/ConditionalOperators.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ConditionalOperators.java 3 | * 4 | * This example shows conditional operators & and | and their shortcuts && and ||. 5 | * See the effects of shortcuts. 6 | */ 7 | 8 | package org.javaturk.oop.ch04; 9 | 10 | public class ConditionalOperators { 11 | 12 | public static void main(String args[]) { 13 | 14 | boolean b1 = true; 15 | boolean b2 = true; 16 | boolean b3 = b1 ^ b2; 17 | // System.out.println(b3); 18 | 19 | if ((getOne() == 7) && (getTwo() == 2)) { 20 | System.out.println("value1 is 1 AND value2 is 2"); 21 | } 22 | 23 | if ((getOne() == 8) || (getTwo() == 1)) { 24 | System.out.println("value1 is 1 OR value2 is 1"); 25 | } 26 | 27 | //Ternary operator 28 | int i = 8; 29 | int j = 11; 30 | int min = (i <= j) ? i : j; 31 | System.out.println(min); 32 | 33 | System.out.println(true ? "true" : "false" ); 34 | System.out.println(false ? "true" : "false" ); 35 | 36 | // String string1 = "I love Java"; 37 | // String string2 = (string1 == "I love Java") ? string1 : "I love nothing"; 38 | 39 | // String string2 = (string1.contains("Java".subSequence(0, 3))) ? string1 : "I love nothing"; 40 | // System.out.println(string2); 41 | //// string1 = "I love C# very much"; 42 | // string2 = (string1.contains("Java".subSequence(0, 3))) ? string1 : "I love nothing"; 43 | // System.out.println(string2); 44 | 45 | String string1 = "I love Java very much"; 46 | String string2 = (string1.matches(".*Java.*")) ? string1 : "I love nothing"; 47 | System.out.println(string2); 48 | 49 | if(string1.matches(".*Java.*")) 50 | string2 = string1; 51 | else 52 | string2 = "I love nothing"; 53 | System.out.println(string2); 54 | 55 | // (i 18; 46 | long distance = returnLong(); 47 | 48 | // Exponentials 49 | double d1 = 1.6e-19; 50 | float f1 = 6.02E23F; 51 | System.out.println(d1 + " " + f1); 52 | 53 | // Overflows 54 | double lowestInt = -Math.pow(2, 31); 55 | double highestInt = Math.pow(2, 31) - 1; 56 | System.out.println("Range of int is from " + lowestInt + " to " + highestInt); 57 | 58 | int i = 2147483647; 59 | int k = 153000; 60 | int m = i * k; 61 | System.out.println("m is " + m); 62 | 63 | // byte b = 128; 64 | // long lo1 = 2147483648; 65 | long lo2 = 2147483648L; 66 | 67 | float lowestfloat = 1.40e-45f; 68 | float biggestfloat = 3.4028235e38f; 69 | System.out.println(lowestfloat + " - " + biggestfloat); 70 | 71 | double lowestDouble= 4.9e-323; //1.40129846432481707e-45 72 | double biggestDouble = 1.7976931348623158e308; 73 | System.out.println(lowestDouble + " - " + biggestDouble); 74 | 75 | /* 76 | double lowestDouble1 = -Math.pow(2, 63); 77 | double highestDouble = Math.pow(2, 63) - 1; 78 | System.out.println("Range of double is from " + lowestDouble1 + " to " + highestDouble); 79 | */ 80 | } 81 | 82 | public static long returnLong() { 83 | return 5L; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/encapsulation/elevator/EncapsulatedElevator.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.encapsulation.elevator; 2 | 3 | public class EncapsulatedElevator { 4 | 5 | private int currentFloor; 6 | private boolean doorOpen; 7 | 8 | private int weight = 0; 9 | 10 | private final int CAPACITY = 1000; 11 | private final int TOP_FLOOR = 5; 12 | private final int BOTTOM_FLOOR = 1; 13 | 14 | public void openDoor() { 15 | doorOpen = true; 16 | System.out.println("Door is open."); 17 | } 18 | 19 | 20 | public void closeDoor() { 21 | calculateWeight(); 22 | 23 | if (weight <= CAPACITY) { 24 | doorOpen = false; 25 | System.out.println("Door is closed."); 26 | } else { 27 | System.out.println("The elevator has exceeded capacity."); 28 | System.out.println("Doors will remain open until someone exits!"); 29 | } 30 | } 31 | 32 | // In reality, the elevator would have weight sensors to 33 | // check the actual weight in the elevator, but for the sake 34 | // of simplicity we just pick a random number to represent the 35 | // weight in the elevator 36 | 37 | private void calculateWeight() { 38 | weight = (int)(Math.random() * 1500); 39 | System.out.println("The weight is " + weight); 40 | } 41 | 42 | public void goUp() { 43 | if (!doorOpen) { 44 | if (currentFloor < TOP_FLOOR) { 45 | currentFloor++; 46 | System.out.println(currentFloor); 47 | } else { 48 | System.out.println("On top floor."); 49 | openDoor(); 50 | } 51 | } else { 52 | System.out.println("Doors are closing!"); 53 | closeDoor(); 54 | } 55 | } 56 | 57 | public void goDown() { 58 | if (!doorOpen) { 59 | if (currentFloor > BOTTOM_FLOOR) { 60 | currentFloor--; 61 | System.out.println(currentFloor); 62 | } else { 63 | System.out.println("Already on bottom floor."); 64 | } 65 | } else { 66 | System.out.println("Doors still open!"); 67 | } 68 | } 69 | 70 | public void setFloor(int desiredFloor) { 71 | if ((desiredFloor >= BOTTOM_FLOOR) && (desiredFloor <= TOP_FLOOR)) { 72 | 73 | while (currentFloor != desiredFloor) { 74 | if (currentFloor < desiredFloor) { 75 | goUp(); 76 | } 77 | 78 | else { 79 | goDown(); 80 | } 81 | } 82 | } else { 83 | System.out.println("Invalid Floor"); 84 | } 85 | } 86 | 87 | public int getFloor() { 88 | return currentFloor; 89 | } 90 | 91 | public boolean getDoorStatus() { 92 | return doorOpen; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch09/composition/Car.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch09.composition; 2 | 3 | public class Car { 4 | private String make; 5 | private String model; 6 | private String year; 7 | private Wheel[] wheels = new Wheel[4]; 8 | private Engine engine; 9 | private int doorCount; 10 | private Door[] doors; 11 | private Transmission transmission; 12 | 13 | private int speed; 14 | private int distance; 15 | 16 | public Car(String make, String model, String year, int doorCount) { 17 | this.make = make; 18 | this.model = model; 19 | this.year = year; 20 | 21 | engine = new Engine(2300, 175); 22 | 23 | transmission = new Transmission(); 24 | 25 | wheels[0] = new Wheel("Left-front"); 26 | wheels[1] = new Wheel("Right-front"); 27 | wheels[2] = new Wheel("Left-rear"); 28 | wheels[3] = new Wheel("Right-rear"); 29 | 30 | this.doorCount = doorCount; 31 | doors = new Door[this.doorCount]; 32 | doors[0] = new Door("Left-front"); 33 | doors[1] = new Door("Right-front"); 34 | 35 | if (this.doorCount == 4) { 36 | doors[2] = new Door("Left-rear"); 37 | doors[3] = new Door("Right-rear"); 38 | } 39 | 40 | System.out.println("A car of " + make + " " + model + ", " + year + " is created.\n"); 41 | } 42 | 43 | public String getMake() { 44 | return make; 45 | } 46 | 47 | public void setMake(String make) { 48 | this.make = make; 49 | } 50 | 51 | public String getModel() { 52 | return model; 53 | } 54 | 55 | public void setModel(String model) { 56 | this.model = model; 57 | } 58 | 59 | public String getYear() { 60 | return year; 61 | } 62 | 63 | public void setYear(String year) { 64 | this.year = year; 65 | } 66 | 67 | public Wheel[] getWheels() { 68 | return wheels; 69 | } 70 | 71 | public void setWheels(Wheel[] wheels) { 72 | this.wheels = wheels; 73 | } 74 | 75 | public Engine getEngine() { 76 | return engine; 77 | } 78 | 79 | public void setEngine(Engine engine) { 80 | this.engine = engine; 81 | } 82 | 83 | public Door[] getDoors() { 84 | return doors; 85 | } 86 | 87 | public void setDoors(Door[] doors) { 88 | this.doors = doors; 89 | } 90 | 91 | public int getSpeed() { 92 | return speed; 93 | } 94 | 95 | public void setSpeed(int speed) { 96 | this.speed = speed; 97 | } 98 | 99 | public int getDistance() { 100 | return distance; 101 | } 102 | 103 | public void setDistance(int distance) { 104 | this.distance = distance; 105 | } 106 | 107 | public Transmission getTransmission() { 108 | return transmission; 109 | } 110 | 111 | public void setTransmission(Transmission transmission) { 112 | this.transmission = transmission; 113 | } 114 | 115 | public void start() { 116 | engine.start(); 117 | } 118 | 119 | public void go(int newDistance) { 120 | for (Door door : doors) { 121 | if (!door.isClosed()) 122 | door.close(); 123 | } 124 | System.out.println("Car is moving."); 125 | distance += newDistance; 126 | System.out.println("Distance: " + distance); 127 | } 128 | 129 | public void accelerate(int newSpeed) { 130 | speed = newSpeed; 131 | System.out.println("Accelerating. Speed: " + speed); 132 | } 133 | 134 | public void stop() { 135 | speed = 0; 136 | System.out.println("Car stopped. Speed: " + speed); 137 | } 138 | 139 | public String getInfo() { 140 | return "Car Info: " + year + " " + make + " " + model + ". Distance: " + distance + " km. and traveling at " 141 | + speed + " kmph."; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/encapsulation/calendar/Calendar.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch08.encapsulation.calendar; 2 | 3 | public class Calendar { 4 | 5 | public Date nextDay(Date date) { 6 | 7 | int day = date.getDay(); 8 | String month = date.getMonth(); 9 | int year = date.getYear(); 10 | 11 | // First check wrong days and months 12 | if (day > 31) { 13 | System.out.println("Wrong date entered: " + date); 14 | System.exit(1); 15 | } 16 | 17 | if (year < 0) { 18 | System.out.println("Wrong date entered: " + date); 19 | System.exit(1); 20 | } 21 | 22 | if (!isMonthCorrect(month)) { 23 | System.out.println("Wrong date entered: " + date); 24 | System.exit(1); 25 | } 26 | 27 | // February is a special case so ahndle it separately 28 | if (month.equalsIgnoreCase("February") || month.equalsIgnoreCase("Subat")) { 29 | if (day == 28) { 30 | boolean isLeapYear = isLeapYear(year); 31 | if (isLeapYear) 32 | day = 29; 33 | else { 34 | day = 1; 35 | month = "March"; 36 | } 37 | } else if (day == 29) { 38 | boolean isLeapYear = isLeapYear(year); 39 | if (isLeapYear) { 40 | day = 1; 41 | month = "March"; 42 | } else { 43 | System.out.println("Wrong date entered: " + date); 44 | System.exit(1); 45 | } 46 | } else if (day == 30 || day == 31) { 47 | System.out.println("Wrong date entered: " + date); 48 | System.exit(1); 49 | } else { 50 | day++; 51 | } 52 | date.setDay(day); 53 | date.setMonth(month); 54 | 55 | return date; 56 | } 57 | 58 | // For other months 59 | if (day < 30) 60 | day++; 61 | else { 62 | if (day == 30) { 63 | day = 1; 64 | if (month.equalsIgnoreCase("April") || month.equalsIgnoreCase("Nisan")) 65 | month = "May"; 66 | else if (month.equalsIgnoreCase("June") || month.equalsIgnoreCase("Haziran")) 67 | month = "July"; 68 | else if (month.equalsIgnoreCase("September") || month.equalsIgnoreCase("Eylul")) 69 | month = "October"; 70 | else if (month.equalsIgnoreCase("November") || month.equalsIgnoreCase("Kasim")) 71 | month = "December"; 72 | 73 | } else if (day == 31) { 74 | day = 1; 75 | if (month.equalsIgnoreCase("January") || month.equalsIgnoreCase("Ocak")) 76 | month = "February"; 77 | else if (month.equalsIgnoreCase("March") || month.equalsIgnoreCase("Mart")) 78 | month = "April"; 79 | else if (month.equalsIgnoreCase("May") || month.equalsIgnoreCase("Mayis")) 80 | month = "June"; 81 | else if (month.equalsIgnoreCase("July") || month.equalsIgnoreCase("Temmuz")) 82 | month = "August"; 83 | else if (month.equalsIgnoreCase("August") || month.equalsIgnoreCase("Agustos")) 84 | month = "September"; 85 | else if (month.equalsIgnoreCase("October") || month.equalsIgnoreCase("Ekim")) 86 | month = "November"; 87 | else if (month.equalsIgnoreCase("December") || month.equalsIgnoreCase("Aralik")) { 88 | month = "January"; 89 | year++; 90 | } 91 | } 92 | } 93 | 94 | date.setDay(day); 95 | date.setMonth(month); 96 | date.setYear(year); 97 | 98 | return date; 99 | } 100 | 101 | private boolean isLeapYear(int year) { 102 | boolean isLeapYear = false; 103 | 104 | // divisible by 4 105 | isLeapYear = (year % 4 == 0); 106 | 107 | // divisible by 4 and not 100 108 | isLeapYear = isLeapYear && (year % 100 != 0); 109 | 110 | // divisible by 4 and not 100 unless divisible by 400 111 | isLeapYear = isLeapYear || (year % 400 == 0); 112 | 113 | return isLeapYear; 114 | } 115 | 116 | private boolean isMonthCorrect(String month) { 117 | boolean isCorrectMonth = false; 118 | if (month.equalsIgnoreCase("January") || month.equalsIgnoreCase("February") || month.equalsIgnoreCase("March") || month.equalsIgnoreCase("April") || month.equalsIgnoreCase("May") || month.equalsIgnoreCase("June") || month.equalsIgnoreCase("July") || month.equalsIgnoreCase("August") || month.equalsIgnoreCase("September") || month.equalsIgnoreCase("October") || month.equalsIgnoreCase("November") || month.equalsIgnoreCase("December") || 119 | month.equalsIgnoreCase("Ocak") || month.equalsIgnoreCase("Subat") || month.equalsIgnoreCase("Mart") || month.equalsIgnoreCase("Nisan") || month.equalsIgnoreCase("Mayis") || month.equalsIgnoreCase("Haziran") || month.equalsIgnoreCase("Temmuz") || month.equalsIgnoreCase("Agustos") || month.equalsIgnoreCase("Eylul") || month.equalsIgnoreCase("Ekim") || month.equalsIgnoreCase("Kasim") || month.equalsIgnoreCase("Aralik")) 120 | isCorrectMonth = true; 121 | 122 | return isCorrectMonth; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch05/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | package org.javaturk.oop.ch05; 2 | 3 | import java.util.Scanner; 4 | 5 | public class FizzBuzz { 6 | private int constant1 = 3; 7 | private int constant2 = 5; 8 | private int multiplesOfConstant1; 9 | private int multiplesOfConstant2; 10 | private int multiplesOfMultiplication; 11 | private int numbers; 12 | 13 | public static void main(String[] args) { 14 | FizzBuzz fb = new FizzBuzz(); 15 | 16 | Scanner sc = new Scanner(System.in); 17 | System.out.print("Enter the upper limit: "); 18 | int limit = sc.nextInt(); 19 | // fb.printFizzBuzzWithMod(limit); 20 | // fb.printFizzBuzzWithMultiples1(limit); 21 | fb.printFizzBuzzWithMultiples2(limit); 22 | } 23 | 24 | public void printFizzBuzzWithMod(int k) { 25 | // In case this method runs after the other method 26 | multiplesOfConstant1 = 0; 27 | multiplesOfConstant2 = 0; 28 | multiplesOfMultiplication = 0; 29 | numbers = 0; 30 | 31 | long start = System.currentTimeMillis(); 32 | for (int i = 1; i <= k; i++) { 33 | if (i % constant1 == 0 && i % constant2 == 0) { 34 | // System.out.println("FizzBuzz"); 35 | multiplesOfMultiplication++; 36 | } else if (i % constant1 == 0) { 37 | // System.out.println("Fizz"); 38 | multiplesOfConstant1++; 39 | } else if (i % constant2 == 0) { 40 | // System.out.println("Buzz"); 41 | multiplesOfConstant2++; 42 | } else { 43 | // System.out.println(i); 44 | numbers++; 45 | } 46 | } 47 | long end = System.currentTimeMillis(); 48 | printCounts(); 49 | System.out.println("Time: " + (end - start) + " ms."); 50 | } 51 | 52 | public void printFizzBuzzWithMultiples1(int k) { 53 | int iteratonLimit = (int)(k/constant1); 54 | // In case this method runs after the other method 55 | multiplesOfConstant1 = 0; 56 | multiplesOfConstant2 = 0; 57 | multiplesOfMultiplication = 0; 58 | numbers = 0; 59 | 60 | long start = System.currentTimeMillis(); 61 | String[] fizzbuzz = new String[k]; 62 | // First fill the array with numbers up to k 63 | for (int i = 0; i < k; i++) 64 | fizzbuzz[i] = new Integer(i + 1).toString(); 65 | 66 | for (int i = 1; i <= iteratonLimit; i++) { 67 | // For multiples of 3 68 | int u = i * 3; 69 | if (!fizzbuzz[u - 1].equals("FizzBuzz")){ 70 | fizzbuzz[u - 1] = "Fizz"; 71 | multiplesOfConstant1++; 72 | } 73 | 74 | // For multiples of 5 75 | int v = i * constant2; 76 | if (v <= k && !fizzbuzz[v - 1].equals("FizzBuzz")){ 77 | fizzbuzz[v - 1] = "Buzz"; 78 | multiplesOfConstant2++; 79 | } 80 | 81 | // For multiples of 3*5 82 | int w = i * constant1 * constant2; 83 | if (w <= k){ 84 | fizzbuzz[w - 1] = "FizzBuzz"; 85 | multiplesOfMultiplication++; 86 | } 87 | } 88 | long end = System.currentTimeMillis(); 89 | 90 | 91 | 92 | // for (String string : fizzbuzz) { 93 | // System.out.println(string); 94 | // if (string.equals("Fizz")) 95 | // multiplesOfConstant1++; 96 | // else if (string.equals("Buzz")) 97 | // multiplesOfConstant2++; 98 | // else if (string.equals("FizzBuzz")) 99 | // multiplesOfMultiplication++; 100 | // else 101 | // numbers++; 102 | // } 103 | printCounts(); 104 | System.out.println("Time: " + (end - start) + " ms."); 105 | } 106 | 107 | public void printFizzBuzzWithMultiples2(int k) { 108 | // In case this method runs after the other method 109 | multiplesOfConstant1 = 0; 110 | multiplesOfConstant2 = 0; 111 | multiplesOfMultiplication = 0; 112 | numbers = 0; 113 | 114 | long start = System.currentTimeMillis(); 115 | int[] fizzbuzz = new int[k]; 116 | // First fill the array with numbers up to k 117 | for (int i = 0; i < k; i++) 118 | fizzbuzz[i] = 0; 119 | 120 | for (int i = 1; i <= 33; i++) { 121 | // For multiples of 3 122 | int u = i * 3; 123 | if (fizzbuzz[u - 1] != 3){ 124 | fizzbuzz[u - 1] = 3; 125 | multiplesOfConstant1++; 126 | } 127 | 128 | // For multiples of 5 129 | int v = i * constant2; 130 | if (v <= k && fizzbuzz[v - 1] != 5){ 131 | fizzbuzz[v - 1] = 5; 132 | multiplesOfConstant2++; 133 | } 134 | 135 | // For multiples of 3*5 136 | int w = i * constant1 * constant2; 137 | if (w <= k){ 138 | fizzbuzz[w - 1] = 15; 139 | multiplesOfMultiplication++; 140 | } 141 | } 142 | long end = System.currentTimeMillis(); 143 | 144 | 145 | 146 | // for (String string : fizzbuzz) { 147 | // System.out.println(string); 148 | // if (string.equals("Fizz")) 149 | // multiplesOfConstant1++; 150 | // else if (string.equals("Buzz")) 151 | // multiplesOfConstant2++; 152 | // else if (string.equals("FizzBuzz")) 153 | // multiplesOfMultiplication++; 154 | // else 155 | // numbers++; 156 | // } 157 | printCounts(); 158 | System.out.println("Time: " + (end - start) + " ms."); 159 | } 160 | 161 | public void printCounts() { 162 | System.out.println("Multiples of" + constant1 + ": " + multiplesOfConstant1); 163 | System.out.println("Multiples of" + constant2 + ": " + multiplesOfConstant2); 164 | System.out.println("Multiples of" + constant1 * constant2 + ": " + multiplesOfMultiplication); 165 | System.out.println("Others: " + numbers); 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch08/Bulp.java: -------------------------------------------------------------------------------- 1 | 2 | package org.javaturk.oop.ch08; 3 | 4 | /** 5 | * A Bulp is a bulp. 6 | * This class simulates a bulp. It has a default constructor that creates a bulp 7 | * with a max luminescence of 60 and another constructor that creates a bulp with 8 | * specified max luminescence. 9 | *

10 | * This class has methods to turn on and turn off the bulp and to increase and 11 | * to decrease the current luminescence of the bulp. 12 | * 13 | * @author Akin Kaldiroglu 14 | * @version 1.3.7 15 | * @see JavaTurk.org 16 | * @since 1.2 17 | */ 18 | public class Bulp{ 19 | 20 | public static final int STANDARD_MAX_LUMINESCENCE = 60; 21 | String name; 22 | private int currentLuminescence; 23 | protected int maxLuminescence; 24 | 25 | /** Creates a new Bulp object with standard luminescence. */ 26 | public Bulp() { 27 | maxLuminescence = STANDARD_MAX_LUMINESCENCE; 28 | currentLuminescence = 0; 29 | System.out.println("A bulb with max luminescence of " + maxLuminescence + " created."); 30 | } 31 | 32 | /** 33 | * Creates a new Bulp object with specified max luminescence 34 | * @param maxLuminescenceValue Max luminescence of the bulp 35 | */ 36 | public Bulp(int maxLuminescenceValue) { 37 | maxLuminescence = maxLuminescenceValue; 38 | currentLuminescence = 0; 39 | name = "bulb" + new String((new Integer(maxLuminescence)).toString()); 40 | System.out.println("A bulb " + name + " with max luminescence of " + maxLuminescence + " created."); 41 | } 42 | 43 | /** 44 | * Sets max luminescence to specified value 45 | * @param value Specified max luminescence 46 | * @exception IllegalArgumentException in case of passing non-positive argument. 47 | */ 48 | public void setMaxLuminescence(int value) throws IllegalArgumentException{ 49 | if(value <= 0) 50 | throw new IllegalArgumentException("Luminescence value passed must be greater than zero: " + value); 51 | maxLuminescence = value; 52 | } 53 | 54 | /** 55 | * Gets max luminescence of the bulp. 56 | * @return max luminescence. 57 | */ 58 | public int getMaxLuminescence(){ 59 | return maxLuminescence; 60 | } 61 | 62 | /** 63 | * Turns on the bulp. 64 | * Curent luminescence is set to max luminescence. 65 | */ 66 | public void on(){ 67 | currentLuminescence = maxLuminescence; 68 | System.out.println("Bulb " + this.name + " is on now"); 69 | } 70 | 71 | /** 72 | * Turns off the bulp. 73 | * Curent luminescence is set to 0. 74 | */ 75 | public void off(){ 76 | currentLuminescence = 0; 77 | System.out.println("Bulp " + this.name + " off now"); 78 | } 79 | 80 | /** Just brightens the bulp. */ 81 | public void brighten(){ 82 | System.out.println("Bulb " + this.name + " is brighter now"); 83 | } 84 | 85 | /** 86 | * Brightens the bulp to specified value. 87 | * @param value New luminescence of the bulp. 88 | * @exception IllegalArgumentException in case of passing non-positive argument. 89 | */ 90 | public void brighten(int value) throws IllegalArgumentException{ 91 | if(value <= 0) 92 | throw new IllegalArgumentException("Luminescence value passed must be greater than zero: " + value); 93 | 94 | if(currentLuminescence < value){ 95 | if(value <= maxLuminescence){ 96 | currentLuminescence = value; 97 | System.out.println("Luminescence of " + this.name + " is increased to " + currentLuminescence); 98 | } 99 | else{ 100 | System.out.println("Max luminescence of " + this.name + " is " + maxLuminescence); 101 | } 102 | } 103 | else{ 104 | System.out.println("Do you want to make " + this.name + " brighter or dimmer?"); 105 | System.out.println("Current luminescence of " + this.name + " is " + currentLuminescence); 106 | System.out.println("You tried to brighten it to " + value); 107 | } 108 | } 109 | 110 | /** Just dims the bulp. */ 111 | public void dim(){ 112 | System.out.println("Bulb " + this.name + " is dimmer now"); 113 | } 114 | 115 | /** 116 | * Dims the bulp to specified value. 117 | * @param value New luminescence of the bulp 118 | */ 119 | public void dim(int value) throws IllegalArgumentException{ 120 | if(value <= 0) 121 | throw new IllegalArgumentException("Luminescence value passed must be greater than zero."); 122 | 123 | if(currentLuminescence > value){ 124 | if(value >= 0){ 125 | currentLuminescence = value; 126 | System.out.println("Luminescence of " + this.name + " is decreased to " + currentLuminescence); 127 | } 128 | else{ 129 | System.out.println("Don't be that silly! I am just a bulb not a black hole"); 130 | } 131 | } 132 | else{ 133 | System.out.println("Do you want to make " + this.name + " brighter or dimmer?"); 134 | System.out.println("Current luminescence of " + this.name + " is " + currentLuminescence); 135 | System.out.println("You tried to dim it to " + value); 136 | } 137 | } 138 | 139 | /** 140 | * Main method to create Bulp object and plays with it. 141 | * @param args[] Standard array of String arguments. 142 | */ 143 | public static void main(String args[]){ 144 | Bulp bulb = new Bulp(); 145 | bulb.setMaxLuminescence(100); 146 | int lum = bulb.getMaxLuminescence(); 147 | System.out.println(lum); 148 | System.out.println(bulb.getMaxLuminescence()); 149 | 150 | Bulp bulb100 = new Bulp(100); 151 | Bulp bulb80 = new Bulp(80); 152 | 153 | bulb100.brighten(80); 154 | bulb80.brighten(40); 155 | bulb100.brighten(40); 156 | bulb80.brighten(); 157 | bulb100.dim(40); 158 | bulb80.dim(); 159 | bulb100.dim(60); 160 | bulb100.brighten(120); 161 | bulb100.dim(40); 162 | } 163 | } 164 | 165 | -------------------------------------------------------------------------------- /org/javaturk/oop/ch03/unicode/CharacterBlock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DBFrame1.java 3 | * 4 | * Created on 18 Temmuz 2002 Per�embe, 14:57 5 | */ 6 | 7 | package org.javaturk.oop.ch03.unicode; 8 | 9 | import java.util.*; 10 | 11 | public class CharacterBlock { 12 | private String name; 13 | private int start; 14 | private int end; 15 | private static Hashtable blocks = new Hashtable(66); 16 | 17 | /* The blocks given here are as listed in The Unicode Standard, 18 | Version 2.0. Ranges are given in hexadecimal as in that 19 | document. It is not difficult to add additional blocks to this 20 | list as they're defined. 21 | */ 22 | static { 23 | 24 | // General Scripts 25 | 26 | makeBlock("Basic Latin", 0x0000, 0x007F); 27 | makeBlock("Latin-1 Supplement", 0x0080, 0x00FF); 28 | makeBlock("Latin Extended-A", 0x0100, 0x017F); 29 | makeBlock("Latin Extended-B", 0x0180, 0x024F); 30 | makeBlock("IPA Extensions", 0x0250, 0x02AF); 31 | makeBlock("Spacing Modifier Letters", 0x02B0, 0x02FF); 32 | makeBlock("Combining Diacritical Marks", 0x0300, 0x036F); 33 | makeBlock("Greek", 0x0370, 0x03FF); 34 | makeBlock("Cyrillic", 0x0400, 0x04FF); 35 | makeBlock("Armenian", 0x0530, 0x058F); 36 | makeBlock("Hebrew", 0x0590, 0x05FF); 37 | makeBlock("Arabic", 0x0600, 0x06FF); 38 | makeBlock("Devanagari", 0x0900, 0x097F); 39 | makeBlock("Bengali", 0x0980, 0x09FF); 40 | makeBlock("Gurmukhi", 0x0A00, 0x0A7F); 41 | makeBlock("Gujarati", 0x0A80, 0x0AFF); 42 | makeBlock("Oriya", 0x0B00, 0x0B7F); 43 | makeBlock("Tamil", 0x0B80, 0x0BFF); 44 | makeBlock("Telugu", 0x0C00, 0x0C7F); 45 | makeBlock("Kannada", 0x0C80, 0x0CFF); 46 | makeBlock("Malayalam", 0x0D00, 0x0D7F); 47 | makeBlock("Thai", 0x0E00, 0x0E7F); 48 | makeBlock("Lao", 0x0E80, 0x0EFF); 49 | makeBlock("Tibetan", 0x0F00, 0x0FBF); 50 | makeBlock("Georgian", 0x10A0, 0x10FF); 51 | makeBlock("Hangul Jamo", 0x1100, 0x11FF); 52 | makeBlock("Latin Extended Additional", 0x1E00, 0x1EFF); 53 | makeBlock("Greek Extended", 0x1F00, 0x1FFF); 54 | 55 | // Symbols 56 | makeBlock("General Punctuation", 0x2000, 0x206F); 57 | makeBlock("Superscripts and Subscripts", 0x2070, 0x209F); 58 | makeBlock("Currency Symbols", 0x20A0, 0x20CF); 59 | makeBlock("Combining Marks for Symbols", 0x20D0, 0x20FF); 60 | makeBlock("Letterlike Symbols", 0x2100, 0x214F); 61 | makeBlock("Number Forms", 0x2150, 0x218F); 62 | makeBlock("Arrows", 0x2190, 0x21FF); 63 | makeBlock("Mathematical Operators", 0x2200, 0x22FF); 64 | makeBlock("Miscellaneous Technical", 0x2300, 0x234F); 65 | makeBlock("Control Pictures", 0x2400, 0x243F); 66 | makeBlock("Optical Character Recognition", 0x2440, 0x245F); 67 | makeBlock("Enclosed Alphanumerics", 0x2460, 0x24FF); 68 | makeBlock("Box Drawing", 0x2500, 0x257F); 69 | makeBlock("Block Elements", 0x2580, 0x259F); 70 | makeBlock("Geometric Shapes", 0x25A0, 0x25FF); 71 | makeBlock("Miscellaneous Symbols", 0x2600, 0x26FF); 72 | makeBlock("Dingbats", 0x2700, 0x27BF); 73 | 74 | // Chinese-Japanese-Korean Phonetics and Symbols 75 | makeBlock("CJK Symbols and Punctuation", 0x3000, 0x303F); 76 | makeBlock("Hiragana", 0x3040, 0x309F); 77 | makeBlock("Katakana", 0x30A0, 0x30FF); 78 | makeBlock("Bopomofo", 0x3100, 0x312F); 79 | makeBlock("Hangul Compatibility Jamo", 0x3130, 0x318F); 80 | makeBlock("Kanbun", 0x3190, 0x319F); 81 | makeBlock("Enclosed CJK Letters and Months", 0x3200, 0x32FF); 82 | makeBlock("CJK Compatibility", 0x3300, 0x33FF); 83 | 84 | // Chinese-Japanese-Korean Ideographs 85 | makeBlock("CJK Unified Ideographs", 0x4E00, 0x9FFF); 86 | 87 | // Hangul Syllables 88 | makeBlock("Hangul Syllables", 0xAC00, 0xD7A3); 89 | 90 | // Surrogates 91 | makeBlock("Surrogates", 0xD800, 0xDFFF); 92 | 93 | // Private Use 94 | makeBlock("Private Use", 0xE000, 0xF8FF); 95 | 96 | // Compatibility and Specials 97 | makeBlock("CJK Compatibility Ideographs", 0xF900, 0xFAFF); 98 | makeBlock("Alphabetic Presentation Forms", 0xFB00, 0xFB4F); 99 | makeBlock("Arabic Presentation Forms", 0xFB50, 0xFDFF); 100 | makeBlock("Combining Half Marks", 0xFE20, 0xFE2F); 101 | makeBlock("CJK Compatibility Forms", 0xFE30, 0xFE4F); 102 | makeBlock("Small Form Variants", 0xFE50, 0xFE6F); 103 | makeBlock("More Arabic Presentation Forms", 0xFE70, 0xFEFF); 104 | makeBlock("Halfwidth and Fullwidth Forms", 0xFF00, 0xFFEF); 105 | makeBlock("Specials", 0xFEFF, 0xFFFF); 106 | } 107 | 108 | /* Originally I inlined this method from the static block where it's invoked. 109 | However, that produced excessively long lines in the static block that 110 | wouldn't look good in the printed book. So I split out this common 111 | invocation here. Since it's final, a good compiler can optimize it away 112 | anyway. 113 | */ 114 | private final static void makeBlock(String name, int start, int end) { 115 | blocks.put(name, new CharacterBlock(name, start, end)); 116 | } 117 | 118 | private CharacterBlock(String name, int start, int end) { 119 | this.name = name; 120 | if (start < Character.MIN_VALUE || start > Character.MAX_VALUE 121 | || end < Character.MIN_VALUE || end > Character.MAX_VALUE) { 122 | throw new IllegalArgumentException("Ranges must fall between 0 and 65,535"); 123 | } 124 | if (end < start) { 125 | throw new IllegalArgumentException("End must come after start"); 126 | } 127 | this.start = start; 128 | this.end = end; 129 | } 130 | 131 | public static CharacterBlock getBlock(String name) { 132 | return (CharacterBlock) blocks.get(name); 133 | } 134 | 135 | public static int getNumBlocks() { 136 | return blocks.size(); 137 | } 138 | 139 | public static String[] getBlockNames() { 140 | CharacterBlock[] cb = new CharacterBlock[blocks.size()]; 141 | Enumeration e = blocks.elements(); 142 | for (int i = 0; e.hasMoreElements(); i++) { 143 | cb[i] = ((CharacterBlock) e.nextElement()); 144 | } 145 | // bubble sort based on start 146 | boolean sorted = false; 147 | while (!sorted) { 148 | sorted = true; 149 | for (int i = 0; i < cb.length-1; i++) { 150 | if (cb[i].start > cb[i+1].start) { 151 | sorted = false; 152 | CharacterBlock temp = cb[i+1]; 153 | cb[i+1] = cb[i]; 154 | cb[i] = temp; 155 | } 156 | } 157 | } 158 | String[] names = new String[blocks.size()]; 159 | for (int i = 0; i < names.length; i++) names[i] = cb[i].name; 160 | return names; 161 | } 162 | 163 | public char[] getCharactersInBlock() { 164 | char[] block = new char[end-start+1]; 165 | int j = 0; 166 | for (int i = start; i <= end; i++) { 167 | if (Character.isDefined((char) i)) { 168 | block[j++] = (char) i; 169 | } 170 | } 171 | char[] result = new char[j]; 172 | System.arraycopy(block, 0, result, 0, j); 173 | return result; 174 | } 175 | 176 | public String getName() { 177 | return this.name; 178 | } 179 | 180 | public int getStart() { 181 | return this.start; 182 | } 183 | 184 | public int getEnd() { 185 | return this.end; 186 | } 187 | } 188 | 189 | --------------------------------------------------------------------------------