├── JavaHomeworks ├── Homework-Java-001.pdf ├── Homework-Java-002.pdf ├── Homework-Java-003.pdf ├── Homework-Java-004.pdf ├── Homework-Java-005.pdf ├── Homework-Java-006.pdf ├── Homework-Java-007.pdf ├── JavaHomeworks001.md ├── JavaHomeworks002.md ├── JavaHomeworks002v2.md ├── JavaHomeworks003.md ├── JavaHomeworks004.md ├── JavaHomeworks005.md └── README.md ├── JavaLesson001 └── java001.md ├── JavaLesson002 └── java002.md ├── JavaLesson003 └── java003.md ├── JavaLesson004 └── java004.md ├── JavaLesson005 └── java005.md ├── JavaLesson006 └── java006.md ├── JavaLesson007 └── java007.md ├── JavaLesson008 └── java008.md ├── JavaLesson009 └── java009.md ├── JavaLesson010 └── java010.md ├── JavaLesson011 └── java011.md ├── JavaLesson012 ├── java012.md └── org │ └── csystem │ ├── app │ ├── App.java │ └── samples │ │ └── dateapp │ │ ├── DateUtil.java │ │ └── DateUtilApp.java │ ├── math │ ├── Complex.java │ └── geometry │ │ ├── Point.java │ │ └── PointF.java │ └── util │ ├── NumberUtil.java │ └── StringUtil.java ├── JavaLesson013 ├── java013.md └── org │ └── csystem │ ├── app │ ├── App.java │ └── samples │ │ ├── dateapp │ │ ├── DateUtil.java │ │ └── DateUtilApp.java │ │ └── lottary │ │ ├── Lottary.java │ │ └── LottaryApp.java │ ├── math │ ├── Complex.java │ └── geometry │ │ ├── Point.java │ │ └── PointF.java │ └── util │ ├── ArrayUtil.java │ ├── NumberUtil.java │ └── StringUtil.java ├── JavaLesson014 ├── java014.md └── org │ └── csystem │ ├── app │ ├── App.java │ └── samples │ │ ├── commandpromptapp │ │ ├── CommandPrompt.java │ │ └── CommandPromptApp.java │ │ ├── dateapp │ │ ├── DateUtil.java │ │ └── DateUtilApp.java │ │ ├── examsimulationapp │ │ ├── ExamSimulation.java │ │ └── ExamSimulationApp.java │ │ ├── lottary │ │ ├── Lottary.java │ │ └── LottaryApp.java │ │ └── studentinfoparserapp │ │ ├── DateUtil.java │ │ ├── StudentInfo.java │ │ ├── StudentInfoParser.java │ │ └── StudentInfoParserApp.java │ ├── math │ ├── Complex.java │ └── geometry │ │ ├── Point.java │ │ └── PointF.java │ └── util │ ├── ArrayUtil.java │ ├── NumberUtil.java │ └── StringUtil.java ├── JavaLesson015 ├── java015.md └── org │ └── csystem │ ├── app │ ├── App.java │ └── samples │ │ ├── commandpromptapp │ │ ├── CommandPrompt.java │ │ └── CommandPromptApp.java │ │ ├── dateapp │ │ ├── DateUtil.java │ │ └── DateUtilApp.java │ │ ├── examsimulationapp │ │ ├── ExamSimulation.java │ │ └── ExamSimulationApp.java │ │ ├── lottary │ │ ├── Lottary.java │ │ └── LottaryApp.java │ │ └── studentinfoparserapp │ │ ├── DateUtil.java │ │ ├── StudentInfo.java │ │ ├── StudentInfoParser.java │ │ └── StudentInfoParserApp.java │ ├── math │ ├── Complex.java │ └── geometry │ │ ├── Circle.java │ │ ├── Point.java │ │ └── PointF.java │ └── util │ ├── ArrayUtil.java │ ├── NumberUtil.java │ └── StringUtil.java ├── JavaLesson016 ├── java016.md └── org │ └── csystem │ ├── app │ ├── App.java │ └── samples │ │ ├── commandpromptapp │ │ ├── CommandPrompt.java │ │ └── CommandPromptApp.java │ │ ├── dateapp │ │ ├── DateUtil.java │ │ └── DateUtilApp.java │ │ ├── examsimulationapp │ │ ├── ExamSimulation.java │ │ └── ExamSimulationApp.java │ │ ├── lottary │ │ ├── Lottary.java │ │ └── LottaryApp.java │ │ └── studentinfoparserapp │ │ ├── DateUtil.java │ │ ├── StudentInfo.java │ │ ├── StudentInfoParser.java │ │ └── StudentInfoParserApp.java │ ├── math │ ├── Complex.java │ └── geometry │ │ ├── Circle.java │ │ ├── Point.java │ │ └── PointF.java │ └── util │ ├── ArrayUtil.java │ ├── IntValue.java │ ├── NumberUtil.java │ ├── StringUtil.java │ └── datetime │ └── Time.java ├── JavaLesson017 ├── java017.md └── org │ └── csystem │ ├── app │ ├── App.java │ └── samples │ │ ├── commandpromptapp │ │ ├── CommandPrompt.java │ │ └── CommandPromptApp.java │ │ ├── examsimulationapp │ │ ├── ExamSimulation.java │ │ └── ExamSimulationApp.java │ │ ├── lottary │ │ ├── Lottary.java │ │ └── LottaryApp.java │ │ └── studentinfoparserapp │ │ ├── StudentInfo.java │ │ ├── StudentInfoParser.java │ │ └── StudentInfoParserApp.java │ ├── math │ ├── Complex.java │ ├── Rational.java │ └── geometry │ │ ├── Circle.java │ │ ├── Point.java │ │ └── PointF.java │ └── util │ ├── ArrayUtil.java │ ├── IntValue.java │ ├── NumberUtil.java │ ├── StringUtil.java │ └── datetime │ ├── Date.java │ ├── DateTime.java │ ├── DayOfWeek.java │ ├── Month.java │ └── Time.java ├── JavaLesson018 └── java018.md ├── JavaLesson019 └── java019.md ├── JavaLesson020 └── java020.md ├── JavaLesson021 └── java021.md ├── JavaLesson022 └── java022.md ├── JavaLesson023 └── java023.md ├── JavaLesson024 └── java024.md ├── JavaLesson025 └── java025.md ├── JavaLesson026 └── java026.md ├── JavaLesson027 └── java027.md ├── JavaLesson028 └── java028.md ├── JavaLesson029 └── java029.md ├── JavaLesson030 └── java030.md └── README.md /JavaHomeworks/Homework-Java-001.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eemustafasahin/Complete_Java_Course_Notes/0062f60f541c45db2418e383917dd36e78c0261a/JavaHomeworks/Homework-Java-001.pdf -------------------------------------------------------------------------------- /JavaHomeworks/Homework-Java-002.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eemustafasahin/Complete_Java_Course_Notes/0062f60f541c45db2418e383917dd36e78c0261a/JavaHomeworks/Homework-Java-002.pdf -------------------------------------------------------------------------------- /JavaHomeworks/Homework-Java-003.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eemustafasahin/Complete_Java_Course_Notes/0062f60f541c45db2418e383917dd36e78c0261a/JavaHomeworks/Homework-Java-003.pdf -------------------------------------------------------------------------------- /JavaHomeworks/Homework-Java-004.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eemustafasahin/Complete_Java_Course_Notes/0062f60f541c45db2418e383917dd36e78c0261a/JavaHomeworks/Homework-Java-004.pdf -------------------------------------------------------------------------------- /JavaHomeworks/Homework-Java-005.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eemustafasahin/Complete_Java_Course_Notes/0062f60f541c45db2418e383917dd36e78c0261a/JavaHomeworks/Homework-Java-005.pdf -------------------------------------------------------------------------------- /JavaHomeworks/Homework-Java-006.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eemustafasahin/Complete_Java_Course_Notes/0062f60f541c45db2418e383917dd36e78c0261a/JavaHomeworks/Homework-Java-006.pdf -------------------------------------------------------------------------------- /JavaHomeworks/Homework-Java-007.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eemustafasahin/Complete_Java_Course_Notes/0062f60f541c45db2418e383917dd36e78c0261a/JavaHomeworks/Homework-Java-007.pdf -------------------------------------------------------------------------------- /JavaHomeworks/JavaHomeworks001.md: -------------------------------------------------------------------------------- 1 | ```java 2 | package csd; 3 | 4 | class App { 5 | public static void main(String [] args) 6 | { 7 | CmpTreeNumTest.run(); 8 | SigNumTest.run(); 9 | GetMidTest.run(); 10 | 11 | } 12 | } 13 | 14 | class SigNumTest { 15 | public static void run() 16 | { 17 | java.util.Scanner kb = new java.util.Scanner(System.in); 18 | System.out.println("Bir tamsayı sayı giriniz:"); 19 | int ival = Integer.parseInt(kb.nextLine()); 20 | 21 | Homework.displaySigNum(ival); 22 | 23 | } 24 | } 25 | 26 | class CmpTreeNumTest{ 27 | public static void run() 28 | { 29 | java.util.Scanner kb = new java.util.Scanner(System.in); 30 | System.out.println("Üç tamsayı sayı giriniz:"); 31 | int ival1 = Integer.parseInt(kb.nextLine()); 32 | 33 | int ival2 = Integer.parseInt(kb.nextLine()); 34 | 35 | int ival3 = Integer.parseInt(kb.nextLine()); 36 | 37 | Homework.cmpTreeNum(ival1, ival2, ival3); 38 | 39 | } 40 | } 41 | 42 | class GetMidTest { 43 | public static void run() 44 | { 45 | java.util.Scanner kb = new java.util.Scanner(System.in); 46 | System.out.println("Üç tamsayı sayı giriniz:"); 47 | int ival1 = Integer.parseInt(kb.nextLine()); 48 | 49 | int ival2 = Integer.parseInt(kb.nextLine()); 50 | 51 | int ival3 = Integer.parseInt(kb.nextLine()); 52 | 53 | System.out.printf("mid sayısı %d%n",Homework.getMid(ival1, ival2, ival3)); 54 | } 55 | } 56 | 57 | class Homework { 58 | public static void displaySigNum(int val) 59 | { 60 | System.out.printf("signum: %d%n",sigNum(val)); 61 | } 62 | 63 | public static void displayComparedNum(int min,int mid,int max) 64 | { 65 | 66 | char sigMinMid = '='; 67 | char sigMidMax = '='; 68 | if (min != mid) 69 | sigMinMid = '<'; 70 | if(mid != max) 71 | sigMidMax = '<'; 72 | System.out.printf("[%d] %c [%d] %c [%d]%n",min,sigMinMid,mid,sigMidMax,max); 73 | 74 | } 75 | 76 | public static void cmpTreeNum(int ival1,int ival2,int ival3) 77 | { 78 | int min = Math.min(Math.min(ival1, ival2),ival3); 79 | int max = Math.max(Math.max(ival1,ival2),ival3); 80 | int mid = getMid(ival1,ival2,ival3); 81 | displayComparedNum(min,mid,max); 82 | } 83 | 84 | public static int getMid(int ival1,int ival2,int ival3) 85 | { 86 | int mid = -1; 87 | if (ival1 <= ival2 && ival2 <= ival3 || ival3 <= ival2 && ival2 <= ival1) 88 | mid = ival2; 89 | else if (ival2 <= ival1 && ival1 <= ival3 || ival3 <= ival1 && ival1 <= ival2) 90 | mid = ival1; 91 | else if (ival1 <= ival3 && ival3 <= ival2 || ival2 <= ival3 && ival3 <= ival1) 92 | mid = ival3; 93 | 94 | return mid; 95 | } 96 | 97 | public static int sigNum(int ival) 98 | { 99 | if(ival != 0) 100 | if (ival > 0) 101 | ival /=ival; 102 | else 103 | ival /=-ival; 104 | 105 | return ival; 106 | } 107 | } 108 | 109 | ``` 110 | -------------------------------------------------------------------------------- /JavaHomeworks/JavaHomeworks002.md: -------------------------------------------------------------------------------- 1 | ```java 2 | package csd; 3 | 4 | class App { 5 | public static void main(String [] args) 6 | { 7 | 8 | //... 9 | 10 | } 11 | } 12 | 13 | class PatternTest { 14 | public static void run() 15 | { 16 | java.util.Scanner kb = new java.util.Scanner(System.in); 17 | System.out.println("Merdiven Çizimi: ?"); 18 | 19 | 20 | 21 | 22 | System.out.println("Yükseklik:"); 23 | int height = Integer.parseInt(kb.nextLine()); 24 | System.out.println("Genişlik:"); 25 | int width = Integer.parseInt(kb.nextLine()); 26 | Pattern.createPattern(height, width); 27 | 28 | 29 | 30 | } 31 | } 32 | 33 | class Pattern{ 34 | public static void createPattern (int height,int width) 35 | { 36 | 37 | boolean starFlag = true; 38 | int starPos = 1; 39 | for(int i = 0; i < height; i++){ 40 | { 41 | System.out.print("<"); 42 | if (starPos <= width && starFlag){ 43 | displayStarLeftToRight(starPos,width); 44 | starPos++; 45 | if (starPos > width){ 46 | starPos -= 2; 47 | starFlag = false; 48 | } 49 | } 50 | else if (starPos <= width && !starFlag) { 51 | displayStarRightToLeft(starPos, width); 52 | starPos--; 53 | if (starPos < 1){ 54 | starPos += 2; 55 | starFlag = true; 56 | } 57 | 58 | } 59 | System.out.println(">"); 60 | 61 | } 62 | 63 | } 64 | } 65 | public static void displayStarLeftToRight(int starPos,int width) 66 | { 67 | for (int i = 1; i <= width; i++){ 68 | if ((i == starPos)) 69 | System.out.print("*"); 70 | else 71 | System.out.print("_"); 72 | } 73 | } 74 | public static void displayStarRightToLeft(int starPos,int width) 75 | { 76 | 77 | for (int i = 1; i <= width; i++){ 78 | if ((i == starPos)) 79 | System.out.print("*"); 80 | else 81 | System.out.print("_"); 82 | } 83 | } 84 | } 85 | 86 | class GetPrimeFactorTest{ 87 | public static void run() 88 | { 89 | java.util.Scanner kb = new java.util.Scanner(System.in); 90 | System.out.println("Bir tamsayı giriniz:"); 91 | int ival; 92 | while( (ival = Integer.parseInt(kb.nextLine())) > 0 ){ 93 | System.out.println("Asal Çarpanlar gösteriliyor:"); 94 | GetPrimeFactor.displayPrimeFactor(ival); 95 | System.out.println("Yeni bir tamsayı giriniz:"); 96 | System.out.println("Çıkmak için 0'a basınız:"); 97 | 98 | } 99 | } 100 | } 101 | class GetPrimeFactor { 102 | 103 | 104 | public static void displayPrimeFactor(int ival) 105 | { 106 | int i = 2; 107 | if (ival < i){ 108 | System.out.printf("%d sayısının asal çarpanları yok",ival); 109 | return; 110 | } 111 | while (i <= ival){ 112 | if ((NumberUtil.isPrime(i) && ival % i == 0)){ 113 | System.out.printf("%d ",i); 114 | ival /= i; 115 | continue; 116 | } 117 | i++; 118 | } 119 | 120 | 121 | 122 | } 123 | } 124 | 125 | 126 | class DisplayDurationAppTest{ 127 | public static void run() 128 | { 129 | java.util.Scanner kb = new java.util.Scanner(System.in); 130 | System.out.println("Tamsayı türden bir saniye değeri giriniz:"); 131 | long second = Long.parseLong(kb.nextLine()); 132 | for(;;){ 133 | int n = 1000000000; 134 | while (n-- > 0) 135 | ; 136 | DisplayDurationApp.displayDuration(second--); 137 | System.out.printf("%n"); 138 | if(second == 0) 139 | break; 140 | } 141 | } 142 | } 143 | 144 | class DisplayDurationApp { 145 | public static void displayDuration(long second) 146 | { 147 | 148 | if (second / 3600 > 0) 149 | System.out.printf("%d hour ",second / 3600); 150 | if ((second % 3600) / 60 > 0) 151 | System.out.printf("%d minute ", (second % 3600) / 60); 152 | if (second % 60 > 0) 153 | System.out.printf("%d second ", second %= 60); 154 | } 155 | 156 | } 157 | 158 | class DiamondShapeAppTest{ 159 | public static void run() 160 | { 161 | java.util.Scanner kb = new java.util.Scanner(System.in); 162 | System.out.printf("Baklava çizimi için bir tamsayı giriniz:"); 163 | int ival = Integer.parseInt(kb.nextLine()); 164 | System.out.println("İşte Nefis Baklava:"); 165 | DiamondShapeApp.CreateDiamondShape(ival); 166 | } 167 | } 168 | 169 | class DiamondShapeApp { 170 | public static void CreateDiamondShape(int n) 171 | { 172 | int maxStarNumber = 2*n - 1; 173 | for (int i = 0, j = 1; i < n; i++, j += 2){ 174 | displayStar(j, maxStarNumber); 175 | if (j == maxStarNumber){ 176 | j -=2; 177 | for (int k = 1; k < n; k++,j -= 2){ 178 | displayStar(j, maxStarNumber); 179 | } 180 | } 181 | } 182 | } 183 | 184 | public static void displayStar(int currentStarNumber,int maxStarNumber) 185 | { 186 | int spaceNumber = maxStarNumber - currentStarNumber; 187 | for (int i = 0;i < maxStarNumber; i++){ 188 | if (i == spaceNumber / 2){ 189 | while(currentStarNumber-- > 0){ 190 | System.out.printf("*"); 191 | i++; 192 | } 193 | } 194 | System.out.printf(" "); 195 | } 196 | System.out.printf("%n"); 197 | } 198 | 199 | } 200 | 201 | ``` 202 | -------------------------------------------------------------------------------- /JavaHomeworks/JavaHomeworks002v2.md: -------------------------------------------------------------------------------- 1 | ```java 2 | package testpackage; 3 | 4 | class Test{ 5 | public static void main(String[] args) 6 | { 7 | BallGameApp.run(); 8 | 9 | } 10 | 11 | } //TestApp 12 | 13 | class BallGameApp { 14 | public static void run() 15 | { 16 | BallGame.run(); 17 | } 18 | } 19 | 20 | class BallGame { 21 | 22 | public static void run() 23 | { 24 | playGame(5,100); 25 | } 26 | 27 | public static void playGame(int width, int height) 28 | { 29 | int starPos = 0; 30 | boolean starPosFlag = true; 31 | for (int i = 1; i <= height ; i++){ 32 | 33 | System.out.print('!'); 34 | fillWithSpace(0,starPos); 35 | System.out.print('*'); 36 | fillWithSpace(starPos+1,width); 37 | System.out.println('!'); 38 | starPosFlag = getFlag(starPos, width,starPosFlag); 39 | starPos = getStarPosition(starPos, width,starPosFlag); 40 | 41 | } 42 | } 43 | 44 | public static int getStarPosition(int starPos,int width,boolean starPosFlag){ 45 | if (width == 1) 46 | return starPos; 47 | 48 | if (starPosFlag) 49 | return ++starPos; 50 | 51 | return --starPos; 52 | } 53 | public static boolean getFlag(int starPos, int width, boolean starPosFlag) 54 | { 55 | if (starPos == width -1) 56 | return false; 57 | if (starPos == 0) 58 | return true; 59 | 60 | return starPosFlag; 61 | } 62 | 63 | public static void fillWithSpace(int start,int end) 64 | { 65 | 66 | for (int i = start; i < end; i++){ 67 | System.out.print(' '); 68 | } 69 | } 70 | 71 | } 72 | ``` 73 | -------------------------------------------------------------------------------- /JavaHomeworks/JavaHomeworks003.md: -------------------------------------------------------------------------------- 1 | ```java 2 | package csd; 3 | 4 | class App { 5 | public static void main(String [] args) 6 | { 7 | 8 | //... 9 | 10 | } 11 | } 12 | 13 | class TripletPrimeNumTest{ 14 | public static void run() 15 | { 16 | java.util.Scanner kb = new java.util.Scanner(System.in); 17 | System.out.println("üç basamaklı TripletPrimeNumbers:"); 18 | for(int i = 100; i < 1000; i++) { 19 | if(TripletPrimeNum.isTripletPrime(i)){ 20 | System.out.printf("%d -- %d%n",i,NumberUtil.getReverse(i)); 21 | 22 | } 23 | 24 | } 25 | } 26 | } 27 | 28 | class TripletPrimeNum{ 29 | public static boolean isTripletPrime(int ival) 30 | { 31 | int reverseVal = NumberUtil.getReverse(ival); 32 | if (reverseVal > ival) 33 | if (NumberUtil.isPrime(ival) && NumberUtil.isPrime(reverseVal)) 34 | if (NumberUtil.isPrime(ival % 100) && NumberUtil.isPrime(reverseVal % 100)) 35 | if (NumberUtil.isPrime(ival / 10) && NumberUtil.isPrime(reverseVal / 10)) 36 | return true; 37 | return false; 38 | } 39 | } 40 | 41 | class XPrimalityTest{ 42 | public static void run() 43 | { 44 | java.util.Scanner kb = new java.util.Scanner(System.in); 45 | int ival; 46 | for(;;){ 47 | System.out.printf("Bir tam sayı giriniz:"); 48 | ival = Integer.parseInt(kb.nextLine()); 49 | if(xPrimality.xPrime(ival)) 50 | System.out.printf("(%d) sayısı Xprime%n",ival); 51 | else 52 | System.out.printf("(%d) sayısı Xprime değildir%n",ival); 53 | System.out.println("Çıkış için negatif bir sayı giriniz"); 54 | if(ival < 0) 55 | break; 56 | 57 | } 58 | System.out.println("Çıkış yaptınız...."); 59 | } 60 | } 61 | 62 | class xPrimality { 63 | public static boolean xPrime(int ival) 64 | { 65 | 66 | while ( NumberUtil.getDigitsCount(ival) > 2){ 67 | if (!NumberUtil.isPrime(ival)) 68 | return false; 69 | ival = NumberUtil.getDigitsSum(ival); 70 | } 71 | 72 | return NumberUtil.isPrime(ival); 73 | } 74 | } 75 | ``` 76 | -------------------------------------------------------------------------------- /JavaHomeworks/JavaHomeworks004.md: -------------------------------------------------------------------------------- 1 | ```java 2 | package csd; 3 | /* 4 | Bütün Code bileşenlerine ve iyileştirmelerine README dosyasından ulaşabilirsiniz... 5 | */ 6 | class App { 7 | public static void main(String [] args) 8 | { 9 | 10 | Homework4.run(); 11 | 12 | 13 | } 14 | } 15 | 16 | class Homework4 { 17 | public static void run() 18 | { 19 | java.util.Scanner kb = new java.util.Scanner(System.in); 20 | System.out.printf("Bir tamsayı giriniz:"); 21 | int ival = Integer.parseInt(kb.nextLine()); 22 | int sum = ival, 23 | min = ival, 24 | max = ival; 25 | int sumCounter = 1; 26 | for (;;){ 27 | System.out.printf("Yeni bir deger girmek istiyor musunuz?%n" 28 | + "[Evet için 1(bir) Hayır için 0(sıfır)%n" 29 | + "değerlerinden birini giriniz]:"); 30 | ival = Integer.parseInt(kb.nextLine()); 31 | if (ival == 0) 32 | break; 33 | System.out.printf("Yeni bir değer giriniz:"); 34 | ival = Integer.parseInt(kb.nextLine()); 35 | if (ival > 100 || ival < 0){ 36 | System.out.printf("Geçersiz bir değer girdiniz%n" 37 | + "Girilen değer [0-100] aralığında olmalıdır..."); 38 | continue; 39 | } 40 | min = Math.min(ival, min); 41 | max = Math.max(ival, max); 42 | sum += ival; 43 | sumCounter++; 44 | 45 | } 46 | System.out.println("Çıkış yaptınız..."); 47 | System.out.printf("Toplam %d değer girildi%n",sumCounter); 48 | System.out.printf("Max = %d%n",max); 49 | System.out.printf("Min = %d%n",min); 50 | System.out.printf("Ortalama = %f%n",NumberUtil.getAverage(sum,sumCounter)); 51 | 52 | } 53 | 54 | } 55 | ``` 56 | -------------------------------------------------------------------------------- /JavaHomeworks/JavaHomeworks005.md: -------------------------------------------------------------------------------- 1 | ```java 2 | package csd; 3 | /* 4 | Programın diğer bileşenlerine README dosyasından ulaşabilirsiniz... 5 | */ 6 | class App { 7 | public static void main(String [] args) 8 | { 9 | 10 | //... 11 | 12 | 13 | } 14 | } 15 | 16 | class IsPerfectTest{ 17 | public static void run() 18 | { 19 | System.out.printf("Dört basamağa perfect sayıları gösteriliyor...%n"); 20 | for (int i = 1; i < 9999;i++){ 21 | if (NumberUtil.isPerfect(i)) 22 | System.out.printf("Perfect number: = %4d Çarpanları toplamı: %4d%n",i,NumberUtil.sumFactors(i)); 23 | } 24 | } 25 | } 26 | class IsFriendNumTest{ 27 | public static void run() 28 | { 29 | System.out.printf("Dört basamaklı arkadaş sayılar gösteriliyor.%n"); 30 | for(int i = 1000; i < 9999; i++) 31 | for (int j = i + 1; j < 9999; j++) 32 | if (NumberUtil.isFriendNum(i, j) && !NumberUtil.isPrime(i) && !NumberUtil.isPrime(j)){ 33 | System.out.printf("%d %d ",i,j); 34 | System.out.printf("Çarpanları toplamı: %d ve %d%n",NumberUtil.sumFactors(i),NumberUtil.sumFactors(j)); 35 | int k = 2000000000; 36 | while (k-- > 0) 37 | ; 38 | } 39 | } 40 | } 41 | class SumFactorsTest{ 42 | public static void run() 43 | { 44 | java.util.Scanner kb = new java.util.Scanner(System.in); 45 | System.out.printf("Bir tamsayı giriniz:"); 46 | int ival = Integer.parseInt(kb.nextLine()); 47 | for(;;){ 48 | if (ival < 0){ 49 | System.out.println("Negatif bir sayı girdiniz. Program sonlandırıldı."); 50 | break; 51 | } 52 | System.out.printf("%d sayısının çarpanları toplamı = %d%n",ival,NumberUtil.sumFactors(ival)); 53 | System.out.printf("%d sayısının çarpanları:",ival); 54 | NumberUtil.displayFactors(ival); 55 | System.out.printf("Yeni bir değer giriniz:"); 56 | ival = Integer.parseInt(kb.nextLine()); 57 | 58 | } 59 | } 60 | } 61 | 62 | 63 | class NumberUtil { 64 | 65 | public static double getAverage(int ival,int counter) 66 | { 67 | return (double)ival / counter ; 68 | } 69 | public static void displayE() 70 | { 71 | java.util.Scanner kb = new java.util.Scanner(System.in); 72 | System.out.println("Bir tamsayı giriniz:"); 73 | int ival = Integer.parseInt(kb.nextLine()); 74 | 75 | System.out.printf("e sayısı = %.15f",getE(ival)); 76 | } 77 | public static double getE(int n) 78 | { 79 | double sum = 0; 80 | while (n > -1){ 81 | sum += 1. / getFactorial(n); 82 | n--; 83 | } 84 | return sum; 85 | } 86 | public static long getFactorial(int n) 87 | { 88 | long result = 1; 89 | if (n == 0 || n == 1) 90 | return 1; 91 | for (int i = 2; i <= n; i++){ 92 | result *= i; 93 | } 94 | return result; 95 | } 96 | public static int sumFactors(int ival) 97 | { 98 | int sum = 0; 99 | if (ival < 0 || ival == 1) 100 | return ival < 0 ? -1: 1; 101 | 102 | for(int i = 1; i < ival; i++){ 103 | if (ival % i == 0) 104 | sum += i; 105 | } 106 | return sum; 107 | } 108 | public static void displayFactors(int ival) 109 | { 110 | if (ival == 1) 111 | System.out.print(1); 112 | for (int i = 1; i < ival ; i++){ 113 | if(ival % i == 0) 114 | System.out.printf("%d ",i); 115 | } 116 | System.out.printf("%n"); 117 | } 118 | public static boolean isFriendNum(int ival1,int ival2) 119 | { 120 | return sumFactors(ival1) == sumFactors(ival2) ? true : false; 121 | 122 | } 123 | public static boolean isPerfect(int ival) 124 | { 125 | return ival == sumFactors(ival); 126 | } 127 | } 128 | 129 | 130 | ``` 131 | -------------------------------------------------------------------------------- /JavaLesson012/org/csystem/app/App.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Diziye ilkdeğer olarak verilen elemanlar sabit ifadesi olmak zorunda değildir 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.app; 5 | 6 | import org.csystem.app.samples.dateapp.DateUtilApp; 7 | 8 | import java.util.Scanner; 9 | 10 | import static org.csystem.app.samples.dateapp.DateUtilApp.*; 11 | 12 | class App { 13 | public static void main(String [] args) 14 | { 15 | Scanner kb = new Scanner(System.in); 16 | DateUtilApp.run(); 17 | } 18 | } 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /JavaLesson012/org/csystem/app/samples/dateapp/DateUtil.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.dateapp; 2 | 3 | public class DateUtil { 4 | public static int getDayOfWeek(int day, int month, int year) 5 | { 6 | int dayOfYear; 7 | 8 | dayOfYear = getDayOfYear(day, month, year); 9 | 10 | if (dayOfYear == -1 || year < 1900) 11 | return -1; 12 | 13 | for (int y = 1900; y < year; ++y) 14 | dayOfYear += isLeapYear(y) ? 366 : 365; 15 | 16 | return dayOfYear % 7; 17 | } 18 | 19 | public static int getDayOfYear(int day, int month, int year) 20 | { 21 | if (!isValidDate(day, month, year)) 22 | return -1; 23 | 24 | int dayOfYear = day; 25 | 26 | switch (month - 1) { 27 | case 11: 28 | dayOfYear += 30; 29 | case 10: 30 | dayOfYear += 31; 31 | case 9: 32 | dayOfYear += 30; 33 | case 8: 34 | dayOfYear += 31; 35 | case 7: 36 | dayOfYear += 31; 37 | case 6: 38 | dayOfYear += 30; 39 | case 5: 40 | dayOfYear += 31; 41 | case 4: 42 | dayOfYear += 30; 43 | case 3: 44 | dayOfYear += 31; 45 | case 2: 46 | dayOfYear += isLeapYear(year) ? 29 : 28; 47 | case 1: 48 | dayOfYear += 31; 49 | } 50 | 51 | return dayOfYear; 52 | } 53 | 54 | public static boolean isValidDate(int day, int month, int year) 55 | { 56 | if (day < 1 || day > 31 || month < 1 || month > 12) 57 | return false; 58 | 59 | int days = 31; 60 | 61 | switch (month) { 62 | case 4: 63 | case 6: 64 | case 9: 65 | case 11: 66 | days = 30; 67 | break; 68 | case 2: 69 | days = isLeapYear(year) ? 29 : 28; 70 | } 71 | 72 | return day <= days; 73 | } 74 | 75 | public static boolean isLeapYear(int year) 76 | { 77 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /JavaLesson012/org/csystem/app/samples/dateapp/DateUtilApp.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Sınıf çalışması: Parametresi ile aldığı int türden gün, ay ve yıl bilgilerine ilişkin tarihin aşağıdaki 3 | açıklamalara göre haftanın hangi gününe geldiğini döndüren getDayOfWeek metodunu yazınız. 4 | Açıklamalar: 5 | - Metot geçersiz bir tarih için -1 değerini döndürecektir. 6 | - Haftanın günü 1.1.1900 ile verilen tarih arasındaki gün sayısının 7 değerine modu alınarak bulunabilir 7 | - 7 değerine mod alındıktan sonra sıfır Pazar, 1 pazartesi, .., 6 cumartesi günü için 8 | elde edilecek değerdir 9 | - 1.1.1900' den önceki tarihler geçersiz kabul edilecektir 10 | ----------------------------------------------------------------------------------------------------------------------*/ 11 | package org.csystem.app.samples.dateapp; 12 | 13 | public class DateUtilApp { 14 | public static void displayTR(int day, int month, int year) 15 | { 16 | int dayOfWeek = DateUtil.getDayOfWeek(day, month, year); 17 | 18 | if (dayOfWeek == -1) { 19 | System.out.println("Geçersiz tarih"); 20 | return; 21 | } 22 | 23 | switch (dayOfWeek) { 24 | case 0: 25 | System.out.printf("%02d/%02d/%04d Pazar%n", day, month, year); 26 | break; 27 | case 1: 28 | System.out.printf("%02d/%02d/%04d Pazartesi%n", day, month, year); 29 | break; 30 | case 2: 31 | System.out.printf("%02d/%02d/%04d Salı%n", day, month, year); 32 | break; 33 | case 3: 34 | System.out.printf("%02d/%02d/%04d Çarşamba%n", day, month, year); 35 | break; 36 | case 4: 37 | System.out.printf("%02d/%02d/%04d Perşembe%n", day, month, year); 38 | break; 39 | case 5: 40 | System.out.printf("%02d/%02d/%04d Cuma%n", day, month, year); 41 | break; 42 | case 6: 43 | System.out.printf("%02d/%02d/%04d Cumartesi%n", day, month, year); 44 | break; 45 | } 46 | 47 | if (dayOfWeek == 0 || dayOfWeek == 6) 48 | System.out.println("Bugün kurs var. Tekrar yaptınız mı?"); 49 | else 50 | System.out.println("Kurs günü yaklaşıyor. Tekrar yapınız!!!!"); 51 | 52 | } 53 | 54 | public static void run() 55 | { 56 | java.util.Scanner kb = new java.util.Scanner(System.in); 57 | 58 | for (;;) { 59 | System.out.print("Gün bilgisini giriniz:"); 60 | int day = Integer.parseInt(kb.nextLine()); 61 | 62 | if (day == 0) 63 | break; 64 | 65 | System.out.print("Ay bilgisini giriniz:"); 66 | int month = Integer.parseInt(kb.nextLine()); 67 | 68 | System.out.print("Yıl bilgisini giriniz:"); 69 | int year = Integer.parseInt(kb.nextLine()); 70 | 71 | displayTR(day, month, year); 72 | } 73 | 74 | } 75 | } 76 | 77 | -------------------------------------------------------------------------------- /JavaLesson012/org/csystem/math/Complex.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Complex sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math; 5 | 6 | public class Complex { 7 | public static Complex add(double re1, double im1, double re2, double im2) //İleride gizlenecek 8 | { 9 | Complex z = new Complex(); 10 | 11 | z.re = re1 + re2; 12 | z.im = im1 + im2; 13 | 14 | return z; 15 | } 16 | 17 | public static Complex subtract(double re1, double im1, double re2, double im2) //İleride gizlenecek 18 | { 19 | return add(re1, im1, -re2, -im2); 20 | } 21 | 22 | public double re; 23 | public double im; 24 | 25 | public Complex() 26 | { 27 | 28 | } 29 | 30 | public Complex(int a) 31 | { 32 | re = a; 33 | } 34 | 35 | public Complex(int a, int b) 36 | { 37 | re = a; 38 | im = b; 39 | } 40 | 41 | public Complex getConjugate() 42 | { 43 | Complex z = new Complex(); 44 | 45 | z.re = re; 46 | z.im = -im; 47 | 48 | return z; 49 | } 50 | 51 | public double getNorm() 52 | { 53 | return Math.sqrt(re * re + im * im); 54 | } 55 | 56 | //add 57 | public static Complex add(double a, Complex z) 58 | { 59 | return add(a, 0, z.re, z.im); 60 | } 61 | 62 | public Complex add(Complex z) 63 | { 64 | return add(re, im, z.re, z.im); 65 | } 66 | 67 | public Complex add(double a) 68 | { 69 | return add(re, im, a, 0); 70 | } 71 | 72 | //subtract 73 | public static Complex subtract(double a, Complex z) 74 | { 75 | return subtract(a, 0, z.re, z.im); 76 | } 77 | 78 | public Complex subtract(Complex z) 79 | { 80 | return subtract(re, im, z.re, z.im); 81 | } 82 | 83 | public Complex subtract(double a) 84 | { 85 | return subtract(re, im, a, 0); 86 | } 87 | 88 | public void offset(double dxy) 89 | { 90 | offset(dxy, dxy); 91 | } 92 | 93 | public void offset(double dx, double dy) 94 | { 95 | re += dx; 96 | im += dy; 97 | } 98 | 99 | public String toString() 100 | { 101 | return String.format("|%.2f + %.2f * i| = %f", re, im, getNorm()); 102 | } 103 | } 104 | 105 | -------------------------------------------------------------------------------- /JavaLesson012/org/csystem/math/geometry/Point.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Point sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class Point { 9 | public int x; 10 | public int y; 11 | 12 | public Point() 13 | { 14 | } 15 | 16 | public Point(int a) 17 | { 18 | x = a; 19 | } 20 | 21 | public Point(int a, int b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(int a, int b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(int dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(int dx, int dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %d, y: %d}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson012/org/csystem/math/geometry/PointF.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | PointF sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class PointF { 9 | public float x; 10 | public float y; 11 | 12 | public PointF() 13 | { 14 | } 15 | 16 | public PointF(float a) 17 | { 18 | x = a; 19 | } 20 | 21 | public PointF(float a, float b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(float a, float b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(float dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(float dx, float dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %f, y: %f}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson012/org/csystem/util/NumberUtil.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | NumberUtil sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | import static java.lang.Math.abs; 7 | import static java.lang.Math.log10; 8 | import static java.lang.Math.sqrt; 9 | 10 | public class NumberUtil { 11 | public static int factorial(int n) 12 | { 13 | if (n < 0) 14 | return -1; 15 | 16 | int result = 1; 17 | 18 | for (int i = 2; i <= n; ++i) 19 | result *= i; 20 | 21 | return result; 22 | } 23 | 24 | public static int getDigitsCount(long val) 25 | { 26 | return val == 0 ? 1 : (int)log10(abs(val)) + 1; 27 | } 28 | 29 | 30 | public static int getDigitsPowSum(int val) 31 | { 32 | int count = getDigitsCount(val); 33 | 34 | int sum = 0; 35 | 36 | while (val != 0) { 37 | sum += pow(val % 10, count); 38 | val /= 10; 39 | } 40 | 41 | return sum; 42 | } 43 | 44 | public static int getDigitsSum(int val) 45 | { 46 | val = abs(val); 47 | int sum = 0; 48 | 49 | while (val != 0) { 50 | sum += val % 10; 51 | val /= 10; 52 | } 53 | 54 | return sum; 55 | } 56 | 57 | public static int getFibonacciNumber(int n) 58 | { 59 | if (n <= 0) 60 | return -1; 61 | 62 | if (n <= 2) 63 | return n - 1; 64 | 65 | int prev1 = 1, prev2 = 0, val = 0; 66 | 67 | for (int i = 2; i < n; ++i) { 68 | val = prev1 + prev2; 69 | prev2 = prev1; 70 | prev1 = val; 71 | } 72 | 73 | return val; 74 | } 75 | 76 | public static int getNextFibonacciNumber(int val) 77 | { 78 | if (val < 0) 79 | return 0; 80 | 81 | int prev1 = 1, prev2 = 0, result; 82 | 83 | for (;;) { 84 | result = prev1 + prev2; 85 | 86 | if (result > val) 87 | return result; 88 | 89 | prev2 = prev1; 90 | prev1 = result; 91 | } 92 | } 93 | 94 | public static int getPrime(int n) 95 | { 96 | if (n <= 0) 97 | return -1; 98 | 99 | int count = 0; 100 | int val = 2; 101 | 102 | for (int i = 2; count < n; ++i) 103 | if (isPrime(i)) { 104 | ++count; 105 | val = i; 106 | } 107 | 108 | return val; 109 | } 110 | 111 | public static int getReverse(int val) 112 | { 113 | int reverse = 0; 114 | 115 | while (val != 0) { 116 | reverse = reverse * 10 + val % 10; 117 | val /= 10; 118 | } 119 | 120 | return reverse; 121 | } 122 | 123 | public static boolean isArmstrong(int val) 124 | { 125 | if (val < 0) 126 | return false; 127 | 128 | return getDigitsPowSum(val) == val; 129 | } 130 | 131 | public static boolean isEven(int val) 132 | { 133 | return val % 2 == 0; 134 | } 135 | 136 | 137 | public static boolean isOdd(int val) 138 | { 139 | return !isEven(val); 140 | } 141 | 142 | public static boolean isPrime(int val) 143 | { 144 | if (val <= 1) 145 | return false; 146 | 147 | if (val % 2 == 0) 148 | return val == 2; 149 | 150 | if (val % 3 == 0) 151 | return val == 3; 152 | 153 | if (val % 5 == 0) 154 | return val == 5; 155 | 156 | if (val % 7 == 0) 157 | return val == 7; 158 | 159 | int sqrtVal = (int)sqrt(val); 160 | 161 | for (int i = 11; i <= sqrtVal; i += 2) 162 | if (val % i == 0) 163 | return false; 164 | 165 | return true; 166 | } 167 | 168 | public static int max(int a, int b, int c) 169 | { 170 | return Math.max(Math.max(a, b), c); 171 | } 172 | 173 | public static int min(int a, int b, int c) 174 | { 175 | return Math.min(Math.min(a, b), c); 176 | } 177 | 178 | public static int pow(int a, int b) 179 | { 180 | if (b <= 0) 181 | return 1; 182 | 183 | int result = 1; 184 | 185 | while (b-- > 0) 186 | result *= a; 187 | 188 | return result; 189 | } 190 | } -------------------------------------------------------------------------------- /JavaLesson012/org/csystem/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | StringUtil sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | import static java.lang.Character.isLetter; 7 | import static java.lang.Character.isWhitespace; 8 | import static java.lang.Character.toLowerCase; 9 | import static java.lang.Character.toUpperCase; 10 | 11 | import java.util.Random; 12 | 13 | public class StringUtil { 14 | public static String capitalize(String s) 15 | { 16 | if (s.isEmpty()) 17 | return s; 18 | 19 | return toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase(); 20 | } 21 | 22 | public static int countString(String s1, String s2) 23 | { 24 | int count = 0; 25 | 26 | for (int i = -1; (i = s1.indexOf(s2, i + 1)) != -1; ++count) 27 | ; 28 | 29 | return count; 30 | } 31 | 32 | public static String getLetters(String s) 33 | { 34 | String str = ""; 35 | int length = s.length(); 36 | 37 | for (int i = 0; i < length; ++i) { 38 | char ch = s.charAt(i); 39 | 40 | if (isLetter(ch)) 41 | str += ch; 42 | } 43 | 44 | return str; 45 | } 46 | 47 | public static String getLongestPalindrome(String s) 48 | { 49 | String text = ""; 50 | 51 | int endIndex = s.length(); 52 | 53 | while (endIndex != 0) { 54 | int beginIndex = 0; 55 | 56 | while (beginIndex != endIndex) { 57 | String str = s.substring(beginIndex++, endIndex); 58 | 59 | if (str.length() > 1 && StringUtil.isPalindrome(str) && str.length() > text.length()) 60 | text = str; 61 | } 62 | 63 | --endIndex; 64 | } 65 | 66 | return text; 67 | } 68 | 69 | public static String getRandomText(Random r, int n, String alphabet) 70 | { 71 | String str = ""; 72 | 73 | for (int i = 0; i < n; ++i) { 74 | char ch = alphabet.charAt(r.nextInt(alphabet.length())); 75 | 76 | if (r.nextBoolean()) 77 | ch = toUpperCase(ch); 78 | 79 | str += ch; 80 | } 81 | 82 | return str; 83 | } 84 | 85 | public static String getRandomTextEN(int n) 86 | { 87 | return getRandomTextEN(new Random(), n); 88 | } 89 | 90 | public static String getRandomTextEN(Random r, int n) 91 | { 92 | return getRandomText(r, n, "abcdefghijklmnopqrstuwxvyz"); 93 | } 94 | 95 | public static String getRandomTextTR(int n) 96 | { 97 | return getRandomTextTR(new Random(), n); 98 | } 99 | 100 | public static String getRandomTextTR(Random r, int n) 101 | { 102 | return getRandomText(r, n, "abcçdefgğhıijklmnoöprsştuüvyz"); 103 | } 104 | 105 | public static boolean isPalindrome(String s) 106 | { 107 | int left = 0; 108 | int right = s.length() - 1; 109 | 110 | while (left < right) { 111 | char chLeft = toLowerCase(s.charAt(left)); 112 | 113 | if (!isLetter(chLeft)) { 114 | ++left; 115 | continue; 116 | } 117 | 118 | char chRight = toLowerCase(s.charAt(right)); 119 | 120 | if (!isLetter(chRight)) { 121 | --right; 122 | continue; 123 | } 124 | 125 | if (chLeft != chRight) 126 | return false; 127 | 128 | ++left; 129 | --right; 130 | } 131 | 132 | return true; 133 | } 134 | 135 | public static boolean isPangram(String text, String alphabet) 136 | { 137 | int length = alphabet.length(); 138 | 139 | for (int i = 0; i < length; ++i) 140 | if (!text.contains(alphabet.charAt(i) + "")) 141 | return false; 142 | 143 | return true; 144 | } 145 | 146 | public static boolean isPangramEN(String text) 147 | { 148 | return isPangram(text.toLowerCase(), "abcdefghijklmnopqrstuwxvyz"); 149 | } 150 | 151 | public static boolean isPangramTR(String text) 152 | { 153 | return isPangram(text.toLowerCase(), "abcçdefgğhıijklmnoöprsştuüvyz"); 154 | } 155 | 156 | public static String padLeft(String s, int length) 157 | { 158 | return padLeft(s, length, ' '); 159 | } 160 | 161 | public static String padLeft(String s, int length, char ch) 162 | { 163 | length -= s.length(); 164 | 165 | if (length <= 0) 166 | return s; 167 | 168 | return repeat(length, ch) + s; 169 | } 170 | 171 | public static String padRight(String s, int length) 172 | { 173 | return padRight(s, length, ' '); 174 | } 175 | 176 | public static String padRight(String s, int length, char ch) 177 | { 178 | length -= s.length(); 179 | 180 | if (length <= 0) 181 | return s; 182 | 183 | return s + repeat(length, ch); 184 | } 185 | 186 | public static String removeWhitespaces(String s) 187 | { 188 | int length = s.length(); 189 | String str = ""; 190 | 191 | for (int i = 0; i < length; ++i) { 192 | char ch = s.charAt(i); 193 | 194 | if (!isWhitespace(ch)) 195 | str += ch; 196 | } 197 | 198 | return str; 199 | } 200 | 201 | public static String repeat(int n, char ch) 202 | { 203 | String s = ""; 204 | 205 | while (n-- > 0) 206 | s += ch; 207 | 208 | return s; 209 | } 210 | 211 | public static String reverse(String str) 212 | { 213 | String revStr = ""; 214 | 215 | for (int i = str.length() - 1; i >= 0; --i) 216 | revStr += str.charAt(i); 217 | 218 | return revStr; 219 | } 220 | 221 | public static String trimLeft(String s) 222 | { 223 | int i; 224 | int len = s.length(); 225 | 226 | for (i = 0; i < len && isWhitespace(s.charAt(i)); ++i) 227 | ; 228 | 229 | return s.substring(i); 230 | } 231 | 232 | public static String trimRight(String s) 233 | { 234 | int i; 235 | 236 | for (i = s.length() - 1; i >= 0 && isWhitespace(s.charAt(i)); --i) 237 | ; 238 | 239 | return s.substring(0, i + 1); 240 | } 241 | } 242 | -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/app/App.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Sınıf çalışması: Parametresi ile aldığı en fazla üç basamaklı bir sayının Türkçe olarak okunuşunu döndüren 3 | numberToText3Digits metodunu yazınız ve test ediniz. Metot 3 basamaktan daha büyü sayılar için kontrol 4 | yapmayacaktır 5 | ----------------------------------------------------------------------------------------------------------------------*/ 6 | package org.csystem.app; 7 | 8 | import org.csystem.util.NumberUtil; 9 | 10 | import java.util.Random; 11 | import java.util.Scanner; 12 | 13 | class App { 14 | public static void main(String [] args) 15 | { 16 | NumberToText3DigitsTest.run(); 17 | } 18 | } 19 | 20 | class NumberToText3DigitsTest { 21 | public static void run() 22 | { 23 | Scanner kb = new Scanner(System.in); 24 | Random r = new Random(); 25 | 26 | for (;;) { 27 | System.out.print("Bir sayı giriniz:"); 28 | int n = Integer.parseInt(kb.nextLine()); 29 | 30 | if (n <= 0) 31 | break; 32 | 33 | for (int i = 0; i < n; ++i) { 34 | int val = r.nextInt(1000 + 999) - 999; 35 | System.out.printf("%d:%s%n", val, NumberUtil.numberToText3DigitsTR(val)); 36 | } 37 | System.out.println("*********************************"); 38 | } 39 | 40 | System.out.println("Tekrar yapıyor musunuz?"); 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/app/samples/dateapp/DateUtil.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.dateapp; 2 | 3 | public class DateUtil { 4 | public static int [] daysOfMonths = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 5 | 6 | public static int getDayOfWeek(int day, int month, int year) 7 | { 8 | int dayOfYear; 9 | 10 | dayOfYear = getDayOfYear(day, month, year); 11 | 12 | if (dayOfYear == -1 || year < 1900) 13 | return -1; 14 | 15 | for (int y = 1900; y < year; ++y) 16 | dayOfYear += isLeapYear(y) ? 366 : 365; 17 | 18 | return dayOfYear % 7; 19 | } 20 | 21 | public static int getDayOfYear(int day, int month, int year) 22 | { 23 | if (!isValidDate(day, month, year)) 24 | return -1; 25 | 26 | int dayOfYear = day; 27 | 28 | for (int m = month - 1; m >= 1; --m) 29 | dayOfYear += daysOfMonths[m]; 30 | 31 | if (month > 2 && isLeapYear(year)) 32 | ++dayOfYear; 33 | 34 | return dayOfYear; 35 | } 36 | 37 | public static boolean isValidDate(int day, int month, int year) 38 | { 39 | if (day < 1 || day > 31 || month < 1 || month > 12) 40 | return false; 41 | 42 | int days = month == 2 && isLeapYear(year) ? 29 : daysOfMonths[month]; 43 | 44 | return day <= days; 45 | } 46 | 47 | public static boolean isLeapYear(int year) 48 | { 49 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/app/samples/dateapp/DateUtilApp.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Sınıf çalışması: Parametresi ile aldığı int türden gün, ay ve yıl bilgilerine ilişkin tarihin aşağıdaki 3 | açıklamalara göre haftanın hangi gününe geldiğini döndüren getDayOfWeek metodunu yazınız. 4 | Açıklamalar: 5 | - Metot geçersiz bir tarih için -1 değerini döndürecektir. 6 | - Haftanın günü 1.1.1900 ile verilen tarih arasındaki gün sayısının 7 değerine modu alınarak bulunabilir 7 | - 7 değerine mod alındıktan sonra sıfır Pazar, 1 pazartesi, .., 6 cumartesi günü için 8 | elde edilecek değerdir 9 | - 1.1.1900' den önceki tarihler geçersiz kabul edilecektir 10 | ----------------------------------------------------------------------------------------------------------------------*/ 11 | package org.csystem.app.samples.dateapp; 12 | 13 | public class DateUtilApp { 14 | public static String [] daysOfWeek = {"Pazar", "Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma", "Cumartesi"}; 15 | public static String [] monthsTR = {"", "Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", 16 | "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık"}; 17 | 18 | public static String [] monthsEN = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 19 | 20 | public static void displayTR(int day, int month, int year) 21 | { 22 | int dayOfWeek = DateUtil.getDayOfWeek(day, month, year); 23 | 24 | if (dayOfWeek == -1) { 25 | System.out.println("Geçersiz tarih"); 26 | return; 27 | } 28 | 29 | System.out.printf("%02d %s %04d %s%n", day, monthsTR[month], year, daysOfWeek[dayOfWeek]); 30 | 31 | if (dayOfWeek == 0 || dayOfWeek == 6) 32 | System.out.println("Bugün kurs var. Tekrar yaptınız mı?"); 33 | else 34 | System.out.println("Kurs günü yaklaşıyor. Tekrar yapınız!!!!"); 35 | } 36 | 37 | public static void run() 38 | { 39 | java.util.Scanner kb = new java.util.Scanner(System.in); 40 | 41 | for (;;) { 42 | System.out.print("Gün bilgisini giriniz:"); 43 | int day = Integer.parseInt(kb.nextLine()); 44 | 45 | if (day == 0) 46 | break; 47 | 48 | System.out.print("Ay bilgisini giriniz:"); 49 | int month = Integer.parseInt(kb.nextLine()); 50 | 51 | System.out.print("Yıl bilgisini giriniz:"); 52 | int year = Integer.parseInt(kb.nextLine()); 53 | 54 | displayTR(day, month, year); 55 | } 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/app/samples/lottary/Lottary.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import java.util.Random; 4 | 5 | public class Lottary { 6 | public Random random; 7 | 8 | public boolean [] getFlags() 9 | { 10 | boolean [] flags = new boolean[50]; 11 | int val; 12 | 13 | for (int i = 0; i < 6; ++i) { 14 | for (;;) { 15 | val = random.nextInt(49) + 1; 16 | if (!flags[val]) 17 | break; 18 | } 19 | flags[val] = true; 20 | } 21 | 22 | return flags; 23 | } 24 | 25 | public int [] getNumbers(boolean [] flags) 26 | { 27 | int [] a = new int[6]; 28 | 29 | int index = 0; 30 | 31 | for (int i = 1; i < 50; ++i) 32 | if (flags[i]) 33 | a[index++] = i; 34 | 35 | return a; 36 | } 37 | 38 | public Lottary() 39 | { 40 | random = new Random(); 41 | } 42 | 43 | public int [] getNumbers() 44 | { 45 | return getNumbers(getFlags()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/app/samples/lottary/LottaryApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import org.csystem.util.ArrayUtil; 4 | 5 | import java.util.Scanner; 6 | 7 | public class LottaryApp { 8 | public static void run() 9 | { 10 | Lottary lottary = new Lottary(); 11 | Scanner kb = new Scanner(System.in); 12 | 13 | for (;;) { 14 | System.out.print("Kaç kupon oynamak istersiniz?"); 15 | int n = Integer.parseInt(kb.nextLine()); 16 | 17 | if (n <= 0) 18 | break; 19 | 20 | for (int i = 0; i < n; ++i) 21 | ArrayUtil.display(2, lottary.getNumbers()); 22 | 23 | System.out.println("///////////////////"); 24 | } 25 | 26 | System.out.println("Tekrar yapıyor musunuz?"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/math/Complex.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Complex sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math; 5 | 6 | public class Complex { 7 | public static Complex add(double re1, double im1, double re2, double im2) //İleride gizlenecek 8 | { 9 | Complex z = new Complex(); 10 | 11 | z.re = re1 + re2; 12 | z.im = im1 + im2; 13 | 14 | return z; 15 | } 16 | 17 | public static Complex subtract(double re1, double im1, double re2, double im2) //İleride gizlenecek 18 | { 19 | return add(re1, im1, -re2, -im2); 20 | } 21 | 22 | public double re; 23 | public double im; 24 | 25 | public Complex() 26 | { 27 | 28 | } 29 | 30 | public Complex(int a) 31 | { 32 | re = a; 33 | } 34 | 35 | public Complex(int a, int b) 36 | { 37 | re = a; 38 | im = b; 39 | } 40 | 41 | public Complex getConjugate() 42 | { 43 | Complex z = new Complex(); 44 | 45 | z.re = re; 46 | z.im = -im; 47 | 48 | return z; 49 | } 50 | 51 | public double getNorm() 52 | { 53 | return Math.sqrt(re * re + im * im); 54 | } 55 | 56 | //add 57 | public static Complex add(double a, Complex z) 58 | { 59 | return add(a, 0, z.re, z.im); 60 | } 61 | 62 | public Complex add(Complex z) 63 | { 64 | return add(re, im, z.re, z.im); 65 | } 66 | 67 | public Complex add(double a) 68 | { 69 | return add(re, im, a, 0); 70 | } 71 | 72 | //subtract 73 | public static Complex subtract(double a, Complex z) 74 | { 75 | return subtract(a, 0, z.re, z.im); 76 | } 77 | 78 | public Complex subtract(Complex z) 79 | { 80 | return subtract(re, im, z.re, z.im); 81 | } 82 | 83 | public Complex subtract(double a) 84 | { 85 | return subtract(re, im, a, 0); 86 | } 87 | 88 | public void offset(double dxy) 89 | { 90 | offset(dxy, dxy); 91 | } 92 | 93 | public void offset(double dx, double dy) 94 | { 95 | re += dx; 96 | im += dy; 97 | } 98 | 99 | public String toString() 100 | { 101 | return String.format("|%.2f + %.2f * i| = %f", re, im, getNorm()); 102 | } 103 | } 104 | 105 | -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/math/geometry/Point.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Point sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class Point { 9 | public int x; 10 | public int y; 11 | 12 | public Point() 13 | { 14 | } 15 | 16 | public Point(int a) 17 | { 18 | x = a; 19 | } 20 | 21 | public Point(int a, int b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(int a, int b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(int dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(int dx, int dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %d, y: %d}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/math/geometry/PointF.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | PointF sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class PointF { 9 | public float x; 10 | public float y; 11 | 12 | public PointF() 13 | { 14 | } 15 | 16 | public PointF(float a) 17 | { 18 | x = a; 19 | } 20 | 21 | public PointF(float a, float b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(float a, float b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(float dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(float dx, float dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %f, y: %f}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/util/NumberUtil.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | NumberUtil sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | import static java.lang.Math.abs; 7 | import static java.lang.Math.log10; 8 | import static java.lang.Math.sqrt; 9 | 10 | public class NumberUtil { 11 | public static String [] onesTR = {"", "bir", "iki", "üç", "dört", "beş", "altı", "yedi", "sekiz", "dokuz"}; 12 | public static String [] tensTR = {"", "on", "yirmi", "otuz", "kırk", "elli", "altmış", "yetmiş", "seksen", "doksan"}; 13 | 14 | public static String numberToText3DigitsTR(int val) 15 | { 16 | if(val == 0) 17 | return "sıfır"; 18 | 19 | String text = val < 0 ? "eksi" : ""; 20 | int value = Math.abs(val); 21 | int a = value / 100; 22 | int b = value / 10 % 10; 23 | int c = value % 10; 24 | 25 | if (a != 0) { 26 | if (a != 1) 27 | text += onesTR[a]; 28 | text += "yüz"; 29 | } 30 | 31 | if (b != 0) 32 | text += tensTR[b]; 33 | 34 | if (c != 0) 35 | text += onesTR[c]; 36 | 37 | return text; 38 | } 39 | 40 | public static int factorial(int n) 41 | { 42 | if (n < 0) 43 | return -1; 44 | 45 | int result = 1; 46 | 47 | for (int i = 2; i <= n; ++i) 48 | result *= i; 49 | 50 | return result; 51 | } 52 | 53 | public static int[] getDigits(int val) 54 | { 55 | return getDigits((long)val); 56 | } 57 | 58 | public static int[] getDigits(long val) 59 | { 60 | int count = getDigitsCount(val); 61 | int [] digits = new int[count]; 62 | long posVal = Math.abs(val); 63 | 64 | for (int i = count - 1; i >= 0; digits[i] = (int)(posVal % 10), posVal /= 10, --i) 65 | ; 66 | 67 | return digits; 68 | } 69 | 70 | public static int getDigitsCount(long val) 71 | { 72 | return val == 0 ? 1 : (int)log10(abs(val)) + 1; 73 | } 74 | 75 | public static int getDigitsPowSum(int val) 76 | { 77 | int count = getDigitsCount(val); 78 | 79 | int sum = 0; 80 | 81 | while (val != 0) { 82 | sum += pow(val % 10, count); 83 | val /= 10; 84 | } 85 | 86 | return sum; 87 | } 88 | 89 | public static int getDigitsSum(int val) 90 | { 91 | val = abs(val); 92 | int sum = 0; 93 | 94 | while (val != 0) { 95 | sum += val % 10; 96 | val /= 10; 97 | } 98 | 99 | return sum; 100 | } 101 | 102 | public static int getFibonacciNumber(int n) 103 | { 104 | if (n <= 0) 105 | return -1; 106 | 107 | if (n <= 2) 108 | return n - 1; 109 | 110 | int prev1 = 1, prev2 = 0, val = 0; 111 | 112 | for (int i = 2; i < n; ++i) { 113 | val = prev1 + prev2; 114 | prev2 = prev1; 115 | prev1 = val; 116 | } 117 | 118 | return val; 119 | } 120 | 121 | public static int getNextFibonacciNumber(int val) 122 | { 123 | if (val < 0) 124 | return 0; 125 | 126 | int prev1 = 1, prev2 = 0, result; 127 | 128 | for (;;) { 129 | result = prev1 + prev2; 130 | 131 | if (result > val) 132 | return result; 133 | 134 | prev2 = prev1; 135 | prev1 = result; 136 | } 137 | } 138 | 139 | public static int getPrime(int n) 140 | { 141 | if (n <= 0) 142 | return -1; 143 | 144 | int count = 0; 145 | int val = 2; 146 | 147 | for (int i = 2; count < n; ++i) 148 | if (isPrime(i)) { 149 | ++count; 150 | val = i; 151 | } 152 | 153 | return val; 154 | } 155 | 156 | public static int getReverse(int val) 157 | { 158 | int reverse = 0; 159 | 160 | while (val != 0) { 161 | reverse = reverse * 10 + val % 10; 162 | val /= 10; 163 | } 164 | 165 | return reverse; 166 | } 167 | 168 | public static boolean isArmstrong(int val) 169 | { 170 | if (val < 0) 171 | return false; 172 | 173 | return getDigitsPowSum(val) == val; 174 | } 175 | 176 | public static boolean isEven(int val) 177 | { 178 | return val % 2 == 0; 179 | } 180 | 181 | 182 | public static boolean isOdd(int val) 183 | { 184 | return !isEven(val); 185 | } 186 | 187 | public static boolean isPrime(int val) 188 | { 189 | if (val <= 1) 190 | return false; 191 | 192 | if (val % 2 == 0) 193 | return val == 2; 194 | 195 | if (val % 3 == 0) 196 | return val == 3; 197 | 198 | if (val % 5 == 0) 199 | return val == 5; 200 | 201 | if (val % 7 == 0) 202 | return val == 7; 203 | 204 | int sqrtVal = (int)sqrt(val); 205 | 206 | for (int i = 11; i <= sqrtVal; i += 2) 207 | if (val % i == 0) 208 | return false; 209 | 210 | return true; 211 | } 212 | 213 | public static int max(int a, int b, int c) 214 | { 215 | return Math.max(Math.max(a, b), c); 216 | } 217 | 218 | public static int min(int a, int b, int c) 219 | { 220 | return Math.min(Math.min(a, b), c); 221 | } 222 | 223 | public static int pow(int a, int b) 224 | { 225 | if (b <= 0) 226 | return 1; 227 | 228 | int result = 1; 229 | 230 | while (b-- > 0) 231 | result *= a; 232 | 233 | return result; 234 | } 235 | } -------------------------------------------------------------------------------- /JavaLesson013/org/csystem/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | StringUtil sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | import java.util.Random; 7 | 8 | import static java.lang.Character.*; 9 | 10 | public class StringUtil { 11 | public static String capitalize(String s) 12 | { 13 | if (s.isEmpty()) 14 | return s; 15 | 16 | return toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase(); 17 | } 18 | 19 | public static int countString(String s1, String s2) 20 | { 21 | int count = 0; 22 | 23 | for (int i = -1; (i = s1.indexOf(s2, i + 1)) != -1; ++count) 24 | ; 25 | 26 | return count; 27 | } 28 | 29 | public static String getLetters(String s) 30 | { 31 | String str = ""; 32 | int length = s.length(); 33 | 34 | for (int i = 0; i < length; ++i) { 35 | char ch = s.charAt(i); 36 | 37 | if (isLetter(ch)) 38 | str += ch; 39 | } 40 | 41 | return str; 42 | } 43 | 44 | public static String getLongestPalindrome(String s) 45 | { 46 | String text = ""; 47 | 48 | int endIndex = s.length(); 49 | 50 | while (endIndex != 0) { 51 | int beginIndex = 0; 52 | 53 | while (beginIndex != endIndex) { 54 | String str = s.substring(beginIndex++, endIndex); 55 | 56 | if (str.length() > 1 && StringUtil.isPalindrome(str) && str.length() > text.length()) 57 | text = str; 58 | } 59 | 60 | --endIndex; 61 | } 62 | 63 | return text; 64 | } 65 | 66 | public static String getRandomText(Random r, int n, String alphabet) 67 | { 68 | char [] c = new char[n]; 69 | 70 | for (int i = 0; i < n; ++i) { 71 | char ch = alphabet.charAt(r.nextInt(alphabet.length())); 72 | 73 | if (r.nextBoolean()) 74 | ch = toUpperCase(ch); 75 | 76 | c[i] = ch; 77 | } 78 | 79 | return String.valueOf(c); 80 | } 81 | 82 | public static String getRandomTextEN(int n) 83 | { 84 | return getRandomTextEN(new Random(), n); 85 | } 86 | 87 | public static String getRandomTextEN(Random r, int n) 88 | { 89 | return getRandomText(r, n, "abcdefghijklmnopqrstuwxvyz"); 90 | } 91 | 92 | public static String getRandomTextTR(int n) 93 | { 94 | return getRandomTextTR(new Random(), n); 95 | } 96 | 97 | public static String getRandomTextTR(Random r, int n) 98 | { 99 | return getRandomText(r, n, "abcçdefgğhıijklmnoöprsştuüvyz"); 100 | } 101 | 102 | public static boolean isPalindrome(String s) 103 | { 104 | int left = 0; 105 | int right = s.length() - 1; 106 | 107 | while (left < right) { 108 | char chLeft = toLowerCase(s.charAt(left)); 109 | 110 | if (!isLetter(chLeft)) { 111 | ++left; 112 | continue; 113 | } 114 | 115 | char chRight = toLowerCase(s.charAt(right)); 116 | 117 | if (!isLetter(chRight)) { 118 | --right; 119 | continue; 120 | } 121 | 122 | if (chLeft != chRight) 123 | return false; 124 | 125 | ++left; 126 | --right; 127 | } 128 | 129 | return true; 130 | } 131 | 132 | public static boolean isPangram(String text, String alphabet) 133 | { 134 | int length = alphabet.length(); 135 | 136 | for (int i = 0; i < length; ++i) 137 | if (!text.contains(alphabet.charAt(i) + "")) 138 | return false; 139 | 140 | return true; 141 | } 142 | 143 | public static boolean isPangramEN(String text) 144 | { 145 | return isPangram(text.toLowerCase(), "abcdefghijklmnopqrstuwxvyz"); 146 | } 147 | 148 | public static boolean isPangramTR(String text) 149 | { 150 | return isPangram(text.toLowerCase(), "abcçdefgğhıijklmnoöprsştuüvyz"); 151 | } 152 | 153 | public static String padLeft(String s, int length) 154 | { 155 | return padLeft(s, length, ' '); 156 | } 157 | 158 | public static String padLeft(String s, int length, char ch) 159 | { 160 | length -= s.length(); 161 | 162 | if (length <= 0) 163 | return s; 164 | 165 | return repeat(length, ch) + s; 166 | } 167 | 168 | public static String padRight(String s, int length) 169 | { 170 | return padRight(s, length, ' '); 171 | } 172 | 173 | public static String padRight(String s, int length, char ch) 174 | { 175 | length -= s.length(); 176 | 177 | if (length <= 0) 178 | return s; 179 | 180 | return s + repeat(length, ch); 181 | } 182 | 183 | public static String removeWhitespaces(String s) 184 | { 185 | int length = s.length(); 186 | String str = ""; 187 | 188 | for (int i = 0; i < length; ++i) { 189 | char ch = s.charAt(i); 190 | 191 | if (!isWhitespace(ch)) 192 | str += ch; 193 | } 194 | 195 | return str; 196 | } 197 | 198 | public static String repeat(int n, char ch) 199 | { 200 | char [] c = new char[n]; 201 | 202 | while (n-- > 0) 203 | c[n] = ch; 204 | 205 | return String.valueOf(c); 206 | } 207 | 208 | public static String reverse(String str) 209 | { 210 | char [] c = str.toCharArray(); 211 | 212 | ArrayUtil.reverse(c); 213 | 214 | return String.valueOf(c); 215 | } 216 | 217 | public static String trimLeft(String s) 218 | { 219 | int i; 220 | int len = s.length(); 221 | 222 | for (i = 0; i < len && isWhitespace(s.charAt(i)); ++i) 223 | ; 224 | 225 | return s.substring(i); 226 | } 227 | 228 | public static String trimRight(String s) 229 | { 230 | int i; 231 | 232 | for (i = s.length() - 1; i >= 0 && isWhitespace(s.charAt(i)); --i) 233 | ; 234 | 235 | return s.substring(0, i + 1); 236 | } 237 | } 238 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/App.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | for-each döngü deyiminin dizi ile kullanımında döngü değişkenine döngünün her adımında o anki elemanın değeri 3 | atanır. Bu işlem dizinin başından sonuna kadar tekrarlanır 4 | ----------------------------------------------------------------------------------------------------------------------*/ 5 | package org.csystem.app; 6 | 7 | class App { 8 | public static void main(String [] args) 9 | { 10 | int [] a = {1, 2, 3, 4, 5}; 11 | 12 | for (int val : a) 13 | System.out.printf("%d ", val); 14 | 15 | System.out.println(); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/commandpromptapp/CommandPrompt.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.commandpromptapp; 2 | 3 | import org.csystem.util.StringUtil; 4 | 5 | import java.util.Scanner; 6 | 7 | public class CommandPrompt { 8 | public static String [] commandsStr = {"length", "reverse", "upper", "lower", "change", "clear"}; 9 | public String prompt; 10 | public Scanner kb; 11 | 12 | public static String getCommandByText(String text) 13 | { 14 | if (text.length() < 3) 15 | return ""; 16 | 17 | for (int i = 0; i < commandsStr.length; ++i) 18 | if (commandsStr[i].startsWith(text)) 19 | return commandsStr[i]; 20 | 21 | return ""; 22 | } 23 | 24 | public void lengthProc(String [] commandsStr) 25 | { 26 | if (commandsStr.length != 2) { 27 | System.out.println("length bir tane argüman almalıdır"); 28 | return; 29 | } 30 | 31 | System.out.println(commandsStr[1].length()); 32 | } 33 | 34 | public void reverseProc(String [] commandsStr) 35 | { 36 | if (commandsStr.length != 2) { 37 | System.out.println("revere bir tane argüman almalıdır"); 38 | return; 39 | } 40 | 41 | System.out.println(StringUtil.reverse(commandsStr[1])); 42 | } 43 | 44 | public void upperProc(String [] commandsStr) 45 | { 46 | if (commandsStr.length != 2) { 47 | System.out.println("upper bir tane argüman almalıdır"); 48 | return; 49 | } 50 | System.out.println(commandsStr[1].toUpperCase()); 51 | } 52 | 53 | public void lowerProc(String [] commandsStr) 54 | { 55 | if (commandsStr.length != 2) { 56 | System.out.println("lower bir tane argüman almalıdır"); 57 | return; 58 | } 59 | System.out.println(commandsStr[1].toLowerCase()); 60 | } 61 | 62 | public void changeProc(String [] commandsStr) 63 | { 64 | if (commandsStr.length != 2) { 65 | System.out.println("change bir tane argüman almalıdır"); 66 | return; 67 | } 68 | prompt = commandsStr[1]; 69 | } 70 | 71 | public void clearProc(String [] commandsStr) 72 | { 73 | for (int i = 0; i < 30; ++i) 74 | System.out.println(); 75 | } 76 | 77 | public void doWorkForCommand(String [] commandInfoStr) 78 | { 79 | switch (commandInfoStr[0]) { 80 | case "length": 81 | lengthProc(commandInfoStr); 82 | break; 83 | case "reverse": 84 | reverseProc(commandInfoStr); 85 | break; 86 | case "upper": 87 | upperProc(commandInfoStr); 88 | break; 89 | case "lower": 90 | lowerProc(commandInfoStr); 91 | break; 92 | case "change": 93 | changeProc(commandInfoStr); 94 | break; 95 | case "clear": 96 | clearProc(commandInfoStr); 97 | break; 98 | } 99 | } 100 | 101 | public void parseCommand(String [] commandInfoStr) 102 | { 103 | String cmd = getCommandByText(commandInfoStr[0]); 104 | 105 | if (!cmd.isEmpty()) { 106 | commandInfoStr[0] = cmd; 107 | doWorkForCommand(commandInfoStr); 108 | } 109 | else 110 | System.out.println("Geçersiz komut"); 111 | } 112 | 113 | public CommandPrompt(String p) 114 | { 115 | prompt = p; 116 | kb = new Scanner(System.in); 117 | } 118 | 119 | public void run() 120 | { 121 | System.out.println("C ve Sistem Programcıları Derneği"); 122 | System.out.println("Homework-013 sorusuna ilişkin bir iskelet"); 123 | 124 | for (;;) { 125 | System.out.print(prompt + ">"); 126 | String cmd = kb.nextLine().trim(); 127 | 128 | if (cmd.equals("quit")) 129 | break; 130 | 131 | parseCommand(cmd.split("[ \t]+")); 132 | } 133 | 134 | System.out.println("C ve Sistem Programcıları Derneği"); 135 | System.out.println("Tekrar yapıyor musunuz?"); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/commandpromptapp/CommandPromptApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.commandpromptapp; 2 | 3 | public class CommandPromptApp { 4 | public static void run() 5 | { 6 | CommandPrompt commandPrompt = new CommandPrompt("CSD"); 7 | 8 | commandPrompt.run(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/dateapp/DateUtil.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.dateapp; 2 | 3 | public class DateUtil { 4 | public static int [] daysOfMonths = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 5 | 6 | public static int getDayOfWeek(int day, int month, int year) 7 | { 8 | int dayOfYear; 9 | 10 | dayOfYear = getDayOfYear(day, month, year); 11 | 12 | if (dayOfYear == -1 || year < 1900) 13 | return -1; 14 | 15 | for (int y = 1900; y < year; ++y) 16 | dayOfYear += isLeapYear(y) ? 366 : 365; 17 | 18 | return dayOfYear % 7; 19 | } 20 | 21 | public static int getDayOfYear(int day, int month, int year) 22 | { 23 | if (!isValidDate(day, month, year)) 24 | return -1; 25 | 26 | int dayOfYear = day; 27 | 28 | for (int m = month - 1; m >= 1; --m) 29 | dayOfYear += daysOfMonths[m]; 30 | 31 | if (month > 2 && isLeapYear(year)) 32 | ++dayOfYear; 33 | 34 | return dayOfYear; 35 | } 36 | 37 | public static boolean isValidDate(int day, int month, int year) 38 | { 39 | if (day < 1 || day > 31 || month < 1 || month > 12) 40 | return false; 41 | 42 | int days = month == 2 && isLeapYear(year) ? 29 : daysOfMonths[month]; 43 | 44 | return day <= days; 45 | } 46 | 47 | public static boolean isLeapYear(int year) 48 | { 49 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/dateapp/DateUtilApp.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Sınıf çalışması: Parametresi ile aldığı int türden gün, ay ve yıl bilgilerine ilişkin tarihin aşağıdaki 3 | açıklamalara göre haftanın hangi gününe geldiğini döndüren getDayOfWeek metodunu yazınız. 4 | Açıklamalar: 5 | - Metot geçersiz bir tarih için -1 değerini döndürecektir. 6 | - Haftanın günü 1.1.1900 ile verilen tarih arasındaki gün sayısının 7 değerine modu alınarak bulunabilir 7 | - 7 değerine mod alındıktan sonra sıfır Pazar, 1 pazartesi, .., 6 cumartesi günü için 8 | elde edilecek değerdir 9 | - 1.1.1900' den önceki tarihler geçersiz kabul edilecektir 10 | ----------------------------------------------------------------------------------------------------------------------*/ 11 | package org.csystem.app.samples.dateapp; 12 | 13 | public class DateUtilApp { 14 | public static String [] daysOfWeek = {"Pazar", "Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma", "Cumartesi"}; 15 | public static String [] monthsTR = {"", "Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", 16 | "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık"}; 17 | 18 | public static String [] monthsEN = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 19 | 20 | public static void displayTR(int day, int month, int year) 21 | { 22 | int dayOfWeek = DateUtil.getDayOfWeek(day, month, year); 23 | 24 | if (dayOfWeek == -1) { 25 | System.out.println("Geçersiz tarih"); 26 | return; 27 | } 28 | 29 | System.out.printf("%02d %s %04d %s%n", day, monthsTR[month], year, daysOfWeek[dayOfWeek]); 30 | 31 | if (dayOfWeek == 0 || dayOfWeek == 6) 32 | System.out.println("Bugün kurs var. Tekrar yaptınız mı?"); 33 | else 34 | System.out.println("Kurs günü yaklaşıyor. Tekrar yapınız!!!!"); 35 | } 36 | 37 | public static void run() 38 | { 39 | java.util.Scanner kb = new java.util.Scanner(System.in); 40 | 41 | for (;;) { 42 | System.out.print("Gün bilgisini giriniz:"); 43 | int day = Integer.parseInt(kb.nextLine()); 44 | 45 | if (day == 0) 46 | break; 47 | 48 | System.out.print("Ay bilgisini giriniz:"); 49 | int month = Integer.parseInt(kb.nextLine()); 50 | 51 | System.out.print("Yıl bilgisini giriniz:"); 52 | int year = Integer.parseInt(kb.nextLine()); 53 | 54 | displayTR(day, month, year); 55 | } 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/examsimulationapp/ExamSimulation.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.examsimulationapp; 2 | 3 | import org.csystem.util.ArrayUtil; 4 | 5 | import java.util.Random; 6 | import java.util.Scanner; 7 | 8 | public class ExamSimulation { 9 | public String lectureName; 10 | public int [][] grades; 11 | public double [] averages; 12 | public double average; 13 | 14 | public void findAverages() 15 | { 16 | int totalNumberOfStudents = 0; 17 | int totalGrades = 0; 18 | 19 | for (int i = 0; i < grades.length; ++i) { 20 | int total = ArrayUtil.sum(grades[i]); 21 | 22 | averages[i] = (double)total / grades[i].length; 23 | totalGrades += total; 24 | totalNumberOfStudents += grades[i].length; 25 | } 26 | 27 | average = (double)totalGrades / totalNumberOfStudents; 28 | } 29 | 30 | public void fillGrades() 31 | { 32 | Random r = new Random(); 33 | Scanner kb = new Scanner(System.in); 34 | System.out.print("Şube sayısını giriniz:"); 35 | grades = new int[Integer.parseInt(kb.nextLine())][]; 36 | averages = new double[grades.length]; 37 | 38 | for (int i = 0; i < grades.length; ++i) { 39 | System.out.printf("%d. şube öğrenci sayısını giriniz:", i + 1); 40 | int count = Integer.parseInt(kb.nextLine()); 41 | grades[i] = ArrayUtil.getRandomArray(r, count, 0, 101); 42 | } 43 | } 44 | 45 | public ExamSimulation(String name) 46 | { 47 | lectureName = name; 48 | } 49 | 50 | public void run() 51 | { 52 | fillGrades(); 53 | findAverages(); 54 | } 55 | 56 | public void displayGrades() 57 | { 58 | System.out.printf("%s dersi sınav notları:%n", lectureName); 59 | System.out.println("*************************************************************"); 60 | for (int i = 0; i < grades.length; ++i) { 61 | System.out.printf("%d. şube notları:", i + 1); 62 | ArrayUtil.display(3, grades[i]); 63 | } 64 | System.out.println("*************************************************************"); 65 | } 66 | 67 | public void displayStatus() 68 | { 69 | displayGrades(); 70 | 71 | System.out.printf("%s dersi için ortalamalar%n", lectureName); 72 | System.out.println("*************************************************************"); 73 | for (int i = 0; i < averages.length; ++i) 74 | System.out.printf("%d.şube not ortalaması:%f%n", i + 1, averages[i]); 75 | 76 | System.out.printf("Okul ortalaması:%f%n", average); 77 | System.out.println("*************************************************************"); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/examsimulationapp/ExamSimulationApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.examsimulationapp; 2 | 3 | public class ExamSimulationApp { 4 | public static void run() 5 | { 6 | ExamSimulation examSimulation = new ExamSimulation("Matematik"); 7 | 8 | examSimulation.run(); 9 | examSimulation.displayGrades(); 10 | examSimulation.displayStatus(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/lottary/Lottary.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import java.util.Random; 4 | 5 | public class Lottary { 6 | public Random random; 7 | 8 | public boolean [] getFlags() 9 | { 10 | boolean [] flags = new boolean[50]; 11 | int val; 12 | 13 | for (int i = 0; i < 6; ++i) { 14 | for (;;) { 15 | val = random.nextInt(49) + 1; 16 | if (!flags[val]) 17 | break; 18 | } 19 | flags[val] = true; 20 | } 21 | 22 | return flags; 23 | } 24 | 25 | public int [] getNumbers(boolean [] flags) 26 | { 27 | int [] a = new int[6]; 28 | 29 | int index = 0; 30 | 31 | for (int i = 1; i < 50; ++i) 32 | if (flags[i]) 33 | a[index++] = i; 34 | 35 | return a; 36 | } 37 | 38 | public Lottary() 39 | { 40 | random = new Random(); 41 | } 42 | 43 | public int [] getNumbers() 44 | { 45 | return getNumbers(getFlags()); 46 | } 47 | 48 | public int [][] getNumbers(int n) 49 | { 50 | int [][] numbers = new int[n][]; 51 | 52 | for (int i = 0; i < n; ++i) 53 | numbers[i] = getNumbers(); 54 | 55 | return numbers; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/lottary/LottaryApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import org.csystem.util.ArrayUtil; 4 | 5 | import java.util.Scanner; 6 | 7 | public class LottaryApp { 8 | public static void run() 9 | { 10 | Lottary lottary = new Lottary(); 11 | Scanner kb = new Scanner(System.in); 12 | 13 | for (;;) { 14 | System.out.print("Kaç kupon oynamak istersiniz?"); 15 | int n = Integer.parseInt(kb.nextLine()); 16 | 17 | if (n <= 0) 18 | break; 19 | 20 | ArrayUtil.display(2, lottary.getNumbers(n)); 21 | System.out.println("///////////////////"); 22 | } 23 | 24 | System.out.println("Tekrar yapıyor musunuz?"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/studentinfoparserapp/DateUtil.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class DateUtil { 4 | public static int [] daysOfMonths = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 5 | public static String [] daysOfWeekTR = {"Pazar", "Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma", "Cumartesi"}; 6 | 7 | public static String getDayOfWeekTR(int day, int month, int year) 8 | { 9 | return daysOfWeekTR[getDayOfWeek(day, month, year)]; 10 | } 11 | 12 | public static int getDayOfWeek(int day, int month, int year) 13 | { 14 | int dayOfYear; 15 | 16 | dayOfYear = getDayOfYear(day, month, year); 17 | 18 | if (dayOfYear == -1 || year < 1900) 19 | return -1; 20 | 21 | for (int y = 1900; y < year; ++y) 22 | dayOfYear += isLeapYear(y) ? 366 : 365; 23 | 24 | return dayOfYear % 7; 25 | } 26 | 27 | public static int getDayOfYear(int day, int month, int year) 28 | { 29 | if (!isValidDate(day, month, year)) 30 | return -1; 31 | 32 | int dayOfYear = day; 33 | 34 | for (int m = month - 1; m >= 1; --m) 35 | dayOfYear += daysOfMonths[m]; 36 | 37 | if (month > 2 && isLeapYear(year)) 38 | ++dayOfYear; 39 | 40 | return dayOfYear; 41 | } 42 | 43 | public static boolean isValidDate(int day, int month, int year) 44 | { 45 | if (day < 1 || day > 31 || month < 1 || month > 12) 46 | return false; 47 | 48 | int days = month == 2 && isLeapYear(year) ? 29 : daysOfMonths[month]; 49 | 50 | return day <= days; 51 | } 52 | 53 | public static boolean isLeapYear(int year) 54 | { 55 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 56 | } 57 | } -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/studentinfoparserapp/StudentInfo.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class StudentInfo { 4 | public String name; 5 | public String birthDate; 6 | public String lectureName; 7 | public int midtermGrade; 8 | public int finalGrade; 9 | 10 | public double getGrade() 11 | { 12 | return midtermGrade * 0.4 + finalGrade * 0.6; 13 | } 14 | 15 | public String getBirthDateDayOfWeekTR() 16 | { 17 | String [] birthDateInfo = birthDate.split("[/]"); 18 | 19 | return DateUtil.getDayOfWeekTR(Integer.parseInt(birthDateInfo[0]), Integer.parseInt(birthDateInfo[1]), Integer.parseInt(birthDateInfo[2])); 20 | } 21 | 22 | public String toString() 23 | { 24 | double grade = getGrade(); 25 | 26 | return String.format("{name:'%s', birthDate:'%s %s', lectureName:%s, midTermGrade:%d, finalGrade: %d, grade:%f, status:'%s'}", 27 | name, birthDate, getBirthDateDayOfWeekTR(), lectureName, midtermGrade, finalGrade, grade, grade >= 50 ? "Geçti" : "Kaldı"); 28 | } 29 | } -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/studentinfoparserapp/StudentInfoParser.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class StudentInfoParser { 4 | public StudentInfo studentInfo; 5 | 6 | public StudentInfoParser(String str) 7 | { 8 | String [] studentInfoStr = str.split("[:]+"); 9 | //... 10 | 11 | studentInfo = new StudentInfo(); 12 | studentInfo.name = studentInfoStr[0]; 13 | studentInfo.birthDate = studentInfoStr[1]; 14 | studentInfo.lectureName = studentInfoStr[2]; 15 | studentInfo.midtermGrade = Integer.parseInt(studentInfoStr[3]); 16 | studentInfo.finalGrade = Integer.parseInt(studentInfoStr[4]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/app/samples/studentinfoparserapp/StudentInfoParserApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | import java.util.Scanner; 4 | 5 | public class StudentInfoParserApp { 6 | public static void run() 7 | { 8 | Scanner kb = new Scanner(System.in); 9 | 10 | for (;;) { 11 | System.out.print("Formatlı yazıyı giriniz:"); 12 | String str = kb.nextLine(); 13 | 14 | if (str.equals("quit")) 15 | break; 16 | 17 | StudentInfoParser studentInfoParser = new StudentInfoParser(str); 18 | 19 | System.out.println(studentInfoParser.studentInfo.toString()); 20 | } 21 | 22 | System.out.println("Tekrar yapıyor musunuz?"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/math/Complex.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Complex sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math; 5 | 6 | public class Complex { 7 | public static Complex add(double re1, double im1, double re2, double im2) //İleride gizlenecek 8 | { 9 | Complex z = new Complex(); 10 | 11 | z.re = re1 + re2; 12 | z.im = im1 + im2; 13 | 14 | return z; 15 | } 16 | 17 | public static Complex subtract(double re1, double im1, double re2, double im2) //İleride gizlenecek 18 | { 19 | return add(re1, im1, -re2, -im2); 20 | } 21 | 22 | public double re; 23 | public double im; 24 | 25 | public Complex() 26 | { 27 | 28 | } 29 | 30 | public Complex(int a) 31 | { 32 | re = a; 33 | } 34 | 35 | public Complex(int a, int b) 36 | { 37 | re = a; 38 | im = b; 39 | } 40 | 41 | public Complex getConjugate() 42 | { 43 | Complex z = new Complex(); 44 | 45 | z.re = re; 46 | z.im = -im; 47 | 48 | return z; 49 | } 50 | 51 | public double getNorm() 52 | { 53 | return Math.sqrt(re * re + im * im); 54 | } 55 | 56 | //add 57 | public static Complex add(double a, Complex z) 58 | { 59 | return add(a, 0, z.re, z.im); 60 | } 61 | 62 | public Complex add(Complex z) 63 | { 64 | return add(re, im, z.re, z.im); 65 | } 66 | 67 | public Complex add(double a) 68 | { 69 | return add(re, im, a, 0); 70 | } 71 | 72 | //subtract 73 | public static Complex subtract(double a, Complex z) 74 | { 75 | return subtract(a, 0, z.re, z.im); 76 | } 77 | 78 | public Complex subtract(Complex z) 79 | { 80 | return subtract(re, im, z.re, z.im); 81 | } 82 | 83 | public Complex subtract(double a) 84 | { 85 | return subtract(re, im, a, 0); 86 | } 87 | 88 | public void offset(double dxy) 89 | { 90 | offset(dxy, dxy); 91 | } 92 | 93 | public void offset(double dx, double dy) 94 | { 95 | re += dx; 96 | im += dy; 97 | } 98 | 99 | public String toString() 100 | { 101 | return String.format("|%.2f + %.2f * i| = %f", re, im, getNorm()); 102 | } 103 | } 104 | 105 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/math/geometry/Point.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Point sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class Point { 9 | public int x; 10 | public int y; 11 | 12 | public Point() 13 | { 14 | } 15 | 16 | public Point(int a) 17 | { 18 | x = a; 19 | } 20 | 21 | public Point(int a, int b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(int a, int b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(int dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(int dx, int dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %d, y: %d}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/math/geometry/PointF.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | PointF sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class PointF { 9 | public float x; 10 | public float y; 11 | 12 | public PointF() 13 | { 14 | } 15 | 16 | public PointF(float a) 17 | { 18 | x = a; 19 | } 20 | 21 | public PointF(float a, float b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(float a, float b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(float dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(float dx, float dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %f, y: %f}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/util/NumberUtil.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | NumberUtil sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | import static java.lang.Math.*; 7 | 8 | public class NumberUtil { 9 | public static String [] onesTR = {"", "bir", "iki", "üç", "dört", "beş", "altı", "yedi", "sekiz", "dokuz"}; 10 | public static String [] tensTR = {"", "on", "yirmi", "otuz", "kırk", "elli", "altmış", "yetmiş", "seksen", "doksan"}; 11 | 12 | public static int[] getDigits(long val, int n) 13 | { 14 | long value = Math.abs(val); 15 | int count = value == 0 ? 1 : (int)Math.log10(value) / n + 1; 16 | int [] digits = new int[count]; 17 | int powerOfTen = (int)Math.pow(10, n); 18 | 19 | for (int i = count - 1; i >= 0; digits[i] = (int)(value % powerOfTen), value /= powerOfTen, --i) 20 | ; 21 | 22 | return digits; 23 | } 24 | 25 | public static String numberToText3DigitsTR(int val) 26 | { 27 | if(val == 0) 28 | return "sıfır"; 29 | 30 | String text = val < 0 ? "eksi" : ""; 31 | int value = Math.abs(val); 32 | int a = value / 100; 33 | int b = value / 10 % 10; 34 | int c = value % 10; 35 | 36 | if (a != 0) { 37 | if (a != 1) 38 | text += onesTR[a]; 39 | text += "yüz"; 40 | } 41 | 42 | if (b != 0) 43 | text += tensTR[b]; 44 | 45 | if (c != 0) 46 | text += onesTR[c]; 47 | 48 | return text; 49 | } 50 | 51 | public static int factorial(int n) 52 | { 53 | if (n < 0) 54 | return -1; 55 | 56 | int result = 1; 57 | 58 | for (int i = 2; i <= n; ++i) 59 | result *= i; 60 | 61 | return result; 62 | } 63 | 64 | public static int[] getDigits(int val) 65 | { 66 | return getDigits((long)val); 67 | } 68 | 69 | public static int[] getDigits(long val) 70 | { 71 | return getDigits(val, 1); 72 | } 73 | 74 | public static int[] getDigitsInTwos(long val) 75 | { 76 | return getDigits(val, 2); 77 | } 78 | 79 | public static int[] getDigitsInThrees(long val) 80 | { 81 | return getDigits(val, 3); 82 | } 83 | 84 | public static int getDigitsCount(long val) 85 | { 86 | return val == 0 ? 1 : (int)log10(abs(val)) + 1; 87 | } 88 | 89 | public static int getDigitsPowSum(int val) 90 | { 91 | int count = getDigitsCount(val); 92 | 93 | int sum = 0; 94 | 95 | while (val != 0) { 96 | sum += pow(val % 10, count); 97 | val /= 10; 98 | } 99 | 100 | return sum; 101 | } 102 | 103 | public static int getDigitsSum(int val) 104 | { 105 | val = abs(val); 106 | int sum = 0; 107 | 108 | while (val != 0) { 109 | sum += val % 10; 110 | val /= 10; 111 | } 112 | 113 | return sum; 114 | } 115 | 116 | public static int getFibonacciNumber(int n) 117 | { 118 | if (n <= 0) 119 | return -1; 120 | 121 | if (n <= 2) 122 | return n - 1; 123 | 124 | int prev1 = 1, prev2 = 0, val = 0; 125 | 126 | for (int i = 2; i < n; ++i) { 127 | val = prev1 + prev2; 128 | prev2 = prev1; 129 | prev1 = val; 130 | } 131 | 132 | return val; 133 | } 134 | 135 | public static int getNextFibonacciNumber(int val) 136 | { 137 | if (val < 0) 138 | return 0; 139 | 140 | int prev1 = 1, prev2 = 0, result; 141 | 142 | for (;;) { 143 | result = prev1 + prev2; 144 | 145 | if (result > val) 146 | return result; 147 | 148 | prev2 = prev1; 149 | prev1 = result; 150 | } 151 | } 152 | 153 | public static int getPrime(int n) 154 | { 155 | if (n <= 0) 156 | return -1; 157 | 158 | int count = 0; 159 | int val = 2; 160 | 161 | for (int i = 2; count < n; ++i) 162 | if (isPrime(i)) { 163 | ++count; 164 | val = i; 165 | } 166 | 167 | return val; 168 | } 169 | 170 | public static int getReverse(int val) 171 | { 172 | int reverse = 0; 173 | 174 | while (val != 0) { 175 | reverse = reverse * 10 + val % 10; 176 | val /= 10; 177 | } 178 | 179 | return reverse; 180 | } 181 | 182 | public static boolean isArmstrong(int val) 183 | { 184 | if (val < 0) 185 | return false; 186 | 187 | return getDigitsPowSum(val) == val; 188 | } 189 | 190 | public static boolean isEven(int val) 191 | { 192 | return val % 2 == 0; 193 | } 194 | 195 | 196 | public static boolean isOdd(int val) 197 | { 198 | return !isEven(val); 199 | } 200 | 201 | public static boolean isPrime(int val) 202 | { 203 | if (val <= 1) 204 | return false; 205 | 206 | if (val % 2 == 0) 207 | return val == 2; 208 | 209 | if (val % 3 == 0) 210 | return val == 3; 211 | 212 | if (val % 5 == 0) 213 | return val == 5; 214 | 215 | if (val % 7 == 0) 216 | return val == 7; 217 | 218 | int sqrtVal = (int)sqrt(val); 219 | 220 | for (int i = 11; i <= sqrtVal; i += 2) 221 | if (val % i == 0) 222 | return false; 223 | 224 | return true; 225 | } 226 | 227 | public static boolean isNotPrime(int val) 228 | { 229 | return !isPrime(val); 230 | } 231 | 232 | public static int max(int a, int b, int c) 233 | { 234 | return Math.max(Math.max(a, b), c); 235 | } 236 | 237 | public static int min(int a, int b, int c) 238 | { 239 | return Math.min(Math.min(a, b), c); 240 | } 241 | 242 | 243 | public static String numberToTextTR(long val) 244 | { 245 | String text; 246 | 247 | //TODO: Homework-012-3. soru 248 | text = numberToText3DigitsTR((int)val); 249 | 250 | return text; 251 | } 252 | 253 | public static int pow(int a, int b) 254 | { 255 | if (b <= 0) 256 | return 1; 257 | 258 | int result = 1; 259 | 260 | while (b-- > 0) 261 | result *= a; 262 | 263 | return result; 264 | } 265 | } -------------------------------------------------------------------------------- /JavaLesson014/org/csystem/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | StringUtil sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | import java.util.Random; 7 | 8 | import static java.lang.Character.*; 9 | 10 | public class StringUtil { 11 | public static String capitalize(String s) 12 | { 13 | if (s.isEmpty()) 14 | return s; 15 | 16 | return toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase(); 17 | } 18 | 19 | public static int countString(String s1, String s2) 20 | { 21 | int count = 0; 22 | 23 | for (int i = -1; (i = s1.indexOf(s2, i + 1)) != -1; ++count) 24 | ; 25 | 26 | return count; 27 | } 28 | 29 | public static String getLetters(String s) 30 | { 31 | String str = ""; 32 | int length = s.length(); 33 | 34 | for (int i = 0; i < length; ++i) { 35 | char ch = s.charAt(i); 36 | 37 | if (isLetter(ch)) 38 | str += ch; 39 | } 40 | 41 | return str; 42 | } 43 | 44 | public static String getLongestPalindrome(String s) 45 | { 46 | String text = ""; 47 | 48 | int endIndex = s.length(); 49 | 50 | while (endIndex != 0) { 51 | int beginIndex = 0; 52 | 53 | while (beginIndex != endIndex) { 54 | String str = s.substring(beginIndex++, endIndex); 55 | 56 | if (str.length() > 1 && StringUtil.isPalindrome(str) && str.length() > text.length()) 57 | text = str; 58 | } 59 | 60 | --endIndex; 61 | } 62 | 63 | return text; 64 | } 65 | 66 | public static String getRandomText(Random r, int n, String alphabet) 67 | { 68 | char [] c = new char[n]; 69 | 70 | for (int i = 0; i < n; ++i) { 71 | char ch = alphabet.charAt(r.nextInt(alphabet.length())); 72 | 73 | if (r.nextBoolean()) 74 | ch = toUpperCase(ch); 75 | 76 | c[i] = ch; 77 | } 78 | 79 | return String.valueOf(c); 80 | } 81 | 82 | public static String getRandomTextEN(int n) 83 | { 84 | return getRandomTextEN(new Random(), n); 85 | } 86 | 87 | public static String getRandomTextEN(Random r, int n) 88 | { 89 | return getRandomText(r, n, "abcdefghijklmnopqrstuwxvyz"); 90 | } 91 | 92 | public static String getRandomTextTR(int n) 93 | { 94 | return getRandomTextTR(new Random(), n); 95 | } 96 | 97 | public static String getRandomTextTR(Random r, int n) 98 | { 99 | return getRandomText(r, n, "abcçdefgğhıijklmnoöprsştuüvyz"); 100 | } 101 | 102 | public static boolean isPalindrome(String s) 103 | { 104 | int left = 0; 105 | int right = s.length() - 1; 106 | 107 | while (left < right) { 108 | char chLeft = toLowerCase(s.charAt(left)); 109 | 110 | if (!isLetter(chLeft)) { 111 | ++left; 112 | continue; 113 | } 114 | 115 | char chRight = toLowerCase(s.charAt(right)); 116 | 117 | if (!isLetter(chRight)) { 118 | --right; 119 | continue; 120 | } 121 | 122 | if (chLeft != chRight) 123 | return false; 124 | 125 | ++left; 126 | --right; 127 | } 128 | 129 | return true; 130 | } 131 | 132 | public static boolean isPangram(String text, String alphabet) 133 | { 134 | int length = alphabet.length(); 135 | 136 | for (int i = 0; i < length; ++i) 137 | if (!text.contains(alphabet.charAt(i) + "")) 138 | return false; 139 | 140 | return true; 141 | } 142 | 143 | public static boolean isPangramEN(String text) 144 | { 145 | return isPangram(text.toLowerCase(), "abcdefghijklmnopqrstuwxvyz"); 146 | } 147 | 148 | public static boolean isPangramTR(String text) 149 | { 150 | return isPangram(text.toLowerCase(), "abcçdefgğhıijklmnoöprsştuüvyz"); 151 | } 152 | 153 | public static String padLeft(String s, int length) 154 | { 155 | return padLeft(s, length, ' '); 156 | } 157 | 158 | public static String padLeft(String s, int length, char ch) 159 | { 160 | length -= s.length(); 161 | 162 | if (length <= 0) 163 | return s; 164 | 165 | return repeat(length, ch) + s; 166 | } 167 | 168 | public static String padRight(String s, int length) 169 | { 170 | return padRight(s, length, ' '); 171 | } 172 | 173 | public static String padRight(String s, int length, char ch) 174 | { 175 | length -= s.length(); 176 | 177 | if (length <= 0) 178 | return s; 179 | 180 | return s + repeat(length, ch); 181 | } 182 | 183 | public static String removeWhitespaces(String s) 184 | { 185 | int length = s.length(); 186 | String str = ""; 187 | 188 | for (int i = 0; i < length; ++i) { 189 | char ch = s.charAt(i); 190 | 191 | if (!isWhitespace(ch)) 192 | str += ch; 193 | } 194 | 195 | return str; 196 | } 197 | 198 | public static String repeat(int n, char ch) 199 | { 200 | char [] c = new char[n]; 201 | 202 | while (n-- > 0) 203 | c[n] = ch; 204 | 205 | return String.valueOf(c); 206 | } 207 | 208 | public static String reverse(String str) 209 | { 210 | char [] c = str.toCharArray(); 211 | 212 | ArrayUtil.reverse(c); 213 | 214 | return String.valueOf(c); 215 | } 216 | 217 | public static String trimLeft(String s) 218 | { 219 | int i; 220 | int len = s.length(); 221 | 222 | for (i = 0; i < len && isWhitespace(s.charAt(i)); ++i) 223 | ; 224 | 225 | return s.substring(i); 226 | } 227 | 228 | public static String trimRight(String s) 229 | { 230 | int i; 231 | 232 | for (i = s.length() - 1; i >= 0 && isWhitespace(s.charAt(i)); --i) 233 | ; 234 | 235 | return s.substring(0, i + 1); 236 | } 237 | } 238 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/App.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.app; 5 | 6 | class App { 7 | public static void main(String [] args) 8 | { 9 | 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/commandpromptapp/CommandPrompt.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.commandpromptapp; 2 | 3 | import org.csystem.util.StringUtil; 4 | 5 | import java.util.Scanner; 6 | 7 | public class CommandPrompt { 8 | private static String [] m_commandsStr = {"length", "reverse", "upper", "lower", "change", "clear"}; 9 | private String m_prompt; 10 | private Scanner m_kb; 11 | 12 | private static String getCommandByText(String text) 13 | { 14 | if (text.length() < 3) 15 | return ""; 16 | 17 | for (String s : m_commandsStr) 18 | if (s.startsWith(text)) 19 | return s; 20 | 21 | return ""; 22 | } 23 | 24 | private void lengthProc(String [] commandsStr) 25 | { 26 | if (commandsStr.length != 2) { 27 | System.out.println("length bir tane argüman almalıdır"); 28 | return; 29 | } 30 | 31 | System.out.println(commandsStr[1].length()); 32 | } 33 | 34 | private void reverseProc(String [] commandsStr) 35 | { 36 | if (commandsStr.length != 2) { 37 | System.out.println("revere bir tane argüman almalıdır"); 38 | return; 39 | } 40 | 41 | System.out.println(StringUtil.reverse(commandsStr[1])); 42 | } 43 | 44 | private void upperProc(String [] commandsStr) 45 | { 46 | if (commandsStr.length != 2) { 47 | System.out.println("upper bir tane argüman almalıdır"); 48 | return; 49 | } 50 | System.out.println(commandsStr[1].toUpperCase()); 51 | } 52 | 53 | private void lowerProc(String [] commandsStr) 54 | { 55 | if (commandsStr.length != 2) { 56 | System.out.println("lower bir tane argüman almalıdır"); 57 | return; 58 | } 59 | System.out.println(commandsStr[1].toLowerCase()); 60 | } 61 | 62 | private void changeProc(String [] commandsStr) 63 | { 64 | if (commandsStr.length != 2) { 65 | System.out.println("change bir tane argüman almalıdır"); 66 | return; 67 | } 68 | m_prompt = commandsStr[1]; 69 | } 70 | 71 | private void clearProc(String [] commandsStr) 72 | { 73 | for (int i = 0; i < 30; ++i) 74 | System.out.println(); 75 | } 76 | 77 | private void doWorkForCommand(String [] commandInfoStr) 78 | { 79 | switch (commandInfoStr[0]) { 80 | case "length": 81 | lengthProc(commandInfoStr); 82 | break; 83 | case "reverse": 84 | reverseProc(commandInfoStr); 85 | break; 86 | case "upper": 87 | upperProc(commandInfoStr); 88 | break; 89 | case "lower": 90 | lowerProc(commandInfoStr); 91 | break; 92 | case "change": 93 | changeProc(commandInfoStr); 94 | break; 95 | case "clear": 96 | clearProc(commandInfoStr); 97 | break; 98 | } 99 | } 100 | 101 | private void parseCommand(String [] commandInfoStr) 102 | { 103 | String cmd = getCommandByText(commandInfoStr[0]); 104 | 105 | if (!cmd.isEmpty()) { 106 | commandInfoStr[0] = cmd; 107 | doWorkForCommand(commandInfoStr); 108 | } 109 | else 110 | System.out.println("Geçersiz komut"); 111 | } 112 | 113 | public CommandPrompt(String p) 114 | { 115 | m_prompt = p; 116 | m_kb = new Scanner(System.in); 117 | } 118 | 119 | public void run() 120 | { 121 | System.out.println("C ve Sistem Programcıları Derneği"); 122 | System.out.println("Homework-013 sorusuna ilişkin bir iskelet"); 123 | 124 | for (;;) { 125 | System.out.print(m_prompt + ">"); 126 | String cmd = m_kb.nextLine().trim(); 127 | 128 | if (cmd.equals("quit")) 129 | break; 130 | 131 | parseCommand(cmd.split("[ \t]+")); 132 | } 133 | 134 | System.out.println("C ve Sistem Programcıları Derneği"); 135 | System.out.println("Tekrar yapıyor musunuz?"); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/commandpromptapp/CommandPromptApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.commandpromptapp; 2 | 3 | public class CommandPromptApp { 4 | private CommandPromptApp() 5 | { 6 | } 7 | 8 | public static void run() 9 | { 10 | CommandPrompt commandPrompt = new CommandPrompt("CSD"); 11 | 12 | commandPrompt.run(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/dateapp/DateUtil.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.dateapp; 2 | 3 | public class DateUtil { 4 | public static int [] daysOfMonths = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 5 | 6 | public static int getDayOfWeek(int day, int month, int year) 7 | { 8 | int dayOfYear; 9 | 10 | dayOfYear = getDayOfYear(day, month, year); 11 | 12 | if (dayOfYear == -1 || year < 1900) 13 | return -1; 14 | 15 | for (int y = 1900; y < year; ++y) 16 | dayOfYear += isLeapYear(y) ? 366 : 365; 17 | 18 | return dayOfYear % 7; 19 | } 20 | 21 | public static int getDayOfYear(int day, int month, int year) 22 | { 23 | if (!isValidDate(day, month, year)) 24 | return -1; 25 | 26 | int dayOfYear = day; 27 | 28 | for (int m = month - 1; m >= 1; --m) 29 | dayOfYear += daysOfMonths[m]; 30 | 31 | if (month > 2 && isLeapYear(year)) 32 | ++dayOfYear; 33 | 34 | return dayOfYear; 35 | } 36 | 37 | public static boolean isValidDate(int day, int month, int year) 38 | { 39 | if (day < 1 || day > 31 || month < 1 || month > 12) 40 | return false; 41 | 42 | int days = month == 2 && isLeapYear(year) ? 29 : daysOfMonths[month]; 43 | 44 | return day <= days; 45 | } 46 | 47 | public static boolean isLeapYear(int year) 48 | { 49 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/dateapp/DateUtilApp.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Sınıf çalışması: Parametresi ile aldığı int türden gün, ay ve yıl bilgilerine ilişkin tarihin aşağıdaki 3 | açıklamalara göre haftanın hangi gününe geldiğini döndüren getDayOfWeek metodunu yazınız. 4 | Açıklamalar: 5 | - Metot geçersiz bir tarih için -1 değerini döndürecektir. 6 | - Haftanın günü 1.1.1900 ile verilen tarih arasındaki gün sayısının 7 değerine modu alınarak bulunabilir 7 | - 7 değerine mod alındıktan sonra sıfır Pazar, 1 pazartesi, .., 6 cumartesi günü için 8 | elde edilecek değerdir 9 | - 1.1.1900' den önceki tarihler geçersiz kabul edilecektir 10 | ----------------------------------------------------------------------------------------------------------------------*/ 11 | package org.csystem.app.samples.dateapp; 12 | 13 | public class DateUtilApp { 14 | public static String [] daysOfWeek = {"Pazar", "Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma", "Cumartesi"}; 15 | public static String [] monthsTR = {"", "Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", 16 | "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık"}; 17 | 18 | public static String [] monthsEN = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 19 | 20 | public static void displayTR(int day, int month, int year) 21 | { 22 | int dayOfWeek = DateUtil.getDayOfWeek(day, month, year); 23 | 24 | if (dayOfWeek == -1) { 25 | System.out.println("Geçersiz tarih"); 26 | return; 27 | } 28 | 29 | System.out.printf("%02d %s %04d %s%n", day, monthsTR[month], year, daysOfWeek[dayOfWeek]); 30 | 31 | if (dayOfWeek == 0 || dayOfWeek == 6) 32 | System.out.println("Bugün kurs var. Tekrar yaptınız mı?"); 33 | else 34 | System.out.println("Kurs günü yaklaşıyor. Tekrar yapınız!!!!"); 35 | } 36 | 37 | public static void run() 38 | { 39 | java.util.Scanner kb = new java.util.Scanner(System.in); 40 | 41 | for (;;) { 42 | System.out.print("Gün bilgisini giriniz:"); 43 | int day = Integer.parseInt(kb.nextLine()); 44 | 45 | if (day == 0) 46 | break; 47 | 48 | System.out.print("Ay bilgisini giriniz:"); 49 | int month = Integer.parseInt(kb.nextLine()); 50 | 51 | System.out.print("Yıl bilgisini giriniz:"); 52 | int year = Integer.parseInt(kb.nextLine()); 53 | 54 | displayTR(day, month, year); 55 | } 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/examsimulationapp/ExamSimulation.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.examsimulationapp; 2 | 3 | import org.csystem.util.ArrayUtil; 4 | 5 | import java.util.Random; 6 | import java.util.Scanner; 7 | 8 | public class ExamSimulation { 9 | private String m_lectureName; 10 | private int [][] m_grades; 11 | private double [] m_averages; 12 | private double m_average; 13 | 14 | private void findAverages() 15 | { 16 | int totalNumberOfStudents = 0; 17 | int totalGrades = 0; 18 | 19 | for (int i = 0; i < m_grades.length; ++i) { 20 | int total = ArrayUtil.sum(m_grades[i]); 21 | 22 | m_averages[i] = (double)total / m_grades[i].length; 23 | totalGrades += total; 24 | totalNumberOfStudents += m_grades[i].length; 25 | } 26 | 27 | m_average = (double)totalGrades / totalNumberOfStudents; 28 | } 29 | 30 | private void fillGrades() 31 | { 32 | Random r = new Random(); 33 | Scanner kb = new Scanner(System.in); 34 | System.out.print("Şube sayısını giriniz:"); 35 | m_grades = new int[Integer.parseInt(kb.nextLine())][]; 36 | m_averages = new double[m_grades.length]; 37 | 38 | for (int i = 0; i < m_grades.length; ++i) { 39 | System.out.printf("%d. şube öğrenci sayısını giriniz:", i + 1); 40 | int count = Integer.parseInt(kb.nextLine()); 41 | 42 | m_grades[i] = ArrayUtil.getRandomArray(r, count, 0, 101); 43 | } 44 | } 45 | 46 | public ExamSimulation(String name) 47 | { 48 | m_lectureName = name; 49 | } 50 | 51 | public String getLectureName() 52 | { 53 | return m_lectureName; 54 | } 55 | 56 | public int getGrade(int i, int k) 57 | { 58 | return m_grades[i][k]; 59 | } 60 | 61 | public double getAverage(int i) 62 | { 63 | return m_averages[i]; 64 | } 65 | 66 | public int getNumberOfClasses() 67 | { 68 | return m_averages.length; 69 | } 70 | 71 | public int getNumberOfStudents(int i) 72 | { 73 | return m_grades[i].length; 74 | } 75 | 76 | public double getAverage() 77 | { 78 | return m_average; 79 | } 80 | 81 | public void run() 82 | { 83 | fillGrades(); 84 | findAverages(); 85 | } 86 | 87 | public void displayGrades() 88 | { 89 | System.out.printf("%s dersi sınav notları:%n", m_lectureName); 90 | System.out.println("*************************************************************"); 91 | for (int i = 0; i < m_grades.length; ++i) { 92 | System.out.printf("%d. şube notları:", i + 1); 93 | ArrayUtil.display(3, m_grades[i]); 94 | } 95 | System.out.println("*************************************************************"); 96 | } 97 | 98 | public void displayStatus() 99 | { 100 | displayGrades(); 101 | 102 | System.out.printf("%s dersi için ortalamalar%n", m_lectureName); 103 | System.out.println("*************************************************************"); 104 | for (int i = 0; i < m_averages.length; ++i) 105 | System.out.printf("%d.şube not ortalaması:%f%n", i + 1, m_averages[i]); 106 | 107 | System.out.printf("Okul ortalaması:%f%n", m_average); 108 | System.out.println("*************************************************************"); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/examsimulationapp/ExamSimulationApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.examsimulationapp; 2 | 3 | public class ExamSimulationApp { 4 | private ExamSimulationApp() 5 | { 6 | } 7 | 8 | public static void run() 9 | { 10 | ExamSimulation examSimulation = new ExamSimulation("Matematik"); 11 | 12 | examSimulation.run(); 13 | examSimulation.displayGrades(); 14 | examSimulation.displayStatus(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/lottary/Lottary.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import java.util.Random; 4 | 5 | public class Lottary { 6 | private Random m_random; 7 | 8 | private boolean [] getFlags() 9 | { 10 | boolean [] flags = new boolean[50]; 11 | int val; 12 | 13 | for (int i = 0; i < 6; ++i) { 14 | for (;;) { 15 | val = m_random.nextInt(49) + 1; 16 | if (!flags[val]) 17 | break; 18 | } 19 | flags[val] = true; 20 | } 21 | 22 | return flags; 23 | } 24 | 25 | private int [] getNumbers(boolean [] flags) 26 | { 27 | int [] a = new int[6]; 28 | 29 | int index = 0; 30 | 31 | for (int i = 1; i < 50; ++i) 32 | if (flags[i]) 33 | a[index++] = i; 34 | 35 | return a; 36 | } 37 | 38 | public Lottary() 39 | { 40 | m_random = new Random(); 41 | } 42 | 43 | public int [] getNumbers() 44 | { 45 | return getNumbers(getFlags()); 46 | } 47 | 48 | public int [][] getNumbers(int n) 49 | { 50 | int [][] numbers = new int[n][]; 51 | 52 | for (int i = 0; i < n; ++i) 53 | numbers[i] = getNumbers(); 54 | 55 | return numbers; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/lottary/LottaryApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import org.csystem.util.ArrayUtil; 4 | 5 | import java.util.Scanner; 6 | 7 | public class LottaryApp { 8 | private LottaryApp() 9 | { 10 | } 11 | 12 | public static void run() 13 | { 14 | Lottary lottary = new Lottary(); 15 | Scanner kb = new Scanner(System.in); 16 | 17 | for (;;) { 18 | System.out.print("Kaç kupon oynamak istersiniz?"); 19 | int n = Integer.parseInt(kb.nextLine()); 20 | 21 | if (n <= 0) 22 | break; 23 | 24 | ArrayUtil.display(2, lottary.getNumbers(n)); 25 | System.out.println("///////////////////"); 26 | } 27 | 28 | System.out.println("Tekrar yapıyor musunuz?"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/studentinfoparserapp/DateUtil.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class DateUtil { 4 | public static int [] daysOfMonths = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 5 | public static String [] daysOfWeekTR = {"Pazar", "Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma", "Cumartesi"}; 6 | 7 | public static String getDayOfWeekTR(int day, int month, int year) 8 | { 9 | return daysOfWeekTR[getDayOfWeek(day, month, year)]; 10 | } 11 | 12 | public static int getDayOfWeek(int day, int month, int year) 13 | { 14 | int dayOfYear; 15 | 16 | dayOfYear = getDayOfYear(day, month, year); 17 | 18 | if (dayOfYear == -1 || year < 1900) 19 | return -1; 20 | 21 | for (int y = 1900; y < year; ++y) 22 | dayOfYear += isLeapYear(y) ? 366 : 365; 23 | 24 | return dayOfYear % 7; 25 | } 26 | 27 | public static int getDayOfYear(int day, int month, int year) 28 | { 29 | if (!isValidDate(day, month, year)) 30 | return -1; 31 | 32 | int dayOfYear = day; 33 | 34 | for (int m = month - 1; m >= 1; --m) 35 | dayOfYear += daysOfMonths[m]; 36 | 37 | if (month > 2 && isLeapYear(year)) 38 | ++dayOfYear; 39 | 40 | return dayOfYear; 41 | } 42 | 43 | public static boolean isValidDate(int day, int month, int year) 44 | { 45 | if (day < 1 || day > 31 || month < 1 || month > 12) 46 | return false; 47 | 48 | int days = month == 2 && isLeapYear(year) ? 29 : daysOfMonths[month]; 49 | 50 | return day <= days; 51 | } 52 | 53 | public static boolean isLeapYear(int year) 54 | { 55 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 56 | } 57 | } -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/studentinfoparserapp/StudentInfo.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class StudentInfo { 4 | public String name; 5 | public String birthDate; 6 | public String lectureName; 7 | public int midtermGrade; 8 | public int finalGrade; 9 | 10 | public double getGrade() 11 | { 12 | return midtermGrade * 0.4 + finalGrade * 0.6; 13 | } 14 | 15 | public String getBirthDateDayOfWeekTR() 16 | { 17 | String [] birthDateInfo = birthDate.split("[/]"); 18 | 19 | return DateUtil.getDayOfWeekTR(Integer.parseInt(birthDateInfo[0]), Integer.parseInt(birthDateInfo[1]), Integer.parseInt(birthDateInfo[2])); 20 | } 21 | 22 | public String toString() 23 | { 24 | double grade = getGrade(); 25 | 26 | return String.format("{name:'%s', birthDate:'%s %s', lectureName:%s, midTermGrade:%d, finalGrade: %d, grade:%f, status:'%s'}", 27 | name, birthDate, getBirthDateDayOfWeekTR(), lectureName, midtermGrade, finalGrade, grade, grade >= 50 ? "Geçti" : "Kaldı"); 28 | } 29 | } -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/studentinfoparserapp/StudentInfoParser.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class StudentInfoParser { 4 | public StudentInfo studentInfo; 5 | 6 | public StudentInfoParser(String str) 7 | { 8 | String [] studentInfoStr = str.split("[:]+"); 9 | //... 10 | 11 | studentInfo = new StudentInfo(); 12 | studentInfo.name = studentInfoStr[0]; 13 | studentInfo.birthDate = studentInfoStr[1]; 14 | studentInfo.lectureName = studentInfoStr[2]; 15 | studentInfo.midtermGrade = Integer.parseInt(studentInfoStr[3]); 16 | studentInfo.finalGrade = Integer.parseInt(studentInfoStr[4]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/app/samples/studentinfoparserapp/StudentInfoParserApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | import java.util.Scanner; 4 | 5 | public class StudentInfoParserApp { 6 | public static void run() 7 | { 8 | Scanner kb = new Scanner(System.in); 9 | 10 | for (;;) { 11 | System.out.print("Formatlı yazıyı giriniz:"); 12 | String str = kb.nextLine(); 13 | 14 | if (str.equals("quit")) 15 | break; 16 | 17 | StudentInfoParser studentInfoParser = new StudentInfoParser(str); 18 | 19 | System.out.println(studentInfoParser.studentInfo.toString()); 20 | } 21 | 22 | System.out.println("Tekrar yapıyor musunuz?"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/math/Complex.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Complex sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math; 5 | 6 | public class Complex { 7 | private static Complex add(double re1, double im1, double re2, double im2) 8 | { 9 | Complex z = new Complex(); 10 | 11 | z.re = re1 + re2; 12 | z.im = im1 + im2; 13 | 14 | return z; 15 | } 16 | 17 | private static Complex subtract(double re1, double im1, double re2, double im2) 18 | { 19 | return add(re1, im1, -re2, -im2); 20 | } 21 | 22 | public double re; 23 | public double im; 24 | 25 | public Complex() 26 | { 27 | 28 | } 29 | 30 | public Complex(int a) 31 | { 32 | re = a; 33 | } 34 | 35 | public Complex(int a, int b) 36 | { 37 | re = a; 38 | im = b; 39 | } 40 | 41 | public Complex getConjugate() 42 | { 43 | Complex z = new Complex(); 44 | 45 | z.re = re; 46 | z.im = -im; 47 | 48 | return z; 49 | } 50 | 51 | public double getNorm() 52 | { 53 | return Math.sqrt(re * re + im * im); 54 | } 55 | 56 | //add 57 | public static Complex add(double a, Complex z) 58 | { 59 | return add(a, 0, z.re, z.im); 60 | } 61 | 62 | public Complex add(Complex z) 63 | { 64 | return add(re, im, z.re, z.im); 65 | } 66 | 67 | public Complex add(double a) 68 | { 69 | return add(re, im, a, 0); 70 | } 71 | 72 | //subtract 73 | public static Complex subtract(double a, Complex z) 74 | { 75 | return subtract(a, 0, z.re, z.im); 76 | } 77 | 78 | public Complex subtract(Complex z) 79 | { 80 | return subtract(re, im, z.re, z.im); 81 | } 82 | 83 | public Complex subtract(double a) 84 | { 85 | return subtract(re, im, a, 0); 86 | } 87 | 88 | public void offset(double dxy) 89 | { 90 | offset(dxy, dxy); 91 | } 92 | 93 | public void offset(double dx, double dy) 94 | { 95 | re += dx; 96 | im += dy; 97 | } 98 | 99 | public String toString() 100 | { 101 | return String.format("|%.2f + %.2f * i| = %f", re, im, getNorm()); 102 | } 103 | } 104 | 105 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/math/geometry/Circle.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Circle sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | public class Circle { 7 | private double m_radius; 8 | private double m_pi; 9 | 10 | public Circle() 11 | { 12 | } 13 | 14 | public Circle(double radius) 15 | { 16 | setRadius(radius); 17 | } 18 | 19 | public Circle(double radius, double pi) 20 | { 21 | setRadius(radius, pi); 22 | } 23 | 24 | public double getRadius() 25 | { 26 | return m_radius; 27 | } 28 | 29 | public void setRadius(double radius) 30 | { 31 | setRadius(radius, Math.PI); 32 | } 33 | 34 | public void setRadius(double radius, double pi) 35 | { 36 | m_radius = Math.abs(radius); 37 | setPI(pi); 38 | } 39 | 40 | public double getArea() 41 | { 42 | return m_pi * m_radius * m_radius; 43 | } 44 | 45 | public double getCircumference() 46 | { 47 | return 2 * m_pi * m_radius; 48 | } 49 | 50 | public void setPI(double pi) 51 | { 52 | m_pi = Math.abs(pi); //Kolaylık için mutlak değer aldık 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/math/geometry/Point.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Point sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class Point { 9 | public int x; 10 | public int y; 11 | 12 | public Point() 13 | { 14 | } 15 | 16 | public Point(int a) 17 | { 18 | x = a; 19 | } 20 | 21 | public Point(int a, int b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(int a, int b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(int dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(int dx, int dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %d, y: %d}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/math/geometry/PointF.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | PointF sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class PointF { 9 | public float x; 10 | public float y; 11 | 12 | public PointF() 13 | { 14 | } 15 | 16 | public PointF(float a) 17 | { 18 | x = a; 19 | } 20 | 21 | public PointF(float a, float b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(float a, float b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(float dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(float dx, float dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %f, y: %f}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson015/org/csystem/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | StringUtil sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | import java.util.Random; 7 | 8 | import static java.lang.Character.*; 9 | 10 | public class StringUtil { 11 | 12 | private StringUtil() 13 | { 14 | } 15 | 16 | public static String capitalize(String s) 17 | { 18 | if (s.isEmpty()) 19 | return s; 20 | 21 | return toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase(); 22 | } 23 | 24 | public static int countString(String s1, String s2) 25 | { 26 | int count = 0; 27 | 28 | for (int i = -1; (i = s1.indexOf(s2, i + 1)) != -1; ++count) 29 | ; 30 | 31 | return count; 32 | } 33 | 34 | public static String getLetters(String s) 35 | { 36 | String str = ""; 37 | int length = s.length(); 38 | 39 | for (int i = 0; i < length; ++i) { 40 | char ch = s.charAt(i); 41 | 42 | if (isLetter(ch)) 43 | str += ch; 44 | } 45 | 46 | return str; 47 | } 48 | 49 | public static String getLongestPalindrome(String s) 50 | { 51 | String text = ""; 52 | 53 | int endIndex = s.length(); 54 | 55 | while (endIndex != 0) { 56 | int beginIndex = 0; 57 | 58 | while (beginIndex != endIndex) { 59 | String str = s.substring(beginIndex++, endIndex); 60 | 61 | if (str.length() > 1 && StringUtil.isPalindrome(str) && str.length() > text.length()) 62 | text = str; 63 | } 64 | 65 | --endIndex; 66 | } 67 | 68 | return text; 69 | } 70 | 71 | public static String getRandomText(Random r, int n, String mainText) 72 | { 73 | char [] c = new char[n]; 74 | 75 | for (int i = 0; i < n; ++i) { 76 | char ch = mainText.charAt(r.nextInt(mainText.length())); 77 | 78 | if (r.nextBoolean()) 79 | ch = toUpperCase(ch); 80 | 81 | c[i] = ch; 82 | } 83 | 84 | return String.valueOf(c); 85 | } 86 | 87 | public static String getRandomTextEN(int n) 88 | { 89 | return getRandomTextEN(new Random(), n); 90 | } 91 | 92 | public static String getRandomTextEN(Random r, int n) 93 | { 94 | return getRandomText(r, n, "abcdefghijklmnopqrstuwxvyz"); 95 | } 96 | 97 | public static String getRandomTextTR(int n) 98 | { 99 | return getRandomTextTR(new Random(), n); 100 | } 101 | 102 | public static String getRandomTextTR(Random r, int n) 103 | { 104 | return getRandomText(r, n, "abcçdefgğhıijklmnoöprsştuüvyz"); 105 | } 106 | 107 | public static boolean isPalindrome(String s) 108 | { 109 | int left = 0; 110 | int right = s.length() - 1; 111 | 112 | while (left < right) { 113 | char chLeft = toLowerCase(s.charAt(left)); 114 | 115 | if (!isLetter(chLeft)) { 116 | ++left; 117 | continue; 118 | } 119 | 120 | char chRight = toLowerCase(s.charAt(right)); 121 | 122 | if (!isLetter(chRight)) { 123 | --right; 124 | continue; 125 | } 126 | 127 | if (chLeft != chRight) 128 | return false; 129 | 130 | ++left; 131 | --right; 132 | } 133 | 134 | return true; 135 | } 136 | 137 | public static boolean isPangram(String text, String alphabet) 138 | { 139 | int length = alphabet.length(); 140 | 141 | for (int i = 0; i < length; ++i) 142 | if (!text.contains(alphabet.charAt(i) + "")) 143 | return false; 144 | 145 | return true; 146 | } 147 | 148 | public static boolean isPangramEN(String text) 149 | { 150 | return isPangram(text.toLowerCase(), "abcdefghijklmnopqrstuwxvyz"); 151 | } 152 | 153 | public static boolean isPangramTR(String text) 154 | { 155 | return isPangram(text.toLowerCase(), "abcçdefgğhıijklmnoöprsştuüvyz"); 156 | } 157 | 158 | public static String padLeft(String s, int length) 159 | { 160 | return padLeft(s, length, ' '); 161 | } 162 | 163 | public static String padLeft(String s, int length, char ch) 164 | { 165 | length -= s.length(); 166 | 167 | if (length <= 0) 168 | return s; 169 | 170 | return repeat(length, ch) + s; 171 | } 172 | 173 | public static String padRight(String s, int length) 174 | { 175 | return padRight(s, length, ' '); 176 | } 177 | 178 | public static String padRight(String s, int length, char ch) 179 | { 180 | length -= s.length(); 181 | 182 | if (length <= 0) 183 | return s; 184 | 185 | return s + repeat(length, ch); 186 | } 187 | 188 | public static String removeWhitespaces(String s) 189 | { 190 | int length = s.length(); 191 | String str = ""; 192 | 193 | for (int i = 0; i < length; ++i) { 194 | char ch = s.charAt(i); 195 | 196 | if (!isWhitespace(ch)) 197 | str += ch; 198 | } 199 | 200 | return str; 201 | } 202 | 203 | public static String repeat(int n, char ch) 204 | { 205 | char [] c = new char[n]; 206 | 207 | while (n-- > 0) 208 | c[n] = ch; 209 | 210 | return String.valueOf(c); 211 | } 212 | 213 | public static String reverse(String str) 214 | { 215 | char [] c = str.toCharArray(); 216 | 217 | ArrayUtil.reverse(c); 218 | 219 | return String.valueOf(c); 220 | } 221 | 222 | public static String trimLeft(String s) 223 | { 224 | int i; 225 | int len = s.length(); 226 | 227 | for (i = 0; i < len && isWhitespace(s.charAt(i)); ++i) 228 | ; 229 | 230 | return s.substring(i); 231 | } 232 | 233 | public static String trimRight(String s) 234 | { 235 | int i; 236 | 237 | for (i = s.length() - 1; i >= 0 && isWhitespace(s.charAt(i)); --i) 238 | ; 239 | 240 | return s.substring(0, i + 1); 241 | } 242 | } 243 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/App.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Time sınıfı ve test kodu 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.app; 5 | 6 | import org.csystem.util.datetime.Time; 7 | 8 | import java.util.Random; 9 | 10 | class App { 11 | public static void main(String [] args) 12 | { 13 | Random r = new Random(); 14 | 15 | for (int i = 0; i < 10; ++i) { 16 | Time time = Time.randomTime(r); 17 | 18 | System.out.println(time.toLongTimeString()); 19 | } 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/commandpromptapp/CommandPrompt.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.commandpromptapp; 2 | 3 | import org.csystem.util.StringUtil; 4 | 5 | import java.util.Scanner; 6 | 7 | public class CommandPrompt { 8 | private static final String [] COMMAND_STRINGS = {"length", "reverse", "upper", "lower", "change", "clear"}; 9 | private String m_prompt; 10 | private final Scanner m_kb; 11 | 12 | private static String getCommandByText(String text) 13 | { 14 | if (text.length() < 3) 15 | return ""; 16 | 17 | for (String s : COMMAND_STRINGS) 18 | if (s.startsWith(text)) 19 | return s; 20 | 21 | return ""; 22 | } 23 | 24 | private void lengthProc(String [] commandsStr) 25 | { 26 | if (commandsStr.length != 2) { 27 | System.out.println("length bir tane argüman almalıdır"); 28 | return; 29 | } 30 | 31 | System.out.println(commandsStr[1].length()); 32 | } 33 | 34 | private void reverseProc(String [] commandsStr) 35 | { 36 | if (commandsStr.length != 2) { 37 | System.out.println("revere bir tane argüman almalıdır"); 38 | return; 39 | } 40 | 41 | System.out.println(StringUtil.reverse(commandsStr[1])); 42 | } 43 | 44 | private void upperProc(String [] commandsStr) 45 | { 46 | if (commandsStr.length != 2) { 47 | System.out.println("upper bir tane argüman almalıdır"); 48 | return; 49 | } 50 | System.out.println(commandsStr[1].toUpperCase()); 51 | } 52 | 53 | private void lowerProc(String [] commandsStr) 54 | { 55 | if (commandsStr.length != 2) { 56 | System.out.println("lower bir tane argüman almalıdır"); 57 | return; 58 | } 59 | System.out.println(commandsStr[1].toLowerCase()); 60 | } 61 | 62 | private void changeProc(String [] commandsStr) 63 | { 64 | if (commandsStr.length != 2) { 65 | System.out.println("change bir tane argüman almalıdır"); 66 | return; 67 | } 68 | m_prompt = commandsStr[1]; 69 | } 70 | 71 | private void clearProc(String [] commandsStr) 72 | { 73 | for (int i = 0; i < 30; ++i) 74 | System.out.println(); 75 | } 76 | 77 | private void doWorkForCommand(String [] commandInfoStr) 78 | { 79 | switch (commandInfoStr[0]) { 80 | case "length": 81 | lengthProc(commandInfoStr); 82 | break; 83 | case "reverse": 84 | reverseProc(commandInfoStr); 85 | break; 86 | case "upper": 87 | upperProc(commandInfoStr); 88 | break; 89 | case "lower": 90 | lowerProc(commandInfoStr); 91 | break; 92 | case "change": 93 | changeProc(commandInfoStr); 94 | break; 95 | case "clear": 96 | clearProc(commandInfoStr); 97 | break; 98 | } 99 | } 100 | 101 | private void parseCommand(String [] commandInfoStr) 102 | { 103 | String cmd = getCommandByText(commandInfoStr[0]); 104 | 105 | if (!cmd.isEmpty()) { 106 | commandInfoStr[0] = cmd; 107 | doWorkForCommand(commandInfoStr); 108 | } 109 | else 110 | System.out.println("Geçersiz komut"); 111 | } 112 | 113 | public CommandPrompt(String p) 114 | { 115 | m_prompt = p; 116 | m_kb = new Scanner(System.in); 117 | } 118 | 119 | public void run() 120 | { 121 | System.out.println("C ve Sistem Programcıları Derneği"); 122 | System.out.println("Homework-013 sorusuna ilişkin bir iskelet"); 123 | 124 | for (;;) { 125 | System.out.print(m_prompt + ">"); 126 | String cmd = m_kb.nextLine().trim(); 127 | 128 | if (cmd.equals("quit")) 129 | break; 130 | 131 | parseCommand(cmd.split("[ \t]+")); 132 | } 133 | 134 | System.out.println("C ve Sistem Programcıları Derneği"); 135 | System.out.println("Tekrar yapıyor musunuz?"); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/commandpromptapp/CommandPromptApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.commandpromptapp; 2 | 3 | public class CommandPromptApp { 4 | private CommandPromptApp() 5 | { 6 | } 7 | 8 | public static void run() 9 | { 10 | CommandPrompt commandPrompt = new CommandPrompt("CSD"); 11 | 12 | commandPrompt.run(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/dateapp/DateUtil.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.dateapp; 2 | 3 | public class DateUtil { 4 | public static int [] daysOfMonths = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 5 | 6 | public static int getDayOfWeek(int day, int month, int year) 7 | { 8 | int dayOfYear; 9 | 10 | dayOfYear = getDayOfYear(day, month, year); 11 | 12 | if (dayOfYear == -1 || year < 1900) 13 | return -1; 14 | 15 | for (int y = 1900; y < year; ++y) 16 | dayOfYear += isLeapYear(y) ? 366 : 365; 17 | 18 | return dayOfYear % 7; 19 | } 20 | 21 | public static int getDayOfYear(int day, int month, int year) 22 | { 23 | if (!isValidDate(day, month, year)) 24 | return -1; 25 | 26 | int dayOfYear = day; 27 | 28 | for (int m = month - 1; m >= 1; --m) 29 | dayOfYear += daysOfMonths[m]; 30 | 31 | if (month > 2 && isLeapYear(year)) 32 | ++dayOfYear; 33 | 34 | return dayOfYear; 35 | } 36 | 37 | public static boolean isValidDate(int day, int month, int year) 38 | { 39 | if (day < 1 || day > 31 || month < 1 || month > 12) 40 | return false; 41 | 42 | int days = month == 2 && isLeapYear(year) ? 29 : daysOfMonths[month]; 43 | 44 | return day <= days; 45 | } 46 | 47 | public static boolean isLeapYear(int year) 48 | { 49 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/dateapp/DateUtilApp.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Sınıf çalışması: Parametresi ile aldığı int türden gün, ay ve yıl bilgilerine ilişkin tarihin aşağıdaki 3 | açıklamalara göre haftanın hangi gününe geldiğini döndüren getDayOfWeek metodunu yazınız. 4 | Açıklamalar: 5 | - Metot geçersiz bir tarih için -1 değerini döndürecektir. 6 | - Haftanın günü 1.1.1900 ile verilen tarih arasındaki gün sayısının 7 değerine modu alınarak bulunabilir 7 | - 7 değerine mod alındıktan sonra sıfır Pazar, 1 pazartesi, .., 6 cumartesi günü için 8 | elde edilecek değerdir 9 | - 1.1.1900' den önceki tarihler geçersiz kabul edilecektir 10 | ----------------------------------------------------------------------------------------------------------------------*/ 11 | package org.csystem.app.samples.dateapp; 12 | 13 | public class DateUtilApp { 14 | public static String [] daysOfWeek = {"Pazar", "Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma", "Cumartesi"}; 15 | public static String [] monthsTR = {"", "Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", 16 | "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık"}; 17 | 18 | public static String [] monthsEN = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 19 | 20 | public static void displayTR(int day, int month, int year) 21 | { 22 | int dayOfWeek = DateUtil.getDayOfWeek(day, month, year); 23 | 24 | if (dayOfWeek == -1) { 25 | System.out.println("Geçersiz tarih"); 26 | return; 27 | } 28 | 29 | System.out.printf("%02d %s %04d %s%n", day, monthsTR[month], year, daysOfWeek[dayOfWeek]); 30 | 31 | if (dayOfWeek == 0 || dayOfWeek == 6) 32 | System.out.println("Bugün kurs var. Tekrar yaptınız mı?"); 33 | else 34 | System.out.println("Kurs günü yaklaşıyor. Tekrar yapınız!!!!"); 35 | } 36 | 37 | public static void run() 38 | { 39 | java.util.Scanner kb = new java.util.Scanner(System.in); 40 | 41 | for (;;) { 42 | System.out.print("Gün bilgisini giriniz:"); 43 | int day = Integer.parseInt(kb.nextLine()); 44 | 45 | if (day == 0) 46 | break; 47 | 48 | System.out.print("Ay bilgisini giriniz:"); 49 | int month = Integer.parseInt(kb.nextLine()); 50 | 51 | System.out.print("Yıl bilgisini giriniz:"); 52 | int year = Integer.parseInt(kb.nextLine()); 53 | 54 | displayTR(day, month, year); 55 | } 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/examsimulationapp/ExamSimulation.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.examsimulationapp; 2 | 3 | import org.csystem.util.ArrayUtil; 4 | 5 | import java.util.Random; 6 | import java.util.Scanner; 7 | 8 | public class ExamSimulation { 9 | private final String m_lectureName; 10 | private int [][] m_grades; 11 | private double [] m_averages; 12 | private double m_average; 13 | 14 | private void findAverages() 15 | { 16 | int totalNumberOfStudents = 0; 17 | int totalGrades = 0; 18 | 19 | for (int i = 0; i < m_grades.length; ++i) { 20 | int total = ArrayUtil.sum(m_grades[i]); 21 | 22 | m_averages[i] = (double)total / m_grades[i].length; 23 | totalGrades += total; 24 | totalNumberOfStudents += m_grades[i].length; 25 | } 26 | 27 | m_average = (double)totalGrades / totalNumberOfStudents; 28 | } 29 | 30 | private void fillGrades() 31 | { 32 | Random r = new Random(); 33 | Scanner kb = new Scanner(System.in); 34 | System.out.print("Şube sayısını giriniz:"); 35 | m_grades = new int[Integer.parseInt(kb.nextLine())][]; 36 | m_averages = new double[m_grades.length]; 37 | 38 | for (int i = 0; i < m_grades.length; ++i) { 39 | System.out.printf("%d. şube öğrenci sayısını giriniz:", i + 1); 40 | int count = Integer.parseInt(kb.nextLine()); 41 | 42 | m_grades[i] = ArrayUtil.getRandomArray(r, count, 0, 101); 43 | } 44 | } 45 | 46 | public ExamSimulation(String name) 47 | { 48 | m_lectureName = name; 49 | } 50 | 51 | public String getLectureName() 52 | { 53 | return m_lectureName; 54 | } 55 | 56 | public int getGrade(int i, int k) 57 | { 58 | return m_grades[i][k]; 59 | } 60 | 61 | public double getAverage(int i) 62 | { 63 | return m_averages[i]; 64 | } 65 | 66 | public int getNumberOfClasses() 67 | { 68 | return m_averages.length; 69 | } 70 | 71 | public int getNumberOfStudents(int i) 72 | { 73 | return m_grades[i].length; 74 | } 75 | 76 | public double getAverage() 77 | { 78 | return m_average; 79 | } 80 | 81 | public void run() 82 | { 83 | fillGrades(); 84 | findAverages(); 85 | } 86 | 87 | public void displayGrades() 88 | { 89 | System.out.printf("%s dersi sınav notları:%n", m_lectureName); 90 | System.out.println("*************************************************************"); 91 | for (int i = 0; i < m_grades.length; ++i) { 92 | System.out.printf("%d. şube notları:", i + 1); 93 | ArrayUtil.display(3, m_grades[i]); 94 | } 95 | System.out.println("*************************************************************"); 96 | } 97 | 98 | public void displayStatus() 99 | { 100 | displayGrades(); 101 | 102 | System.out.printf("%s dersi için ortalamalar%n", m_lectureName); 103 | System.out.println("*************************************************************"); 104 | for (int i = 0; i < m_averages.length; ++i) 105 | System.out.printf("%d.şube not ortalaması:%f%n", i + 1, m_averages[i]); 106 | 107 | System.out.printf("Okul ortalaması:%f%n", m_average); 108 | System.out.println("*************************************************************"); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/examsimulationapp/ExamSimulationApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.examsimulationapp; 2 | 3 | public class ExamSimulationApp { 4 | private ExamSimulationApp() 5 | { 6 | } 7 | 8 | public static void run() 9 | { 10 | ExamSimulation examSimulation = new ExamSimulation("Matematik"); 11 | 12 | examSimulation.run(); 13 | examSimulation.displayGrades(); 14 | examSimulation.displayStatus(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/lottary/Lottary.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import java.util.Random; 4 | 5 | public class Lottary { 6 | private final Random m_random; 7 | 8 | private boolean [] getFlags() 9 | { 10 | boolean [] flags = new boolean[50]; 11 | int val; 12 | 13 | for (int i = 0; i < 6; ++i) { 14 | for (;;) { 15 | val = m_random.nextInt(49) + 1; 16 | if (!flags[val]) 17 | break; 18 | } 19 | flags[val] = true; 20 | } 21 | 22 | return flags; 23 | } 24 | 25 | private int [] getNumbers(boolean [] flags) 26 | { 27 | int [] a = new int[6]; 28 | 29 | int index = 0; 30 | 31 | for (int i = 1; i < 50; ++i) 32 | if (flags[i]) 33 | a[index++] = i; 34 | 35 | return a; 36 | } 37 | 38 | public Lottary() 39 | { 40 | m_random = new Random(); 41 | } 42 | 43 | public int [] getNumbers() 44 | { 45 | return getNumbers(getFlags()); 46 | } 47 | 48 | public int [][] getNumbers(int n) 49 | { 50 | int [][] numbers = new int[n][]; 51 | 52 | for (int i = 0; i < n; ++i) 53 | numbers[i] = getNumbers(); 54 | 55 | return numbers; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/lottary/LottaryApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import org.csystem.util.ArrayUtil; 4 | 5 | import java.util.Scanner; 6 | 7 | public class LottaryApp { 8 | private LottaryApp() 9 | { 10 | } 11 | 12 | public static void run() 13 | { 14 | Lottary lottary = new Lottary(); 15 | Scanner kb = new Scanner(System.in); 16 | 17 | for (;;) { 18 | System.out.print("Kaç kupon oynamak istersiniz?"); 19 | int n = Integer.parseInt(kb.nextLine()); 20 | 21 | if (n <= 0) 22 | break; 23 | 24 | ArrayUtil.display(2, lottary.getNumbers(n)); 25 | System.out.println("///////////////////"); 26 | } 27 | 28 | System.out.println("Tekrar yapıyor musunuz?"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/studentinfoparserapp/DateUtil.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class DateUtil { 4 | public static int [] daysOfMonths = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 5 | public static String [] daysOfWeekTR = {"Pazar", "Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma", "Cumartesi"}; 6 | 7 | public static String getDayOfWeekTR(int day, int month, int year) 8 | { 9 | return daysOfWeekTR[getDayOfWeek(day, month, year)]; 10 | } 11 | 12 | public static int getDayOfWeek(int day, int month, int year) 13 | { 14 | int dayOfYear; 15 | 16 | dayOfYear = getDayOfYear(day, month, year); 17 | 18 | if (dayOfYear == -1 || year < 1900) 19 | return -1; 20 | 21 | for (int y = 1900; y < year; ++y) 22 | dayOfYear += isLeapYear(y) ? 366 : 365; 23 | 24 | return dayOfYear % 7; 25 | } 26 | 27 | public static int getDayOfYear(int day, int month, int year) 28 | { 29 | if (!isValidDate(day, month, year)) 30 | return -1; 31 | 32 | int dayOfYear = day; 33 | 34 | for (int m = month - 1; m >= 1; --m) 35 | dayOfYear += daysOfMonths[m]; 36 | 37 | if (month > 2 && isLeapYear(year)) 38 | ++dayOfYear; 39 | 40 | return dayOfYear; 41 | } 42 | 43 | public static boolean isValidDate(int day, int month, int year) 44 | { 45 | if (day < 1 || day > 31 || month < 1 || month > 12) 46 | return false; 47 | 48 | int days = month == 2 && isLeapYear(year) ? 29 : daysOfMonths[month]; 49 | 50 | return day <= days; 51 | } 52 | 53 | public static boolean isLeapYear(int year) 54 | { 55 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 56 | } 57 | } -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/studentinfoparserapp/StudentInfo.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class StudentInfo { 4 | public String name; 5 | public String birthDate; 6 | public String lectureName; 7 | public int midtermGrade; 8 | public int finalGrade; 9 | 10 | public double getGrade() 11 | { 12 | return midtermGrade * 0.4 + finalGrade * 0.6; 13 | } 14 | 15 | public String getBirthDateDayOfWeekTR() 16 | { 17 | String [] birthDateInfo = birthDate.split("[/]"); 18 | 19 | return DateUtil.getDayOfWeekTR(Integer.parseInt(birthDateInfo[0]), Integer.parseInt(birthDateInfo[1]), Integer.parseInt(birthDateInfo[2])); 20 | } 21 | 22 | public String toString() 23 | { 24 | double grade = getGrade(); 25 | 26 | return String.format("{name:'%s', birthDate:'%s %s', lectureName:%s, midTermGrade:%d, finalGrade: %d, grade:%f, status:'%s'}", 27 | name, birthDate, getBirthDateDayOfWeekTR(), lectureName, midtermGrade, finalGrade, grade, grade >= 50 ? "Geçti" : "Kaldı"); 28 | } 29 | } -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/studentinfoparserapp/StudentInfoParser.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class StudentInfoParser { 4 | public StudentInfo studentInfo; 5 | 6 | public StudentInfoParser(String str) 7 | { 8 | String [] studentInfoStr = str.split("[:]+"); 9 | //... 10 | 11 | studentInfo = new StudentInfo(); 12 | studentInfo.name = studentInfoStr[0]; 13 | studentInfo.birthDate = studentInfoStr[1]; 14 | studentInfo.lectureName = studentInfoStr[2]; 15 | studentInfo.midtermGrade = Integer.parseInt(studentInfoStr[3]); 16 | studentInfo.finalGrade = Integer.parseInt(studentInfoStr[4]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/app/samples/studentinfoparserapp/StudentInfoParserApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | import java.util.Scanner; 4 | 5 | public class StudentInfoParserApp { 6 | private StudentInfoParserApp() 7 | {} 8 | 9 | public static void run() 10 | { 11 | Scanner kb = new Scanner(System.in); 12 | 13 | for (;;) { 14 | System.out.print("Formatlı yazıyı giriniz:"); 15 | String str = kb.nextLine(); 16 | 17 | if (str.equals("quit")) 18 | break; 19 | 20 | StudentInfoParser studentInfoParser = new StudentInfoParser(str); 21 | 22 | System.out.println(studentInfoParser.studentInfo.toString()); 23 | } 24 | 25 | System.out.println("Tekrar yapıyor musunuz?"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/math/Complex.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Complex sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math; 5 | 6 | public class Complex { 7 | private static Complex add(double re1, double im1, double re2, double im2) 8 | { 9 | Complex z = new Complex(); 10 | 11 | z.re = re1 + re2; 12 | z.im = im1 + im2; 13 | 14 | return z; 15 | } 16 | 17 | private static Complex subtract(double re1, double im1, double re2, double im2) 18 | { 19 | return add(re1, im1, -re2, -im2); 20 | } 21 | 22 | public double re; 23 | public double im; 24 | 25 | public Complex() 26 | { 27 | 28 | } 29 | 30 | public Complex(int a) 31 | { 32 | re = a; 33 | } 34 | 35 | public Complex(int a, int b) 36 | { 37 | re = a; 38 | im = b; 39 | } 40 | 41 | public Complex getConjugate() 42 | { 43 | Complex z = new Complex(); 44 | 45 | z.re = re; 46 | z.im = -im; 47 | 48 | return z; 49 | } 50 | 51 | public double getNorm() 52 | { 53 | return Math.sqrt(re * re + im * im); 54 | } 55 | 56 | //add 57 | public static Complex add(double a, Complex z) 58 | { 59 | return add(a, 0, z.re, z.im); 60 | } 61 | 62 | public Complex add(Complex z) 63 | { 64 | return add(re, im, z.re, z.im); 65 | } 66 | 67 | public Complex add(double a) 68 | { 69 | return add(re, im, a, 0); 70 | } 71 | 72 | //subtract 73 | public static Complex subtract(double a, Complex z) 74 | { 75 | return subtract(a, 0, z.re, z.im); 76 | } 77 | 78 | public Complex subtract(Complex z) 79 | { 80 | return subtract(re, im, z.re, z.im); 81 | } 82 | 83 | public Complex subtract(double a) 84 | { 85 | return subtract(re, im, a, 0); 86 | } 87 | 88 | public void offset(double dxy) 89 | { 90 | offset(dxy, dxy); 91 | } 92 | 93 | public void offset(double dx, double dy) 94 | { 95 | re += dx; 96 | im += dy; 97 | } 98 | 99 | public String toString() 100 | { 101 | return String.format("|%.2f + %.2f * i| = %f", re, im, getNorm()); 102 | } 103 | } 104 | 105 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/math/geometry/Circle.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Circle sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | public class Circle { 7 | private double m_radius; 8 | private double m_pi; 9 | 10 | public Circle() 11 | { 12 | } 13 | 14 | public Circle(double radius) 15 | { 16 | setRadius(radius); 17 | } 18 | 19 | public Circle(double radius, double pi) 20 | { 21 | setRadius(radius, pi); 22 | } 23 | 24 | public double getRadius() 25 | { 26 | return m_radius; 27 | } 28 | 29 | public void setRadius(double radius) 30 | { 31 | setRadius(radius, Math.PI); 32 | } 33 | 34 | public void setRadius(double radius, double pi) 35 | { 36 | m_radius = Math.abs(radius); 37 | setPI(pi); 38 | } 39 | 40 | public double getArea() 41 | { 42 | return m_pi * m_radius * m_radius; 43 | } 44 | 45 | public double getCircumference() 46 | { 47 | return 2 * m_pi * m_radius; 48 | } 49 | 50 | public void setPI(double pi) 51 | { 52 | m_pi = Math.abs(pi); //Kolaylık için mutlak değer aldık 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/math/geometry/Point.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Point sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class Point { 9 | public int x; 10 | public int y; 11 | 12 | public Point() 13 | { 14 | } 15 | 16 | public Point(int a) 17 | { 18 | x = a; 19 | } 20 | 21 | public Point(int a, int b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(int a, int b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(int dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(int dx, int dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %d, y: %d}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/math/geometry/PointF.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | PointF sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class PointF { 9 | public float x; 10 | public float y; 11 | 12 | public PointF() 13 | { 14 | } 15 | 16 | public PointF(float a) 17 | { 18 | x = a; 19 | } 20 | 21 | public PointF(float a, float b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(float a, float b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(float dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(float dx, float dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %f, y: %f}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/util/IntValue.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | IntValue sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | public class IntValue { 7 | private static final IntValue[] CACHE = new IntValue[256]; 8 | private final int m_val; 9 | 10 | private IntValue(int val) 11 | { 12 | m_val = val; 13 | } 14 | 15 | public static final IntValue ZERO = of(0); 16 | public static final IntValue ONE = of(1); 17 | 18 | public static IntValue of(int val) 19 | { 20 | if (val < -128 || val > 127) 21 | return new IntValue(val); 22 | 23 | if (CACHE[val + 128] == null) 24 | CACHE[val + 128] = new IntValue(val); 25 | 26 | return CACHE[val + 128]; 27 | } 28 | 29 | public int getVal() 30 | { 31 | return m_val; 32 | } 33 | 34 | public int compareTo(IntValue other) 35 | { 36 | return m_val - other.m_val; 37 | } 38 | 39 | public IntValue add(int val) 40 | { 41 | return of(m_val + val); 42 | } 43 | 44 | public IntValue add(IntValue intValue) 45 | { 46 | return add(intValue.m_val); 47 | } 48 | 49 | public IntValue subtract(int val) 50 | { 51 | return add(-val); 52 | } 53 | 54 | public IntValue subtract(IntValue intValue) 55 | { 56 | return subtract(intValue.m_val); 57 | } 58 | 59 | 60 | public IntValue multiply(int val) 61 | { 62 | return of(m_val * val); 63 | } 64 | 65 | public IntValue mul(IntValue intValue) 66 | { 67 | return multiply(intValue.m_val); 68 | } 69 | 70 | public IntValue increment() 71 | { 72 | return add(1); 73 | } 74 | 75 | public IntValue decrement() 76 | { 77 | return subtract(1); 78 | } 79 | 80 | public String toString() 81 | { 82 | return m_val + ""; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | StringUtil sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | import java.util.Random; 7 | 8 | import static java.lang.Character.*; 9 | 10 | public class StringUtil { 11 | private static final String ALPHABET_TR = "abcçdefgğhıijklmnoöprsştuüvyz"; 12 | private static final String ALPHABET_EN = "abcdefghijklmnopqrstuwxvyz"; 13 | 14 | private StringUtil() 15 | { 16 | } 17 | 18 | public static String capitalize(String s) 19 | { 20 | if (s.isEmpty()) 21 | return s; 22 | 23 | return toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase(); 24 | } 25 | 26 | public static int countString(String s1, String s2) 27 | { 28 | int count = 0; 29 | 30 | for (int i = -1; (i = s1.indexOf(s2, i + 1)) != -1; ++count) 31 | ; 32 | 33 | return count; 34 | } 35 | 36 | public static String getLetters(String s) 37 | { 38 | String str = ""; 39 | int length = s.length(); 40 | 41 | for (int i = 0; i < length; ++i) { 42 | char ch = s.charAt(i); 43 | 44 | if (isLetter(ch)) 45 | str += ch; 46 | } 47 | 48 | return str; 49 | } 50 | 51 | public static String getLongestPalindrome(String s) 52 | { 53 | String text = ""; 54 | 55 | int endIndex = s.length(); 56 | 57 | while (endIndex != 0) { 58 | int beginIndex = 0; 59 | 60 | while (beginIndex != endIndex) { 61 | String str = s.substring(beginIndex++, endIndex); 62 | 63 | if (str.length() > 1 && StringUtil.isPalindrome(str) && str.length() > text.length()) 64 | text = str; 65 | } 66 | 67 | --endIndex; 68 | } 69 | 70 | return text; 71 | } 72 | 73 | public static String getRandomText(Random r, int n, String mainText) 74 | { 75 | char [] c = new char[n]; 76 | 77 | for (int i = 0; i < n; ++i) { 78 | char ch = mainText.charAt(r.nextInt(mainText.length())); 79 | 80 | if (r.nextBoolean()) 81 | ch = toUpperCase(ch); 82 | 83 | c[i] = ch; 84 | } 85 | 86 | return String.valueOf(c); 87 | } 88 | 89 | public static String getRandomTextEN(int n) 90 | { 91 | return getRandomTextEN(new Random(), n); 92 | } 93 | 94 | public static String getRandomTextEN(Random r, int n) 95 | { 96 | return getRandomText(r, n, ALPHABET_EN); 97 | } 98 | 99 | public static String getRandomTextTR(int n) 100 | { 101 | return getRandomTextTR(new Random(), n); 102 | } 103 | 104 | public static String getRandomTextTR(Random r, int n) 105 | { 106 | return getRandomText(r, n, ALPHABET_TR); 107 | } 108 | 109 | public static boolean isPalindrome(String s) 110 | { 111 | int left = 0; 112 | int right = s.length() - 1; 113 | 114 | while (left < right) { 115 | char chLeft = toLowerCase(s.charAt(left)); 116 | 117 | if (!isLetter(chLeft)) { 118 | ++left; 119 | continue; 120 | } 121 | 122 | char chRight = toLowerCase(s.charAt(right)); 123 | 124 | if (!isLetter(chRight)) { 125 | --right; 126 | continue; 127 | } 128 | 129 | if (chLeft != chRight) 130 | return false; 131 | 132 | ++left; 133 | --right; 134 | } 135 | 136 | return true; 137 | } 138 | 139 | public static boolean isPangram(String text, String alphabet) 140 | { 141 | int length = alphabet.length(); 142 | 143 | for (int i = 0; i < length; ++i) 144 | if (!text.contains(alphabet.charAt(i) + "")) 145 | return false; 146 | 147 | return true; 148 | } 149 | 150 | public static boolean isPangramEN(String text) 151 | { 152 | return isPangram(text.toLowerCase(), ALPHABET_EN); 153 | } 154 | 155 | public static boolean isPangramTR(String text) 156 | { 157 | return isPangram(text.toLowerCase(), ALPHABET_TR); 158 | } 159 | 160 | public static String padLeft(String s, int length) 161 | { 162 | return padLeft(s, length, ' '); 163 | } 164 | 165 | public static String padLeft(String s, int length, char ch) 166 | { 167 | length -= s.length(); 168 | 169 | if (length <= 0) 170 | return s; 171 | 172 | return repeat(length, ch) + s; 173 | } 174 | 175 | public static String padRight(String s, int length) 176 | { 177 | return padRight(s, length, ' '); 178 | } 179 | 180 | public static String padRight(String s, int length, char ch) 181 | { 182 | length -= s.length(); 183 | 184 | if (length <= 0) 185 | return s; 186 | 187 | return s + repeat(length, ch); 188 | } 189 | 190 | public static String removeWhitespaces(String s) 191 | { 192 | int length = s.length(); 193 | String str = ""; 194 | 195 | for (int i = 0; i < length; ++i) { 196 | char ch = s.charAt(i); 197 | 198 | if (!isWhitespace(ch)) 199 | str += ch; 200 | } 201 | 202 | return str; 203 | } 204 | 205 | public static String repeat(int n, char ch) 206 | { 207 | char [] c = new char[n]; 208 | 209 | while (n-- > 0) 210 | c[n] = ch; 211 | 212 | return String.valueOf(c); 213 | } 214 | 215 | public static String reverse(String str) 216 | { 217 | char [] c = str.toCharArray(); 218 | 219 | ArrayUtil.reverse(c); 220 | 221 | return String.valueOf(c); 222 | } 223 | 224 | public static String trimLeft(String s) 225 | { 226 | int i; 227 | int len = s.length(); 228 | 229 | for (i = 0; i < len && isWhitespace(s.charAt(i)); ++i) 230 | ; 231 | 232 | return s.substring(i); 233 | } 234 | 235 | public static String trimRight(String s) 236 | { 237 | int i; 238 | 239 | for (i = s.length() - 1; i >= 0 && isWhitespace(s.charAt(i)); --i) 240 | ; 241 | 242 | return s.substring(0, i + 1); 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /JavaLesson016/org/csystem/util/datetime/Time.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Time sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util.datetime; 5 | 6 | import java.util.Calendar; 7 | import java.util.Random; 8 | 9 | public class Time { 10 | private int m_hour; 11 | private int m_minute; 12 | private int m_second; 13 | private int m_millisecond; 14 | 15 | private static void doWorkForException(String msg) 16 | { 17 | System.out.println(msg); 18 | System.exit(-1); // exception işlemlerine kadar sabredin 19 | } 20 | 21 | private static boolean isValidForBounds(int val, int max) //[0, max] 22 | { 23 | return 0 <= val && val <= max; 24 | } 25 | 26 | private static boolean isValidForHour(int val) 27 | { 28 | return isValidForBounds(val, 23); 29 | } 30 | 31 | private static boolean isValidForMinute(int val) 32 | { 33 | return isValidForBounds(val, 59); 34 | } 35 | 36 | private static boolean isValidForSecond(int val) 37 | { 38 | return isValidForBounds(val, 59); 39 | } 40 | 41 | private static boolean isValidForMillisecond(int val) 42 | { 43 | return isValidForBounds(val, 999); 44 | } 45 | 46 | private static boolean isValidForTime(int hour, int minute, int second, int millisecond) 47 | { 48 | return isValidForHour(hour) && isValidForMinute(minute) 49 | && isValidForSecond(second) && isValidForMillisecond(millisecond); 50 | } 51 | 52 | private static void checkForHour(int val) 53 | { 54 | if (!isValidForHour(val)) 55 | doWorkForException("Invalid hour value"); 56 | } 57 | 58 | private static void checkForMinute(int val) 59 | { 60 | if (!isValidForMinute(val)) 61 | doWorkForException("Invalid minute value"); 62 | } 63 | 64 | private static void checkForSecond(int val) 65 | { 66 | if (!isValidForSecond(val)) 67 | doWorkForException("Invalid second value"); 68 | } 69 | 70 | private static void checkForMillisecond(int val) 71 | { 72 | if (!isValidForMillisecond(val)) 73 | doWorkForException("Invalid millisecond value"); 74 | } 75 | 76 | private static void checkForTime(int hour, int minute, int second, int millisecond) 77 | { 78 | if (!isValidForTime(hour, minute, second, millisecond)) 79 | doWorkForException("Invalid time values"); 80 | } 81 | 82 | public static Time randomTime() 83 | { 84 | return randomTime(new Random()); 85 | } 86 | 87 | public static Time randomTime(Random r) 88 | { 89 | return new Time(r.nextInt(24), r.nextInt(60), r.nextInt(60), r.nextInt(1000)); 90 | } 91 | 92 | public Time() //Sistemden o anki zaman alınıyor. Buradaki yazılan kodu anlamanız gerekmiyor 93 | { 94 | Calendar now = Calendar.getInstance(); 95 | 96 | m_hour = now.get(Calendar.HOUR_OF_DAY); 97 | m_minute = now.get(Calendar.MINUTE); 98 | m_second = now.get(Calendar.SECOND); 99 | m_millisecond = now.get(Calendar.MILLISECOND); 100 | 101 | } 102 | 103 | //... 104 | 105 | public Time(int hour, int minute, int second, int millisecond) 106 | { 107 | checkForTime(hour, minute, second, millisecond); 108 | m_hour = hour; 109 | m_minute = minute; 110 | m_second = second; 111 | m_millisecond = millisecond; 112 | } 113 | 114 | public int getHour() 115 | { 116 | return m_hour; 117 | } 118 | 119 | public void setHour(int val) 120 | { 121 | if (val == m_hour) 122 | return; 123 | 124 | checkForHour(val); 125 | m_hour = val; 126 | } 127 | 128 | public int getMinute() 129 | { 130 | return m_minute; 131 | } 132 | 133 | public void setMinute(int val) 134 | { 135 | if (val == m_minute) 136 | return; 137 | 138 | checkForMinute(val); 139 | m_minute = val; 140 | } 141 | 142 | public int getSecond() 143 | { 144 | return m_second; 145 | } 146 | 147 | public void setSecond(int val) 148 | { 149 | if (val == m_second) 150 | return; 151 | 152 | checkForSecond(val); 153 | m_second = val; 154 | } 155 | 156 | public int getMillisecond() 157 | { 158 | return m_millisecond; 159 | } 160 | 161 | public void setMillisecond(int val) 162 | { 163 | if (val == m_millisecond) 164 | return; 165 | 166 | checkForMillisecond(val); 167 | m_millisecond = val; 168 | } 169 | 170 | public String toString() 171 | { 172 | return String.format("%s:%02d", toShortTimeString(), m_second); 173 | } 174 | 175 | public String toLongTimeString() 176 | { 177 | return String.format("%s.%03d", toString(), m_millisecond); 178 | } 179 | 180 | public String toShortTimeString() 181 | { 182 | return String.format("%02d:%02d", m_hour, m_minute); 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/App.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Homework-015 için bir ipucu. Detaylar gözrmezden gelindi 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.app; 5 | 6 | class App { 7 | public static void main(String [] args) 8 | { 9 | Card [] deck; 10 | 11 | deck = Card.getShuffledDeck(); 12 | 13 | for (Card card : deck) 14 | System.out.printf("%s-%s%n", card.getType(), card.getValue()); 15 | } 16 | } 17 | 18 | class Card 19 | { 20 | private CardValue m_value; 21 | private CardType m_type; 22 | //... 23 | 24 | public Card(CardValue v, CardType t) 25 | { 26 | m_value = v; 27 | m_type = t; 28 | } 29 | 30 | public CardValue getValue() 31 | { 32 | return m_value; 33 | } 34 | 35 | public CardType getType() 36 | { 37 | return m_type; 38 | } 39 | //.... 40 | public static Card[] getShuffledDeck() 41 | { 42 | Card[] deck = new Card[52]; 43 | 44 | int index = 0; 45 | 46 | for (CardType cardType : CardType.values()) 47 | for (CardValue cardValue : CardValue.values()) 48 | deck[index++] = new Card(cardValue, cardType); 49 | 50 | //... 51 | 52 | return deck; 53 | } 54 | 55 | //… 56 | } 57 | 58 | 59 | enum CardType 60 | { 61 | SPADE, CLUB, DIAMOND, HEART 62 | } 63 | 64 | enum CardValue 65 | { 66 | TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, KNAVE, QUEEN, KING, ACE 67 | } 68 | 69 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/samples/commandpromptapp/CommandPrompt.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.commandpromptapp; 2 | 3 | import org.csystem.util.StringUtil; 4 | 5 | import java.util.Scanner; 6 | 7 | public class CommandPrompt { 8 | private static final String [] COMMAND_STRINGS = {"length", "reverse", "upper", "lower", "change", "clear"}; 9 | private String m_prompt; 10 | private final Scanner m_kb; 11 | 12 | private static String getCommandByText(String text) 13 | { 14 | if (text.length() < 3) 15 | return ""; 16 | 17 | for (String s : COMMAND_STRINGS) 18 | if (s.startsWith(text)) 19 | return s; 20 | 21 | return ""; 22 | } 23 | 24 | private void lengthProc(String [] commandsStr) 25 | { 26 | if (commandsStr.length != 2) { 27 | System.out.println("length bir tane argüman almalıdır"); 28 | return; 29 | } 30 | 31 | System.out.println(commandsStr[1].length()); 32 | } 33 | 34 | private void reverseProc(String [] commandsStr) 35 | { 36 | if (commandsStr.length != 2) { 37 | System.out.println("revere bir tane argüman almalıdır"); 38 | return; 39 | } 40 | 41 | System.out.println(StringUtil.reverse(commandsStr[1])); 42 | } 43 | 44 | private void upperProc(String [] commandsStr) 45 | { 46 | if (commandsStr.length != 2) { 47 | System.out.println("upper bir tane argüman almalıdır"); 48 | return; 49 | } 50 | System.out.println(commandsStr[1].toUpperCase()); 51 | } 52 | 53 | private void lowerProc(String [] commandsStr) 54 | { 55 | if (commandsStr.length != 2) { 56 | System.out.println("lower bir tane argüman almalıdır"); 57 | return; 58 | } 59 | System.out.println(commandsStr[1].toLowerCase()); 60 | } 61 | 62 | private void changeProc(String [] commandsStr) 63 | { 64 | if (commandsStr.length != 2) { 65 | System.out.println("change bir tane argüman almalıdır"); 66 | return; 67 | } 68 | m_prompt = commandsStr[1]; 69 | } 70 | 71 | private void clearProc(String [] commandsStr) 72 | { 73 | for (int i = 0; i < 30; ++i) 74 | System.out.println(); 75 | } 76 | 77 | private void doWorkForCommand(String [] commandInfoStr) 78 | { 79 | switch (commandInfoStr[0]) { 80 | case "length": 81 | lengthProc(commandInfoStr); 82 | break; 83 | case "reverse": 84 | reverseProc(commandInfoStr); 85 | break; 86 | case "upper": 87 | upperProc(commandInfoStr); 88 | break; 89 | case "lower": 90 | lowerProc(commandInfoStr); 91 | break; 92 | case "change": 93 | changeProc(commandInfoStr); 94 | break; 95 | case "clear": 96 | clearProc(commandInfoStr); 97 | break; 98 | } 99 | } 100 | 101 | private void parseCommand(String [] commandInfoStr) 102 | { 103 | String cmd = getCommandByText(commandInfoStr[0]); 104 | 105 | if (!cmd.isEmpty()) { 106 | commandInfoStr[0] = cmd; 107 | doWorkForCommand(commandInfoStr); 108 | } 109 | else 110 | System.out.println("Geçersiz komut"); 111 | } 112 | 113 | public CommandPrompt(String p) 114 | { 115 | m_prompt = p; 116 | m_kb = new Scanner(System.in); 117 | } 118 | 119 | public void run() 120 | { 121 | System.out.println("C ve Sistem Programcıları Derneği"); 122 | System.out.println("Homework-013 sorusuna ilişkin bir iskelet"); 123 | 124 | for (;;) { 125 | System.out.print(m_prompt + ">"); 126 | String cmd = m_kb.nextLine().trim(); 127 | 128 | if (cmd.equals("quit")) 129 | break; 130 | 131 | parseCommand(cmd.split("[ \t]+")); 132 | } 133 | 134 | System.out.println("C ve Sistem Programcıları Derneği"); 135 | System.out.println("Tekrar yapıyor musunuz?"); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/samples/commandpromptapp/CommandPromptApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.commandpromptapp; 2 | 3 | public class CommandPromptApp { 4 | private CommandPromptApp() 5 | { 6 | } 7 | 8 | public static void run() 9 | { 10 | CommandPrompt commandPrompt = new CommandPrompt("CSD"); 11 | 12 | commandPrompt.run(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/samples/examsimulationapp/ExamSimulation.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.examsimulationapp; 2 | 3 | import org.csystem.util.ArrayUtil; 4 | 5 | import java.util.Random; 6 | import java.util.Scanner; 7 | 8 | public class ExamSimulation { 9 | private final String m_lectureName; 10 | private int [][] m_grades; 11 | private double [] m_averages; 12 | private double m_average; 13 | 14 | private void findAverages() 15 | { 16 | int totalNumberOfStudents = 0; 17 | int totalGrades = 0; 18 | 19 | for (int i = 0; i < m_grades.length; ++i) { 20 | int total = ArrayUtil.sum(m_grades[i]); 21 | 22 | m_averages[i] = (double)total / m_grades[i].length; 23 | totalGrades += total; 24 | totalNumberOfStudents += m_grades[i].length; 25 | } 26 | 27 | m_average = (double)totalGrades / totalNumberOfStudents; 28 | } 29 | 30 | private void fillGrades() 31 | { 32 | Random r = new Random(); 33 | Scanner kb = new Scanner(System.in); 34 | System.out.print("Şube sayısını giriniz:"); 35 | m_grades = new int[Integer.parseInt(kb.nextLine())][]; 36 | m_averages = new double[m_grades.length]; 37 | 38 | for (int i = 0; i < m_grades.length; ++i) { 39 | System.out.printf("%d. şube öğrenci sayısını giriniz:", i + 1); 40 | int count = Integer.parseInt(kb.nextLine()); 41 | 42 | m_grades[i] = ArrayUtil.getRandomArray(r, count, 0, 101); 43 | } 44 | } 45 | 46 | public ExamSimulation(String name) 47 | { 48 | m_lectureName = name; 49 | } 50 | 51 | public String getLectureName() 52 | { 53 | return m_lectureName; 54 | } 55 | 56 | public int getGrade(int i, int k) 57 | { 58 | return m_grades[i][k]; 59 | } 60 | 61 | public double getAverage(int i) 62 | { 63 | return m_averages[i]; 64 | } 65 | 66 | public int getNumberOfClasses() 67 | { 68 | return m_averages.length; 69 | } 70 | 71 | public int getNumberOfStudents(int i) 72 | { 73 | return m_grades[i].length; 74 | } 75 | 76 | public double getAverage() 77 | { 78 | return m_average; 79 | } 80 | 81 | public void run() 82 | { 83 | fillGrades(); 84 | findAverages(); 85 | } 86 | 87 | public void displayGrades() 88 | { 89 | System.out.printf("%s dersi sınav notları:%n", m_lectureName); 90 | System.out.println("*************************************************************"); 91 | for (int i = 0; i < m_grades.length; ++i) { 92 | System.out.printf("%d. şube notları:", i + 1); 93 | ArrayUtil.display(3, m_grades[i]); 94 | } 95 | System.out.println("*************************************************************"); 96 | } 97 | 98 | public void displayStatus() 99 | { 100 | displayGrades(); 101 | 102 | System.out.printf("%s dersi için ortalamalar%n", m_lectureName); 103 | System.out.println("*************************************************************"); 104 | for (int i = 0; i < m_averages.length; ++i) 105 | System.out.printf("%d.şube not ortalaması:%f%n", i + 1, m_averages[i]); 106 | 107 | System.out.printf("Okul ortalaması:%f%n", m_average); 108 | System.out.println("*************************************************************"); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/samples/examsimulationapp/ExamSimulationApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.examsimulationapp; 2 | 3 | public class ExamSimulationApp { 4 | private ExamSimulationApp() 5 | { 6 | } 7 | 8 | public static void run() 9 | { 10 | ExamSimulation examSimulation = new ExamSimulation("Matematik"); 11 | 12 | examSimulation.run(); 13 | examSimulation.displayGrades(); 14 | examSimulation.displayStatus(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/samples/lottary/Lottary.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import java.util.Random; 4 | 5 | public class Lottary { 6 | private final Random m_random; 7 | 8 | private boolean [] getFlags() 9 | { 10 | boolean [] flags = new boolean[50]; 11 | int val; 12 | 13 | for (int i = 0; i < 6; ++i) { 14 | for (;;) { 15 | val = m_random.nextInt(49) + 1; 16 | if (!flags[val]) 17 | break; 18 | } 19 | flags[val] = true; 20 | } 21 | 22 | return flags; 23 | } 24 | 25 | private int [] getNumbers(boolean [] flags) 26 | { 27 | int [] a = new int[6]; 28 | 29 | int index = 0; 30 | 31 | for (int i = 1; i < 50; ++i) 32 | if (flags[i]) 33 | a[index++] = i; 34 | 35 | return a; 36 | } 37 | 38 | public Lottary() 39 | { 40 | m_random = new Random(); 41 | } 42 | 43 | public int [] getNumbers() 44 | { 45 | return getNumbers(getFlags()); 46 | } 47 | 48 | public int [][] getNumbers(int n) 49 | { 50 | int [][] numbers = new int[n][]; 51 | 52 | for (int i = 0; i < n; ++i) 53 | numbers[i] = getNumbers(); 54 | 55 | return numbers; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/samples/lottary/LottaryApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.lottary; 2 | 3 | import org.csystem.util.ArrayUtil; 4 | 5 | import java.util.Scanner; 6 | 7 | public class LottaryApp { 8 | private LottaryApp() 9 | { 10 | } 11 | 12 | public static void run() 13 | { 14 | Lottary lottary = new Lottary(); 15 | Scanner kb = new Scanner(System.in); 16 | 17 | for (;;) { 18 | System.out.print("Kaç kupon oynamak istersiniz?"); 19 | int n = Integer.parseInt(kb.nextLine()); 20 | 21 | if (n <= 0) 22 | break; 23 | 24 | ArrayUtil.display(2, lottary.getNumbers(n)); 25 | System.out.println("///////////////////"); 26 | } 27 | 28 | System.out.println("Tekrar yapıyor musunuz?"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/samples/studentinfoparserapp/StudentInfo.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | import org.csystem.util.datetime.Date; 4 | 5 | public class StudentInfo { 6 | private String m_name; 7 | private Date m_birthDate; 8 | private String m_lectureName; 9 | private int m_midtermGrade; 10 | private int m_finalGrade; 11 | 12 | public StudentInfo(String name, int day, int mon, int year, String lectureName, int midtermGrade, int finalGrade) 13 | { 14 | //... 15 | m_name = name; 16 | m_birthDate = new Date(day, mon, year); 17 | m_lectureName = lectureName; 18 | m_midtermGrade = midtermGrade; 19 | m_finalGrade = finalGrade; 20 | } 21 | 22 | public StudentInfo(String name, String birthDateStr, String lectureName, int midtermGrade, int finalGrade) 23 | { 24 | //... 25 | m_name = name; 26 | m_lectureName = lectureName; 27 | m_midtermGrade = midtermGrade; 28 | m_finalGrade = finalGrade; 29 | setBirthDate(birthDateStr); 30 | } 31 | 32 | public double getGrade() 33 | { 34 | return m_midtermGrade * 0.4 + m_finalGrade * 0.6; 35 | } 36 | 37 | public void setName(String name) 38 | { 39 | m_name = name; 40 | } 41 | 42 | public void setLectureName(String lectureName) 43 | { 44 | m_lectureName = lectureName; 45 | } 46 | 47 | public void setMidtermGrade(int midtermGrade) 48 | { 49 | m_midtermGrade = midtermGrade; 50 | } 51 | 52 | public void setFinalGrade(int finalGrade) 53 | { 54 | m_finalGrade = finalGrade; 55 | } 56 | 57 | public void setBirthDate(String birthDateStr) 58 | { 59 | String [] birthDateInfo = birthDateStr.split("[/]"); 60 | 61 | m_birthDate = new Date(Integer.parseInt(birthDateInfo[0]), Integer.parseInt(birthDateInfo[1]), Integer.parseInt(birthDateInfo[2])); 62 | } 63 | 64 | public String toString() 65 | { 66 | double grade = getGrade(); 67 | 68 | return String.format("{name:'%s', birthDate:'%s', lectureName:%s, midTermGrade:%d, finalGrade: %d, grade:%f, status:'%s'}", 69 | m_name, m_birthDate.toLongDateStringTR(), m_lectureName, m_midtermGrade, m_finalGrade, grade, grade >= 50 ? "Geçti" : "Kaldı"); 70 | } 71 | } -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/samples/studentinfoparserapp/StudentInfoParser.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | public class StudentInfoParser { 4 | private StudentInfo m_studentInfo; 5 | 6 | public StudentInfoParser(String str) 7 | { 8 | String [] studentInfoStr = str.split("[:]+"); 9 | //... 10 | 11 | m_studentInfo = new StudentInfo(studentInfoStr[0], studentInfoStr[1], studentInfoStr[2], Integer.parseInt(studentInfoStr[3]), Integer.parseInt(studentInfoStr[4])); 12 | } 13 | 14 | public StudentInfo getStudentInfo() 15 | { 16 | return m_studentInfo; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/app/samples/studentinfoparserapp/StudentInfoParserApp.java: -------------------------------------------------------------------------------- 1 | package org.csystem.app.samples.studentinfoparserapp; 2 | 3 | import java.util.Scanner; 4 | 5 | public class StudentInfoParserApp { 6 | private StudentInfoParserApp() 7 | {} 8 | 9 | public static void run() 10 | { 11 | Scanner kb = new Scanner(System.in); 12 | 13 | for (;;) { 14 | System.out.print("Formatlı yazıyı giriniz:"); 15 | String str = kb.nextLine(); 16 | 17 | if (str.equals("quit")) 18 | break; 19 | 20 | StudentInfoParser studentInfoParser = new StudentInfoParser(str); 21 | 22 | System.out.println(studentInfoParser.getStudentInfo().toString()); 23 | } 24 | 25 | System.out.println("Tekrar yapıyor musunuz?"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/math/Complex.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Complex sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math; 5 | 6 | public class Complex { 7 | private static Complex add(double re1, double im1, double re2, double im2) 8 | { 9 | return new Complex(re1 + re2, im1 + im2); 10 | } 11 | 12 | private static Complex subtract(double re1, double im1, double re2, double im2) 13 | { 14 | return add(re1, im1, -re2, -im2); 15 | } 16 | 17 | public double re; 18 | public double im; 19 | 20 | public Complex() 21 | { 22 | 23 | } 24 | 25 | public Complex(double a) 26 | { 27 | re = a; 28 | } 29 | 30 | public Complex(double a, double b) 31 | { 32 | re = a; 33 | im = b; 34 | } 35 | 36 | public Complex getConjugate() 37 | { 38 | Complex z = new Complex(); 39 | 40 | z.re = re; 41 | z.im = -im; 42 | 43 | return z; 44 | } 45 | 46 | public double getNorm() 47 | { 48 | return Math.sqrt(re * re + im * im); 49 | } 50 | 51 | //add 52 | public static Complex add(double a, Complex z) 53 | { 54 | return add(a, 0, z.re, z.im); 55 | } 56 | 57 | public Complex add(Complex z) 58 | { 59 | return add(re, im, z.re, z.im); 60 | } 61 | 62 | public Complex add(double a) 63 | { 64 | return add(re, im, a, 0); 65 | } 66 | 67 | //subtract 68 | public static Complex subtract(double a, Complex z) 69 | { 70 | return subtract(a, 0, z.re, z.im); 71 | } 72 | 73 | public Complex subtract(Complex z) 74 | { 75 | return subtract(re, im, z.re, z.im); 76 | } 77 | 78 | public Complex subtract(double a) 79 | { 80 | return subtract(re, im, a, 0); 81 | } 82 | 83 | public void offset(double dxy) 84 | { 85 | offset(dxy, dxy); 86 | } 87 | 88 | public void offset(double dx, double dy) 89 | { 90 | re += dx; 91 | im += dy; 92 | } 93 | 94 | public String toString() 95 | { 96 | return String.format("|%.2f + %.2f * i| = %f", re, im, getNorm()); 97 | } 98 | } 99 | 100 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/math/Rational.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Rational sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math; 5 | 6 | public class Rational { 7 | private int m_a; 8 | private int m_b; 9 | 10 | private static Rational add(int a1, int b1, int a2, int b2) 11 | { 12 | return new Rational(a1 * b2 + a2 * b1, b1 * b2); 13 | } 14 | 15 | private static Rational subtract(int a1, int b1, int a2, int b2) 16 | { 17 | return add(a1, b1, -a2, b2); 18 | } 19 | 20 | private static Rational multiply(int a1, int b1, int a2, int b2) 21 | { 22 | return new Rational(a1 * a2, b1 * b2); 23 | } 24 | 25 | private static Rational divide(int a1, int b1, int a2, int b2) 26 | { 27 | return multiply(a1, b1, b2, a2); 28 | } 29 | 30 | private static void check(int a, int b) 31 | { 32 | if (b == 0) { 33 | if (a == 0) 34 | System.out.println("Belirsiz"); 35 | else 36 | System.out.println("Tanımsız"); 37 | 38 | System.exit(-1); //Exception işlemlerine kadar sabredin 39 | } 40 | } 41 | 42 | private void simplify() 43 | { 44 | int min = Math.min(Math.abs(m_a), m_b); 45 | 46 | for (int i = min; i >= 2; --i) 47 | if (m_a % i == 0 && m_b % i == 0) { 48 | m_a /= i; 49 | m_b /= i; 50 | break; 51 | } 52 | } 53 | 54 | private void configureSign() 55 | { 56 | if (m_b < 0) { 57 | m_a = -m_a; 58 | m_b = -m_b; 59 | } 60 | } 61 | 62 | private void set(int a, int b) 63 | { 64 | if (a == 0) { 65 | m_a = 0; 66 | m_b = 1; 67 | return; 68 | } 69 | m_a = a; 70 | m_b = b; 71 | configureSign(); 72 | simplify(); 73 | } 74 | 75 | public Rational() 76 | { 77 | m_b = 1; 78 | } 79 | 80 | public Rational(int a) 81 | { 82 | m_a = a; 83 | m_b = 1; 84 | } 85 | 86 | public Rational(int a, int b) 87 | { 88 | check(a, b); 89 | set(a, b); 90 | } 91 | 92 | public int getNumerator() 93 | { 94 | return m_a; 95 | } 96 | 97 | public void setNumerator(int val) 98 | { 99 | if (val == m_a) 100 | return; 101 | 102 | set(val, m_b); 103 | } 104 | 105 | public void setDenominator(int val) 106 | { 107 | if (val == m_b) 108 | return; 109 | 110 | check(m_a, val); 111 | set(m_a, val); 112 | } 113 | 114 | public int getDenominator() 115 | { 116 | return m_b; 117 | } 118 | 119 | public double getRealValue() 120 | { 121 | return (double) m_a / m_b; 122 | } 123 | 124 | //add 125 | public Rational add(Rational r) 126 | { 127 | return add(m_a, m_b, r.m_a, r.m_b); 128 | } 129 | 130 | public Rational add(int val) 131 | { 132 | return add(m_a, m_b, val, 1); 133 | } 134 | 135 | public static Rational add(int val, Rational r) 136 | { 137 | return add(val, 1, r.m_a, r.m_b); 138 | } 139 | 140 | //subtract 141 | public Rational subtract(Rational r) 142 | { 143 | return subtract(m_a, m_b, r.m_a, r.m_b); 144 | } 145 | 146 | public Rational subtract(int val) 147 | { 148 | return subtract(m_a, m_b, val, 1); 149 | } 150 | 151 | public static Rational subtract(int val, Rational r) 152 | { 153 | return subtract(val, 1, r.m_a, r.m_b); 154 | } 155 | 156 | //multiply 157 | public Rational multiply(Rational r) 158 | { 159 | return multiply(m_a, m_b, r.m_a, r.m_b); 160 | } 161 | 162 | public Rational multiply(int val) 163 | { 164 | return multiply(m_a, m_b, val, 1); 165 | } 166 | 167 | public static Rational multiply(int val, Rational r) 168 | { 169 | return multiply(val, 1, r.m_a, r.m_b); 170 | } 171 | 172 | //divide 173 | public Rational divide(Rational r) 174 | { 175 | return divide(m_a, m_b, r.m_a, r.m_b); 176 | } 177 | 178 | public Rational divide(int val) 179 | { 180 | return divide(m_a, m_b, val, 1); 181 | } 182 | 183 | public static Rational divide(int val, Rational r) 184 | { 185 | return divide(val, 1, r.m_a, r.m_b); 186 | } 187 | 188 | //increment 189 | public void increment(int val) 190 | { 191 | m_a += m_b * val; 192 | } 193 | 194 | public void increment() 195 | { 196 | increment(1); 197 | } 198 | 199 | //decrement 200 | public void decrement(int val) 201 | { 202 | increment(-val); 203 | } 204 | 205 | public void decrement() 206 | { 207 | decrement(1); 208 | } 209 | 210 | public String toString() 211 | { 212 | return String.format("%d / %d = %f", m_a, m_b, getRealValue()); 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/math/geometry/Circle.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Circle sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | public class Circle { 7 | private double m_radius; 8 | private double m_pi; 9 | 10 | public Circle() 11 | { 12 | } 13 | 14 | public Circle(double radius) 15 | { 16 | setRadius(radius); 17 | } 18 | 19 | public Circle(double radius, double pi) 20 | { 21 | setRadius(radius, pi); 22 | } 23 | 24 | public double getRadius() 25 | { 26 | return m_radius; 27 | } 28 | 29 | public void setRadius(double radius) 30 | { 31 | setRadius(radius, Math.PI); 32 | } 33 | 34 | public void setRadius(double radius, double pi) 35 | { 36 | m_radius = Math.abs(radius); 37 | setPI(pi); 38 | } 39 | 40 | public double getArea() 41 | { 42 | return m_pi * m_radius * m_radius; 43 | } 44 | 45 | public double getCircumference() 46 | { 47 | return 2 * m_pi * m_radius; 48 | } 49 | 50 | public void setPI(double pi) 51 | { 52 | m_pi = Math.abs(pi); //Kolaylık için mutlak değer aldık 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/math/geometry/Point.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Point sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class Point { 9 | public int x; 10 | public int y; 11 | 12 | public Point() 13 | { 14 | } 15 | 16 | public Point(int a) 17 | { 18 | x = a; 19 | } 20 | 21 | public Point(int a, int b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(int a, int b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(int dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(int dx, int dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %d, y: %d}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/math/geometry/PointF.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | PointF sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.math.geometry; 5 | 6 | import static java.lang.Math.sqrt; 7 | 8 | public class PointF { 9 | public float x; 10 | public float y; 11 | 12 | public PointF() 13 | { 14 | } 15 | 16 | public PointF(float a) 17 | { 18 | x = a; 19 | } 20 | 21 | public PointF(float a, float b) 22 | { 23 | x = a; 24 | y = b; 25 | } 26 | 27 | public double distance(Point p) 28 | { 29 | return distance(p.x, p.y); 30 | } 31 | 32 | public double distance(float a, float b) 33 | { 34 | return sqrt((x - a) * (x - a) + (y - b) * (y - b)); 35 | } 36 | 37 | public void offset(float dxy) 38 | { 39 | offset(dxy, dxy); 40 | } 41 | 42 | public void offset(float dx, float dy) 43 | { 44 | x += dx; 45 | y += dy; 46 | } 47 | 48 | public String toString() 49 | { 50 | return String.format("{x: %f, y: %f}", x, y); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/util/IntValue.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | IntValue sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | public class IntValue { 7 | private static final IntValue[] CACHE = new IntValue[256]; 8 | private final int m_val; 9 | 10 | private IntValue(int val) 11 | { 12 | m_val = val; 13 | } 14 | 15 | public static final IntValue ZERO = of(0); 16 | public static final IntValue ONE = of(1); 17 | 18 | public static IntValue of(int val) 19 | { 20 | if (val < -128 || val > 127) 21 | return new IntValue(val); 22 | 23 | if (CACHE[val + 128] == null) 24 | CACHE[val + 128] = new IntValue(val); 25 | 26 | return CACHE[val + 128]; 27 | } 28 | 29 | public int getVal() 30 | { 31 | return m_val; 32 | } 33 | 34 | public int compareTo(IntValue other) 35 | { 36 | return m_val - other.m_val; 37 | } 38 | 39 | public IntValue add(int val) 40 | { 41 | return of(m_val + val); 42 | } 43 | 44 | public IntValue add(IntValue intValue) 45 | { 46 | return add(intValue.m_val); 47 | } 48 | 49 | public IntValue subtract(int val) 50 | { 51 | return add(-val); 52 | } 53 | 54 | public IntValue subtract(IntValue intValue) 55 | { 56 | return subtract(intValue.m_val); 57 | } 58 | 59 | 60 | public IntValue multiply(int val) 61 | { 62 | return of(m_val * val); 63 | } 64 | 65 | public IntValue mul(IntValue intValue) 66 | { 67 | return multiply(intValue.m_val); 68 | } 69 | 70 | public IntValue increment() 71 | { 72 | return add(1); 73 | } 74 | 75 | public IntValue decrement() 76 | { 77 | return subtract(1); 78 | } 79 | 80 | public String toString() 81 | { 82 | return m_val + ""; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | StringUtil sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util; 5 | 6 | import java.util.Random; 7 | 8 | import static java.lang.Character.*; 9 | 10 | public class StringUtil { 11 | private static final String ALPHABET_TR = "abcçdefgğhıijklmnoöprsştuüvyz"; 12 | private static final String ALPHABET_EN = "abcdefghijklmnopqrstuwxvyz"; 13 | 14 | private StringUtil() 15 | { 16 | } 17 | 18 | public static String capitalize(String s) 19 | { 20 | if (s.isEmpty()) 21 | return s; 22 | 23 | return toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase(); 24 | } 25 | 26 | public static int countString(String s1, String s2) 27 | { 28 | int count = 0; 29 | 30 | for (int i = -1; (i = s1.indexOf(s2, i + 1)) != -1; ++count) 31 | ; 32 | 33 | return count; 34 | } 35 | 36 | public static String getLetters(String s) 37 | { 38 | String str = ""; 39 | int length = s.length(); 40 | 41 | for (int i = 0; i < length; ++i) { 42 | char ch = s.charAt(i); 43 | 44 | if (isLetter(ch)) 45 | str += ch; 46 | } 47 | 48 | return str; 49 | } 50 | 51 | public static String getLongestPalindrome(String s) 52 | { 53 | String text = ""; 54 | 55 | int endIndex = s.length(); 56 | 57 | while (endIndex != 0) { 58 | int beginIndex = 0; 59 | 60 | while (beginIndex != endIndex) { 61 | String str = s.substring(beginIndex++, endIndex); 62 | 63 | if (str.length() > 1 && StringUtil.isPalindrome(str) && str.length() > text.length()) 64 | text = str; 65 | } 66 | 67 | --endIndex; 68 | } 69 | 70 | return text; 71 | } 72 | 73 | public static String getRandomText(Random r, int n, String mainText) 74 | { 75 | char [] c = new char[n]; 76 | 77 | for (int i = 0; i < n; ++i) { 78 | char ch = mainText.charAt(r.nextInt(mainText.length())); 79 | 80 | if (r.nextBoolean()) 81 | ch = toUpperCase(ch); 82 | 83 | c[i] = ch; 84 | } 85 | 86 | return String.valueOf(c); 87 | } 88 | 89 | public static String getRandomTextEN(int n) 90 | { 91 | return getRandomTextEN(new Random(), n); 92 | } 93 | 94 | public static String getRandomTextEN(Random r, int n) 95 | { 96 | return getRandomText(r, n, ALPHABET_EN); 97 | } 98 | 99 | public static String getRandomTextTR(int n) 100 | { 101 | return getRandomTextTR(new Random(), n); 102 | } 103 | 104 | public static String getRandomTextTR(Random r, int n) 105 | { 106 | return getRandomText(r, n, ALPHABET_TR); 107 | } 108 | 109 | public static boolean isPalindrome(String s) 110 | { 111 | int left = 0; 112 | int right = s.length() - 1; 113 | 114 | while (left < right) { 115 | char chLeft = toLowerCase(s.charAt(left)); 116 | 117 | if (!isLetter(chLeft)) { 118 | ++left; 119 | continue; 120 | } 121 | 122 | char chRight = toLowerCase(s.charAt(right)); 123 | 124 | if (!isLetter(chRight)) { 125 | --right; 126 | continue; 127 | } 128 | 129 | if (chLeft != chRight) 130 | return false; 131 | 132 | ++left; 133 | --right; 134 | } 135 | 136 | return true; 137 | } 138 | 139 | public static boolean isPangram(String text, String alphabet) 140 | { 141 | int length = alphabet.length(); 142 | 143 | for (int i = 0; i < length; ++i) 144 | if (!text.contains(alphabet.charAt(i) + "")) 145 | return false; 146 | 147 | return true; 148 | } 149 | 150 | public static boolean isPangramEN(String text) 151 | { 152 | return isPangram(text.toLowerCase(), ALPHABET_EN); 153 | } 154 | 155 | public static boolean isPangramTR(String text) 156 | { 157 | return isPangram(text.toLowerCase(), ALPHABET_TR); 158 | } 159 | 160 | public static String padLeft(String s, int length) 161 | { 162 | return padLeft(s, length, ' '); 163 | } 164 | 165 | public static String padLeft(String s, int length, char ch) 166 | { 167 | length -= s.length(); 168 | 169 | if (length <= 0) 170 | return s; 171 | 172 | return repeat(length, ch) + s; 173 | } 174 | 175 | public static String padRight(String s, int length) 176 | { 177 | return padRight(s, length, ' '); 178 | } 179 | 180 | public static String padRight(String s, int length, char ch) 181 | { 182 | length -= s.length(); 183 | 184 | if (length <= 0) 185 | return s; 186 | 187 | return s + repeat(length, ch); 188 | } 189 | 190 | public static String removeWhitespaces(String s) 191 | { 192 | int length = s.length(); 193 | String str = ""; 194 | 195 | for (int i = 0; i < length; ++i) { 196 | char ch = s.charAt(i); 197 | 198 | if (!isWhitespace(ch)) 199 | str += ch; 200 | } 201 | 202 | return str; 203 | } 204 | 205 | public static String repeat(int n, char ch) 206 | { 207 | char [] c = new char[n]; 208 | 209 | while (n-- > 0) 210 | c[n] = ch; 211 | 212 | return String.valueOf(c); 213 | } 214 | 215 | public static String reverse(String str) 216 | { 217 | char [] c = str.toCharArray(); 218 | 219 | ArrayUtil.reverse(c); 220 | 221 | return String.valueOf(c); 222 | } 223 | 224 | public static String trimLeft(String s) 225 | { 226 | int i; 227 | int len = s.length(); 228 | 229 | for (i = 0; i < len && isWhitespace(s.charAt(i)); ++i) 230 | ; 231 | 232 | return s.substring(i); 233 | } 234 | 235 | public static String trimRight(String s) 236 | { 237 | int i; 238 | 239 | for (i = s.length() - 1; i >= 0 && isWhitespace(s.charAt(i)); --i) 240 | ; 241 | 242 | return s.substring(0, i + 1); 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/util/datetime/DateTime.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | DateTime sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util.datetime; 5 | 6 | public class DateTime { 7 | //... 8 | } 9 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/util/datetime/DayOfWeek.java: -------------------------------------------------------------------------------- 1 | package org.csystem.util.datetime; 2 | 3 | public enum DayOfWeek { 4 | SUN("Pazar", "Sun"), MON("Pazartesi", "Mon"), TUE("Salı", "Tue"), WED("Çarşamba", "Wed"), 5 | THU("Perşembe", "Thu"), FRI("Cuma", "Fri"), SAT("Cumartesi", "Sat"); 6 | 7 | DayOfWeek(String nameTR, String nameEN) 8 | { 9 | NAME_TR = nameTR; 10 | NAME_EN = nameEN; 11 | } 12 | 13 | final String NAME_TR, NAME_EN; 14 | } -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/util/datetime/Month.java: -------------------------------------------------------------------------------- 1 | package org.csystem.util.datetime; 2 | 3 | public enum Month { 4 | JAN(31), FEB(28), MAR(31), APR(30), MAY(31), JUN(30), 5 | JUL(31), AUG(31), SEP(30), OCT(31), NOV(30), DEC(31); 6 | 7 | private final int m_days; 8 | 9 | Month(int days) 10 | { 11 | m_days = days; 12 | } 13 | 14 | static boolean isLeapYear(int year) 15 | { 16 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 17 | } 18 | 19 | int getDaysByYear(int year) 20 | { 21 | return ordinal() == 1 && isLeapYear(year) ? 29 : m_days; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /JavaLesson017/org/csystem/util/datetime/Time.java: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------------------------------------------- 2 | Time sınıfı 3 | ----------------------------------------------------------------------------------------------------------------------*/ 4 | package org.csystem.util.datetime; 5 | 6 | import java.util.Calendar; 7 | import java.util.Random; 8 | 9 | public class Time { 10 | private int m_hour; 11 | private int m_minute; 12 | private int m_second; 13 | private int m_millisecond; 14 | 15 | private static void doWorkForException(String msg) 16 | { 17 | System.out.println(msg); 18 | System.exit(-1); // exception işlemlerine kadar sabredin 19 | } 20 | 21 | private static boolean isValidForBounds(int val, int max) //[0, max] 22 | { 23 | return 0 <= val && val <= max; 24 | } 25 | 26 | private static boolean isValidForHour(int val) 27 | { 28 | return isValidForBounds(val, 23); 29 | } 30 | 31 | private static boolean isValidForMinute(int val) 32 | { 33 | return isValidForBounds(val, 59); 34 | } 35 | 36 | private static boolean isValidForSecond(int val) 37 | { 38 | return isValidForBounds(val, 59); 39 | } 40 | 41 | private static boolean isValidForMillisecond(int val) 42 | { 43 | return isValidForBounds(val, 999); 44 | } 45 | 46 | private static boolean isValidForTime(int hour, int minute, int second, int millisecond) 47 | { 48 | return isValidForHour(hour) && isValidForMinute(minute) 49 | && isValidForSecond(second) && isValidForMillisecond(millisecond); 50 | } 51 | 52 | private static void checkForHour(int val) 53 | { 54 | if (!isValidForHour(val)) 55 | doWorkForException("Invalid hour value"); 56 | } 57 | 58 | private static void checkForMinute(int val) 59 | { 60 | if (!isValidForMinute(val)) 61 | doWorkForException("Invalid minute value"); 62 | } 63 | 64 | private static void checkForSecond(int val) 65 | { 66 | if (!isValidForSecond(val)) 67 | doWorkForException("Invalid second value"); 68 | } 69 | 70 | private static void checkForMillisecond(int val) 71 | { 72 | if (!isValidForMillisecond(val)) 73 | doWorkForException("Invalid millisecond value"); 74 | } 75 | 76 | private static void checkForTime(int hour, int minute, int second, int millisecond) 77 | { 78 | if (!isValidForTime(hour, minute, second, millisecond)) 79 | doWorkForException("Invalid time values"); 80 | } 81 | 82 | public static Time randomTime() 83 | { 84 | return randomTime(new Random()); 85 | } 86 | 87 | public static Time randomTime(Random r) 88 | { 89 | return new Time(r.nextInt(24), r.nextInt(60), r.nextInt(60), r.nextInt(1000)); 90 | } 91 | 92 | public Time() //Sistemden o anki zaman alınıyor. Buradaki yazılan kodu anlamanız gerekmiyor 93 | { 94 | Calendar now = Calendar.getInstance(); 95 | 96 | m_hour = now.get(Calendar.HOUR_OF_DAY); 97 | m_minute = now.get(Calendar.MINUTE); 98 | m_second = now.get(Calendar.SECOND); 99 | m_millisecond = now.get(Calendar.MILLISECOND); 100 | } 101 | 102 | //... 103 | 104 | public Time(int hour, int minute, int second, int millisecond) 105 | { 106 | checkForTime(hour, minute, second, millisecond); 107 | m_hour = hour; 108 | m_minute = minute; 109 | m_second = second; 110 | m_millisecond = millisecond; 111 | } 112 | 113 | public int getHour() 114 | { 115 | return m_hour; 116 | } 117 | 118 | public void setHour(int val) 119 | { 120 | if (val == m_hour) 121 | return; 122 | 123 | checkForHour(val); 124 | m_hour = val; 125 | } 126 | 127 | public int getMinute() 128 | { 129 | return m_minute; 130 | } 131 | 132 | public void setMinute(int val) 133 | { 134 | if (val == m_minute) 135 | return; 136 | 137 | checkForMinute(val); 138 | m_minute = val; 139 | } 140 | 141 | public int getSecond() 142 | { 143 | return m_second; 144 | } 145 | 146 | public void setSecond(int val) 147 | { 148 | if (val == m_second) 149 | return; 150 | 151 | checkForSecond(val); 152 | m_second = val; 153 | } 154 | 155 | public int getMillisecond() 156 | { 157 | return m_millisecond; 158 | } 159 | 160 | public void setMillisecond(int val) 161 | { 162 | if (val == m_millisecond) 163 | return; 164 | 165 | checkForMillisecond(val); 166 | m_millisecond = val; 167 | } 168 | 169 | public String toString() 170 | { 171 | return String.format("%s:%02d", toShortTimeString(), m_second); 172 | } 173 | 174 | public String toLongTimeString() 175 | { 176 | return String.format("%s.%03d", toString(), m_millisecond); 177 | } 178 | 179 | public String toShortTimeString() 180 | { 181 | return String.format("%02d:%02d", m_hour, m_minute); 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /JavaLesson027/java027.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /JavaLesson028/java028.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /JavaLesson029/java029.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /JavaLesson030/java030.md: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------------------------------------------------