└── src ├── lessons ├── imports │ ├── mest │ │ └── A.java │ ├── test │ │ ├── A.java │ │ └── Math.java │ ├── DuplicateErrorExample.java │ ├── StaticImport.java │ ├── SingleTypeDeclaration.java │ └── SingleTypeStaticImport.java ├── namelookup │ ├── test │ │ └── B.java │ ├── C.java │ └── A.java ├── App.java ├── oop │ ├── basics │ │ ├── constructors │ │ │ ├── basicshoppingapp │ │ │ │ ├── BasicBankApp.java │ │ │ │ ├── BasicShoppingApp.java │ │ │ │ ├── BasicBankTest.java │ │ │ │ ├── Phone.java │ │ │ │ ├── Wallet.java │ │ │ │ ├── BasicShoppingTest.java │ │ │ │ ├── Card.java │ │ │ │ ├── Customer.java │ │ │ │ ├── BankApp.java │ │ │ │ └── Market.java │ │ │ └── intro │ │ │ │ └── Intro.java │ │ ├── nonstatics │ │ │ ├── Example3.java │ │ │ ├── Example1.java │ │ │ ├── Example2.java │ │ │ ├── Intro.java │ │ │ └── Example.java │ │ ├── UserDefinedType.java │ │ ├── Cast.java │ │ ├── examples │ │ │ ├── Tricky.java │ │ │ ├── TwoReferenceOneInstance.java │ │ │ ├── ReferanceAndObject.java │ │ │ ├── date │ │ │ │ └── DateUtil.java │ │ │ ├── EquationSolver.java │ │ │ └── point │ │ │ │ └── Point.java │ │ ├── ClassMembers.java │ │ ├── Example1.java │ │ ├── statics │ │ │ ├── Intro.java │ │ │ └── GameApp.java │ │ ├── Heap.java │ │ ├── randomclass │ │ │ ├── Example1.java │ │ │ ├── Example.java │ │ │ ├── Intro.java │ │ │ ├── Example2.java │ │ │ ├── SameTwoDiceAppSimple.java │ │ │ ├── CoinSimulationApp.java │ │ │ └── Seed.java │ │ ├── StackExample.java │ │ └── Defaults.java │ ├── BasicCharacter.java │ └── stringexamples │ │ ├── CMD.java │ │ ├── StringMethodTest.java │ │ ├── BasicCharacterTest.java │ │ └── StringMethods.java ├── arrays │ ├── lottery │ │ ├── NumericLotteryApp.java │ │ ├── Customer.java │ │ ├── NumericLottery.java │ │ └── NumericLotteryShop.java │ ├── Initialization.java │ ├── Assign.java │ ├── Intro.java │ └── examples │ │ ├── ArrayMethods.java │ │ └── ArrayMethodsTest.java ├── consolio │ ├── stringintro │ │ ├── Trim.java │ │ ├── ChartAt.java │ │ ├── Immutable.java │ │ ├── SubString.java │ │ ├── StringClass.java │ │ ├── IndexOf.java │ │ ├── Length.java │ │ ├── Equals.java │ │ ├── UpperLower.java │ │ └── CompareTo.java │ ├── PrintMethods.java │ ├── ScannerClass.java │ └── ScannerExamples.java ├── loops │ ├── forloops │ │ ├── ForExample8.java │ │ ├── ForExample4.java │ │ ├── ForExample2.java │ │ ├── ForIntro.java │ │ ├── ForExample9.java │ │ ├── ForExample1.java │ │ ├── ForExample3.java │ │ ├── ForExample5.java │ │ ├── ForExample7.java │ │ └── ForExample6.java │ ├── Break.java │ ├── whileloops │ │ ├── WhileExample2.java │ │ ├── InfinityLoopWithWhile.java │ │ ├── WhileExample6.java │ │ ├── WhileExample.java │ │ ├── WhileExample5.java │ │ ├── WhileExample7.java │ │ ├── WhileIntro.java │ │ ├── WhileExample3.java │ │ └── WhileExample4.java │ ├── dowhileloops │ │ ├── DoWhileIntro.java │ │ └── DoWhileExample.java │ ├── BreakIntro.java │ ├── ContinueExample.java │ └── LabeledBreak.java ├── methods │ ├── VoidTypeMethods.java │ ├── MethodIntro.java │ ├── MethodIntroExample.java │ ├── MethodCalling.java │ ├── MethodsWithParameters.java │ ├── methodoverloading │ │ ├── Example2.java │ │ ├── Example1.java │ │ └── MethodOverloading.java │ └── ReturnTypeMethods.java ├── flowcontrol │ ├── switchcase │ │ ├── LeapYear.java │ │ ├── SwitchExample.java │ │ ├── SwitchIntro.java │ │ ├── SwitchExample2.java │ │ └── MenuApp.java │ └── ifelse │ │ ├── IfElseExample2.java │ │ ├── IfElseExample4.java │ │ ├── IfElseExample3.java │ │ ├── BooleanExpressions.java │ │ ├── IfElseExample.java │ │ └── IfElse.java └── languagebasics │ ├── TernaryOperator.java │ ├── ExpressionsAndAssignment.java │ ├── Operators.java │ ├── OrOperators.java │ ├── PrimitiveTypes.java │ ├── AndOperators.java │ ├── OperatorsExample.java │ └── README.md ├── homeworks ├── chapter1 │ ├── Q5.java │ ├── Q9.java │ ├── Q7.java │ ├── Q6.java │ ├── Q3.java │ ├── Q8.java │ ├── Q4.java │ ├── Q1.java │ └── Q2.java ├── extra │ └── hw1 │ │ ├── Q3.java │ │ ├── Q2.java │ │ ├── Q5.java │ │ ├── Q4.java │ │ └── Q1.java ├── chapter2 │ ├── Q8.java │ ├── Q10.java │ ├── Q1.java │ ├── Q7.java │ ├── Q5.java │ ├── Q6.java │ ├── Q9.java │ ├── Q3.java │ ├── Q4.java │ └── Q2.java └── chapter3 │ ├── Q4.java │ ├── chapter3 │ ├── Q4.java │ ├── Q2.java │ ├── Q1.java │ └── Q3.java │ ├── Q2.java │ ├── Q1.java │ ├── Q3.java │ ├── Q6.java │ ├── Q7.java │ └── Q5.java └── hackerrank └── easy ├── SampleInput.java └── SmallestAndLargest.java /src/lessons/imports/mest/A.java: -------------------------------------------------------------------------------- 1 | package lessons.imports.mest; 2 | 3 | public class A { 4 | } 5 | -------------------------------------------------------------------------------- /src/lessons/imports/test/A.java: -------------------------------------------------------------------------------- 1 | package lessons.imports.test; 2 | 3 | public class A { 4 | } 5 | -------------------------------------------------------------------------------- /src/lessons/namelookup/test/B.java: -------------------------------------------------------------------------------- 1 | package lessons.namelookup.test; 2 | 3 | public class B { 4 | public int s; 5 | } 6 | -------------------------------------------------------------------------------- /src/lessons/App.java: -------------------------------------------------------------------------------- 1 | package lessons; 2 | 3 | public class App { 4 | public static void main(String[] args) 5 | { 6 | 7 | } 8 | } 9 | 10 | -------------------------------------------------------------------------------- /src/lessons/namelookup/C.java: -------------------------------------------------------------------------------- 1 | package lessons.namelookup; 2 | 3 | public class C { 4 | public static void main(String[] args) { 5 | B b = new B(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/BasicBankApp.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class BasicBankApp { 4 | public static void main(String[] args) 5 | { 6 | BasicBankTest.run(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/homeworks/chapter1/Q5.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter1; 2 | 3 | public class Q5 { 4 | public static void main(String[] args) 5 | { 6 | String text = "I hate you"; 7 | 8 | System.out.println(text.replace("hate", "love")); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/BasicShoppingApp.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class BasicShoppingApp { 4 | public static void main(String[] args) 5 | { 6 | BasicShoppingTest.run(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/lessons/imports/DuplicateErrorExample.java: -------------------------------------------------------------------------------- 1 | package lessons.imports; 2 | 3 | import lessons.imports.test.*; 4 | import lessons.imports.mest.*; 5 | 6 | public class DuplicateErrorExample { 7 | public static void run() 8 | { 9 | // A a = new A(); // -> ambiguous error 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/lessons/arrays/lottery/NumericLotteryApp.java: -------------------------------------------------------------------------------- 1 | package lessons.arrays.lottery; 2 | 3 | import lessons.arrays.examples.ArrayMethods; 4 | 5 | import java.util.Random; 6 | 7 | public class NumericLotteryApp { 8 | public static void main(String[] args) 9 | { 10 | NumericLotteryShop.run(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/Trim.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class Trim { 4 | public static void main(String[] args) 5 | { 6 | String s = " Efficient house 35th video "; 7 | System.out.printf("(%s)%n", s); 8 | System.out.printf("(%s)\n", s.trim()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/nonstatics/Example3.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.nonstatics; 2 | 3 | public class Example3 { 4 | public int x; 5 | 6 | public static void changeX(Example3 example3, int a) 7 | { 8 | example3.changeXWithNonstatic(a); 9 | } 10 | 11 | public void changeXWithNonstatic(int a) 12 | { 13 | x = a; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/lessons/imports/StaticImport.java: -------------------------------------------------------------------------------- 1 | package lessons.imports; 2 | 3 | import static lessons.imports.test.Math.*; 4 | 5 | public class StaticImport { 6 | public static void run() 7 | { 8 | System.out.println(add(5, 3)); 9 | System.out.println(subst(10, 5)); 10 | System.out.println(multiply(6, 9)); 11 | System.out.println(divide(10, 3)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/nonstatics/Example1.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.nonstatics; 2 | 3 | public class Example1 { 4 | public static void main(String[] args) 5 | { 6 | Z.changeX(10); 7 | } 8 | } 9 | 10 | class Z { 11 | public int x; 12 | 13 | public static void changeX(int a) 14 | { 15 | // x = a; // ERROR: Non-static field 'x' cannot be referenced from a static context 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/homeworks/chapter1/Q9.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter1; 2 | 3 | public class Q9 { 4 | public static void main(String[] args) 5 | { 6 | int minimumWeight = 110; 7 | int heightFt = 6; 8 | int heightInch = heightFt * 12; 9 | int bodyWeight = minimumWeight + (heightInch % 60) * 5; 10 | 11 | System.out.printf("Height = %d(ft) = Ideal Body Weight: %d(pound)", heightFt, bodyWeight); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForExample8.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForExample8 { 4 | public static void main(String[] args) 5 | { 6 | System.out.println(factorial(5)); 7 | } 8 | 9 | public static int factorial(int n) 10 | { 11 | int fact = 1; 12 | 13 | for (int i = 2; i <= n; i++) { 14 | fact *= i; 15 | } 16 | 17 | return fact; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/UserDefinedType.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics; 2 | 3 | public class UserDefinedType { 4 | public static void main(String[] args) 5 | { 6 | Car c; // reference variable 7 | Bottle b; // reference variable 8 | Door d; // reference variable 9 | } 10 | } 11 | 12 | class Car { 13 | //... 14 | } 15 | 16 | class Bottle { 17 | //... 18 | } 19 | 20 | class Door { 21 | //... 22 | } 23 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/BasicBankTest.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class BasicBankTest { 4 | public static void run() 5 | { 6 | Customer yavuz = new Customer("Yavuz", new Wallet(new Card("99999", "1234", 1000), 10),new Phone("536 663 336"), "434445", "12345"); 7 | 8 | BankApp ziraat = new BankApp(); 9 | ziraat.login(yavuz); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/Cast.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics; 2 | 3 | public class Cast { 4 | public static void main(String[] args) 5 | { 6 | X x; 7 | Y y; 8 | 9 | x = new X(); 10 | 11 | // y = (Y)x; ERROR 12 | } 13 | } 14 | 15 | class X { 16 | public int x; 17 | public boolean b; 18 | 19 | //... 20 | } 21 | 22 | class Y { 23 | public int x; 24 | public boolean b; 25 | 26 | //... 27 | } 28 | -------------------------------------------------------------------------------- /src/lessons/imports/test/Math.java: -------------------------------------------------------------------------------- 1 | package lessons.imports.test; 2 | 3 | public class Math { 4 | public static int add(int a, int b) { 5 | return a + b; 6 | } 7 | 8 | public static int subst(int a, int b) { 9 | 10 | return a - b; 11 | } 12 | 13 | public static int multiply(int a, int b) { 14 | return a * b; 15 | } 16 | 17 | public static double divide(int a, int b) { 18 | return (double)a / b; 19 | } 20 | 21 | //... 22 | } 23 | -------------------------------------------------------------------------------- /src/homeworks/chapter1/Q7.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter1; 2 | 3 | public class Q7 { 4 | public static void main(String[] args) 5 | { 6 | int seconds = 50391; 7 | 8 | int hours = seconds / 3600; 9 | seconds %= 3600; 10 | 11 | int minutes = seconds / 60; 12 | seconds %= 60; 13 | 14 | System.out.printf("%02d:%02d:%02d%n", hours, minutes, seconds); 15 | System.out.printf("%02d hours, %02d minutes, %02d seconds", hours, minutes, seconds); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/homeworks/chapter1/Q6.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter1; 2 | 3 | public class Q6 { 4 | public static void main(String[] args) 5 | { 6 | final double HOUR_TO_MINUTE = 60; 7 | final double PI = 3.14; 8 | final double INCH_TO_MILE = (1. / 12) * (1. / 5280); 9 | 10 | double gearSize = 100; 11 | double cadence = 90; 12 | 13 | double speed = gearSize * PI * INCH_TO_MILE * cadence * HOUR_TO_MINUTE; 14 | System.out.printf("Speed: %f (mph)", speed); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/hackerrank/easy/SampleInput.java: -------------------------------------------------------------------------------- 1 | package hackerrank.easy; 2 | 3 | public class SampleInput { 4 | public static void main(String[] args) { 5 | java.util.Scanner scan = new java.util.Scanner(System.in); 6 | 7 | int i = scan.nextInt(); 8 | double d = scan.nextDouble(); 9 | scan.nextLine(); 10 | String s = scan.nextLine(); 11 | 12 | 13 | System.out.println("String: " + s); 14 | System.out.println("Double: " + d); 15 | System.out.println("Int: " + i); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/homeworks/chapter1/Q3.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter1; 2 | 3 | public class Q3 { 4 | public static void main(String[] args) { 5 | String first = "walt"; 6 | String last = "savitch"; 7 | 8 | String pigLatinFirst = first.substring(1, 2).toUpperCase() + first.substring(2) + first.substring(0, 1) + "ay"; 9 | String pigLatinLast = last.substring(1, 2).toUpperCase() + last.substring(2) + last.substring(0, 1) + "ay"; 10 | System.out.println(pigLatinFirst + " " + pigLatinLast); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForExample4.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForExample4 { 4 | public static void main(String[] args) 5 | { 6 | int n = 5; 7 | int i; 8 | 9 | for (i = 0; i < n; ++i); 10 | System.out.printf("%d", i); // 5 11 | 12 | System.out.println("\n--------------"); 13 | 14 | 15 | for (i = n - 1; i >= 0; --i) 16 | System.out.printf("%d ", i); // 4 3 2 1 0 17 | 18 | System.out.printf("%ni = %d", i); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/examples/Tricky.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.examples; 2 | 3 | public class Tricky { 4 | public static void main(String[] args) 5 | { 6 | int a = 10; 7 | int b; 8 | 9 | b = 10; 10 | 11 | System.out.println(a); // 10 12 | System.out.println(b); // 10 13 | 14 | ++a; 15 | 16 | System.out.println(a); // 11 17 | System.out.println(b); // 10 18 | } 19 | } 20 | 21 | class X { 22 | public int a; 23 | public boolean b; 24 | 25 | //... 26 | } 27 | -------------------------------------------------------------------------------- /src/homeworks/chapter1/Q8.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter1; 2 | 3 | public class Q8 { 4 | public static final double DISTANCE = 6.21; 5 | 6 | public static void main(String[] args) 7 | { 8 | double time, pace; 9 | 10 | System.out.println("This program calculates your pace given a time" 11 | + "and distance traveled."); 12 | 13 | time = 35.5; /* 35 minutes and 30 seconds */ 14 | pace = time / DISTANCE; 15 | 16 | System.out.printf("Your pace is %.2f miles per hour.", pace); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/nonstatics/Example2.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.nonstatics; 2 | 3 | public class Example2 { 4 | public static void main(String[] args) 5 | { 6 | K k = new K(); 7 | k.bar(); 8 | } 9 | } 10 | 11 | class K { 12 | public static void foo() 13 | { 14 | // bar(); ERROR: Non-static method 'bar()' cannot be referenced from a static context 15 | System.out.println("foo()"); 16 | } 17 | 18 | public void bar() 19 | { 20 | System.out.println("bar()"); 21 | foo(); 22 | } 23 | } -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/Phone.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class Phone { 4 | public String phoneNumber; 5 | 6 | Phone(String newPhoneNumber) 7 | { 8 | phoneNumber = newPhoneNumber; 9 | } 10 | 11 | public String getPhoneNumber() 12 | { 13 | return phoneNumber; 14 | } 15 | 16 | public void call(Customer customer) 17 | { 18 | System.out.printf("(%s) - (%s) calling... %n", customer.getName(), customer.getPhone().getPhoneNumber()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/lessons/loops/Break.java: -------------------------------------------------------------------------------- 1 | package lessons.loops; 2 | 3 | public class Break { 4 | public static void main(String[] args) 5 | { 6 | boolean breakAllLoop = false; 7 | 8 | for (int i = 1; i < 10; ++i) { 9 | for (int j = 20; j >= 2; --j) { 10 | System.out.printf("(%d, %d)", i, j); 11 | 12 | if ((i + j) % 6 == 0) { 13 | breakAllLoop = true; 14 | break; 15 | } 16 | } 17 | if (breakAllLoop) 18 | break; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/lessons/loops/whileloops/WhileExample2.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.whileloops; 2 | 3 | public class WhileExample2 { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner kb = new java.util.Scanner(System.in); 7 | 8 | System.out.print("How many times you want to loop through:"); 9 | int n = kb.nextInt(); 10 | 11 | System.out.println("----------"); 12 | 13 | int i = 0; 14 | 15 | while (i < n--) { 16 | System.out.println(n); 17 | } 18 | 19 | System.out.printf("n = %d", n); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForExample2.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForExample2 { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner kb = new java.util.Scanner(System.in); 7 | System.out.println("Number: "); 8 | int n = kb.nextInt(); 9 | 10 | for (int i = 0; i < n; i++) { 11 | System.out.printf("%d ", i); 12 | } 13 | 14 | System.out.println("------------------"); 15 | 16 | for (int i = 0; i < n; i++) { 17 | System.out.printf("%d ", i); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/ClassMembers.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics; 2 | 3 | public class ClassMembers { 4 | public static void main(String[] args) 5 | { 6 | int a; 7 | } 8 | } 9 | 10 | class A { 11 | public int x; // field / member field 12 | 13 | public static void foo() 14 | { 15 | //... 16 | } 17 | } 18 | 19 | class B { 20 | public int a; 21 | 22 | public static void foo(int x) 23 | { 24 | int a; 25 | } 26 | 27 | // double a; // error 28 | } 29 | 30 | class C { 31 | public int a, b, c; 32 | protected static double d, k; 33 | } 34 | -------------------------------------------------------------------------------- /src/lessons/loops/dowhileloops/DoWhileIntro.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.dowhileloops; 2 | 3 | public class DoWhileIntro { 4 | public static void main(String[] args) 5 | { 6 | /* 7 | do { 8 | 9 | } while (condition); 10 | */ 11 | 12 | java.util.Scanner kb = new java.util.Scanner(System.in); 13 | 14 | System.out.println("Number:"); 15 | int n = kb.nextInt(); 16 | 17 | int i = 0; 18 | 19 | do { 20 | System.out.printf("%d ", i); 21 | ++i; 22 | } while (i < n); 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/nonstatics/Intro.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.nonstatics; 2 | 3 | public class Intro { 4 | public static void main(String[] args) 5 | { 6 | A a = new A(); 7 | a.foo(); // Static member 'lessons.oop.intro.nonstatics.A.foo()' accessed via instance reference 8 | 9 | // A.bar(); --Non-static method 'bar()' cannot be referenced from a static context 10 | 11 | A.foo(); // ++ 12 | } 13 | } 14 | 15 | class A { 16 | public static void foo() 17 | { 18 | //... 19 | } 20 | 21 | public void bar() 22 | { 23 | //.. 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/lessons/imports/SingleTypeDeclaration.java: -------------------------------------------------------------------------------- 1 | package lessons.imports; 2 | 3 | import java.util.Random; 4 | 5 | public class SingleTypeDeclaration { 6 | public static void run() { 7 | java.util.Scanner kb = new java.util.Scanner(System.in); 8 | Random r = new Random(); 9 | 10 | System.out.println("Number:"); 11 | int n = Integer.parseInt(kb.nextLine()); 12 | 13 | 14 | for (int i = 0; i < n; ++i) 15 | System.out.printf("%d.random number: %d%n", i, r.nextInt(100)); 16 | 17 | Scanner s = new Scanner(); 18 | } 19 | } 20 | 21 | class Scanner { 22 | // ... 23 | } 24 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/Wallet.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class Wallet { 4 | public Card card; 5 | public int cashMoney; 6 | 7 | public Wallet(Card newCard, int newCashMoney) 8 | { 9 | card = newCard; 10 | cashMoney = newCashMoney; 11 | } 12 | 13 | public Card getCard() 14 | { 15 | return card; 16 | } 17 | 18 | public int getCashMoney() 19 | { 20 | return cashMoney; 21 | } 22 | 23 | public void setCashMoney(int money) 24 | { 25 | cashMoney = money; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/examples/TwoReferenceOneInstance.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.examples; 2 | 3 | public class TwoReferenceOneInstance { 4 | public static void main(String[] args) 5 | { 6 | A z; 7 | A y; 8 | 9 | z = new A(); 10 | y = z; 11 | 12 | z.x = 5; 13 | z.b = true; 14 | 15 | System.out.println(y.x); 16 | System.out.println(y.b); 17 | 18 | y.x = 10; 19 | y.b = false; 20 | 21 | System.out.println(z.x); 22 | System.out.println(z.b); 23 | } 24 | } 25 | 26 | class A { 27 | public int x; 28 | public boolean b; 29 | } 30 | -------------------------------------------------------------------------------- /src/lessons/loops/whileloops/InfinityLoopWithWhile.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.whileloops; 2 | 3 | public class InfinityLoopWithWhile { 4 | public static void main(String[] args) 5 | { 6 | byte number = 1; 7 | 8 | while (number > 0) { 9 | System.out.println("Its not infinite"); 10 | ++number; 11 | System.out.println(number); 12 | } 13 | 14 | while (true) { 15 | System.out.println("Its infinite ;)"); 16 | ++number; 17 | System.out.println(number); 18 | 19 | if (number == 127) 20 | return; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/lessons/methods/VoidTypeMethods.java: -------------------------------------------------------------------------------- 1 | package lessons.methods; 2 | 3 | public class VoidTypeMethods { 4 | public static void main(String[] args) 5 | { 6 | isEven(10); 7 | isEven(11); 8 | isEven(-3); 9 | } 10 | 11 | public static void isEven(int val) 12 | { 13 | if (val < 0) { 14 | System.out.println("Illegal input: You can not enter negative value."); 15 | return; 16 | } 17 | 18 | if (val % 2 == 0) 19 | System.out.printf("%d is Even number%n", val); 20 | else 21 | System.out.printf("%d is not Even number%n", val); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/homeworks/extra/hw1/Q3.java: -------------------------------------------------------------------------------- 1 | package homeworks.extra.hw1; 2 | 3 | public class Q3 { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner keyboard = new java.util.Scanner(System.in); 12 | 13 | System.out.println("number: "); 14 | int val = keyboard.nextInt(); 15 | 16 | System.out.println(signum(val)); 17 | } 18 | 19 | public static int signum(int number) 20 | { 21 | if (number > 0) 22 | return 1; 23 | 24 | if (number == 0) 25 | return 0; 26 | 27 | return -1; 28 | } 29 | } -------------------------------------------------------------------------------- /src/lessons/loops/BreakIntro.java: -------------------------------------------------------------------------------- 1 | package lessons.loops; 2 | 3 | public class BreakIntro { 4 | public static void main(String[] args) 5 | { 6 | // finds the sum of all values entered until 0 is entered from the input ? 7 | 8 | java.util.Scanner kb = new java.util.Scanner(System.in); 9 | 10 | int sum = 0; 11 | 12 | while (true) { 13 | System.out.print("Number:"); 14 | int val = kb.nextInt(); 15 | 16 | if (val == 0) { 17 | break; 18 | } 19 | 20 | sum += val; 21 | } 22 | System.out.printf("sum: %d%n", sum); 23 | } 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/Example1.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics; 2 | 3 | public class Example1 { 4 | public static void main(String[] args) 5 | { 6 | Sample s , k; 7 | 8 | s = new Sample(); 9 | k = new Sample(); 10 | 11 | s.x = 10; 12 | s.b = true; 13 | 14 | k.x = 20; 15 | k.b = false; 16 | 17 | System.out.println(s.x); 18 | System.out.println(s.b); 19 | System.out.println("-----------------------------------"); 20 | System.out.println(k.x); 21 | System.out.println(k.b); 22 | } 23 | } 24 | 25 | class Sample { 26 | int x; 27 | boolean b; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/switchcase/LeapYear.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.switchcase; 2 | 3 | public class LeapYear { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | System.out.print("Year: "); 14 | int year = kb.nextInt(); 15 | 16 | if (isLeapYear(year)) 17 | System.out.printf("%d is Leap Year", year); 18 | } 19 | 20 | public static boolean isLeapYear(int year) 21 | { 22 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/lessons/methods/MethodIntro.java: -------------------------------------------------------------------------------- 1 | 2 | package lessons.methods; 3 | 4 | public class MethodIntro { 5 | public static void main(String[] args) 6 | { 7 | System.out.println("main starts ;)"); 8 | MethodIntro.foo(); 9 | MethodIntro.bar(); 10 | tar(); 11 | System.out.println("main ends ;("); 12 | } 13 | 14 | public static void foo() 15 | { 16 | System.out.println("foo()"); 17 | } 18 | 19 | public static void bar() 20 | { 21 | System.out.println("bar()"); 22 | } 23 | 24 | public static void tar() 25 | { 26 | System.out.println("tar()"); 27 | } 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/BasicShoppingTest.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class BasicShoppingTest { 4 | public static void run() 5 | { 6 | // Customer.goToMarket(new Customer("Eren", new Wallet(new Card("1234", 1000), 1000), new Phone("536 999 99 99"))); 7 | // 8 | // 9 | // Customer customer1 = new Customer("Eren", new Wallet(new Card("1234", 1000), 1000), new Phone("536 999 99 99")); 10 | // Customer customer2 = new Customer("Tuncay", new Wallet(new Card("1122", 5000), 2000), new Phone("536 1111 11 11")); 11 | // 12 | // customer1.getPhone().call(customer2); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/nonstatics/Example.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.nonstatics; 2 | 3 | public class Example { 4 | public static void main(String[] args) 5 | { 6 | Y y; 7 | 8 | y = new Y(); 9 | System.out.printf("Y.x: %d%n", y.x); // 0 10 | 11 | y.changeX(10); 12 | System.out.printf("Y.x: %d%n", y.x); // 10 13 | 14 | Y y1 = y; 15 | System.out.printf("Y.x: %d%n", y1.x); // 10 16 | 17 | y1.changeX(11); 18 | System.out.printf("Y.x: %d%n", y.x); // 11 19 | } 20 | } 21 | 22 | class Y { 23 | public int x; 24 | 25 | public void changeX(int a) 26 | { 27 | x = a; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/ChartAt.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class ChartAt { 4 | public static void main(String[] args) 5 | { 6 | //Returns: the char value at the specified index of this string. The first char value is at index 0. 7 | 8 | String str = "EFFICIENT HOUSE "; 9 | 10 | int lastIndex = str.length() - 1; 11 | System.out.println(lastIndex); 12 | 13 | char lastChar = str.charAt(lastIndex); 14 | System.out.println(str.charAt(lastIndex)); 15 | 16 | for (int i = 0; i < str.length(); i = i + 1) { 17 | System.out.printf("%c " , str.charAt(i)); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/lessons/methods/MethodIntroExample.java: -------------------------------------------------------------------------------- 1 | package lessons.methods; 2 | 3 | public class MethodIntroExample { 4 | public static void main(String[] args) 5 | { 6 | System.out.println("main method starts ;)"); 7 | foo(); 8 | bar(); 9 | tar(); 10 | System.out.println("main method ends ;("); 11 | } 12 | 13 | public static void foo() 14 | { 15 | System.out.println("foo()"); 16 | bar(); 17 | } 18 | 19 | public static void bar() 20 | { 21 | System.out.println("bar()"); 22 | tar(); 23 | } 24 | 25 | public static void tar() 26 | { 27 | System.out.println("tar()"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/switchcase/SwitchExample.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.switchcase; 2 | 3 | public class SwitchExample { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner kb = new java.util.Scanner(System.in); 7 | 8 | System.out.println("alan kodu:"); 9 | int alanKodu = kb.nextInt(); 10 | 11 | switch (alanKodu) { 12 | case 216: 13 | System.out.println("Istanbul"); 14 | case 212: 15 | System.out.println("Avrupa"); 16 | break; 17 | case 242: 18 | System.out.println("Antalya"); 19 | break; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/lessons/arrays/Initialization.java: -------------------------------------------------------------------------------- 1 | package lessons.arrays; 2 | 3 | public class Initialization { 4 | public static void initExamples() 5 | { 6 | int[] nums = new int[5]; 7 | int[] ages = {1, 2, 3, 5}; 8 | double[] heights = new double[] {1.86, 1.93, 1.78, 1.56}; 9 | 10 | for (int i = 0; i < ages.length; ++i) { 11 | System.out.printf("ages[%d] = %d%n", i, ages[i]); 12 | } 13 | 14 | System.out.println("------------------------"); 15 | 16 | for (int i = heights.length - 1; i >= 0; --i) { 17 | System.out.printf("ages[%d] = %f%n", i, heights[i]); 18 | heights[i] = heights[i] * 2; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForIntro.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForIntro { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner kb = new java.util.Scanner(System.in); 7 | System.out.println("Kac defa donmek istiyorsunuz"); 8 | int n = kb.nextInt(); 9 | 10 | int i = 0; // init 11 | 12 | while (i < n) { //boolean exp. 13 | System.out.println(i); 14 | //... 15 | 16 | ++i; 17 | } 18 | 19 | System.out.println("------------------------"); 20 | 21 | 22 | for (int i2 = 0; i2 < 5; i2++) { 23 | System.out.println(i2); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q8.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | 3 | public class Q8 { 4 | 5 | public static void main(String[] args) 6 | { 7 | java.util.Scanner kb = new java.util.Scanner(System.in); 8 | 9 | System.out.print("time: "); 10 | double time = Double.parseDouble(kb.nextLine()); 11 | 12 | System.out.print("distance: "); 13 | double distance = Double.parseDouble(kb.nextLine()); 14 | 15 | System.out.println("This program calculates your pace given a time " 16 | + "and distance traveled."); 17 | 18 | double pace = time / distance; 19 | 20 | System.out.printf("Your pace is %.2f miles per hour.", pace); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/lessons/arrays/lottery/Customer.java: -------------------------------------------------------------------------------- 1 | package lessons.arrays.lottery; 2 | 3 | public class Customer { 4 | public int balance; 5 | public int age; 6 | 7 | public Customer(int newBalance, int newAge) 8 | { 9 | balance = newBalance; 10 | age = newAge; 11 | } 12 | 13 | public int getAge() 14 | { 15 | return age; 16 | } 17 | 18 | public boolean isSufficientBalance(int amount) 19 | { 20 | return balance >= amount; 21 | } 22 | 23 | public void pay(int payAmount) 24 | { 25 | balance = balance - payAmount; 26 | } 27 | 28 | public void earn(int earnedAmount) 29 | { 30 | balance += earnedAmount; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/lessons/methods/MethodCalling.java: -------------------------------------------------------------------------------- 1 | package lessons.methods; 2 | 3 | public class MethodCalling { 4 | public static void main(String[] args) 5 | { 6 | System.out.println("main method starts ;)"); 7 | foo("call foo()"); 8 | bar("call bar()"); 9 | tar("call tar()"); 10 | System.out.println("main method ends ;("); 11 | } 12 | 13 | public static void foo(String message) 14 | { 15 | System.out.println(message); 16 | } 17 | 18 | public static void bar(String message) 19 | { 20 | System.out.println(message); 21 | } 22 | 23 | public static void tar(String message) 24 | { 25 | System.out.println(message); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/Immutable.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class Immutable { 4 | public static void main(String[] args) 5 | { 6 | // Immutable Class (String) 7 | String isim = "Apple"; 8 | String isim2 = "Apple"; 9 | String isim3 = "Apple"; 10 | 11 | if (isim == isim2) // Checks if they have same references (Memory address) on memory 12 | System.out.println("Same object"); 13 | else 14 | System.out.println("Different object"); 15 | 16 | if (isim == isim3) 17 | System.out.println("Same object"); 18 | else 19 | System.out.println("Different object"); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/lessons/imports/SingleTypeStaticImport.java: -------------------------------------------------------------------------------- 1 | package lessons.imports; 2 | 3 | import static lessons.imports.test.Math.add; 4 | import static lessons.imports.test.Math.subst; 5 | import static lessons.imports.test.Math.multiply; 6 | import static lessons.imports.test.Math.divide; 7 | import static java.lang.Math.*; 8 | import static java.lang.System.*; 9 | 10 | public class SingleTypeStaticImport { 11 | public static void run() 12 | { 13 | System.out.println(add(5, 3)); 14 | System.out.println(subst(10, 5)); 15 | System.out.println(multiply(6, 9)); 16 | System.out.println(divide(10, 3)); 17 | 18 | abs(-5); 19 | 20 | out.println("Efficient House - Static import examples"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/statics/Intro.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.statics; 2 | 3 | public class Intro { 4 | public static void main(String[] args) 5 | { 6 | System.out.printf("X.i = %d%n", X.i); 7 | System.out.printf("X.d = %f%n", X.d); 8 | 9 | X.i = 33; 10 | X.d = 41.41f; 11 | 12 | System.out.printf("X.i = %d%n", X.i); 13 | System.out.printf("X.d = %f%n", X.d); 14 | 15 | X x = new X(); 16 | System.out.println(x.l); 17 | 18 | X x1 = new X(); 19 | System.out.println(x1.l); 20 | 21 | // X.l = 5; --ERROR 22 | } 23 | } 24 | 25 | class X { 26 | public static int i; 27 | public static double d; 28 | 29 | public long l = 12; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/Heap.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics; 2 | 3 | public class Heap { 4 | public static void main(String[] args) 5 | { 6 | Human eren; // reference 7 | eren = new Human(); 8 | 9 | System.out.println(eren.age); // 0 10 | System.out.println(eren.height); // 0 11 | System.out.println(eren.weight); // 0 12 | 13 | eren.age = 23; 14 | eren.height = 1.82f; 15 | eren.weight = 100.5f; 16 | 17 | System.out.println(eren.age); // 23 18 | System.out.println(eren.height); // 1.82 19 | System.out.println(eren.weight); // 100.5 20 | } 21 | } 22 | 23 | class Human { 24 | public int age; 25 | public float height; 26 | public float weight; 27 | } -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForExample9.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForExample9 { 4 | public static void main(String[] args) 5 | { 6 | // 3 digits Armstrong number with for loops ;) 7 | 8 | for (int a = 1; a <= 9; ++a) 9 | for (int b = 0; b <= 9; ++b) 10 | for (int c = 0; c <= 9; ++c) 11 | if (pow(a, 3) + pow(b, 3) + pow(c, 3) == 100 * a + 10 * b + c) 12 | System.out.printf("%d%d%d%n", a, b, c); 13 | } 14 | 15 | 16 | public static int pow(int a, int b) 17 | { 18 | int result = 1; 19 | 20 | for (;b != 0; b--) { 21 | result *= a; 22 | } 23 | 24 | return result; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForExample1.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForExample1 { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner kb = new java.util.Scanner(System.in); 7 | System.out.println("Number: "); 8 | int n = kb.nextInt(); 9 | 10 | int i = 0; 11 | 12 | for (System.out.println("INITIALIZE"); i < n; System.out.println("UPDATE")) { 13 | System.out.println("STATEMENT"); 14 | ++i; 15 | } 16 | //... 17 | 18 | int j; 19 | 20 | for (j = 1; j < n;) { 21 | System.out.println(j); 22 | //... 23 | 24 | j += 2; 25 | } 26 | 27 | // System.out.println(j); ERROR 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/lessons/namelookup/A.java: -------------------------------------------------------------------------------- 1 | package lessons.namelookup; 2 | 3 | public class A { 4 | 5 | int b; 6 | 7 | public static void main(String[] args) 8 | { 9 | B b = new B(); // B niteliksiz (unqualified) 10 | 11 | B.bar(); // B niteliksiz (unqualified) || bar() (qualified) 12 | 13 | b = new B(); // b niteliksiz (unqualified) 14 | b.bar(); // niteliksiz (unqualified) 15 | 16 | System.out.println("B.s = " + b.s); // System (unqualified) || out qualified || println qualified || b unqualified || s qualified 17 | } 18 | } 19 | 20 | 21 | class B { 22 | int s; 23 | static int x; 24 | 25 | public static void bar() 26 | { 27 | System.out.println(x); 28 | System.out.println("B.bar()"); 29 | } 30 | } -------------------------------------------------------------------------------- /src/lessons/languagebasics/TernaryOperator.java: -------------------------------------------------------------------------------- 1 | package lessons.languagebasics; 2 | 3 | public class TernaryOperator { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner keyboard = new java.util.Scanner(System.in); 7 | 8 | System.out.print("First number: "); 9 | int firstNumber = keyboard.nextInt(); 10 | 11 | System.out.print("Second number: "); 12 | int secondNumber = keyboard.nextInt(); 13 | 14 | int max; 15 | 16 | if (firstNumber >= secondNumber) { 17 | max = firstNumber; 18 | } else { 19 | max = secondNumber; 20 | } 21 | 22 | max = (firstNumber >= secondNumber) ? firstNumber : secondNumber; 23 | System.out.println(max); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForExample3.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForExample3 { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner kb = new java.util.Scanner(System.in); 7 | System.out.print("Number: "); 8 | int n = kb.nextInt(); 9 | 10 | int i; // declare 11 | 12 | for (i = 0; i < n; i++) { // init; boolean exp; update 13 | System.out.printf("%d ", i); 14 | } 15 | 16 | System.out.println(i); 17 | // i = 5 18 | 19 | System.out.println("-------------------"); 20 | 21 | for (System.out.println("init"); i < n; i++) { 22 | System.out.printf("%d ", i); 23 | } 24 | 25 | System.out.println("\nBye"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/ifelse/IfElseExample2.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.ifelse; 2 | 3 | public class IfElseExample2 { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner keyboard = new java.util.Scanner(System.in); 7 | 8 | System.out.print("Age: "); 9 | int age = keyboard.nextInt(); 10 | 11 | // Display Generation 12 | 13 | if (age < 5) { 14 | System.out.println("Baby"); 15 | } else if (age < 13) { 16 | System.out.println("Child"); 17 | } else if (age < 25) { 18 | System.out.println("Young"); 19 | } else if (age < 35) { 20 | System.out.println("Mid"); 21 | } else { 22 | System.out.println("Old"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/randomclass/Example1.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.randomclass; 2 | 3 | public class Example1 { 4 | public static void main(String[] args) 5 | { 6 | java.util.Random r = new java.util.Random(); 7 | java.util.Scanner kb = new java.util.Scanner(System.in); 8 | 9 | 10 | System.out.print("How many random numbers do you want: "); 11 | int n = Integer.parseInt(kb.nextLine()); 12 | 13 | System.out.print("Begin index: "); 14 | int begin = Integer.parseInt(kb.nextLine()); 15 | 16 | System.out.print("End index (inclusive) : "); 17 | int end = Integer.parseInt(kb.nextLine()); 18 | 19 | for (int i = 0; i < n; ++i) 20 | System.out.println(r.nextInt(end - begin + 1) + begin); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/SubString.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class SubString { 4 | public static void main(String[] args) 5 | { 6 | String name = "Efficient Hub"; 7 | System.out.println(name.substring(0, 9)); // substring([0, 9)) 8 | System.out.println(name.substring(10)); 9 | 10 | 11 | String text = " Hello my name is John "; // HELLO (String = Immutable class) 12 | String result = text.trim().toUpperCase().substring(0, 5); 13 | // String result = text.trim(); "Hello my name is John" 14 | // String result = text.toUpperCase(); "HELLO MY NAME IS JOHN" 15 | // String result = text.substring(0, 5); "HELLO" 16 | System.out.println(result); 17 | System.out.println(text); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForExample5.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForExample5 { 4 | public static void main(String[] args) 5 | { 6 | for (int i = 0, j = 10; i < 5 && j >= 0; ++i, --j) 7 | System.out.printf("{i: %d, j: %d}%n", i, j); 8 | 9 | System.out.println("-------------"); 10 | 11 | for (int i = 0, j = 10; i < 5 || j >= 0; ++i, --j) 12 | System.out.printf("{i: %d, j: %d}%n", i, j); 13 | 14 | /* 15 | {i: 0, j: 10} 0 < 5 -> true || -> true 16 | {i: 1, j: 9} 1 < 5 -> true || -> true 17 | .... 18 | 19 | {i: 4, j: 6} (4 < 5) -> true || -> true 20 | {i: 5, j: 5} (5 < 5) || (5 >= 0) -> (false || true) -> true 21 | */ 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/homeworks/extra/hw1/Q2.java: -------------------------------------------------------------------------------- 1 | package homeworks.extra.hw1; 2 | 3 | public class Q2 { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | System.out.print("a: "); 14 | int a = kb.nextInt(); 15 | 16 | System.out.print("b: "); 17 | int b = kb.nextInt(); 18 | 19 | System.out.print("c: "); 20 | int c = kb.nextInt(); 21 | 22 | System.out.println(mid(a, b, c)); 23 | } 24 | 25 | public static int mid(int a, int b, int c) 26 | { 27 | if (b <= a && a <= c || c <= a && a <= b) 28 | return a; 29 | 30 | if (a <= b && b <= c || c <= b && b <= a) 31 | return b; 32 | 33 | return c; 34 | } 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/ifelse/IfElseExample4.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.ifelse; 2 | 3 | public class IfElseExample4 { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner keyboard = new java.util.Scanner(System.in); 7 | 8 | System.out.print("Username: "); 9 | String userName = keyboard.nextLine(); 10 | 11 | if (userName.charAt(2) == 's') { 12 | 13 | System.out.print("Password: "); 14 | String password = keyboard.nextLine(); 15 | 16 | if (password.charAt(password.length() - 3) == 'x') { 17 | System.out.println("Welcome to Efficient House"); 18 | } else { 19 | System.out.println("Your password is wrong"); 20 | } 21 | } else { 22 | System.out.println("There is no such user"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/lessons/loops/dowhileloops/DoWhileExample.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.dowhileloops; 2 | 3 | /* 4 | Write a method named countDigits that will take an int argument and returns total digits count. 5 | */ 6 | 7 | public class DoWhileExample { 8 | public static void main(String[] args) 9 | { 10 | run(); 11 | } 12 | 13 | public static void run() 14 | { 15 | java.util.Scanner kb = new java.util.Scanner(System.in); 16 | 17 | System.out.println("Number:"); 18 | int val = kb.nextInt(); 19 | 20 | System.out.printf("%d -> total digits count: %d", val, countDigits(val)); 21 | } 22 | 23 | public static int countDigits(int val) 24 | { 25 | int count = 0; 26 | 27 | do { 28 | val /= 10; 29 | ++count; 30 | } while (val > 0); 31 | 32 | return count; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/lessons/loops/whileloops/WhileExample6.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.whileloops; 2 | /* 3 | Write a method named sumDigits that will take an int argument and returns sum of all digits. 4 | */ 5 | public class WhileExample6 { 6 | public static void main(String[] args) 7 | { 8 | run(); 9 | } 10 | 11 | public static void run() 12 | { 13 | java.util.Scanner kb = new java.util.Scanner(System.in); 14 | 15 | System.out.println("Number:"); 16 | int val = kb.nextInt(); 17 | 18 | System.out.printf("%d -> sum of digits = %d%n", val, sumDigits(val)); 19 | } 20 | 21 | public static int sumDigits(int val) 22 | { // 1123 23 | int sum = 0; 24 | 25 | while (val > 0) { 26 | sum += val % 10; // 3 + 2 + 1 + 1 = 7 27 | val /= 10; // 112 11 1 0 28 | } 29 | 30 | return sum; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/StackExample.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics; 2 | 3 | public class StackExample { 4 | public static void main(String[] args) 5 | { 6 | foo(6, 7); 7 | // int x = 6; x created in Stack // long y = 7; y created in Stack 8 | // First s then y then x pop from Stack 9 | 10 | { 11 | int a; // created in Stack 12 | double d; // created in Stack 13 | } //// First d then a pop from Stack 14 | 15 | 16 | byte b; // created in Stack 17 | short s; // created in Stack 18 | 19 | { 20 | float f; // created in Stack 21 | { 22 | boolean bool; // created in Stack 23 | } // bool pop from Stack 24 | } // f pop from Stack 25 | } // First s then b pop from Stack 26 | 27 | 28 | public static void foo(int x, long y) 29 | { 30 | short s; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q10.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | /* 3 | Scientists estimate that roughly 10 grams of caffeine consumed at one time is a 4 | lethal overdose. Write a program with a variable that holds the number of milligrams of caffeine in a drink and 5 | outputs how many drinks it takes to kill a person. 6 | A 12-ounce can of cola has approximately 34 mg of caffeine, while a 16-ounce cup 7 | of coffee has approximately 160 mg of caffeine. 8 | */ 9 | 10 | public class Q10 { 11 | public static void main(String[] args) { 12 | 13 | int colaCount, coffeeCount; 14 | int milligram = 10000; 15 | 16 | colaCount = milligram / 34; 17 | coffeeCount = milligram / 160; 18 | 19 | System.out.printf("If you drink %d of the 12-ounce Cokes, it's an overdose.%n", colaCount); 20 | System.out.printf("If you drink %d of the 16-ounce Coffees, it's an overdose.", coffeeCount); 21 | } 22 | } -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q1.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | 3 | /* 4 | Body Mass Index (BMI) helps in specifying the weight category a person 5 | belongs to, depending on their body weight. BMI is estimated using the following formula: 6 | 7 | BMI = Weight in kilograms / (Height in meters)2 8 | 9 | Write a program that calculates and outputs the BMI. Assume various input values 10 | wherever required. 11 | */ 12 | 13 | public class Q1 { 14 | public static void main(String[] args) 15 | { 16 | double kilogram, meter, bmi; 17 | java.util.Scanner scanner = new java.util.Scanner(System.in); 18 | 19 | System.out.println("Kilogram: "); 20 | kilogram = scanner.nextDouble(); 21 | 22 | System.out.println("Meter: "); 23 | meter = scanner.nextDouble(); 24 | 25 | bmi = kilogram / (meter * meter); 26 | System.out.printf("Body Mass Index is: %.2f", bmi); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/examples/ReferanceAndObject.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.examples; 2 | 3 | public class ReferanceAndObject { 4 | public static void main(String[] args) 5 | { 6 | Sample x, y, z; 7 | 8 | x = new Sample(); 9 | y = new Sample(); 10 | z = new Sample(); 11 | 12 | x.a = 10; 13 | x.b = true; 14 | 15 | y.a = 20; 16 | y.b = false; 17 | 18 | z = x; 19 | 20 | System.out.println(z.a); 21 | System.out.println(z.b); 22 | 23 | z.a++; 24 | 25 | System.out.println(x.a); 26 | 27 | y = z; 28 | 29 | System.out.println(y.a); 30 | System.out.println(y.b); 31 | 32 | y.b = false; 33 | y.a--; 34 | 35 | System.out.println(x.a); 36 | System.out.println(x.b); 37 | 38 | } 39 | } 40 | 41 | class Sample { 42 | public int a; 43 | public boolean b; 44 | } 45 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/StringClass.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class StringClass { 4 | public static void main(String[] args) 5 | { 6 | //Concatenation 7 | String firstName = "John"; 8 | String lastName = "Matt"; 9 | int age = 23; 10 | double height = 1.84; 11 | System.out.println("First name: " + firstName + " Last name: " + lastName + " Age: " + age + " Height: " + height); 12 | 13 | System.out.println("99" + 1 + 1); // 9911 14 | System.out.println(999 + "1" + 2); // 99912 15 | System.out.println(999 + 1 + "999"); // 1000999 16 | 17 | System.out.println("--------------"); 18 | 19 | int a = 11; 20 | System.out.println(a++); // 11 21 | // public void println(int x) | int x = a++; 22 | // int x = a; 23 | // a = a + 1; 24 | System.out.println(a); // 12 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/ifelse/IfElseExample3.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.ifelse; 2 | 3 | public class IfElseExample3 { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner keyboard = new java.util.Scanner(System.in); 7 | 8 | System.out.print("Username: "); 9 | String userName = keyboard.nextLine(); 10 | 11 | // Username = "efficient" + Password = "compareTo" 12 | 13 | if (userName.equals("efficient")) { 14 | 15 | System.out.print("Password: "); 16 | String password = keyboard.nextLine(); 17 | 18 | if (password.equals("compareTo")) { 19 | System.out.println("Welcome to Efficient House"); 20 | } else { 21 | System.out.println("Your password is wrong"); 22 | } 23 | } else { 24 | System.out.println("There is no such user"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/Card.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class Card { 4 | public String iban; 5 | public String password; 6 | public int balance; 7 | 8 | public Card(String newIban) 9 | { 10 | iban = newIban; 11 | } 12 | 13 | public Card(String newIban, String newPassword, int newBalance) 14 | { 15 | iban = newIban; 16 | password = newPassword; 17 | balance = newBalance; 18 | } 19 | 20 | public String getPassword() 21 | { 22 | return password; 23 | } 24 | 25 | public void setBalance(int newBalance) 26 | { 27 | balance = newBalance; 28 | } 29 | 30 | public int getBalance() 31 | { 32 | return balance; 33 | } 34 | 35 | public boolean checkBalance(int balance, int cost) 36 | { 37 | return balance >= cost; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/homeworks/chapter3/Q4.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3; 2 | 3 | public class Q4 { 4 | public static void main(String[] args) 5 | { 6 | inflationCalculator(); 7 | } 8 | 9 | public static void inflationCalculator() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | System.out.print("Please enter the item cost : "); 14 | double itemCost = kb.nextDouble(); 15 | 16 | System.out.print("Please enter how many years later you will buy it : "); 17 | int year = kb.nextInt(); 18 | 19 | System.out.print("Please enter the inflation rate : "); 20 | double inflationRate = kb.nextDouble() / 100; 21 | 22 | for(int i = 0; i < year; i++) 23 | itemCost += itemCost * inflationRate; 24 | 25 | System.out.printf("%nThe price of the item after %d years with %.2f inflation rate : %.2f",year,inflationRate,itemCost); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/examples/date/DateUtil.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.examples.date; 2 | 3 | public class DateUtil { 4 | 5 | public static void changeDate(Date d, int day, int month, int year) 6 | { 7 | //... 8 | 9 | d.day = day; 10 | d.month = month; 11 | d.year = year; 12 | } 13 | 14 | public static Date createDate(int day, int month, int year) 15 | { 16 | Date date; 17 | date = new Date(); 18 | 19 | date.day = day; 20 | date.month = month; 21 | date.year = year; 22 | 23 | return date; // Date temp = date 24 | } 25 | 26 | public static void printDate(Date date) // Date date = birthDate; 27 | { 28 | System.out.printf("%02d/%02d/%04d%n", date.day, date.month, date.year); 29 | } 30 | } 31 | 32 | class Date { 33 | public int day; 34 | public int month; 35 | public int year; 36 | 37 | //... 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q7.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | /* 3 | Write a program that outputs the number of hours, minutes, and seconds that 4 | corresponds to 50,391 total seconds. The output should be 13 hours, 59 minutes, 5 | and 51 seconds. Test your program with a different number of total seconds to 6 | ensure that it works for other cases. 7 | */ 8 | 9 | public class Q7 { 10 | public static void main(String[] args) 11 | { 12 | java.util.Scanner kb = new java.util.Scanner(System.in); 13 | 14 | System.out.print("Seconds: "); 15 | int seconds = Integer.parseInt(kb.nextLine()); 16 | 17 | int hours = seconds / 3600; 18 | seconds %= 3600; 19 | 20 | int minutes = seconds / 60; 21 | seconds %= 60; 22 | 23 | System.out.printf("%02d:%02d:%02d%n", hours, minutes, seconds); 24 | System.out.printf("%02d hours, %02d minutes, %02d seconds", hours, minutes, seconds); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/homeworks/chapter3/chapter3/Q4.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3.chapter3; 2 | 3 | public class Q4 { 4 | public static void main(String[] args) 5 | { 6 | inflationCalculator(); 7 | } 8 | 9 | public static void inflationCalculator() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | System.out.print("Please enter the item cost : "); 14 | double itemCost = kb.nextDouble(); 15 | 16 | System.out.print("Please enter how many years later you will buy it : "); 17 | int year = kb.nextInt(); 18 | 19 | System.out.print("Please enter the inflation rate : "); 20 | double inflationRate = kb.nextDouble() / 100; 21 | 22 | for(int i = 0; i < year; i++) 23 | itemCost += itemCost * inflationRate; 24 | 25 | System.out.printf("%nThe price of the item after %d years with %.2f inflation rate : %.2f",year,inflationRate,itemCost); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/lessons/languagebasics/ExpressionsAndAssignment.java: -------------------------------------------------------------------------------- 1 | package lessons.languagebasics; 2 | 3 | public class ExpressionsAndAssignment { 4 | public static void main(String[] args) 5 | { 6 | int _A = 4; 7 | int a5555 = 5; 8 | int a$ = 5; // int temp; 9 | 10 | int age; 11 | int ageOfTree; 12 | 13 | byte b = 127; // it's valid for the first initialization 14 | short s = 32535; // it's valid for the first initialization 15 | int i = 223232; 16 | long l = 213654515144L; 17 | 18 | float f = 5.3f; //IEEE 754 19 | double d = 3.5; //IEEE 754 20 | 21 | boolean bool = true; //(true, false) 22 | char ch = 67; //Character 23 | char ch2 = 'C'; 24 | 25 | int x, y, z; 26 | z = y = x = 10; 27 | 28 | double x1 = 4.5, x2 = 5.6, x3 = 5L; 29 | 30 | int eren; //declaration 31 | eren = 5; 32 | 33 | int eren2 = 3; //initialization 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/lessons/loops/whileloops/WhileExample.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.whileloops; 2 | 3 | public class WhileExample { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner kb = new java.util.Scanner(System.in); 7 | 8 | System.out.println("How many times you want to loop through:"); 9 | int n = kb.nextInt(); 10 | 11 | System.out.println("------------------"); 12 | 13 | int i = 0; 14 | 15 | while (i < n) { 16 | System.out.println(i); 17 | i = i + 1; 18 | } 19 | 20 | System.out.printf("i = %d%n", i); 21 | 22 | System.out.println("How many times you want to loop through:"); 23 | int n2 = kb.nextInt(); 24 | 25 | System.out.println("------------------"); 26 | 27 | int j = 1; 28 | 29 | while (j <= n2) { 30 | System.out.println(j); 31 | j++; 32 | } 33 | 34 | System.out.printf("j = %d", j); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/lessons/arrays/lottery/NumericLottery.java: -------------------------------------------------------------------------------- 1 | package lessons.arrays.lottery; 2 | 3 | import lessons.arrays.examples.ArrayMethods; 4 | 5 | import java.util.Random; 6 | 7 | public class NumericLottery { 8 | public int[] nums = new int[6]; 9 | 10 | public void fillNumsAndSortAscending(Random r) 11 | { 12 | for (int i = 0; i < nums.length; ++i) { 13 | int num; 14 | 15 | do { 16 | num = r.nextInt(90) + 1; 17 | } while(contains(num)); 18 | 19 | nums[i] = num; 20 | } 21 | } 22 | 23 | public boolean contains(int n) 24 | { 25 | for (int i = 0; i < nums.length; ++i) { 26 | if (nums[i] == n) 27 | return true; 28 | } 29 | 30 | return false; 31 | } 32 | 33 | public int[] getNums(Random random) 34 | { 35 | fillNumsAndSortAscending(random); 36 | ArrayMethods.bubbleSortAscending(nums); 37 | return nums; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/lessons/arrays/Assign.java: -------------------------------------------------------------------------------- 1 | package lessons.arrays; 2 | 3 | import java.util.Random; 4 | import java.util.Scanner; 5 | 6 | public class Assign { 7 | public static void assignExample() 8 | { 9 | Scanner kb = new Scanner(System.in); 10 | Random r = new Random(); 11 | 12 | System.out.print("keys count: "); 13 | int n = Integer.parseInt(kb.nextLine()); 14 | 15 | boolean[] keys = new boolean[n]; 16 | 17 | for (int i = 0; i <= keys.length -1; ++i) { 18 | keys[i] = r.nextBoolean(); 19 | } 20 | 21 | System.out.println("Which doors is open ?"); 22 | 23 | for (int i = 0; i < keys.length; ++i) { 24 | if (keys[i]) 25 | System.out.printf("%d. door is open :)%n", i); 26 | else 27 | System.out.printf("%d. door is not open :(%n", i); 28 | } 29 | } 30 | } 31 | 32 | 33 | class Class { 34 | Student [] students; 35 | } 36 | 37 | class Student { 38 | 39 | } -------------------------------------------------------------------------------- /src/lessons/oop/basics/randomclass/Example.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.randomclass; 2 | 3 | public class Example { 4 | public static void main(String[] args) 5 | { 6 | java.util.Random r = new java.util.Random(); 7 | java.util.Scanner kb = new java.util.Scanner(System.in); 8 | 9 | 10 | System.out.print("How many random numbers do you want: "); 11 | int n = Integer.parseInt(kb.nextLine()); 12 | 13 | System.out.print("Begin index: "); 14 | int begin = Integer.parseInt(kb.nextLine()); 15 | 16 | System.out.print("End index (exclusive) : "); 17 | int end = Integer.parseInt(kb.nextLine()); 18 | 19 | for (int i = 0; i < n; ++i) { 20 | System.out.println(r.nextInt(begin, end)); 21 | } 22 | 23 | System.out.println("----------------------"); 24 | 25 | for (int i = 0; i < n; ++i) { 26 | System.out.println(r.nextInt(end - begin) + begin); 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/lessons/loops/ContinueExample.java: -------------------------------------------------------------------------------- 1 | package lessons.loops; 2 | 3 | public class ContinueExample { 4 | public static void run() 5 | { 6 | java.util.Scanner kb = new java.util.Scanner(System.in); 7 | 8 | System.out.print("n: "); 9 | int n = kb.nextInt(); 10 | 11 | for (int i = 0; i <= n; ++i) { 12 | if (!isPrime(i)) 13 | continue; 14 | 15 | System.out.println(i); 16 | } 17 | } 18 | 19 | public static boolean isPrime(int a) 20 | { 21 | if (a <= 1) 22 | return false; 23 | 24 | if (a % 2 == 0) 25 | return a == 2; 26 | 27 | if (a % 3 == 0) 28 | return a == 3; 29 | 30 | if (a % 5 == 0) 31 | return a == 5; 32 | 33 | if (a % 7 == 0) 34 | return a == 7; 35 | 36 | for (int i = 11; i * i <= a; i += 2) 37 | if (a % i == 0) 38 | return false; 39 | 40 | return true; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/IndexOf.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class IndexOf { 4 | public static void main(String[] args) 5 | { 6 | String s = "I want to be a very good Java programmer"; 7 | 8 | char ch = 'J'; 9 | int index = s.indexOf(ch); 10 | System.out.printf("%c Index:%d%n", ch ,index); 11 | 12 | char chr = 'f'; 13 | int index2 = s.indexOf(chr); // -1 if the character does not occur before that point. 14 | System.out.printf("%c Index:%d%n", chr ,index2); 15 | 16 | String s2 = "Java"; 17 | int index3 = s.indexOf(s2); 18 | System.out.printf("%s Index:%d%n", s2, index3); 19 | 20 | int index4 = s.indexOf(s2, 17); 21 | System.out.println(index4); 22 | 23 | int index5 = s.lastIndexOf(s2); 24 | System.out.println(index5); 25 | 26 | String s3 = "Java programmer"; 27 | int index6 = s.indexOf(s3); 28 | System.out.println(index6); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/Length.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class Length { 4 | public static void main(String[] args) 5 | { 6 | String str = "Efficient House, we are learning Java. This is not a CACIK Bootcamp."; 7 | int length = str.length(); //Returns: the length of the sequence of characters represented by this object. 8 | 9 | // printf (format – A format string as described in Format string syntax) 10 | // %s = String || %d = decimal types || %f = floating types || %b = boolean || %c = char 11 | System.out.printf("%s length = %d%n", str, length); 12 | System.out.println(str + " length = " + length); 13 | 14 | 15 | System.out.printf("the concentration of this mixture is: %%%d %n", 99); 16 | System.out.printf("PI = %.2f%n", Math.PI); 17 | 18 | int day = 5; 19 | int month = 9; 20 | int year = 2022; 21 | System.out.printf("Date : %05d / %03d / %d%n", day, month, year); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/homeworks/extra/hw1/Q5.java: -------------------------------------------------------------------------------- 1 | package homeworks.extra.hw1; 2 | 3 | public class Q5 { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | while (true) { 14 | System.out.print("number:"); 15 | long l = kb.nextLong(); 16 | 17 | if (l == 0) 18 | return; 19 | 20 | if (isPrime(l)) 21 | System.out.printf("%d is prime %n", l); 22 | else 23 | System.out.printf("%d is not prime%n", l); 24 | } 25 | } 26 | 27 | public static boolean isPrime(long a) 28 | { 29 | if (a <= 1) 30 | return false; 31 | 32 | if (a % 2 == 0) 33 | return a == 2; 34 | 35 | for (long i = 3; i * i <= a; i += 2) 36 | if (a % i == 0) 37 | return false; 38 | 39 | return true; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/switchcase/SwitchIntro.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.switchcase; 2 | 3 | public class SwitchIntro { 4 | public static void main(String[] args) { 5 | java.util.Scanner kb = new java.util.Scanner(System.in); 6 | 7 | System.out.print("Postal code: "); 8 | int postalCode = kb.nextInt(); 9 | 10 | switch (postalCode) { 11 | case 55060: 12 | System.out.println("Samsun"); 13 | break; 14 | case 35580: 15 | System.out.println("Izmir"); 16 | break; 17 | case 25100: 18 | System.out.println("Erzurum"); 19 | break; 20 | case 34212: 21 | System.out.println("Istanbul"); 22 | break; 23 | case 42000: 24 | System.out.println("Konya"); 25 | break; 26 | default: 27 | System.out.println("Invalid Postal code"); 28 | } 29 | } 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/Equals.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class Equals { 4 | public static void main(String[] args) 5 | { 6 | String s = "Efficient House"; // it's cached 7 | String s2 = "Efficient House"; // Uses cached object instead of creating new object 8 | if (s == s2) 9 | System.out.println("Same object"); 10 | else 11 | System.out.println("Different object"); 12 | 13 | if (s.equals(s2)) 14 | System.out.println("These keep exactly the same characters"); 15 | else 16 | System.out.println("These do not hold the same characters"); 17 | 18 | 19 | String str = "EFFICIENT HOUSE"; 20 | String str2 = "EffIciENt HouSE"; 21 | 22 | 23 | if (str.equalsIgnoreCase(str2)) 24 | System.out.println("Same character set except for case sensitive"); 25 | else 26 | System.out.println("Different character set except for case sensitive"); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/hackerrank/easy/SmallestAndLargest.java: -------------------------------------------------------------------------------- 1 | package hackerrank.easy; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SmallestAndLargest { 6 | public static void main(String [] args) 7 | { 8 | Scanner kb = new Scanner(System.in); 9 | System.out.print("Substring length: "); 10 | int k = Integer.parseInt(kb.nextLine()); 11 | 12 | System.out.print("Text: "); 13 | String s = kb.nextLine(); 14 | 15 | 16 | System.out.println(getSmallestAndLargest(s, k)); 17 | } 18 | 19 | public static String getSmallestAndLargest(String s, int k) 20 | { 21 | String max = s.substring(0, k); 22 | String min = s.substring(0, k); 23 | 24 | for (int i = 1; i <= s.length() - k; ++i) { 25 | 26 | String text = s.substring(i, i + k); 27 | if (max.compareTo(text) < 0) 28 | max = text; 29 | 30 | if (min.compareTo(text) > 0) 31 | min = text; 32 | } 33 | 34 | return max + "\n" + min; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/UpperLower.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class UpperLower { 4 | public static void main(String[] args) 5 | { 6 | String s = "five"; 7 | String s2 = s.toUpperCase(); // Returns: the String, converted to uppercase. 8 | 9 | // String's are constant; their values cannot be changed after they are created. 10 | // Because String objects are immutable they can be shared. For example: 11 | 12 | if (s == s2) 13 | System.out.println("Same object"); 14 | else 15 | System.out.println("Different object"); 16 | 17 | System.out.println(s); 18 | System.out.println(s2); 19 | 20 | 21 | String s3 = "FIVE"; 22 | String s4 = s3.toLowerCase(); // Returns: the String, converted to lowercase. 23 | 24 | System.out.println(s4); 25 | 26 | if (s == s4) 27 | System.out.println("Same object"); 28 | else 29 | System.out.println("Different object"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/lessons/loops/whileloops/WhileExample5.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.whileloops; 2 | /* 3 | Write a method named countDigits that will take an int argument and returns total digits count. 4 | */ 5 | public class WhileExample5 { 6 | public static void main(String[] args) 7 | { 8 | run(); 9 | } 10 | 11 | public static void run() 12 | { 13 | java.util.Scanner kb = new java.util.Scanner(System.in); 14 | 15 | System.out.println("Number:"); 16 | int val = abs(kb.nextInt()); 17 | 18 | if (val != 0) 19 | System.out.printf("%d -> total digits count: %d", val, countDigits(val)); 20 | else 21 | System.out.println("Invalid input (now)"); 22 | } 23 | 24 | public static int countDigits(int val) 25 | { 26 | int count = 0; 27 | 28 | while (val > 0) { 29 | val /= 10; 30 | ++count; 31 | } 32 | 33 | return count; 34 | } 35 | 36 | public static int abs(int x) 37 | { 38 | return x < 0 ? -x : x; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/ifelse/BooleanExpressions.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.ifelse; 2 | 3 | public class BooleanExpressions { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner keyboard = new java.util.Scanner(System.in); 7 | 8 | System.out.print("First number: "); 9 | int firstNumber = keyboard.nextInt(); 10 | 11 | System.out.print("Second number: "); 12 | int secondNumber = keyboard.nextInt(); 13 | 14 | boolean isEqual = firstNumber == secondNumber; 15 | boolean isFirstNumberGreater = firstNumber > secondNumber; 16 | boolean isFirstNumberLess = firstNumber < secondNumber; 17 | 18 | boolean isFirstNumberGreaterOrEquals = firstNumber >= secondNumber; 19 | boolean isFirstNumberLessOrEquals = firstNumber <= secondNumber; 20 | 21 | boolean isNotEqual = firstNumber != secondNumber; // *** 22 | 23 | boolean isAlive = true; 24 | //... 25 | 26 | if (!isAlive) { 27 | System.out.println("DEAD"); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/lessons/loops/whileloops/WhileExample7.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.whileloops; 2 | /* 3 | Write a method named reversed that will take an int argument and returns reverse of argument. 4 | 5 | 123 -> 3 -> 3 * 10 + 2 = 32 -> 32 * 10 + 1 = 321 6 | */ 7 | public class WhileExample7 { 8 | public static void main(String[] args) 9 | { 10 | run(); 11 | } 12 | 13 | public static void run() 14 | { 15 | java.util.Scanner kb = new java.util.Scanner(System.in); 16 | 17 | int val; 18 | System.out.println("Number:"); 19 | 20 | while ((val = kb.nextInt()) != 0) { 21 | System.out.printf("%d -> reversed: %d%n", val, reversed(val)); 22 | System.out.println("Number:"); 23 | } 24 | } 25 | 26 | public static int reversed(int val) // 123 27 | { 28 | int result = 0; 29 | 30 | while (val != 0) { // 123 12 1 31 | result = result * 10 + val % 10 ; // 3 32 321 32 | val /= 10; 33 | } 34 | 35 | return result; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/randomclass/Intro.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.randomclass; 2 | 3 | 4 | public class Intro extends Object{ 5 | public static void main(String[] args) 6 | { 7 | java.util.Random r = new java.util.Random(); 8 | 9 | for (int i = 0; i < 10; ++i) 10 | System.out.println(r.nextInt()); 11 | 12 | System.out.println("-----------------------"); 13 | 14 | for (int i = 0; i < 10; ++i) 15 | System.out.println(r.nextInt(10)); // [0, 10) 16 | 17 | System.out.println("-----------------------"); 18 | 19 | for (int i = 0; i < 10; ++i) 20 | System.out.println(r.nextInt(10, 100)); // since Java 17 with (RandomGenerator interface) 21 | 22 | System.out.println("-----------------------"); 23 | 24 | for (int i = 0; i < 10; ++i) 25 | System.out.println(r.nextInt(90) + 10); // [0, 90) + 10 -> [0, 100] 26 | 27 | for (int i = 0; i < 10; ++i) 28 | System.out.println(r.nextInt(20)- 5); // [0, 20) - 5 -> [-5, 15) 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/randomclass/Example2.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.randomclass; 2 | 3 | public class Example2 { 4 | public static void main(String[] args) 5 | { 6 | java.util.Random r = new java.util.Random(); 7 | 8 | for (int i = 0; i < 5; ++i) 9 | System.out.println(r.nextLong()); 10 | 11 | System.out.println("------------------------"); 12 | 13 | for (int i = 0; i < 5; ++i) 14 | System.out.println(r.nextLong(1_000_000_000_000L)); // [0, 1_000_000_000_000L) 15 | 16 | System.out.println("------------------------"); 17 | 18 | for (int i = 0; i < 5; ++i) 19 | System.out.println(r.nextDouble() * 3 + 80); // [80, 83) 20 | 21 | System.out.println("------------------------"); 22 | 23 | for (int i = 0; i < 5; ++i) 24 | System.out.println(r.nextDouble(80, 83)); 25 | 26 | System.out.println("------------------------"); 27 | 28 | for (int i = 0; i < 5; ++i) 29 | System.out.println(r.nextBoolean()); 30 | 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/lessons/oop/BasicCharacter.java: -------------------------------------------------------------------------------- 1 | package lessons.oop; 2 | 3 | public class BasicCharacter { 4 | 5 | private char ch; 6 | 7 | private BasicCharacter(char c) 8 | { 9 | ch = c; 10 | } 11 | 12 | public boolean isDigit() 13 | { 14 | return ch >= 48 && ch <= 57; 15 | } 16 | 17 | public boolean isLetter() 18 | { 19 | return ch >= 65 && ch <= 90 || ch >= 97 && ch <= 122; 20 | } 21 | 22 | public boolean isLowerCase() 23 | { 24 | return ch >= 97 && ch <= 122; 25 | } 26 | 27 | public boolean isUpperCase() 28 | { 29 | return ch >= 65 && ch <= 90; 30 | } 31 | 32 | public boolean isWhiteSpace() 33 | { 34 | return ch == 32; 35 | } 36 | 37 | public char toLowerCase() 38 | { 39 | return isUpperCase() ? (char)(ch + 32) : ch; 40 | } 41 | 42 | public char toUpperCase() 43 | { 44 | return isLowerCase() ? (char)(ch - 32) : ch; 45 | } 46 | 47 | public static BasicCharacter valueOf(char c) 48 | { 49 | return new BasicCharacter(c); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q5.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | /* 3 | Write a program that starts with a line of text and then outputs that line of text 4 | with the first occurrence of "hate" changed to "love". For example, a possible 5 | sample output might be 6 | 7 | The line of text to be changed is: 8 | I hate you. 9 | I have rephrased that line to read: 10 | I love you. 11 | 12 | You can assume that the word "hate" occurs in the input. If the word "hate" 13 | occurs more than once in the line, your program will replace only the first occurrence of "hate". Since we will not discuss input until Chapter 2, use a defined 14 | constant for the string to be changed. To make your program work for another 15 | string, you should only need to change the definition of this defined constant. 16 | 17 | */ 18 | public class Q5 { 19 | public static void main(String[] args) 20 | { 21 | java.util.Scanner kb = new java.util.Scanner(System.in); 22 | 23 | System.out.print("TEXT:"); 24 | String text = kb.nextLine(); 25 | 26 | System.out.println(text.replace("hate", "love")); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q6.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | /* 3 | Write a program for calculating the simple interest on a loan when the initial principal amount (princi_amnt) is $1000, 4 | the initial interest rate (int_rate) is 5.0%, 5 | and the number of years (no_of_yrs) is 5. Use suitable data types to declare these 6 | variables. Simple interest is calculated using the following equation: 7 | 8 | Simple interest = (Principal amount * Interest rate * Number of years) / 100 9 | 10 | */ 11 | public class Q6 { 12 | public static void main(String[] args) { 13 | 14 | java.util.Scanner kb = new java.util.Scanner(System.in); 15 | 16 | System.out.println("Principal Amount: "); 17 | double principalAmount = kb.nextDouble(); 18 | 19 | System.out.println("Interest Rate: "); 20 | double interestRate = kb.nextDouble(); 21 | 22 | System.out.println("Numbers of Years:"); 23 | double numberOfYears = kb.nextDouble(); 24 | 25 | double simpleInterest = principalAmount * interestRate * numberOfYears / 100; 26 | System.out.println(simpleInterest); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/Defaults.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics; 2 | 3 | public class Defaults { 4 | public static void main(String[] args) 5 | { 6 | K k; 7 | 8 | k = new K(); 9 | 10 | System.out.printf("k.b: %d%n", k.b); // 0 11 | System.out.printf("k.s: %d%n", k.s); // 0 12 | System.out.printf("k.i: %d%n", k.i); // 0 13 | System.out.printf("k.l: %d%n", k.l); // 0 14 | 15 | System.out.println("---------------------------"); 16 | 17 | System.out.printf("k.f: %f%n", k.f); // 0.0 18 | System.out.printf("k.d: %f%n", k.d); // 0.0 19 | 20 | System.out.println("---------------------------"); 21 | 22 | 23 | System.out.printf("k.b: %c%n", k.ch); // character at zero index from unicode table 24 | System.out.printf("k.b: %b%n", k.bool); // false 25 | } 26 | } 27 | 28 | class K { 29 | public byte b; // 1 2 4 8 4 8 2 --1 30 | public short s; 31 | public int i; 32 | public long l; 33 | 34 | public float f; 35 | public double d; 36 | 37 | public char ch; 38 | public boolean bool; 39 | } 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/lessons/arrays/Intro.java: -------------------------------------------------------------------------------- 1 | package lessons.arrays; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Intro { 7 | public static void IntroArrays() 8 | { 9 | int [] a; // array ref. 10 | boolean b[]; 11 | double[] d; 12 | 13 | Scanner kb = new Scanner(System.in); 14 | 15 | System.out.print("Length: "); 16 | int n = Integer.parseInt(kb.nextLine()); 17 | 18 | a = new int[n]; 19 | 20 | System.out.println("Array length: " + a.length); 21 | 22 | for (int i = 0; i < a.length; ++i) { 23 | System.out.printf("a[%d] = %d%n", i, a[i]); 24 | } 25 | 26 | System.out.println("--------------------------"); 27 | 28 | for (int i = 0; i < a.length; ++i) { 29 | a[i] = i; 30 | System.out.printf("a[%d] = %d%n", i, a[i]); 31 | } 32 | 33 | 34 | String[] names = new String[6]; // indexes -> [0, 6) 35 | 36 | for (int i = 0; i < names.length; ++i) { 37 | System.out.printf("names[%s] = %s%n", i, names[i].concat("x")); 38 | } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/ifelse/IfElseExample.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.ifelse; 2 | 3 | public class IfElseExample { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner keyboard = new java.util.Scanner(System.in); 7 | 8 | System.out.print("First number: "); 9 | int firstNumber = keyboard.nextInt(); 10 | 11 | System.out.print("Second number: "); 12 | int secondNumber = keyboard.nextInt(); 13 | 14 | // First > Second 15 | if (firstNumber > secondNumber) { 16 | System.out.println("First number > Second Number"); 17 | } 18 | 19 | System.out.print("Yasin age: "); 20 | int yasinAge = keyboard.nextInt(); 21 | 22 | 23 | if (yasinAge >= 18) { 24 | 25 | System.out.print("Do you have girlfriend ? "); 26 | boolean hasGirlFriend = keyboard.nextBoolean(); 27 | 28 | if (hasGirlFriend) { 29 | System.out.println("You can enter the disco"); 30 | } else { 31 | System.out.println("You cant enter"); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/lessons/languagebasics/Operators.java: -------------------------------------------------------------------------------- 1 | package lessons.languagebasics; 2 | 3 | public class Operators { 4 | public static void main(String[] args) 5 | { 6 | final double EXCHANGE_RATE = 18.54; //CONSTANT 7 | final double PI = 3.142; //CONSTANT 8 | // PI = 5.6; //ERROR 9 | // EXCHANGE_RATE = 18.56; //ERROR 10 | 11 | double b = 5 / 3; 12 | System.out.println(b); 13 | 14 | mathOp(); 15 | } 16 | 17 | public static void mathOp() 18 | { 19 | int a = 5, b = 10; 20 | 21 | System.out.println(a + b); 22 | System.out.println(a - b); 23 | System.out.println(a * b); 24 | System.out.println(a / b); 25 | System.out.println(a % b); 26 | 27 | // a += b; 28 | // a -= b; 29 | // a *= b; 30 | // a /= b; 31 | // a %= b; 32 | 33 | a++; 34 | 35 | b = a++; // first assign b's value to a's value after that increment "a" with one 36 | b = ++a; // increment "a" with one after that assign b's value to a's value after that 37 | b = --a; 38 | b = a--; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/Customer.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class Customer { 4 | public String id; 5 | public String password; 6 | public String name; 7 | public Wallet wallet; 8 | public Phone phone; 9 | 10 | public Customer(String newName, Wallet newWallet, Phone newPhone, String newId, String newPassword) 11 | { 12 | name = newName; 13 | wallet = newWallet; 14 | phone = newPhone; 15 | id = newId; 16 | password = newPassword; 17 | } 18 | 19 | 20 | public String getId() 21 | { 22 | return id; 23 | } 24 | 25 | public String getPassword() 26 | { 27 | return password; 28 | } 29 | 30 | public String getName() 31 | { 32 | return name; 33 | } 34 | 35 | public Wallet getWallet() 36 | { 37 | return wallet; 38 | } 39 | 40 | public Phone getPhone() 41 | { 42 | return phone; 43 | } 44 | 45 | public static void goToMarket(Customer customer) 46 | { 47 | Market.shopping(customer); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForExample7.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForExample7 { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | System.out.print("Number:"); 13 | int val = kb.nextInt(); 14 | 15 | System.out.printf("%d. prime number: %d", val, getPrime(val)); 16 | } 17 | 18 | public static int getPrime(int n) 19 | { 20 | int val = 2; 21 | int count = 0; 22 | 23 | while (true) { 24 | if (isPrime(val)) 25 | ++count; 26 | 27 | if (count == n) 28 | return val; 29 | 30 | val++; 31 | } 32 | } 33 | 34 | public static boolean isPrime(long a) 35 | { 36 | if (a <= 1) 37 | return false; 38 | 39 | if (a % 2 == 0) 40 | return a == 2; 41 | 42 | for (long i = 3; i * i <= a; i += 2) 43 | if (a % i == 0) 44 | return false; 45 | 46 | return true; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/homeworks/chapter3/Q2.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3; 2 | 3 | public class Q2 { 4 | public static void main(String[] args) 5 | { 6 | display(); 7 | } 8 | 9 | public static String rgb() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | String result = ""; 14 | String colorCharHolder = ""; 15 | 16 | for (int i = 1; i <= 5; i++) { 17 | 18 | System.out.printf("Please enter the %d. color : ", i); 19 | String colorChar = kb.nextLine().substring(0,1); 20 | 21 | if (!"RGB".contains(colorChar)) { 22 | return "You have entered invalid color."; 23 | } 24 | 25 | if(colorCharHolder.equals(colorChar)) { 26 | return "You have entered the same color consecutively : " + colorCharHolder + colorChar; 27 | } 28 | 29 | colorCharHolder = colorChar; 30 | result += colorChar; 31 | } 32 | 33 | return result; 34 | } 35 | 36 | public static void display() 37 | { 38 | System.out.printf("Your color combination is : %s.",rgb()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/lessons/methods/MethodsWithParameters.java: -------------------------------------------------------------------------------- 1 | package lessons.methods; 2 | 3 | public class MethodsWithParameters { 4 | public static void main(String[] args) 5 | { 6 | addTwoNumbers(5, 6); 7 | // int a = 5; -> int b = 6; 8 | // System.out.printf("%d + %d = %d", 5, 6, 11); 9 | // 5 + 6 = 11 10 | 11 | int a = 5; 12 | int b = 6; 13 | addTwoNumbers(a, b); 14 | // addTwoNumbers(a, b); a = 5 -> b = 6 15 | // int a = 5; -> int b = 6; 16 | // System.out.printf("%d + %d = %d", 5, 6, 11); 17 | // 5 + 6 = 11 18 | 19 | addThreeNumbers(a, 5, b); 20 | // addThreeNumbers(5, 5, 6); 21 | // int a = 5; -> int b = 5; -> int c = 6; 22 | // System.out.printf("%d + %d = %d", 5, 5, 6, 16); 23 | // 5 + 5 + 6 = 16 24 | } 25 | 26 | public static void addTwoNumbers(int a, int b) // int a = 5; -> int b = 6; 27 | { 28 | System.out.printf("%d + %d = %d%n", a, b, a + b); 29 | } 30 | 31 | public static void addThreeNumbers(int a, int b, int c) 32 | { 33 | System.out.printf("%d + %d + %d = %d%n", a, b, c, a + b + c); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/homeworks/chapter3/Q1.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3; 2 | 3 | public class Q1 { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | System.out.print("Enter the a number: "); 14 | double n = Double.parseDouble(kb.nextLine()); 15 | 16 | if(n < 0) { 17 | System.out.print("You entered a negative number. Try again."); 18 | return; 19 | } 20 | 21 | System.out.printf("The square root of %.2f is %.2f%n", n, babylonianAlgorithm(n)); 22 | } 23 | 24 | public static double babylonianAlgorithm(double n) 25 | { 26 | double guess = n / 2; 27 | double previousGuess = guess; 28 | double differenceBetweenGuesses = 1; 29 | 30 | while (differenceBetweenGuesses >= 0.01) { 31 | double r = n / guess; 32 | guess = (guess + r) / 2; 33 | differenceBetweenGuesses = (previousGuess - guess) / guess; 34 | previousGuess = guess; 35 | } 36 | 37 | return guess; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q9.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | /* 3 | A simple rule to estimate your ideal body weight is to allow 110 pounds for the first 5 4 | feet of height and 5 pounds for each additional inch. Write a program with a variable 5 | for the height of a person in feet and another variable for the additional inches. Assume 6 | the person is at least 5 feet tall. For example, a person that is 6 feet and 3 inches tall 7 | would be represented with a variable that stores the number 6 and another variable that 8 | stores the number 3. Based on these values, calculate and output the ideal body weight. 9 | */ 10 | 11 | public class Q9 { 12 | public static void main(String[] args) { 13 | 14 | java.util.Scanner kb = new java.util.Scanner(System.in); 15 | 16 | System.out.println("Feet: "); 17 | int feet = kb.nextInt(); 18 | 19 | System.out.println("Inch: "); 20 | int inch = kb.nextInt(); 21 | 22 | System.out.println("Pound: "); 23 | int pound = kb.nextInt(); 24 | 25 | feet -= 5; 26 | inch = inch + (feet * 12); 27 | pound = pound + (inch * 5); 28 | 29 | System.out.println(pound); 30 | } 31 | } -------------------------------------------------------------------------------- /src/homeworks/chapter3/chapter3/Q2.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3.chapter3; 2 | 3 | public class Q2 { 4 | public static void main(String[] args) 5 | { 6 | display(); 7 | } 8 | 9 | public static String rgb() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | String result = ""; 14 | String colorCharHolder = ""; 15 | 16 | for (int i = 1; i <= 5; i++) { 17 | 18 | System.out.printf("Please enter the %d. color : ", i); 19 | String colorChar = kb.nextLine().substring(0,1); 20 | 21 | if (!"RGB".contains(colorChar)) { 22 | return "You have entered invalid color."; 23 | } 24 | 25 | if(colorCharHolder.equals(colorChar)) { 26 | return "You have entered the same color consecutively : " + colorCharHolder + colorChar; 27 | } 28 | 29 | colorCharHolder = colorChar; 30 | result += colorChar; 31 | } 32 | 33 | return result; 34 | } 35 | 36 | public static void display() 37 | { 38 | System.out.printf("Your color combination is : %s.",rgb()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/ifelse/IfElse.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.ifelse; 2 | 3 | public class IfElse { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner keyboard = new java.util.Scanner(System.in); 7 | 8 | System.out.print("Yasin age: "); 9 | int yasinAge = keyboard.nextInt(); // 21 10 | 11 | System.out.print("Akif age: "); 12 | int akifAge = keyboard.nextInt(); // 29 13 | 14 | System.out.print("Eren age: "); 15 | int erenAge = keyboard.nextInt(); //17 16 | 17 | // Let's go to the bar 18 | 19 | if (yasinAge >= 18) 20 | System.out.println("Yasin can enter the bar"); 21 | else 22 | System.out.println("Yasin cant enter the bar"); 23 | 24 | if (akifAge >= 18) { 25 | System.out.println("Akif can enter the bar"); 26 | } 27 | else { 28 | System.out.println("Akif cant enter the bar"); 29 | } 30 | 31 | if (erenAge >= 18) { 32 | System.out.println("Eren can enter the bar"); 33 | } else { 34 | System.out.println("Eren cant enter the bar"); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/homeworks/chapter3/chapter3/Q1.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3.chapter3; 2 | 3 | public class Q1 { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | System.out.print("Enter the a number: "); 14 | double n = Double.parseDouble(kb.nextLine()); 15 | 16 | if(n < 0) { 17 | System.out.print("You entered a negative number. Try again."); 18 | return; 19 | } 20 | 21 | System.out.printf("The square root of %.2f is %.2f%n", n, babylonianAlgorithm(n)); 22 | } 23 | 24 | public static double babylonianAlgorithm(double n) 25 | { 26 | double guess = n / 2; 27 | double previousGuess = guess; 28 | double differenceBetweenGuesses = 1; 29 | 30 | while (differenceBetweenGuesses >= 0.01) { 31 | double r = n / guess; 32 | guess = (guess + r) / 2; 33 | differenceBetweenGuesses = (previousGuess - guess) / guess; 34 | previousGuess = guess; 35 | } 36 | 37 | return guess; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/lessons/loops/LabeledBreak.java: -------------------------------------------------------------------------------- 1 | package lessons.loops; 2 | 3 | public class LabeledBreak { 4 | public static void main(String[] args) 5 | { 6 | EXIT_LOOP: 7 | for (int i = 1; i < 10; ++i) 8 | for (int j = 20; j >= 2; --j) { 9 | System.out.printf("(%d, %d)", i, j); 10 | 11 | if ((i + j) % 6 == 0) 12 | break EXIT_LOOP; 13 | } 14 | 15 | complexLabeledBreak(); 16 | } 17 | 18 | public static void complexLabeledBreak() 19 | { 20 | EXIT_LOOP: 21 | for (int i = 6; i < 100; i += 2) 22 | EXIT_INNER_LOOP: 23 | for (int j = 3; j < 87; j++) 24 | for (int k = 20; k >= 0; --k) { 25 | System.out.printf("(%d, %d, %d)%n", i, j, k); 26 | 27 | if ((i + j + k) % 13 == 0) 28 | break; 29 | 30 | if ((i + j + k) % 7 == 0) 31 | break EXIT_INNER_LOOP; 32 | 33 | if ((i + j + k) % 5 == 0) 34 | break EXIT_LOOP; 35 | } 36 | 37 | System.out.println("EFFICIENT HOUSE"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/lessons/oop/stringexamples/CMD.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.stringexamples; 2 | 3 | class CMD { 4 | static String cmd = "C:\\Users\\EREN\\Desktop\\Java"; 5 | static java.util.Scanner kb = new java.util.Scanner(System.in); 6 | 7 | public static void run() 8 | { 9 | for (;;) { 10 | System.out.print(cmd + ":"); 11 | 12 | String input = kb.nextLine(); 13 | 14 | if (input.equals("exit")) 15 | break; 16 | 17 | 18 | 19 | if (input.startsWith("cd")) { 20 | if (input.startsWith("cd ..")) { 21 | int index = cmd.lastIndexOf("\\"); 22 | cdToUpper(cmd.substring(0, index)); 23 | } else 24 | cd(input.substring(3)); 25 | } 26 | 27 | if (input.equals("date")) 28 | date(); 29 | } 30 | } 31 | 32 | public static void date() 33 | { 34 | System.out.println("4/13/2023"); 35 | } 36 | 37 | public static void cd(String path) 38 | { 39 | cmd += "\\" + path; 40 | } 41 | 42 | public static void cdToUpper(String path) 43 | { 44 | cmd = path; 45 | } 46 | } -------------------------------------------------------------------------------- /src/lessons/loops/forloops/ForExample6.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.forloops; 2 | 3 | public class ForExample6 { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | System.out.print("Number:"); 13 | int val = kb.nextInt(); 14 | 15 | 16 | if (isPrime(val)) 17 | System.out.printf("%d. is prime. ", getPrimeIndex(val)); 18 | else 19 | System.out.println("please enter the prime number"); 20 | } 21 | 22 | public static int getPrimeIndex(int n) 23 | { 24 | int count = 1; 25 | 26 | for (int i = 1; i <= n; i += 2) { 27 | if (isPrime(i)) 28 | count++; 29 | } 30 | 31 | return count; 32 | } 33 | 34 | public static boolean isPrime(long a) 35 | { 36 | if (a <= 1) 37 | return false; 38 | 39 | if (a % 2 == 0) 40 | return a == 2; 41 | 42 | for (long i = 3; i * i <= a; i += 2) 43 | if (a % i == 0) 44 | return false; 45 | 46 | return true; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/randomclass/SameTwoDiceAppSimple.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.randomclass; 2 | 3 | 4 | public class SameTwoDiceAppSimple { 5 | public static void main(String[] args) 6 | { 7 | Test.run(); 8 | } 9 | } 10 | 11 | class Test { 12 | public static void run() 13 | { 14 | for (;;) { 15 | System.out.print("How many time you want to dice:"); 16 | int diceCount = new java.util.Scanner(System.in).nextInt(); 17 | 18 | if (diceCount == 0) 19 | return; 20 | 21 | System.out.printf("%d per times rolling dice percent: %%%.3f%n", diceCount, 22 | SameTwoDiceCalculation.sameDiceChangeRatio(diceCount, new java.util.Random()) * 100); 23 | } 24 | } 25 | } 26 | 27 | 28 | class SameTwoDiceCalculation { 29 | 30 | public static double sameDiceChangeRatio(int loopTimes, java.util.Random r) 31 | { 32 | int diceOne = r.nextInt(6) + 1; 33 | int count = 0; 34 | 35 | for (int i = 3; i <= loopTimes + 3; ++i) 36 | if (r.nextInt(6) + 1 == diceOne) 37 | ++count; 38 | 39 | 40 | return (double)count / loopTimes; 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/lessons/loops/whileloops/WhileIntro.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.whileloops; 2 | 3 | public class WhileIntro { 4 | public static void main(String[] args) { 5 | /* 6 | while () // true 7 | 8 | */ 9 | 10 | int count = 0; 11 | 12 | if (count < 5) { 13 | System.out.println(count++); 14 | } 15 | 16 | if (count < 5) { 17 | System.out.println(count++); 18 | } // count = 2 19 | 20 | if (count < 5) { 21 | System.out.println(count++); 22 | } // count = 3 23 | 24 | if (count < 5) { 25 | System.out.println(count++); 26 | } // count = 4 27 | 28 | if (count < 5) { 29 | System.out.println(count++); 30 | } // count = 5 31 | 32 | if (count < 5) { 33 | System.out.println(count++); 34 | } // count = 5 35 | 36 | System.out.printf("count = %d%n", count); 37 | 38 | 39 | int count2 = 0; 40 | 41 | while (count2 < 5) { 42 | System.out.println(count2); 43 | ++count2; 44 | } 45 | 46 | System.out.printf("count2 = %d", count2); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/homeworks/extra/hw1/Q4.java: -------------------------------------------------------------------------------- 1 | package homeworks.extra.hw1; 2 | 3 | public class Q4 { 4 | public static void main(String[] args) { 5 | test(); 6 | } 7 | 8 | public static void test() { 9 | int index = 0; 10 | 11 | while (index++ <= 999999) 12 | if (isArmstrong(index)) 13 | System.out.printf("%d is Armstrong .%n", index); 14 | } 15 | 16 | public static boolean isArmstrong(int a) { 17 | return a == getDigitsPowSum(a) && a > 0; 18 | } 19 | 20 | public static int getDigitsPowSum(int a) { // -- 21 | int digits = countDigits(a); 22 | int result = 0; 23 | 24 | while (a != 0) { 25 | result += pow(a % 10, digits); 26 | a /= 10; 27 | } 28 | 29 | return result; 30 | } 31 | 32 | public static int pow(int a, int b) { 33 | int result = 1; 34 | 35 | while (b-- > 0) { 36 | result *= a; 37 | } 38 | 39 | return result; 40 | } 41 | 42 | public static int countDigits(int a) { 43 | int count = 0; 44 | 45 | do { 46 | count++; 47 | a /= 10; 48 | } while (a != 0); 49 | 50 | return count; 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /src/lessons/languagebasics/OrOperators.java: -------------------------------------------------------------------------------- 1 | package lessons.languagebasics; 2 | 3 | public class OrOperators { 4 | public static void main(String[] args) 5 | { 6 | if (getTrue() | getFalse()) 7 | System.out.println("True"); 8 | 9 | System.out.println("------------"); 10 | 11 | if (getTrue() | getTrue()) 12 | System.out.println("True"); 13 | 14 | System.out.println("---------------"); 15 | 16 | if (getFalse() | getFalse()) 17 | System.out.println("True"); 18 | 19 | System.out.println("---------------"); 20 | 21 | if (getTrue() || getFalse()) 22 | System.out.println("True"); 23 | 24 | System.out.println("------------"); 25 | 26 | if (getTrue() || getTrue()) 27 | System.out.println("True"); 28 | 29 | System.out.println("---------------"); 30 | 31 | if (getFalse() || getFalse()) 32 | System.out.println("True"); 33 | } 34 | 35 | public static boolean getTrue() 36 | { 37 | System.out.println("getTrue()"); 38 | return true; 39 | } 40 | 41 | public static boolean getFalse() 42 | { 43 | System.out.println("getFalse()"); 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/lessons/methods/methodoverloading/Example2.java: -------------------------------------------------------------------------------- 1 | package lessons.methods.methodoverloading; 2 | 3 | public class Example2 { 4 | public static void main(String[] args) 5 | { 6 | // Over.foo(5, 5); -> Error: Ambiguous method call. Both 7 | } 8 | } 9 | 10 | class Over { 11 | public static void foo(long l, int i) 12 | { 13 | //... 14 | } 15 | 16 | 17 | public static void foo(int i, double d) 18 | { 19 | //... 20 | } 21 | 22 | 23 | public static void foo(float f, int i) 24 | { 25 | //... 26 | } 27 | 28 | } 29 | 30 | class Calculator { 31 | public long add(long a, long b) 32 | { 33 | return add(a, b, 0); 34 | } 35 | 36 | public long add(long a, long b, long c) 37 | { 38 | return add(a, b, c, 0); 39 | } 40 | 41 | public long add(long a, long b, long c, long d) 42 | { 43 | return a + b + c + d; 44 | } 45 | 46 | public double add(double a, double b) 47 | { 48 | return add(a, b, 0.); 49 | } 50 | 51 | public double add(double a, double b, double c) 52 | { 53 | return add(a, b, c, 0.); 54 | } 55 | 56 | public double add(double a, double b, double c, double d) 57 | { 58 | return a + b + c + d; 59 | } 60 | } -------------------------------------------------------------------------------- /src/lessons/oop/basics/randomclass/CoinSimulationApp.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.randomclass; 2 | 3 | public class CoinSimulationApp { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | CoinSimulation coinSimulation = new CoinSimulation(); 13 | 14 | for (;;) { 15 | System.out.print("How many times do you want to flip the coin ? "); 16 | int n = Integer.parseInt(kb.nextLine()); 17 | 18 | // TODO 19 | 20 | if (n <= 0) 21 | break; 22 | 23 | coinSimulation.run(n); 24 | 25 | System.out.printf("Tail odds: %f%n", coinSimulation.odds); 26 | } 27 | } 28 | } 29 | 30 | class CoinSimulation { 31 | public double odds; 32 | 33 | public static int getTailCount(int n) 34 | { 35 | java.util.Random r = new java.util.Random(); 36 | int count = 0; 37 | 38 | for (int i = 0; i < n; ++i) 39 | if (r.nextBoolean()) 40 | ++count; 41 | 42 | return count; 43 | } 44 | 45 | public void run(int n) 46 | { 47 | odds = getTailCount(n) / (double)n; 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/homeworks/extra/hw1/Q1.java: -------------------------------------------------------------------------------- 1 | package homeworks.extra.hw1; 2 | 3 | public class Q1 { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | System.out.print("a: "); 14 | int a = kb.nextInt(); 15 | 16 | System.out.print("b: "); 17 | int b = kb.nextInt(); 18 | 19 | System.out.print("c: "); 20 | int c = kb.nextInt(); 21 | 22 | display(a, b, c); 23 | } 24 | 25 | public static void display(int a, int b, int c) 26 | { 27 | int min = min(a, min(b, c)); 28 | int max = max(a, max(b, c)); 29 | int mid = a + b + c - (min + max); 30 | 31 | if (min < mid) 32 | System.out.printf("%d < %d ", min, mid); 33 | else 34 | System.out.printf("%d = %d ", min, mid); 35 | 36 | if (mid < max) 37 | System.out.printf("< %d", max); 38 | else 39 | System.out.printf("= %d", max); 40 | } 41 | 42 | public static int max(int a, int b) 43 | { 44 | return a >= b ? a : b; 45 | } 46 | 47 | public static int min(int a, int b) 48 | { 49 | return a <= b ? a : b; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/lessons/consolio/PrintMethods.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio; 2 | 3 | public class PrintMethods { 4 | public static void main(String[] args) 5 | { 6 | System.out.print("Efficient\n"); // System.out.println("Efficient"); 7 | System.out.print("House\n"); 8 | System.out.println("Efficient\tHouse"); 9 | 10 | System.out.println(10.0 + 20.0 + 30); // 60.0 11 | System.out.println(10 + 20 + "30"); // 3030 12 | System.out.println("30" + 10 + 20); // 301020 13 | 14 | double price = 21.98; 15 | String organization = "Efficient House"; 16 | 17 | System.out.printf("$ %.2f each%n", price); 18 | System.out.printf("%s%n", organization); 19 | 20 | String name = "John"; 21 | int age = 23; 22 | double height = 1.86; 23 | float weight = 100.2f; 24 | boolean greenEyes = true; 25 | char myFavoriteLetter = 'E'; 26 | 27 | System.out.printf("Name: %s, Age: %d, Height: %.2f, Weight: %.1f, Green Eyes: %b, Favorite Letter: %c%n", 28 | name, age, height, weight, greenEyes, myFavoriteLetter); 29 | 30 | 31 | int day = 2; 32 | int month = 3; 33 | int year = 1999; 34 | 35 | System.out.printf("Date: %02d/%02d/%04d", day, month, year); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/statics/GameApp.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.statics; 2 | 3 | public class GameApp { 4 | public static void main(String[] args) 5 | { 6 | WarGameApp.run(); 7 | } 8 | } 9 | 10 | class WarGameApp { 11 | public static void run() 12 | { 13 | for (int i = 0; i < 10; ++i) { 14 | Warrior warrior = new Warrior(); 15 | 16 | //.. 17 | Worker worker = new Worker(); 18 | worker.countIncrease(); 19 | } 20 | 21 | //... 22 | 23 | for (int i = 0; i < 20; ++i) { 24 | Worker worker = new Worker(); 25 | 26 | //.. 27 | 28 | worker.countIncrease(); 29 | } 30 | 31 | System.out.printf("Number of warrior: %d%n", Warrior.count); 32 | System.out.printf("Number of worker: %d%n", Worker.count); 33 | } 34 | } 35 | 36 | class Warrior { 37 | public static int count; 38 | 39 | public int power; 40 | public int numberOfGuns; 41 | 42 | public void countIncrease() 43 | { 44 | count++; 45 | } 46 | } 47 | 48 | class Worker { 49 | public static int count; 50 | 51 | public int energy; 52 | 53 | public void countIncrease() 54 | { 55 | count++; 56 | } 57 | } 58 | 59 | class Animal { 60 | //... 61 | } 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /src/lessons/languagebasics/PrimitiveTypes.java: -------------------------------------------------------------------------------- 1 | package lessons.languagebasics; 2 | 3 | public class PrimitiveTypes { 4 | public static void main(String[] args) 5 | { 6 | int i = 0b1010; //binary 7 | System.out.println(i); 8 | 9 | int i2 = 1010; //decimal 10 | System.out.println(i2); 11 | 12 | int i3 = 0137; //Octal 13 | System.out.println(i3); 14 | 15 | int i4 = 0x35A; // Hexadecimal 16 | System.out.println(i4); 17 | 18 | char ch = 65535; //int (decimal) 19 | System.out.println(ch); 20 | 21 | char ch2 = 0x59; // Hexadecimal 22 | System.out.println(ch2); 23 | 24 | char ch3 = 0b1011001; // Binary 25 | System.out.println(ch3); 26 | 27 | char ch4 = 0131; // Octal (8) 28 | System.out.println(ch4); 29 | 30 | char ch5 = 0xFFFF; // char -> [0, 65535] 31 | System.out.println(ch5); 32 | 33 | System.out.println("Y = \u0059 \n A = \u2161"); 34 | } 35 | 36 | public static void decimalNumbers() 37 | { 38 | byte b = 127; 39 | short s = 32333; 40 | int i = 5342342; 41 | long l = 1_000_000_000_000L; 42 | } 43 | 44 | public static void floatingPointNumbers() 45 | { 46 | float f = 3.14f; 47 | final double AVAGADRO_NUMBER = 6.023e23; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/lessons/methods/ReturnTypeMethods.java: -------------------------------------------------------------------------------- 1 | package lessons.methods; 2 | 3 | public class ReturnTypeMethods { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner keyboard = new java.util.Scanner(System.in); 7 | 8 | int sum = addTwoNumbers(5, 10); 9 | // addTwoNumbers(int a, int b) a = 5 || b = 10 10 | // int temp = 15 11 | // int sum = temp; 12 | System.out.println(sum); // 15 13 | 14 | sum = addThreeNumbers(sum, 12, 13); // 40 15 | // addThreeNumbers(sum, 12, 13) sum = 15 16 | // addThreeNumbers(int a, int b, int c) a = 15 || b = 12 || c = 13 17 | // return 15 + 12 + 13; 18 | // int temp = 40; 19 | // int sum = temp; 20 | // int sum = 40; 21 | System.out.println(sum); // 40 22 | 23 | System.out.print("val: "); 24 | int val = keyboard.nextInt(); 25 | 26 | System.out.printf("%d is Even ? = %b", val, isEven(val)); 27 | 28 | } 29 | 30 | public static int addTwoNumbers(int a, int b) 31 | { 32 | return a + b; // int temp = a + b; 33 | } 34 | 35 | public static int addThreeNumbers(int a, int b, int c) 36 | { 37 | return a + b + c; 38 | } 39 | 40 | public static boolean isEven(int val) 41 | { 42 | return val % 2 == 0; 43 | // boolean temp = val % 2 == 0; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/randomclass/Seed.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.randomclass; 2 | 3 | import java.util.Random; 4 | import java.util.Scanner; 5 | 6 | // Random Tuncay 13: 328 7 | // Random Berk 11: 205 8 | public class Seed { 9 | public static void main(String[] args) 10 | { 11 | java.util.Random r = new java.util.Random(); 12 | 13 | algorithm(new Random(), new Scanner(System.in)); 14 | 15 | r.setSeed(10); 16 | 17 | for (int i = 0; i < 12; ++i) 18 | System.out.printf("%d ", r.nextInt(9) + 1); 19 | 20 | System.out.println(); 21 | } 22 | 23 | public static void algorithm(java.util.Random r, java.util.Scanner kb) 24 | { 25 | int sumForFirstSeed = 0; 26 | int sumForSecondSeed = 0; 27 | 28 | System.out.print("seed: "); 29 | r.setSeed(kb.nextLong()); 30 | 31 | for (int i = 0; i < 5; ++i) { 32 | sumForFirstSeed += r.nextInt(100); 33 | } 34 | 35 | System.out.println("----------------------------------------"); 36 | 37 | System.out.print("seed: "); 38 | r.setSeed(kb.nextLong()); 39 | 40 | for (int i = 0; i < 5; i++) { 41 | sumForSecondSeed += r.nextInt(100); 42 | } 43 | 44 | System.out.printf("Random Tuncay: %d%n", sumForFirstSeed); 45 | System.out.printf("Random Berk: %d%n", sumForSecondSeed); 46 | 47 | 48 | } 49 | } -------------------------------------------------------------------------------- /src/lessons/consolio/ScannerClass.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio; 2 | 3 | 4 | public class ScannerClass { 5 | public static void main(String[] args) 6 | { 7 | java.util.Scanner input = new java.util.Scanner(System.in); 8 | 9 | int x = 5; 10 | int y = 6; 11 | 12 | if (x < y) // Condition 'x < y' is always 'true' 13 | System.out.println(y); 14 | 15 | 16 | System.out.println("First int: "); 17 | int firstNumber = input.nextInt(); 18 | 19 | System.out.println("Second int: "); 20 | int secondNumber = input.nextInt(); 21 | 22 | if (firstNumber < secondNumber) 23 | System.out.println("second number > first number"); 24 | 25 | 26 | System.out.print("Double: "); 27 | double d = input.nextDouble(); 28 | 29 | System.out.print("float: "); 30 | float f = input.nextFloat(); 31 | 32 | System.out.print("byte: "); 33 | byte b = input.nextByte(); 34 | 35 | System.out.print("short: "); 36 | short s = input.nextShort(); 37 | 38 | System.out.print("boolean: "); 39 | boolean bool = input.nextBoolean(); 40 | 41 | String temp = input.nextLine(); 42 | System.out.println(temp.isEmpty()); 43 | 44 | System.out.print("text:" ); 45 | String str = input.nextLine(); 46 | System.out.print(str.isEmpty()); 47 | } 48 | } 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q3.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | /* 3 | Write a program that starts with the string variable first set to your first name 4 | and the string variable last set to your last name. Both names should be all lowercase. Your program should then create a new string that contains your full name in 5 | pig latin with the first letter capitalized for the first and last name. Use only the pig 6 | latin rule of moving the first letter to the end of the word and adding “ay.” Output 7 | the pig latin name to the screen. Use the substring and toUpperCase methods 8 | to construct the new name. 9 | 10 | For example, given 11 | 12 | first = "walt"; 13 | last = "savitch"; 14 | 15 | the program should create a new string with the text “Altway Avitchsay” and print it. 16 | */ 17 | public class Q3 { 18 | public static void main(String[] args) { 19 | java.util.Scanner kb = new java.util.Scanner(System.in); 20 | 21 | System.out.print("First name:"); 22 | String first =kb.nextLine(); 23 | 24 | System.out.print("Last name:"); 25 | String last = kb.nextLine(); 26 | 27 | String pigLatinFirst = first.substring(1, 2).toUpperCase() + first.substring(2) + first.substring(0, 1) + "ay"; 28 | String pigLatinLast = last.substring(1, 2).toUpperCase() + last.substring(2) + last.substring(0, 1) + "ay"; 29 | System.out.println(pigLatinFirst + " " + pigLatinLast); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/homeworks/chapter1/Q4.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter1; 2 | 3 | public class Q4 { 4 | public static void main(String[] args) 5 | { 6 | final double artificialSweetener = 0.001; 7 | double mouseWeight = .3; 8 | double killMouse = mouseWeight * 0.1; 9 | double sodaCountForMouse = killMouse / artificialSweetener; 10 | 11 | System.out.println(sodaCountForMouse); 12 | 13 | double humanWeight = 20; 14 | double sodaCountForHuman = humanWeight / mouseWeight * sodaCountForMouse; 15 | 16 | System.out.println(sodaCountForHuman); 17 | 18 | /* Solution 2 19 | // Constant variables related to Mouse (Assuming, variables in kg) 20 | double amountSweetenerToKillMouse = 0.0000013; 21 | final double weightOfMouse = 0.03; 22 | 23 | // Constant variables related to Dieter (Assuming, variables in kg) 24 | double startingWeightDieter = 110.0; 25 | double desiredWeightDieter = 85.0; 26 | 27 | 28 | // Calculating the proportionate fatal amount of soda 29 | double sodaForSafeUse = ((amountSweetenerToKillMouse / weightOfMouse) * desiredWeightDieter) / 0.001; 30 | 31 | // Printing result to the screen 32 | System.out.println("To lose " + (startingWeightDieter - desiredWeightDieter) + " kilograms."); 33 | System.out.println("Safe soda limit to have: " + sodaForSafeUse); 34 | */ 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q4.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | 3 | public class Q4 { 4 | public static void main(String[] args) 5 | { 6 | final double artificialSweetener = 0.001; 7 | double mouseWeight = .3; 8 | double killMouse = mouseWeight * 0.1; 9 | double sodaCountForMouse = killMouse / artificialSweetener; 10 | 11 | System.out.println(sodaCountForMouse); 12 | 13 | double humanWeight = 20; 14 | double sodaCountForHuman = humanWeight / mouseWeight * sodaCountForMouse; 15 | 16 | System.out.println(sodaCountForHuman); 17 | 18 | /* Solution 2 19 | // Constant variables related to Mouse (Assuming, variables in kg) 20 | double amountSweetenerToKillMouse = 0.0000013; 21 | final double weightOfMouse = 0.03; 22 | 23 | // Constant variables related to Dieter (Assuming, variables in kg) 24 | double startingWeightDieter = 110.0; 25 | double desiredWeightDieter = 85.0; 26 | 27 | 28 | // Calculating the proportionate fatal amount of soda 29 | double sodaForSafeUse = ((amountSweetenerToKillMouse / weightOfMouse) * desiredWeightDieter) / 0.001; 30 | 31 | // Printing result to the screen 32 | System.out.println("To lose " + (startingWeightDieter - desiredWeightDieter) + " kilograms."); 33 | System.out.println("Safe soda limit to have: " + sodaForSafeUse); 34 | */ 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/lessons/languagebasics/AndOperators.java: -------------------------------------------------------------------------------- 1 | package lessons.languagebasics; 2 | 3 | public class AndOperators { 4 | public static void main(String[] args) 5 | { 6 | if (getTrue() & getFalse()) // true AND false = false 7 | System.out.println("True"); 8 | 9 | System.out.println("------------"); 10 | 11 | if (getTrue() & getTrue()) // true AND true = true 12 | System.out.println("True"); 13 | 14 | System.out.println("---------------"); 15 | 16 | if (getFalse() & getFalse()) // false AND false = false 17 | System.out.println("True"); 18 | 19 | System.out.println("---------------"); 20 | 21 | if (getFalse() && getTrue()) // false -> false 22 | System.out.println("True"); 23 | 24 | System.out.println("------------"); 25 | 26 | if (getTrue() && getTrue()) // true AND true = true 27 | System.out.println("True"); 28 | 29 | System.out.println("---------------"); 30 | 31 | if (getFalse() && getFalse()) // false -> false 32 | System.out.println("True"); 33 | } 34 | 35 | public static boolean getTrue() 36 | { 37 | //... 38 | System.out.println("getTrue()"); 39 | return true; 40 | } 41 | 42 | public static boolean getFalse() 43 | { 44 | //... 45 | System.out.println("getFalse()"); 46 | return false; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/lessons/consolio/stringintro/CompareTo.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio.stringintro; 2 | 3 | public class CompareTo { 4 | public static void main(String[] args) 5 | { 6 | char ch = 65; 7 | System.out.println(ch); 8 | 9 | // A: 65 || B: 66 || C: 67 ....... Z: 90 10 | // a: 97 || b: 98 || c: 99 ....... z: 122 11 | 12 | // z: 122 > Z: 90 (+) 13 | // b: 98 > A: 65 (+) 14 | // C: 67 > z: 122 (-) 15 | // C: 67 == C: 67 (0) 16 | 17 | String s1 = "A"; // 65 18 | String s2 = "B"; // 66 19 | 20 | System.out.println(s1.compareTo(s2)); // 65 > 66 || 65 - 66 = -1 (returns) 21 | System.out.println(s2.compareTo(s1)); // 66 > 65 || 66 - 65 = 1 22 | 23 | System.out.println("E".compareTo("e")); 24 | // 69 > 101 || (-) 69 - 101 = (-)32; 25 | 26 | System.out.println("E".toLowerCase().compareTo("e".toUpperCase())); 27 | // "E".toLowerCase() // "e" 28 | // "e".compareTo("e".toUpperCase()) 29 | // "e".compareTo("E") || 101 - 69 = +32 30 | 31 | 32 | String s3 = "abc"; 33 | String s4 = "abd"; 34 | 35 | System.out.println(s3.compareTo(s4)); 36 | // "a".compareTo("a") || 0 37 | // "b".compareTo("b") || 0 38 | // "c".compareTo("d") || -1 39 | 40 | 41 | String s5 = " abd"; 42 | String s6 = "abc"; 43 | 44 | System.out.println(s5.trim().compareTo(s6)); // 1 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/homeworks/chapter1/Q1.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter1; 2 | 3 | public class Q1 { 4 | public static void main(String[] args) 5 | { 6 | double weight = 150; // pound 7 | 8 | int running = 10; //METS (Metabolic equivalents) 9 | int basketball = 8; 10 | int sleeping = 1; 11 | 12 | int runningTimeInMinutes = 30; 13 | int basketballTimeInMinutes = 30; 14 | int sleepingTimeInMinutes = 6 * 60; 15 | 16 | // Calories = 0.0175 x MET x Weight in kilograms x Minute(s) 17 | 18 | double totalRunningCalories = 0.0175 * running * runningTimeInMinutes * weight; 19 | double totalBasketCalories = 0.0175 * basketball * basketballTimeInMinutes * weight; 20 | double totalSleepingCalories = 0.0175 * sleeping * sleepingTimeInMinutes * weight; 21 | 22 | System.out.println("Exercise\t\tTime(min)\tCalories(cal)"); 23 | System.out.println("--------------------------------------"); 24 | System.out.printf("Running%11d\t%14.2f%n", runningTimeInMinutes, totalRunningCalories); 25 | System.out.printf("Basketball%8d\t%14.2f%n", basketballTimeInMinutes, totalBasketCalories); 26 | System.out.printf("Sleeping%11d\t%14.2f%n", sleepingTimeInMinutes, totalSleepingCalories); 27 | System.out.println("--------------------------------------"); 28 | System.out.printf("Total Calories: %19.2f%n", totalRunningCalories + totalBasketCalories + totalSleepingCalories); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/lessons/loops/whileloops/WhileExample3.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.whileloops; 2 | 3 | /* 4 | Write a method named countPositiveAndNegatives that takes no argument and that will sum positive and 5 | negative numbers and until entered zero from input. This will display total positives and negatives on the 6 | screen. 7 | */ 8 | 9 | public class WhileExample3 { 10 | public static void main(String[] args) 11 | { 12 | countPositiveAndNegatives(); 13 | } 14 | 15 | public static void countPositiveAndNegatives() 16 | { 17 | java.util.Scanner kb = new java.util.Scanner(System.in); 18 | System.out.println("Enter numbers (EXIT = 0):"); 19 | int val; 20 | 21 | int positive = 0, negative = 0; 22 | 23 | while ((val = kb.nextInt()) != 0) { 24 | if (val > 0) 25 | positive += val; 26 | else 27 | negative += val; 28 | } 29 | 30 | display(positive, negative); 31 | } 32 | 33 | public static void display(int positive, int negative) 34 | { 35 | if (positive > 0) 36 | System.out.printf("Sum of Positive numbers: %d%n", positive); 37 | else 38 | System.out.println("You have not entered any Positive number"); 39 | 40 | if (negative < 0) 41 | System.out.printf("Sum of Negative numbers: %d%n", negative); 42 | else 43 | System.out.println("You have not entered any Negative number"); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/lessons/consolio/ScannerExamples.java: -------------------------------------------------------------------------------- 1 | package lessons.consolio; 2 | 3 | public class ScannerExamples { 4 | public static void main(String[] args) 5 | { 6 | java.util.Scanner input = new java.util.Scanner(System.in); 7 | 8 | System.out.print("Text: "); 9 | String mainText = input.next(); 10 | 11 | System.out.println("(" + mainText.trim() + ")"); 12 | System.out.println("(" + mainText.charAt(4) + ")"); 13 | System.out.println("(" + mainText.trim().substring(0, 5) + ")"); 14 | System.out.println("(" + mainText.toUpperCase() + ")"); 15 | System.out.println("(" + mainText.toLowerCase() + ")"); 16 | 17 | // Immutable Class (String) 18 | String mainText2 = mainText.trim(); 19 | String mainText3 = mainText2.substring(0, 5); 20 | String mainText4 = mainText3.toUpperCase(); 21 | String mainText5 = mainText4.toLowerCase(); 22 | 23 | System.out.println(mainText); 24 | System.out.println(mainText2); 25 | System.out.println(mainText3); 26 | System.out.println(mainText4); 27 | System.out.println(mainText5); 28 | 29 | java.util.Scanner input2 = new java.util.Scanner(System.in); 30 | input2.useDelimiter("-"); 31 | 32 | System.out.println("Enter number1 and number2"); // 123-456- 33 | int number1 = input2.nextInt(); 34 | int number2 = input2.nextInt(); 35 | 36 | System.out.println(number1); // 123 37 | System.out.println(number2); // 456 38 | } 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/intro/Intro.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.intro; 2 | 3 | public class Intro { 4 | public static void main(String[] args) 5 | { 6 | Phone phone1; 7 | phone1 = new Phone(120, 60); 8 | 9 | System.out.println(phone1.height); 10 | System.out.println(phone1.width); 11 | 12 | System.out.println("-----------------------"); 13 | 14 | Phone phone2 = new Phone(); 15 | 16 | System.out.println(phone2.height); 17 | System.out.println(phone2.width); 18 | 19 | System.out.println("-----------------------"); 20 | 21 | Cup cup1 = new Cup(100, "Red", true); 22 | Cup cup2 = new Cup(150, "Black", false); 23 | 24 | Cup cup3 = new Cup(); 25 | cup3.volume = 250; 26 | cup3.color = "Orange"; 27 | cup3.hasSticker = true; 28 | } 29 | } 30 | 31 | class Phone { 32 | public int height; 33 | public int width; 34 | 35 | Phone(int newHeight, int newWidth) { 36 | height = newHeight; 37 | width = newWidth; 38 | } 39 | 40 | Phone() 41 | {} 42 | } 43 | 44 | class Cup { 45 | public double volume; 46 | public String color; 47 | public boolean hasSticker; 48 | 49 | public Cup(double newVolume, String newColor, boolean newHasSticker) 50 | { 51 | volume = newVolume; 52 | color = newColor; 53 | hasSticker = newHasSticker; 54 | 55 | System.out.println("Instance is ready - CTOR"); 56 | } 57 | 58 | public Cup() 59 | { 60 | } 61 | } -------------------------------------------------------------------------------- /src/lessons/loops/whileloops/WhileExample4.java: -------------------------------------------------------------------------------- 1 | package lessons.loops.whileloops; 2 | /* 3 | Write a method named countPositiveAndNegatives that takes no argument and that will count positive, 4 | negative numbers and until entered zero from input. This will display total count of positives and negatives on the 5 | screen. 6 | */ 7 | public class WhileExample4 { 8 | public static void main(String[] args) 9 | { 10 | countPositiveAndNegatives(); 11 | } 12 | 13 | public static void countPositiveAndNegatives() { 14 | java.util.Scanner kb = new java.util.Scanner(System.in); 15 | 16 | System.out.println("Sayilari girmeye basla !!"); 17 | 18 | int posSum = 0, negSum = 0; 19 | int posCount = 0, negCount = 0; 20 | int val; 21 | 22 | while ((val = kb.nextInt()) != 0) { 23 | if (val > 0) { 24 | posSum += val; 25 | ++posCount; 26 | } else { 27 | negSum += val; 28 | ++negCount; 29 | } 30 | } 31 | 32 | display(posSum, posCount, negSum, negCount); 33 | } 34 | 35 | public static void display(int posSum, int posCount, int negSum, int negCount) 36 | { 37 | if (posCount > 0) 38 | System.out.printf("%d numbers of positive sum:%d%n", posCount, posSum); 39 | else 40 | System.out.println("You have not entered any Positive number"); 41 | 42 | if (negCount > 0) 43 | System.out.printf("%d numbers of negative sum:%d%n", negCount, negSum); 44 | else 45 | System.out.println("You have not entered any Negative number"); 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/examples/EquationSolver.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.examples; 2 | 3 | public class EquationSolver { 4 | public static void main(String[] args) 5 | { 6 | EquationSolverApp.run(); 7 | } 8 | } 9 | 10 | class EquationSolverApp { 11 | public static void run() 12 | { 13 | java.util.Scanner kb = new java.util.Scanner(System.in); 14 | 15 | System.out.print("a:"); 16 | int a = kb.nextInt(); 17 | 18 | System.out.print("b:"); 19 | int b = kb.nextInt(); 20 | 21 | System.out.print("c:"); 22 | int c = kb.nextInt(); 23 | 24 | NumberUtil.printRoots(a, b, c); 25 | } 26 | } 27 | 28 | class NumberUtil { 29 | public static void calculateForDifferentRoots(double delta, int a, int b) 30 | { 31 | double x1, x2; 32 | double sqrtDelta; 33 | 34 | sqrtDelta = Math.sqrt(delta); 35 | 36 | x1 = (-b - sqrtDelta) / 2 * a; 37 | x2 = (-b + sqrtDelta) / 2 * a; 38 | 39 | System.out.printf("x1 = %f, x2 = %f", x1, x2); 40 | } 41 | 42 | public static void calculateForSameRoots(int a, int b) 43 | { 44 | double x; 45 | 46 | x = -b / (2. * a); 47 | 48 | System.out.printf("x = %f", x); 49 | } 50 | 51 | public static double getDelta(int a, int b, int c) 52 | { 53 | return Math.pow(b, 2) - 4 * a * c; 54 | } 55 | 56 | public static void printRoots(int a, int b, int c) 57 | { 58 | double delta = getDelta(a, b, c); 59 | 60 | if (delta > 0) 61 | calculateForDifferentRoots(delta, a, b); 62 | else if (delta == 0) 63 | calculateForSameRoots(a, b); 64 | else 65 | System.out.println("No real root"); 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/homeworks/chapter3/Q3.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3; 2 | 3 | public class Q3 { 4 | public static void main(String[] args) 5 | { 6 | calculateNewton(); 7 | } 8 | 9 | public static void calculateNewton() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | while (true) { 14 | 15 | if (makeChoice() == 0) 16 | break; 17 | 18 | System.out.print("Please enter the car kilogram : "); 19 | double carKilogram = kb.nextInt(); 20 | 21 | displayByChoice(makeChoice(), carKilogram); 22 | } 23 | } 24 | 25 | 26 | public static int makeChoice() 27 | { 28 | java.util.Scanner kb = new java.util.Scanner(System.in); 29 | 30 | System.out.println("\n\" 0 \" --> Exit"); 31 | System.out.println("\" 1 \" --> Calculate newton on earth."); 32 | System.out.println("\" 2 \" --> Calculate newton on moon.\n"); 33 | 34 | System.out.print("Please choose an option : "); 35 | 36 | return kb.nextInt(); 37 | } 38 | 39 | public static double calculateNewtonOnEarth(double carKilogram) 40 | { 41 | return carKilogram * 9.81; 42 | } 43 | 44 | public static double calculateNewtonOnMoon(double carKilogram) 45 | { 46 | return calculateNewtonOnEarth(carKilogram) / 6; 47 | } 48 | 49 | public static void displayByChoice(int choice, double carKilogram) 50 | { 51 | if (choice == 1) { 52 | System.out.printf("%.2f kg car on earth is %.2f Newton.%n", carKilogram, calculateNewtonOnEarth(carKilogram)); 53 | } 54 | else if ( choice == 2) { 55 | System.out.printf("%.2f kg car on moon is %.2f Newton.%n", carKilogram, calculateNewtonOnMoon(carKilogram)); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/homeworks/chapter3/chapter3/Q3.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3.chapter3; 2 | 3 | public class Q3 { 4 | public static void main(String[] args) 5 | { 6 | calculateNewton(); 7 | } 8 | 9 | public static void calculateNewton() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | while (true) { 14 | 15 | if (makeChoice() == 0) 16 | break; 17 | 18 | System.out.print("Please enter the car kilogram : "); 19 | double carKilogram = kb.nextInt(); 20 | 21 | displayByChoice(makeChoice(), carKilogram); 22 | } 23 | } 24 | 25 | 26 | public static int makeChoice() 27 | { 28 | java.util.Scanner kb = new java.util.Scanner(System.in); 29 | 30 | System.out.println("\n\" 0 \" --> Exit"); 31 | System.out.println("\" 1 \" --> Calculate newton on earth."); 32 | System.out.println("\" 2 \" --> Calculate newton on moon.\n"); 33 | 34 | System.out.print("Please choose an option : "); 35 | 36 | return kb.nextInt(); 37 | } 38 | 39 | public static double calculateNewtonOnEarth(double carKilogram) 40 | { 41 | return carKilogram * 9.81; 42 | } 43 | 44 | public static double calculateNewtonOnMoon(double carKilogram) 45 | { 46 | return calculateNewtonOnEarth(carKilogram) / 6; 47 | } 48 | 49 | public static void displayByChoice(int choice, double carKilogram) 50 | { 51 | if (choice == 1) { 52 | System.out.printf("%.2f kg car on earth is %.2f Newton.%n", carKilogram, calculateNewtonOnEarth(carKilogram)); 53 | } 54 | else if ( choice == 2) { 55 | System.out.printf("%.2f kg car on moon is %.2f Newton.%n", carKilogram, calculateNewtonOnMoon(carKilogram)); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/lessons/arrays/lottery/NumericLotteryShop.java: -------------------------------------------------------------------------------- 1 | package lessons.arrays.lottery; 2 | 3 | import lessons.arrays.examples.ArrayMethods; 4 | 5 | import java.util.Random; 6 | import java.util.Scanner; 7 | 8 | public class NumericLotteryShop { 9 | public static Random random = new Random(); 10 | public static Scanner kb = new Scanner(System.in); 11 | public static int costOfRow = 10; 12 | 13 | public static int[] generateNumericLottery() 14 | { 15 | return new NumericLottery().getNums(random); 16 | } 17 | 18 | public static void getInfoFromCustomer(Customer customer) 19 | { 20 | System.out.println("Clerk: How old are you ?"); 21 | System.out.printf("Customer: %d%n", customer.age); 22 | 23 | if (customer.age < 19) { 24 | System.out.println("You are not allowed to play"); 25 | return; 26 | } 27 | 28 | System.out.print("Clerk: How many row do you want ?"); 29 | int rowCount = Integer.parseInt(kb.nextLine()); 30 | System.out.printf("Customer: %d%n", rowCount); 31 | 32 | int price = rowCount * costOfRow; 33 | 34 | System.out.printf("Clerk: %d $ please%n", price); 35 | 36 | if (!customer.isSufficientBalance(price)) { 37 | return; 38 | } 39 | 40 | customer.pay(price); 41 | 42 | System.out.println("----------------Lottery Machine Sound----------------"); 43 | System.out.println("-----------------------------------------------------"); 44 | 45 | for (int i = 0; i < rowCount; ++i) { 46 | ArrayMethods.displayArray(generateNumericLottery()); 47 | } 48 | 49 | System.out.println("-----------------------------------------------------"); 50 | 51 | } 52 | 53 | public static void run() 54 | { 55 | getInfoFromCustomer(new Customer(1000, 22)); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/homeworks/chapter1/Q2.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter1; 2 | 3 | public class Q2 { 4 | public static void main(String[] args) 5 | { 6 | for (int i = 0; i < 1000; ++i) { 7 | System.out.printf("Coupon = %d || ", i); 8 | couponStore(i); 9 | System.out.println("---------------------------------------"); 10 | } 11 | 12 | } 13 | 14 | public static void couponStore(int coupons) 15 | { 16 | int candyCost = 10; 17 | int gumballCost = 3; 18 | int candy; 19 | int gumball; 20 | 21 | if (coupons % candyCost == 0) { 22 | System.out.printf("%d = %d Candy%n", coupons, coupons / candyCost); 23 | return; 24 | } 25 | 26 | if (coupons % gumballCost == 0) { 27 | System.out.printf("Gumball = %d || Coupon remaining: %d%n", coupons / gumballCost, coupons % gumballCost); 28 | return; 29 | } 30 | 31 | candy = coupons / candyCost; 32 | System.out.printf("Candy: %d || ", candy); 33 | coupons %= candyCost; 34 | 35 | gumball = coupons / gumballCost; 36 | System.out.printf("Gumball = %d || ", gumball); 37 | coupons %= gumballCost; 38 | 39 | System.out.printf("Coupon remaining: %d%n", coupons); 40 | 41 | 42 | if (candy >= 1 && coupons == 2) { 43 | --candy; 44 | gumball += 4; 45 | coupons -= 2; 46 | 47 | System.out.println("============ OR ============"); 48 | System.out.printf("Candy: %d || Gumball = %d || Coupon remaining: %d%n", candy, gumball, coupons); 49 | return; 50 | } 51 | 52 | if (candy >= 2 && coupons == 1) { 53 | candy -= 2; 54 | gumball += 7; 55 | --coupons; 56 | System.out.println("============ OR ============"); 57 | System.out.printf("Candy: %d || Gumball = %d || Coupon remaining: %d%n", candy, gumball, coupons); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/examples/point/Point.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.examples.point; 2 | 3 | public class Point { 4 | public int x; 5 | public int y; 6 | 7 | public double distance(Point point) 8 | { 9 | return Math.sqrt((Math.pow(x - point.x, 2) + Math.pow(y - point.y, 2))); 10 | } 11 | 12 | public static double distanceWithTwoPoint(Point p1, Point p2) 13 | { 14 | return p1.distance(p2); 15 | } 16 | } 17 | 18 | class PointFactory { 19 | public static Point createPoint(int x, int y) 20 | { 21 | Point p = new Point(); 22 | 23 | p.x = x; 24 | p.y = y; 25 | 26 | return p; 27 | } 28 | 29 | public static Point createPoint(java.util.Scanner kb) 30 | { 31 | System.out.print("x:"); 32 | int x = kb.nextInt(); 33 | 34 | System.out.print("y:"); 35 | int y = kb.nextInt(); 36 | 37 | return createPoint(x, y); 38 | } 39 | 40 | public static Point createPoint(java.util.Random random) 41 | { 42 | System.out.print("x:"); 43 | int x = random.nextInt(10); 44 | 45 | System.out.print("y:"); 46 | int y = random.nextInt(10); 47 | 48 | return createPoint(x, y); 49 | } 50 | } 51 | 52 | class Test { 53 | public static void main(String[] args) 54 | { 55 | Point p1 = PointFactory.createPoint(new java.util.Scanner(System.in)); 56 | Point p2 = PointFactory.createPoint(new java.util.Scanner(System.in)); 57 | 58 | System.out.println(p1.distance(p2)); 59 | System.out.println(p1.distance(p1)); 60 | 61 | System.out.println(Point.distanceWithTwoPoint(p1, p2)); 62 | System.out.println(Point.distanceWithTwoPoint(p2, p2)); 63 | 64 | Point p3 = PointFactory.createPoint(new java.util.Random()); 65 | Point p4 = PointFactory.createPoint(new java.util.Random()); 66 | 67 | System.out.println(p3.distance(p4)); 68 | System.out.println(Point.distanceWithTwoPoint(p3, p4)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/switchcase/SwitchExample2.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.switchcase; 2 | 3 | public class SwitchExample2 { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void run() 10 | { 11 | java.util.Scanner kb = new java.util.Scanner(System.in); 12 | 13 | for (;;) { 14 | System.out.print("day:"); 15 | int day = kb.nextInt(); 16 | 17 | System.out.print("month:"); 18 | int month = kb.nextInt(); 19 | 20 | System.out.print("year:"); 21 | int year = kb.nextInt(); 22 | 23 | if (day == 0) 24 | break; 25 | 26 | System.out.println("------------------------"); 27 | 28 | if (isValidDate(day, month, year)) 29 | System.out.printf("%02d/%02d/%d is Valid Date%n", day, month, year); 30 | else 31 | System.out.println("invalid date"); 32 | 33 | System.out.println("------------------------"); 34 | 35 | } 36 | } 37 | 38 | public static boolean isValidDate(int day, int month, int year) 39 | { 40 | return 1 <= day && day <= 31 && 1 <= month && month <= 12 && day <= getDays(month, year); 41 | } 42 | 43 | public static int getDays(int month, int year) 44 | { 45 | int day = 31; 46 | 47 | switch (month) { 48 | case 4: 49 | case 6: 50 | case 9: 51 | case 11: 52 | day = 30; 53 | break; 54 | case 2: 55 | day = 28; 56 | if (isLeapYear(year)) 57 | ++day; 58 | } 59 | 60 | /* 61 | 62 | switch (month) { 63 | case 4, 6, 9, 11 -> day = 30; 64 | case 2 -> { 65 | day = 28; 66 | if (isLeapYear(year)) 67 | ++day; 68 | } 69 | } 70 | 71 | */ 72 | return day; 73 | } 74 | 75 | public static boolean isLeapYear(int year) 76 | { 77 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/homeworks/chapter3/Q6.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3; 2 | /* 3 | The Fibonacci numbers Fn are defined as follows: F0 is 1, F1 is 1, and 4 | 5 | Fi+2 = Fi + Fi+1 6 | 7 | i = 0, 1, 2, . . . . In other words, each number is the sum of the previous two numbers. The first few Fibonacci numbers are 1, 1, 2, 3, 5, and 8. One place where these 8 | numbers occur is as certain population growth rates. If a population has no deaths, 9 | then the series shows the size of the population after each time period. It takes an 10 | organism two time periods to mature to reproducing age, and then the organism 11 | reproduces once every time period. The formula applies most straightforwardly to 12 | asexual reproduction at a rate of one offspring per time period. In any event, the 13 | green crud population grows at this rate and has a time period of five days. Hence, if 14 | a green crud population starts out as 10 pounds of crud, then in 5 days, there is still 15 | 10 pounds of crud; in 10 days, there is 20 pounds of crud; in 15 days, 30 pounds; 16 | in 20 days, 50 pounds; and so forth. Write a program that takes both the initial size 17 | of a green crud population (in pounds) and a number of days as input and outputs 18 | the number of pounds of green crud after that many days. Assume that the population size is the same for four days and then increases every fifth day. Your program 19 | should allow the user to repeat this calculation as often as desired. 20 | */ 21 | public class Q6 { 22 | public static void main(String[] args) 23 | { 24 | displayAndRun(); 25 | } 26 | 27 | public static void displayAndRun() 28 | { 29 | java.util.Scanner scanner = new java.util.Scanner(System.in); 30 | int day = 0; 31 | 32 | while (true) { 33 | day += 5; 34 | System.out.println(poundCalculator(day)); 35 | 36 | System.out.print("Press 0 to exit: "); 37 | int choice = scanner.nextInt(); 38 | 39 | if(choice == 0) 40 | break; 41 | } 42 | } 43 | 44 | public static int poundCalculator(int day) 45 | { 46 | int a = 0; 47 | int b = 10; 48 | 49 | for(int i = 0; i < day / 5; i++) { 50 | int temp; 51 | temp = a; 52 | a = b; 53 | b += temp; 54 | } 55 | return a; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/homeworks/chapter3/Q7.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3; 2 | /* 3 | An Armstrong number is an n-digit number that equals the sum of the nth power 4 | of its digits. For example 153 is a three-digit number where the sum of the cubes 5 | of the individual digits (13 + 53 + 33) equals the number itself (153). 6 | 7 | Write a program that takes as input the start and end numbers of an Armstrong 8 | number range to be printed. Your program should prompt for new start and end 9 | numbers until the user decides that she or he is through. Use variables of the type 10 | integer to store the start and end numbers of the range. 11 | */ 12 | public class Q7 { 13 | public static void main(String[] args) 14 | { 15 | displayAndRun(); 16 | } 17 | 18 | public static void displayAndRun() 19 | { 20 | java.util.Scanner scanner = new java.util.Scanner(System.in); 21 | 22 | while(true) { 23 | 24 | System.out.print("First number: "); 25 | int firstNumber = scanner.nextInt(); 26 | 27 | System.out.print("Last number: "); 28 | int lastNumber = scanner.nextInt(); 29 | 30 | for(int i = firstNumber; i < lastNumber; i++) 31 | if(isArmStrong(i)) 32 | System.out.printf("%d is an armstrong number.%n", i); 33 | 34 | System.out.print("Want to exit press 0"); 35 | int exitNumber = scanner.nextInt(); 36 | if(exitNumber == 0) 37 | break; 38 | } 39 | } 40 | 41 | public static boolean isArmStrong(int a) 42 | { 43 | return a == findArmstrong(a); 44 | } 45 | 46 | public static int findArmstrong(int a) 47 | { 48 | int sum; 49 | int digit = countDigits(a); 50 | 51 | for(sum = 0; a != 0; a /= 10) { 52 | sum += pow(a % 10, digit); 53 | } 54 | 55 | return sum; 56 | } 57 | 58 | public static int pow(int a, int digit) 59 | { 60 | int pow; 61 | int temp = digit; 62 | 63 | for(pow = 1; temp != 0; temp--) { 64 | pow *= a; 65 | } 66 | 67 | return pow; 68 | 69 | } 70 | 71 | public static int countDigits(int a) 72 | { 73 | 74 | int count; 75 | 76 | for(count = 0; a > 0; a /= 10) { 77 | count++; 78 | } 79 | 80 | return count; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/lessons/flowcontrol/switchcase/MenuApp.java: -------------------------------------------------------------------------------- 1 | package lessons.flowcontrol.switchcase; 2 | 3 | public class MenuApp { 4 | public static void main(String[] args) 5 | { 6 | run(); 7 | } 8 | 9 | public static void displayMenu() 10 | { 11 | System.out.println("1- Mercimek"); 12 | System.out.println("2- Kuru Fasulye"); 13 | System.out.println("3- Beyran"); 14 | System.out.println("4- Cacik"); 15 | System.out.println("5- Istemiyorum"); 16 | } 17 | 18 | public static void doWorkForMercimek() 19 | { 20 | System.out.println("--------------------------"); 21 | System.out.println("Mercimek is loading"); 22 | System.out.println("--------------------------"); 23 | } 24 | 25 | public static void doWorkForKuru() 26 | { 27 | System.out.println("--------------------------"); 28 | System.out.println("Kuru is loading"); 29 | System.out.println("--------------------------"); 30 | } 31 | 32 | public static void doWorkForBeyran() 33 | { 34 | System.out.println("--------------------------"); 35 | System.out.println("Beyran is loading"); 36 | System.out.println("--------------------------"); 37 | } 38 | 39 | public static void doWorkForCacik() 40 | { 41 | System.out.println("--------------------------"); 42 | System.out.println("Cacik is loading"); 43 | System.out.println("--------------------------"); 44 | } 45 | 46 | public static void doWorkFor(int option) 47 | { 48 | if (option == 1) 49 | doWorkForMercimek(); 50 | else if (option == 2) 51 | doWorkForKuru(); 52 | else if (option == 3) 53 | doWorkForBeyran(); 54 | else 55 | doWorkForCacik(); 56 | } 57 | 58 | public static void run() 59 | { 60 | java.util.Scanner kb = new java.util.Scanner(System.in); 61 | 62 | while (true) { 63 | displayMenu(); 64 | 65 | int option = kb.nextInt(); 66 | 67 | if (option < 1 || option > 5) { 68 | System.out.println("Invalid option"); 69 | continue; 70 | } 71 | 72 | if (option == 5) 73 | break; 74 | 75 | doWorkFor(option); 76 | } 77 | 78 | System.out.println("Tekrardan bekleriz. Bizi tercih ettiginiz icin tesekkur ederiz"); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/homeworks/chapter2/Q2.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter2; 2 | 3 | /* 4 | The video game machines at your local arcade output coupons according to 5 | how well you play the game. You can redeem 10 coupons for a candy bar or 3 6 | coupons for a gumball. You prefer candy bars to gumballs. Write a program that 7 | defines a variable initially assigned to the number of coupons you win. Next, 8 | the program should output how many candy bars and gumballs you can get if 9 | you spend all of your coupons on candy bars first, and any remaining coupons 10 | on gumballs. 11 | */ 12 | 13 | public class Q2 { 14 | public static void main(String[] args) 15 | { 16 | java.util.Scanner kb = new java.util.Scanner(System.in); 17 | 18 | System.out.print("Coupon count:"); 19 | int couponCount = Integer.parseInt(kb.nextLine()); 20 | 21 | couponStore(couponCount); 22 | } 23 | 24 | public static void couponStore(int coupons) 25 | { 26 | int candyCost = 10; 27 | int gumballCost = 3; 28 | int candy; 29 | int gumball; 30 | 31 | if (coupons % candyCost == 0) { 32 | System.out.printf("%d = %d Candy%n", coupons, coupons / candyCost); 33 | return; 34 | } 35 | 36 | if (coupons % gumballCost == 0) { 37 | System.out.printf("Gumball = %d || Coupon remaining: %d%n", coupons / gumballCost, coupons % gumballCost); 38 | return; 39 | } 40 | 41 | candy = coupons / candyCost; 42 | System.out.printf("Candy: %d || ", candy); 43 | coupons %= candyCost; 44 | 45 | gumball = coupons / gumballCost; 46 | System.out.printf("Gumball = %d || ", gumball); 47 | coupons %= gumballCost; 48 | 49 | System.out.printf("Coupon remaining: %d%n", coupons); 50 | 51 | 52 | if (candy >= 1 && coupons == 2) { 53 | --candy; 54 | gumball += 4; 55 | coupons -= 2; 56 | 57 | System.out.println("============ OR ============"); 58 | System.out.printf("Candy: %d || Gumball = %d || Coupon remaining: %d%n", candy, gumball, coupons); 59 | return; 60 | } 61 | 62 | if (candy >= 2 && coupons == 1) { 63 | candy -= 2; 64 | gumball += 7; 65 | --coupons; 66 | System.out.println("============ OR ============"); 67 | System.out.printf("Candy: %d || Gumball = %d || Coupon remaining: %d%n", candy, gumball, coupons); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/lessons/languagebasics/OperatorsExample.java: -------------------------------------------------------------------------------- 1 | package lessons.languagebasics; 2 | 3 | public class OperatorsExample { 4 | public static void main(String[] args) 5 | { 6 | int a = 10; 7 | int b; 8 | 9 | // ++a; // a = a + 1; 10 | // System.out.println(a); 11 | 12 | // b = a++; 13 | // b = ++a; 14 | 15 | b = 5 + a++; 16 | 17 | // System.out.println(b); 18 | // System.out.println(a); 19 | 20 | int x = 10; 21 | int y = 20; 22 | int z; 23 | 24 | z = ++x - y++ + 12; 25 | // z = 11 - 20 + 12; 26 | // z = 3; 27 | // y = y + 1; 28 | 29 | System.out.println(z); // 3 30 | System.out.println(x); // 11 31 | System.out.println(y); // 21 32 | 33 | // int x1 = 10; 34 | // int x2 = 1 - --x1; // x2 = -8; // x1 = 9; 35 | // System.out.println(x1); // 9 36 | // System.out.println(x2); // -8 37 | 38 | System.out.println("----------------"); 39 | 40 | int x2 = 10; 41 | int y2 = 9; 42 | int z2 = 6; 43 | 44 | int q = x2++ - --y2 + z2++ - 15; // q = 10 - 8 + 6 - 15 // q = -7 45 | System.out.println(x2); // 11 46 | System.out.println(y2); // 8 47 | System.out.println(z2); // 7 48 | System.out.println(q); // -7 49 | 50 | 51 | int total = 10 * 6 / 3 + 11 - 21; 52 | // 1 - total = 60 / 3 + 11 - 21; 53 | // 2 - total = 20 + 11 - 21; 54 | // 3 - total = 31 - 21; 55 | // 4 - total = 10; 56 | 57 | int total2 = 10 * 6 / (3 + 11) - 21; 58 | // 1 - total = 10 * 6 / 14 - 21; 59 | // 2 - total = 60 / 14 - 21; 60 | // 3 - total = 4 - 21; 61 | // 4 - total = -17; 62 | 63 | int k = 5; 64 | int l = 8; 65 | int m = -13; 66 | 67 | int total3 = k++ * 12 + --l - 15 / m-- * 3; 68 | // 1 - total = 5 * 12 + 7 - 15 / -13 * 3; 69 | // 2 - total = 60 + 7 - 15 / -13 * 3; 70 | // 3 - total = 60 + 7 - -1 * 3; 71 | // 4 - total = 60 + 7 - -3; 72 | // 5 - total = 67 - -3; 73 | // 5 - total = 70; 74 | 75 | System.out.println(k); // 6 76 | System.out.println(l); // 7 77 | System.out.println(m); // -14 78 | System.out.println(total3); // 70 79 | 80 | System.out.println("---------------------"); 81 | int q1 = 10; 82 | q1 = q1++; // Brain f*ck 83 | 84 | System.out.println(q1); // 10 85 | 86 | q1 = ++q1; // q1 = (q1 + 1;); 87 | 88 | System.out.println(q1); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/lessons/oop/stringexamples/StringMethodTest.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.stringexamples; 2 | 3 | /* 4 | - '\t' Horizontal tabulation 5 | 6 | - '\n' Line feed 7 | 8 | - '\f form feed 9 | 10 | - '\r' carriage return 11 | 12 | byte b = Byte.parseByte("127"); 13 | short s = Short.parseShort("320"); 14 | int i = Integer.parseInt("66000"); 15 | long l = Long.parseLong("200000000000"); 16 | 17 | float f = Float.parseFloat("3.2f"); 18 | double d = Double.parseDouble("5.5"); 19 | 20 | boolean bool = Boolean.parseBoolean("false"); 21 | */ 22 | 23 | import java.util.Random; 24 | 25 | public class StringMethodTest { 26 | public static void main(String[] args) 27 | { 28 | // CapitalizeTest.run(); 29 | // RemoveAllWhitespacesTest.run(); 30 | // IsBlankTest.run(); 31 | // LeftRightPadTest.run(); 32 | // PangramTest.run(); 33 | RandomTextTest.run(); 34 | } 35 | } 36 | 37 | class CapitalizeTest { 38 | public static void run() 39 | { 40 | System.out.println(StringMethods.capitalize(")=__09mER?)()_hAba")); // 41 | } 42 | } 43 | 44 | class RemoveAllWhitespacesTest { 45 | public static void run() 46 | { 47 | System.out.println("(" + StringMethods.removeAllWhiteSpaces(" Efficient House ") + ")"); 48 | } 49 | } 50 | 51 | class IsBlankTest { 52 | public static void run() 53 | { 54 | String str = " "; 55 | System.out.printf("(%s) is Empty ? -> %b%n",str, str.isEmpty()); 56 | System.out.printf("(%s) is Blank or Empty ? -> %b%n",str, str.isBlank()); 57 | } 58 | } 59 | 60 | class LeftRightPadTest { 61 | public static void run() 62 | { 63 | String str = "Efficient"; 64 | System.out.printf("(%s)%n", StringMethods.leftPad(str, 12)); 65 | System.out.printf("(%s)\n", StringMethods.leftPad(str, 12, '*')); 66 | System.out.printf("(%s)%n", StringMethods.rightPad(str, 12)); 67 | System.out.printf("(%s)\n", StringMethods.rightPad(str, 12, '*')); 68 | } 69 | } 70 | 71 | class PangramTest { 72 | public static void run() { 73 | System.out.println(StringMethods.isPangramTR("Açgözlü ve hoşnut, prestije feci bağımlıydık.")); 74 | System.out.println(StringMethods.isPangramEN("the quick brown fox jumps over a lazy dog")); 75 | } 76 | } 77 | 78 | class RandomTextTest { 79 | public static void run() 80 | { 81 | java.util.Random r = new java.util.Random(); 82 | r.setSeed(13); 83 | 84 | System.out.println(StringMethods.getRandomTextTR(r, 10)); 85 | } 86 | } -------------------------------------------------------------------------------- /src/homeworks/chapter3/Q5.java: -------------------------------------------------------------------------------- 1 | package homeworks.chapter3; 2 | /* 3 | You have just purchased a stereo system that cost $1,000 on the following credit 4 | plan: no down payment, an interest rate of 18% per year (and hence 1.5% per 5 | month), and monthly payments of $50. The monthly payment of $50 is used to 6 | pay the interest, and whatever is left is used to pay part of the remaining debt. 7 | Hence, the first month you pay 1.5% of $1,000 in interest. That is $15 in interest. 8 | So, the remaining $35 is deducted from your debt, which leaves you with a debt of 9 | $965.00. The next month, you pay interest of 1.5% of $965.00, which is $14.48. 10 | Hence, you can deduct $35.52 (which is $50 – $14.48) from the amount you owe. 11 | Write a program that tells you how many months it will take you to pay off the 12 | loan, as well as the total amount of interest paid over the life of the loan. Use a loop 13 | to calculate the amount of interest and the size of the debt after each month. (Your 14 | final program need not output the monthly amount of interest paid and remaining 15 | debt, but you may want to write a preliminary version of the program that does output these values.) 16 | Use a variable to count the number of loop iterations and hence, 17 | the number of months until the debt is zero. You may want to use other variables as 18 | well. The last payment may be less than $50 if the debt is small, but do not forget 19 | the interest. If you owe $50, your monthly payment of $50 will not pay off your 20 | debt, although it will come close. One month’s interest on $50 is only 75 cents. 21 | */ 22 | public class Q5 { 23 | public static void main(String[] args) 24 | { 25 | displayAndRun(); 26 | } 27 | 28 | public static void displayAndRun() 29 | { 30 | java.util.Scanner scanner = new java.util.Scanner(System.in); 31 | 32 | System.out.print("Cost of item: "); 33 | double item = scanner.nextDouble(); 34 | 35 | interest(item); 36 | } 37 | 38 | public static void interest(double val) 39 | { 40 | double interest = 0.015; 41 | int months = 0; 42 | double monthlyPayment = 50; 43 | double sumOfInterests = 0; 44 | 45 | while (val > 0) { 46 | if (val < 49) 47 | monthlyPayment = val + (val * interest); 48 | 49 | sumOfInterests += val * interest; 50 | val = val - (monthlyPayment - (val * interest)); 51 | 52 | months++; 53 | } 54 | 55 | System.out.printf("Sum Of Interests: %.2f%n", sumOfInterests); 56 | System.out.println("Total months: " + months); 57 | 58 | } 59 | } -------------------------------------------------------------------------------- /src/lessons/oop/stringexamples/BasicCharacterTest.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.stringexamples; 2 | 3 | import lessons.oop.BasicCharacter; 4 | 5 | public class BasicCharacterTest { 6 | public static void main(String[] args) 7 | { 8 | // String text = " e 25 f 09 f i c i 2 e 0 n t 2 h o 2 u s e "; 9 | 10 | String str = "M1e1... rh1a1/*b..1a= 4 be nim= ad2313im= ***Er12563e n=/11*2 b4e6n=*Ef f i2*46c i e5n*8t= Ho-14 5*us33e= u*35y3*e4231*si34*yi*m="; 11 | String letters = ""; 12 | String numbers = ""; 13 | int spaceCountInText = 0; 14 | String encryptMessage = ""; 15 | 16 | for (int i = 0; i < str.length(); ++i) { 17 | BasicCharacter character = BasicCharacter.valueOf(str.charAt(i)); 18 | 19 | if (character.isDigit()) 20 | numbers += str.charAt(i); 21 | 22 | if (character.isLetter()) 23 | letters += str.charAt(i); 24 | 25 | if (character.isWhiteSpace()) 26 | ++spaceCountInText; 27 | } 28 | 29 | for (int i = 0; i < str.length(); ++i) 30 | { 31 | BasicCharacter character = BasicCharacter.valueOf(str.charAt(i)); 32 | 33 | if (character.isLetter()) 34 | encryptMessage += str.charAt(i); 35 | 36 | if (str.charAt(i) == '=') 37 | encryptMessage += " "; 38 | } 39 | 40 | 41 | String result = ""; 42 | for (int i = 0; i < letters.length(); ++i) { 43 | BasicCharacter character = BasicCharacter.valueOf(letters.charAt(i)); 44 | 45 | result += i % 2 == 0 ? character.toLowerCase() : character.toUpperCase(); 46 | } 47 | 48 | System.out.printf("Numbers: %s%n", numbers); 49 | System.out.printf("Letters: %s%n", letters); 50 | System.out.printf("Space Count: %d%n", spaceCountInText); 51 | System.out.printf("Letters (lower case): %s%n", result); 52 | System.out.printf("sifre: %s%n", encryptMessage); 53 | 54 | /* 55 | System.out.println((byte)0xFFFF); // - 1 56 | System.out.println((short)0xFFFF); // - 1 57 | System.out.println((int)((char)0xFFFF)); // 65535 58 | System.out.println(0xFFFF); // 65535 59 | System.out.printf("%.32f%n", (float)0xFFFF); // IEEE754 60 | System.out.printf("%.32f%n", (double)0xFFFF); // IEEE754 61 | 62 | long l = 0b10100100; 63 | System.out.println(l); 64 | int i = (int)l; 65 | System.out.println(i); 66 | short s = (short)i; 67 | System.out.println(s); 68 | byte b = (byte)s; 69 | System.out.println(b); 70 | */ 71 | 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /src/lessons/methods/methodoverloading/Example1.java: -------------------------------------------------------------------------------- 1 | package lessons.methods.methodoverloading; 2 | 3 | import java.util.Random; 4 | 5 | public class Example1 { 6 | public static void main(String[] args) 7 | { 8 | java.util.Random random = new Random(); 9 | 10 | AnimalFactory animalFactory = new AnimalFactory(); 11 | 12 | Human human = animalFactory.createHuman(random, "Numan"); 13 | Dog dog = animalFactory.createDog(random, "Findik"); 14 | Cat cat = animalFactory.createCat(random, "Duman"); 15 | 16 | DefineAndDisplay.displayProperties(human); 17 | DefineAndDisplay.makeNoise(human); 18 | 19 | DefineAndDisplay.displayProperties(dog); 20 | DefineAndDisplay.makeNoise(dog); 21 | 22 | DefineAndDisplay.displayProperties(cat); 23 | DefineAndDisplay.makeNoise(cat); 24 | } 25 | } 26 | 27 | class AnimalFactory { 28 | 29 | public Human createHuman(java.util.Random random, String name) 30 | { 31 | Human human = new Human(); 32 | 33 | human.name = name; 34 | human.height = random.nextInt(50) + 150; 35 | 36 | return human; 37 | } 38 | 39 | public Dog createDog(java.util.Random random, String name) 40 | { 41 | Dog dog = new Dog(); 42 | 43 | dog.name = name; 44 | dog.age = random.nextInt(13) + 1; 45 | 46 | return dog; 47 | } 48 | 49 | public Cat createCat(java.util.Random random, String name) 50 | { 51 | Cat cat = new Cat(); 52 | 53 | cat.name = name; 54 | cat.isEyesColored = random.nextBoolean(); 55 | 56 | return cat; 57 | } 58 | } 59 | 60 | class DefineAndDisplay { 61 | 62 | public static void makeNoise(Human human) 63 | { 64 | System.out.println(human.makeNoise()); 65 | } 66 | 67 | public static void makeNoise(Dog dog) 68 | { 69 | System.out.println(dog.makeNoise()); 70 | } 71 | 72 | public static void makeNoise(Cat cat) 73 | { 74 | System.out.println(cat.makeNoise()); 75 | } 76 | 77 | public static void displayProperties(Human human) 78 | { 79 | System.out.println(human.name); 80 | } 81 | 82 | public static void displayProperties(Dog dog) 83 | { 84 | System.out.println(dog.name); 85 | } 86 | 87 | public static void displayProperties(Cat cat) 88 | { 89 | System.out.println(cat.name); 90 | } 91 | } 92 | 93 | class Human { 94 | public String name; 95 | public int height; 96 | 97 | public String makeNoise() 98 | { 99 | return "Hello"; 100 | } 101 | } 102 | 103 | class Dog { 104 | public String name; 105 | public int age; 106 | 107 | public String makeNoise() 108 | { 109 | return "woof"; 110 | } 111 | } 112 | 113 | class Cat { 114 | public String name; 115 | public boolean isEyesColored; 116 | 117 | public String makeNoise() 118 | { 119 | return "meow"; 120 | } 121 | } -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/BankApp.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class BankApp { 4 | public static java.util.Scanner kb = new java.util.Scanner(System.in); 5 | public static Customer customer; 6 | 7 | public void login(Customer newCustomer) 8 | { 9 | customer = newCustomer; 10 | 11 | for (int i = 2; i >= 0; i--) { 12 | System.out.print("Customer Id: "); 13 | String inputId = kb.nextLine(); 14 | 15 | System.out.print("Password: "); 16 | String inputPassword = kb.nextLine(); 17 | 18 | if (loginCheck(inputId, inputPassword)) { 19 | System.out.println("-------------------"); 20 | System.out.println("Login successfully"); 21 | System.out.println("-------------------"); 22 | menu(); 23 | break; 24 | } 25 | else 26 | System.out.printf("Wrong id or password. Remaining left: %d%n", i); 27 | } 28 | } 29 | 30 | public boolean loginCheck(String inputId, String inputPw) 31 | { 32 | return customer.getId().equals(inputId) && customer.getPassword().equals(inputPw); 33 | } 34 | 35 | public void menu() 36 | { 37 | for (;;) { 38 | displayMenu(); 39 | 40 | String choice = kb.nextLine(); 41 | menuOptions(choice); 42 | 43 | if (choice.equals("0")) 44 | break; 45 | } 46 | } 47 | 48 | public void displayMenu() 49 | { 50 | System.out.println("1- Show Balance\n" + 51 | "2- Withdraw\n" + 52 | "3- Deposit\n" + 53 | "0- Exit\n" + 54 | "-------------------"); 55 | } 56 | 57 | public void menuOptions(String choice) 58 | { 59 | switch (choice) { 60 | case "0": 61 | System.out.println("Exiting menu"); 62 | break; 63 | case "1": 64 | showBalance(); 65 | break; 66 | case "2": 67 | withdraw(); 68 | break; 69 | case "3": 70 | deposit(); 71 | break; 72 | default: 73 | System.out.println("Invalid Input"); 74 | } 75 | } 76 | 77 | public void showBalance() 78 | { 79 | System.out.printf("Balance: %d%n", customer.getWallet().getCard().getBalance()); 80 | } 81 | 82 | public void withdraw() 83 | { 84 | Card sender = customer.getWallet().getCard(); 85 | 86 | System.out.println("Iban: "); 87 | Card receiver = new Card(kb.nextLine()); 88 | 89 | System.out.println("How much to wanna send:"); 90 | int toSendMoney = Integer.parseInt(kb.nextLine()); 91 | 92 | if (sender.checkBalance(sender.balance, toSendMoney)) { 93 | sender.setBalance(sender.getBalance() - toSendMoney); 94 | receiver.setBalance(receiver.getBalance() + toSendMoney); 95 | System.out.println("Your transaction is completed successfully"); 96 | } 97 | else { 98 | System.out.println("Insufficient Balance"); 99 | } 100 | } 101 | 102 | public void deposit() 103 | { 104 | System.out.println("How much do you want to deposit"); 105 | int amount = Integer.parseInt(kb.nextLine()); 106 | 107 | Wallet wallet = customer.getWallet(); 108 | Card card = wallet.getCard(); 109 | 110 | card.setBalance(card.getBalance() + amount); 111 | wallet.setCashMoney(wallet.getCashMoney() - amount); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/lessons/methods/methodoverloading/MethodOverloading.java: -------------------------------------------------------------------------------- 1 | package lessons.methods.methodoverloading; 2 | 3 | public class MethodOverloading { 4 | 5 | public static void main(String[] args) { 6 | 7 | // // Sample Method for see to explanation method and candidates 8 | // System.out.println("Efficient House"); 9 | // 10 | // // When methods 1, 2 are written 11 | // lessons.methods.methodoverloading.MyMethods.foo(5, 5); 12 | // 13 | // // lessons.methods.methodoverloading.MethodOverloading.foo(5,5); -> reaching with source path 14 | // MyMethods.foo(5, 5); 15 | // MyMethods.foo(5, 5.0); 16 | // 17 | // // When YourMethod class was written 18 | // 19 | // // YourMethods yourMethod = new YourMethods(); 20 | // // yourMethod.foo(5,5); -> There is no OVERLOADING 21 | // 22 | // // When methods 1, 2, 3, 4 are written 23 | // MyMethods myMethod = new MyMethods(); 24 | // myMethod.foo(5.5, 5); 25 | // myMethod.foo(5, 5); 26 | 27 | // When just 7, 8, 9 methods are written (AMBIGUITY) 28 | short s = 5; 29 | MyMethods.foo(s, 5); 30 | } 31 | } 32 | 33 | class MyMethods { 34 | 35 | // 1 36 | public static void foo(int a, int b) { 37 | System.out.println("You are in 1. method -> void method(int, int)"); 38 | System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b); 39 | } 40 | 41 | // 2 42 | public static void foo(int a, double b) { 43 | System.out.println("You are in 2. method -> void method(int, double)"); 44 | System.out.printf("You called this method -> MyMethod.method(%d, %.2f);", a, b); 45 | } 46 | 47 | // 3 48 | public static void foo(short a, long b) { 49 | System.out.println("You are in 3. method -> void method(short, long)"); 50 | System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b); 51 | } 52 | 53 | // 4 non-static 54 | public void foo(double a, int b) { 55 | System.out.println("You are in 4. method -> void method(double, int)"); 56 | System.out.printf("You called this method -> MyMethod.method(%.2f, %d);", a, b); 57 | } 58 | 59 | // 5 60 | public static void foo(short a, byte b) { 61 | System.out.println("You are in 5. method -> void method(short, byte)"); 62 | System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b); 63 | } 64 | 65 | // 6 66 | public static void foo(short a, int b) { 67 | System.out.println("You are in 6. method -> void method(short, int)"); 68 | System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b); 69 | } 70 | 71 | // 7 72 | public static void foo(short a, double b) { 73 | System.out.println("You are in 7. method -> void method(short, double)"); 74 | System.out.printf("You called this method -> MyMethod.method(%d, %.2f);", a, b); 75 | } 76 | 77 | 78 | // 8 79 | public static void foo(int a, float b) { 80 | System.out.println("You are in 8. method -> void method(int, float)"); 81 | System.out.printf("You called this method -> MyMethod.method(%d, %.2f);", a, b); 82 | } 83 | 84 | // 9 85 | public static void foo(int a, long b) { 86 | System.out.println("You are in 9. method -> void method(int, long)"); 87 | System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b); 88 | } 89 | } 90 | 91 | class YourMethods { 92 | public void foo(int a, int b) { 93 | System.out.println("This is YourMethods class"); 94 | } 95 | 96 | // public static void foo(int a, int b) { 97 | // System.out.println("This is YourMethods class"); 98 | // } 99 | } 100 | -------------------------------------------------------------------------------- /src/lessons/oop/basics/constructors/basicshoppingapp/Market.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.basics.constructors.basicshoppingapp; 2 | 3 | public class Market { 4 | public static java.util.Scanner kb = new java.util.Scanner(System.in); 5 | 6 | public static void displayProducts() 7 | { 8 | System.out.println("Products"); 9 | System.out.println("--------------------"); 10 | System.out.println("1- Milk - 10"); 11 | System.out.println("2- Bread - 5"); 12 | System.out.println("3- Egg - 7"); 13 | System.out.println("4- Apple - 3"); 14 | System.out.println("5- Newspaper - 2"); 15 | System.out.println("0- '-End shopping -> Go to Payment'"); 16 | } 17 | 18 | public static int getProduct() 19 | { 20 | int cost = 0; 21 | int count = 0; 22 | 23 | displayProducts(); 24 | 25 | while (true) { 26 | 27 | int choice = Integer.parseInt(kb.nextLine()); 28 | 29 | if (checkInput(choice)) 30 | continue; 31 | 32 | switch (choice) { 33 | case 1 : cost += 10; 34 | break; 35 | case 2 : cost += 5; 36 | break; 37 | case 3 : cost += 7; 38 | break; 39 | case 4 : cost += 3; 40 | break; 41 | case 5 : cost += 2; 42 | break; 43 | default : 44 | System.out.printf("-----------------------%n" + 45 | "%d products - %d $" + 46 | "%n-----------------------%n", count, cost); 47 | return cost; 48 | } 49 | 50 | count++; 51 | } 52 | } 53 | 54 | public static void shopping(Customer customer) 55 | { 56 | int cost = getProduct(); 57 | 58 | System.out.println("How do you want to pay ? \n" + 59 | "Card or Cash"); 60 | 61 | Wallet wallet = customer.getWallet(); 62 | String paymentChoice = kb.nextLine(); 63 | 64 | if (paymentChoice.equalsIgnoreCase("card")) 65 | paymentWithCard(wallet.getCard(), cost); 66 | else if (paymentChoice.equalsIgnoreCase("cash")) 67 | paymentWithCash(wallet, cost); 68 | else 69 | System.out.println("Invalid Input"); 70 | } 71 | 72 | public static void paymentWithCard(Card card, int cost) 73 | { 74 | for (int i = 3; 0 < i; i--) { 75 | System.out.print("password:"); 76 | if (kb.nextLine().equals(card.getPassword())) { 77 | if (checkBalance(card.getBalance(), cost)) { 78 | card.setBalance(card.getBalance() - cost); 79 | System.out.println("Successfully Paid"); 80 | System.out.printf("Remaining Card Balance : %d", card.getBalance()); 81 | } else { 82 | System.out.println("Insufficient Balance"); 83 | break; 84 | } 85 | } else { 86 | System.out.printf("Wrong password - Your remaining left %d%n", i); 87 | } 88 | } 89 | } 90 | 91 | public static void paymentWithCash(Wallet wallet, int cost) 92 | { 93 | if (checkBalance(wallet.getCashMoney(), cost)) { 94 | wallet.setCashMoney(wallet.getCashMoney() - cost); 95 | System.out.println("Thanks for your choosing us"); 96 | System.out.printf("New Balance : %d", wallet.getCashMoney()); 97 | } 98 | else 99 | System.out.println("You did not give enough money"); 100 | } 101 | 102 | public static boolean checkBalance(int balance, int cost) 103 | { 104 | return balance >= cost; 105 | } 106 | 107 | public static boolean checkInput(int choice) 108 | { 109 | boolean flag = false; 110 | 111 | if (choice < 0 || choice > 5) { 112 | System.out.println("Invalid Input"); 113 | flag = true; 114 | } 115 | 116 | return flag; 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/lessons/arrays/examples/ArrayMethods.java: -------------------------------------------------------------------------------- 1 | package lessons.arrays.examples; 2 | 3 | import java.util.Random; 4 | 5 | public class ArrayMethods { 6 | 7 | public static void bubbleSortAscending(int[] a) 8 | { 9 | for (int i = a.length - 1; i > 0; --i) 10 | for (int j = 0; j < i; ++j) 11 | if (a[j] > a[j + 1]) 12 | swap(a, j, j + 1); 13 | } 14 | 15 | public static void bubbleSortDescending(int[] a) 16 | { 17 | for (int i = 0; i < a.length - 1; ++i) 18 | for (int j = a.length -1; j > i; --j) 19 | if (a[j] > a[j - 1]) 20 | swap(a, j , j -1); 21 | } 22 | 23 | public static void displayArray(int[] a) 24 | { 25 | for (int i = 0; i < a.length; ++i) { 26 | System.out.print(a[i] + " "); 27 | } 28 | 29 | System.out.println(); 30 | } 31 | 32 | public static int[] getRandomIntArray(int length, int start, int end) 33 | { 34 | return getRandomIntArray(new Random(), length, start, end); 35 | } 36 | 37 | public static int[] getRandomIntArray(Random r, int length, int start, int end) 38 | { 39 | int[] randomArray = new int[length]; 40 | 41 | for (int i = 0; i < randomArray.length; ++i) { 42 | randomArray[i] = r.nextInt(start, end); 43 | } 44 | 45 | return randomArray; 46 | } 47 | 48 | public static void insertionShort(int[] a) 49 | { 50 | insertionShortAscending(a); 51 | } 52 | 53 | public static void insertionShortAscending(int[] a) 54 | { 55 | for (int i = 1; i < a.length; i++) { 56 | int temp = a[i]; 57 | 58 | int j = i - 1; 59 | while (j >= 0 && a[j] > temp) { 60 | a[j + 1] = a[j]; 61 | j--; 62 | } 63 | a[j + 1] = temp; 64 | } 65 | } 66 | 67 | public static void insertionShortDescending(int[] a) 68 | { 69 | for (int i = 1; i < a.length; i++) { 70 | int temp = a[i]; 71 | 72 | int j = i - 1; 73 | while (j >= 0 && a[j] < temp) { 74 | a[j + 1] = a[j]; 75 | j--; 76 | } 77 | a[j + 1] = temp; 78 | } 79 | } 80 | 81 | public static int max(int[] nums) 82 | { 83 | int max = nums[0]; 84 | 85 | for (int i = 1; i < nums.length; ++i) { 86 | if (nums[i] > max) 87 | max = nums[i]; 88 | } 89 | 90 | return max; 91 | } 92 | 93 | public static int min(int[] nums) 94 | { 95 | int min = nums[0]; 96 | 97 | for (int i = 1; i < nums.length; ++i) { 98 | if (nums[i] < min) 99 | min = nums[i]; 100 | } 101 | 102 | return min; 103 | } 104 | 105 | public static double average(int[] nums) 106 | { 107 | return (double) sum(nums) / nums.length; 108 | } 109 | 110 | public static void reverse(int[] nums) 111 | { 112 | int lastIndex = nums.length - 1; 113 | 114 | for (int i = 0; i < nums.length / 2; ++i) { 115 | int temp = nums[i]; 116 | nums[i] = nums[lastIndex - i]; 117 | nums[lastIndex - i] = temp; 118 | } 119 | } 120 | 121 | public static void selectionSortAscending(int[] a) 122 | { 123 | for (int i = 0; i < a.length - 1; ++i) { 124 | 125 | int minIndex = i; 126 | 127 | for (int j = i + 1; j < a.length; ++j) 128 | if (a[j] < a[minIndex]) 129 | minIndex = j; 130 | 131 | 132 | swap(a, i, minIndex); 133 | } 134 | } 135 | 136 | public static void selectionSortDescending(int[] a) 137 | { 138 | for (int i = 0; i < a.length - 1; ++i) { 139 | 140 | int maxIndex = i; 141 | 142 | for (int j = i + 1; j < a.length; ++j) 143 | if (a[maxIndex] < a[j]) 144 | maxIndex = j; 145 | 146 | swap(a, i, maxIndex); 147 | } 148 | } 149 | 150 | public static int sum(int[] nums) 151 | { 152 | int result = 0; 153 | 154 | for (int i = 0; i < nums.length; ++i) { 155 | result += nums[i]; 156 | } 157 | 158 | return result; 159 | } 160 | 161 | public static void swap(int[] a, int i, int j) 162 | { 163 | int temp = a[i]; 164 | a[i] = a[j]; 165 | a[j] = temp; 166 | } 167 | } 168 | 169 | -------------------------------------------------------------------------------- /src/lessons/oop/stringexamples/StringMethods.java: -------------------------------------------------------------------------------- 1 | package lessons.oop.stringexamples; 2 | 3 | import java.util.Random; 4 | 5 | public class StringMethods{ 6 | public static String TRAlphabet = "abcçdefgğhıijklmnoöprsştuüvyz"; 7 | public static String ENAlphabet = "abcdefghijklmnopqrstuvwxyz"; 8 | public static Random random = new Random(); 9 | 10 | public static String capitalize(String str) 11 | { 12 | int idx; 13 | 14 | for (idx = 0; idx < str.length() && !Character.isLetter(str.charAt(idx)); ++idx); 15 | 16 | return str.substring(0, idx) + Character.toUpperCase(str.charAt(idx)) + str.substring(idx + 1).toLowerCase(); 17 | } 18 | 19 | public static String concatWithDelimiter(String delimiter) 20 | { 21 | java.util.Scanner kb = new java.util.Scanner(System.in); 22 | System.out.println("Enter your texts:"); 23 | String result = ""; 24 | 25 | while (true) { 26 | String text = kb.nextLine(); 27 | 28 | if (text.equals("quit")) 29 | break; 30 | 31 | result = result.concat(text).concat(delimiter); 32 | } 33 | 34 | return result.substring(0, result.length() - delimiter.length()); 35 | } 36 | 37 | public static String concatWithDelimiter(char delimeter) 38 | { 39 | return concatWithDelimiter(String.valueOf(delimeter)); 40 | } 41 | 42 | public static void displayFullAddress() 43 | { 44 | java.util.Scanner kb = new java.util.Scanner(System.in); 45 | 46 | System.out.print("web address:"); 47 | String address = kb.nextLine(); // www.google.com 48 | 49 | if (!address.startsWith("https://")) 50 | address = "https://www." + address; // 51 | 52 | if (!address.endsWith(".com")) 53 | address += ".com"; 54 | 55 | System.out.println(address); 56 | } 57 | 58 | /** 59 | 60 | **/ 61 | public static String getRandomText(java.util.Random random, int len, String alphabet) 62 | { 63 | String resultText = ""; 64 | 65 | for (int i = 0; i < len; ++i) { 66 | resultText += alphabet.charAt(random.nextInt(alphabet.length())); 67 | } 68 | 69 | return resultText; 70 | } 71 | 72 | public static String getRandomTextEN(int len) 73 | { 74 | return getRandomText(random, len, ENAlphabet); 75 | } 76 | 77 | public static String getRandomTextEN(java.util.Random r, int len) 78 | { 79 | return getRandomText(r, len, ENAlphabet); 80 | } 81 | 82 | public static String getRandomTextTR(int len) 83 | { 84 | return getRandomText(random, len, TRAlphabet); 85 | } 86 | 87 | public static String getRandomTextTR(java.util.Random r, int len) 88 | { 89 | return getRandomText(r, len, TRAlphabet); 90 | } 91 | 92 | public static String leftPad(String str, int len, char ch) 93 | { 94 | int length = str.length(); 95 | 96 | return length >= len ? str : (ch + "").repeat(len - length) + str; 97 | } 98 | 99 | public static String leftPad(String str, int len) 100 | { 101 | return leftPad(str, len, ' '); 102 | } 103 | 104 | public static boolean isPangramEN(String text) 105 | { 106 | return isPangram(text, ENAlphabet); 107 | } 108 | 109 | public static boolean isPangramTR(String text) 110 | { 111 | return isPangram(text, TRAlphabet); 112 | } 113 | 114 | public static boolean isPangram(String text, String alphabet) 115 | { 116 | String lowerText = text.toLowerCase(); 117 | 118 | for (int i = 0; i < alphabet.length(); ++i) { 119 | if (!lowerText.contains(alphabet.charAt(i) + "")) 120 | return false; 121 | } 122 | 123 | return true; 124 | } 125 | 126 | public static boolean isPalindrome(String str) 127 | { 128 | return str.equals(reversed(str)); 129 | } 130 | 131 | 132 | public static String reversed(String str) 133 | { 134 | char[] chars = str.toCharArray(); 135 | int lastIndex = chars.length - 1; 136 | 137 | for (int i = 0; i < chars.length / 2; ++i) { 138 | char temp = chars[lastIndex - i]; 139 | chars[lastIndex - i] = chars[i]; 140 | chars[i] = temp; 141 | } 142 | 143 | return String.valueOf(chars); 144 | } 145 | 146 | public static String removeAllWhiteSpaces(String str) // It's not best practice 147 | { 148 | String result = ""; 149 | 150 | for (int i = 0; i < str.length(); ++i) 151 | if (str.charAt(i) != ' ') 152 | result += str.charAt(i); 153 | 154 | return result; 155 | } 156 | 157 | public static String rightPad(String str, int len, char ch) 158 | { 159 | int length = str.length(); 160 | 161 | return length >= len ? str : str + (ch + "").repeat(len - length) ; 162 | } 163 | 164 | public static String rightPad(String str, int len) 165 | { 166 | return rightPad(str, len, ' '); 167 | } 168 | 169 | } 170 | 171 | 172 | -------------------------------------------------------------------------------- /src/lessons/arrays/examples/ArrayMethodsTest.java: -------------------------------------------------------------------------------- 1 | package lessons.arrays.examples; 2 | 3 | import java.util.Random; 4 | import java.util.Scanner; 5 | import static lessons.arrays.examples.ArrayMethods.*; 6 | 7 | public class ArrayMethodsTest { 8 | public static Scanner kb = new Scanner(System.in); 9 | public static Random r = new Random(); 10 | 11 | public static void main(String[] args) 12 | { 13 | insertionShortTest(); 14 | } 15 | 16 | public static void reverseTest() 17 | { 18 | for (;;) { 19 | System.out.print("Arrays Length:"); 20 | int length = Integer.parseInt(kb.nextLine()); 21 | 22 | if (length <= 0) 23 | break; 24 | 25 | System.out.print("Begin (inclusive):"); 26 | int begin = Integer.parseInt(kb.nextLine()); 27 | 28 | System.out.print("End (exclusive):"); 29 | int end = Integer.parseInt(kb.nextLine()); 30 | 31 | int [] randomArray = getRandomIntArray(r, length, begin, end); 32 | 33 | displayArray(randomArray); 34 | System.out.print("reversed array: "); 35 | reverse(randomArray); 36 | displayArray(randomArray); 37 | } 38 | } 39 | 40 | public static void sumTest() 41 | { 42 | for (;;) { 43 | System.out.print("Arrays Length:"); 44 | int length = Integer.parseInt(kb.nextLine()); 45 | 46 | if (length <= 0) 47 | break; 48 | 49 | System.out.print("Begin (inclusive):"); 50 | int begin = Integer.parseInt(kb.nextLine()); 51 | 52 | System.out.print("End (exclusive):"); 53 | int end = Integer.parseInt(kb.nextLine()); 54 | 55 | int [] randomArray = getRandomIntArray(r, length, begin, end); 56 | 57 | displayArray(randomArray); 58 | System.out.printf("Sum of the numbers: %d%n", sum(randomArray)); 59 | } 60 | } 61 | 62 | public static void selectionSortDescendingTest() 63 | { 64 | for (;;) { 65 | System.out.print("Arrays Length:"); 66 | int length = Integer.parseInt(kb.nextLine()); 67 | 68 | if (length <= 0) 69 | break; 70 | 71 | System.out.print("Begin (inclusive):"); 72 | int begin = Integer.parseInt(kb.nextLine()); 73 | 74 | System.out.print("End (exclusive):"); 75 | int end = Integer.parseInt(kb.nextLine()); 76 | 77 | int [] randomArray = getRandomIntArray(r, length, begin, end); 78 | displayArray(randomArray); 79 | selectionSortDescending(randomArray); 80 | displayArray(randomArray); 81 | 82 | } 83 | } 84 | 85 | 86 | public static void selectionSortAscendingTest() 87 | { 88 | for (;;) { 89 | System.out.print("Arrays Length:"); 90 | int length = Integer.parseInt(kb.nextLine()); 91 | 92 | if (length <= 0) 93 | break; 94 | 95 | System.out.print("Begin (inclusive):"); 96 | int begin = Integer.parseInt(kb.nextLine()); 97 | 98 | System.out.print("End (exclusive):"); 99 | int end = Integer.parseInt(kb.nextLine()); 100 | 101 | int [] randomArray = getRandomIntArray(r, length, begin, end); 102 | displayArray(randomArray); 103 | selectionSortAscending(randomArray); 104 | displayArray(randomArray); 105 | 106 | } 107 | } 108 | 109 | public static void bubbleSortAscendingTest() 110 | { 111 | for (;;) { 112 | System.out.print("Arrays Length:"); 113 | int length = Integer.parseInt(kb.nextLine()); 114 | 115 | if (length <= 0) 116 | break; 117 | 118 | System.out.print("Begin (inclusive):"); 119 | int begin = Integer.parseInt(kb.nextLine()); 120 | 121 | System.out.print("End (exclusive):"); 122 | int end = Integer.parseInt(kb.nextLine()); 123 | 124 | int [] randomArray = getRandomIntArray(r, length, begin, end); 125 | displayArray(randomArray); 126 | bubbleSortAscending(randomArray); 127 | displayArray(randomArray); 128 | 129 | } 130 | } 131 | 132 | public static void bubbleSortDescendingTest() 133 | { 134 | System.out.print("Arrays Length:"); 135 | int length = Integer.parseInt(kb.nextLine()); 136 | 137 | System.out.print("Begin (inclusive):"); 138 | int begin = Integer.parseInt(kb.nextLine()); 139 | 140 | System.out.print("End (exclusive):"); 141 | int end = Integer.parseInt(kb.nextLine()); 142 | 143 | int [] randomArray = getRandomIntArray(r, length, begin, end); 144 | displayArray(randomArray); 145 | 146 | long startTime = System.currentTimeMillis(); 147 | bubbleSortDescending(randomArray); 148 | long endTime = System.currentTimeMillis(); 149 | 150 | System.out.printf("Bircan: %d%n", endTime - startTime); 151 | displayArray(randomArray); 152 | } 153 | 154 | public static void insertionShortTest() 155 | { 156 | System.out.print("Arrays Length:"); 157 | int length = Integer.parseInt(kb.nextLine()); 158 | 159 | System.out.print("Begin (inclusive):"); 160 | int begin = Integer.parseInt(kb.nextLine()); 161 | 162 | System.out.print("End (exclusive):"); 163 | int end = Integer.parseInt(kb.nextLine()); 164 | 165 | int [] randomArray = getRandomIntArray(r, length, begin, end); 166 | displayArray(randomArray); 167 | 168 | long startTime = System.nanoTime(); 169 | insertionShort(randomArray); 170 | long endTime = System.nanoTime(); 171 | 172 | System.out.println("--------------------------"); 173 | displayArray(randomArray); 174 | System.out.printf("Time efficiency: %d%n", endTime - startTime); 175 | } 176 | public static void insertionShortAscendingTest() 177 | { 178 | System.out.print("Arrays Length:"); 179 | int length = Integer.parseInt(kb.nextLine()); 180 | 181 | System.out.print("Begin (inclusive):"); 182 | int begin = Integer.parseInt(kb.nextLine()); 183 | 184 | System.out.print("End (exclusive):"); 185 | int end = Integer.parseInt(kb.nextLine()); 186 | 187 | int [] randomArray = getRandomIntArray(r, length, begin, end); 188 | displayArray(randomArray); 189 | 190 | long startTime = System.nanoTime(); 191 | insertionShortAscending(randomArray); 192 | long endTime = System.nanoTime(); 193 | 194 | System.out.println("--------------------------"); 195 | displayArray(randomArray); 196 | System.out.printf("Time efficiency: %d%n", endTime - startTime); 197 | } 198 | public static void insertionShortDescendingTest() 199 | { 200 | System.out.print("Arrays Length:"); 201 | int length = Integer.parseInt(kb.nextLine()); 202 | 203 | System.out.print("Begin (inclusive):"); 204 | int begin = Integer.parseInt(kb.nextLine()); 205 | 206 | System.out.print("End (exclusive):"); 207 | int end = Integer.parseInt(kb.nextLine()); 208 | 209 | int [] randomArray = getRandomIntArray(r, length, begin, end); 210 | displayArray(randomArray); 211 | 212 | long startTime = System.nanoTime(); 213 | insertionShortDescending(randomArray); 214 | long endTime = System.nanoTime(); 215 | 216 | System.out.println("--------------------------"); 217 | displayArray(randomArray); 218 | System.out.printf("Time efficiency: %d%n", endTime - startTime); 219 | } 220 | 221 | } 222 | -------------------------------------------------------------------------------- /src/lessons/languagebasics/README.md: -------------------------------------------------------------------------------- 1 | # What is Java? 2 | 3 | Java is well-known as a programming language for Internet applications. 4 | 5 | In 1991, James Gosling led a team at Sun Microsystems that developed the first version of Java ( It was Oak ) 6 | 7 | Java is an object-oriented programming (OOP) language. 8 | 9 | Java is called high-level language The language that the computer can directly understand is called machine language , 10 | 11 | Machine language or any language similar to machine language is called a low-level language. 12 | 13 | Must be translated into a program in machine language before the program can be run. The program that does the translating is called a compiler, and the translation process is called compiling. 14 | 15 | 22 | 23 | One disadvantage of most programming languages is that the compiler translates the high-level-language program directly into the machine language for your computer. 24 | Since different computers have different machine languages, this means you need a different compiler for each type of computer. 25 | 26 | Java, however, uses a slightly different and much more versatile approach to compiling. 27 | 28 | The Java compiler translates your Java program into a language called byte-code. 29 | Byte-code is not the machine language for any particular computer; it is the machine language for a fictitious computer called the Java Virtual Machine (JVM). 30 | The Java Virtual Machine is very similar to all typical computers. Thus, it is easy to translate a program written in byte-code into a program in the machine language for any particular computer. 31 | There are two ways the JVM can do this translation: through an interpreter and through a Just-In-Time (JIT) compiler 32 | 33 | An interpreter combines the translation of the byte-code and the execution of the corresponding machine language instructions. 34 | The interpreter works by translating an instruction of byte-code into instructions expressed in your computer’s machine language and then executing those instructions on your computer. 35 | It does this one byte-code instruction at a time. Thus, an interpreter translates and executes the instructions in the byte-code one after the other, rather than translating the entire byte-code program at once. 36 | 37 | Modern implementations of the JVM use a JIT compiler, which uses a combination of interpretation and compilation. 38 | The JIT compiler reads the byte-code in chunks and compiles entire chunks to native machine language instructions as needed. 39 | The compiled machine language instructions are remembered—that is, cached—for future use, so the chunk needs to be compiled only once. 40 | This model generally runs programs faster than the interpreted model, which always has to translate the next byte-code instruction to machine code instructions 41 | 42 | . To run a Java program, first use the compiler to translate the Java program into byte-code., 43 | Then, use the JVM for your computer to translate byte-code instructions to machine language and to run the machine language instructions. (It sounds as though Java byte-code just adds an extra step in the process.) 44 | 45 | ### Why not write compilers that translate directly from Java to the machine language for your particular computer? 46 | 47 | This is what is done for most other programming languages. 48 | 49 | However, Java byte-code makes your Java program very portable. 50 | After you compile your Java program into byte-code, you can use that byte-code on any computer. When you run your program on another type of computer, you do not need to recompile it. 51 | This means that you can send your byte-code over the Internet to another computer and have it easily run on that computer. This is one of the reasons Java is good for Internet applications. 52 | This model is also more secure. If a Java program behaves badly, it only does so within the context of the JVM instead of behaving badly directly on your native machine. 53 | Of course, every kind of computer must have its own program to implement the Java Virtual Machine. 54 | 55 | 60 | 61 | # Primitive Types 62 | 63 | - byte = 1bit : 8 byte ; The `byte`data type is an 8-bit signed two's complement integer (-128 , 127) The `byte` data type can be useful for where the memory savings actually matters. 64 | 65 | - short = 2bit : 16byte ; The `short`data type is a 16-bit signed two's complement integer. 66 | 67 | (-2^15 , 2^15 -1) 68 | 69 | - int = 4bit : 32byte ; the `int`data type is a 32-bit signed two's complement integer (-2^32 , 2^31-1) 70 | 71 | 75 | 76 | > Use the Integer class to use `int`data type as an unsigned integer 77 | > 78 | 79 | 83 | 84 | - long = 8bit : 64byte ; The `long`data type is a 64-bit signed two's complement integer (2^63 , 2^63-1) 85 | 86 | 90 | 91 | Use this data type when you need a range of values wider than those provided by `int`. 92 | 93 | Floating Point Numbers 94 | 95 | - float = 4byte :32bit ;The `float`data type is a single-precision 32-bit IEEE 754 floating point. 96 | 97 | This is signed. ( -2^31 , 2^31 -1) 98 | 99 | - double = 8byte : 64bit ; The `double`data type is a double-precision 64-bit IEEE 754 floating point (-2^63 , 2^63 -1) 100 | 101 | > **For decimal values, this data type is generally the default choice. Cause** , Double is more precise than float and can store 64 bits, double of the number of bits float can store. Double is more precise and for storing large numbers, we prefer double over float. 102 | > 103 | 104 | • **boolean**: The `boolean` data type 1bit and has only two possible values: `true` and `false`. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined. 105 | 106 | 110 | 111 | > • Why is a boolean 1 byte and not 1 bit of size? 112 | > 113 | 114 | ( because we said before boolean can be true(1bit) or false(1bit) but boolean 8-bit Because the CPU can't address anything smaller than a byte. 115 | 116 | - char : The `char`data type is a single 16-bit Unicode character. This is unsigned. This Type has 2^16 values. 117 | 118 | 122 | 123 | ### Type Casting 124 | 125 | A type cast takes a value of one type and produces a value of another type that is Java’s best guess of an equivalent value. 126 | 127 | ![Untitled](Primitive%20Types%208567912cbe204feea68d12a49a5a1eb8/Untitled.png) 128 | 129 | ``` 130 | byte b = 4; 131 | 132 | short c = b; 133 | 134 | int a = c; 135 | 136 | They can easily be converted to each other because int includes short and short' byte. 137 | 138 | Ancak 139 | int c = 100000; 140 | byte b = (byte)c; 141 | 142 | When converting from int to byte we should use casting operator, forcing. 143 | ``` 144 | 145 | --------------------------------------------------------------------------------