|
| 1 | +1. Klavyeden alınan int türden üç sayı arasındaki büyüklük küçüklük ilişkisini küçükten büyüğe doğru < ve = sembolleriyle gösteriniz: |
| 2 | + |
| 3 | +Açıklama : Program üç tane int türden sayı isteyecek, aralarındaki ilişkiyi ekranda gösterecek. İşte birkaç örnek : |
| 4 | + |
| 5 | +- [x] Giriş: 10 20 30 |
| 6 | + |
| 7 | +- [ ] Yanıt: 10 < 20 < 30 |
| 8 | + |
| 9 | +- [x] Giriş: 30 10 20 |
| 10 | + |
| 11 | +- [ ] Yanıt: 10 < 20 < 30 |
| 12 | + |
| 13 | +- [x] Giriş: 10 10 15 |
| 14 | + |
| 15 | +- [ ] Yanıt: 10 = 10 < 15 |
| 16 | + |
| 17 | +- [x] Giriş: 40 50 50 |
| 18 | + |
| 19 | +- [ ] Yanıt: 40 < 50 = 50 |
| 20 | + |
| 21 | +2. Parametresi ile ald ığı int türden 3 sayıdan ortancasına geri dönen mid isimli metodu yazınız ve test ediniz. |
| 22 | +3. Parametresi ile aldığı int türden bir sayının negatif mi, 0(sıfır) mı, pozitif mi olduğunu test eden s ignum isimli metodu yazınız ve test ediniz. Metot pozitif iç in 1(bir), negatif için 1(eksi bir) ve sıfır için 0(sıfır) döndürecektir. |
| 23 | + |
| 24 | +```java |
| 25 | +package csd; |
| 26 | + |
| 27 | +class App { |
| 28 | + public static void main(String [] args) |
| 29 | + { |
| 30 | + CmpTreeNumTest.run(); |
| 31 | + SigNumTest.run(); |
| 32 | + GetMidTest.run(); |
| 33 | + |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class SigNumTest { |
| 38 | + public static void run() |
| 39 | + { |
| 40 | + java.util.Scanner kb = new java.util.Scanner(System.in); |
| 41 | + System.out.println("Bir tamsayı sayı giriniz:"); |
| 42 | + int ival = Integer.parseInt(kb.nextLine()); |
| 43 | + |
| 44 | + Homework.displaySigNum(ival); |
| 45 | + |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class CmpTreeNumTest{ |
| 50 | + public static void run() |
| 51 | + { |
| 52 | + java.util.Scanner kb = new java.util.Scanner(System.in); |
| 53 | + System.out.println("Üç tamsayı sayı giriniz:"); |
| 54 | + int ival1 = Integer.parseInt(kb.nextLine()); |
| 55 | + |
| 56 | + int ival2 = Integer.parseInt(kb.nextLine()); |
| 57 | + |
| 58 | + int ival3 = Integer.parseInt(kb.nextLine()); |
| 59 | + |
| 60 | + Homework.cmpTreeNum(ival1, ival2, ival3); |
| 61 | + |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class GetMidTest { |
| 66 | + public static void run() |
| 67 | + { |
| 68 | + java.util.Scanner kb = new java.util.Scanner(System.in); |
| 69 | + System.out.println("Üç tamsayı sayı giriniz:"); |
| 70 | + int ival1 = Integer.parseInt(kb.nextLine()); |
| 71 | + |
| 72 | + int ival2 = Integer.parseInt(kb.nextLine()); |
| 73 | + |
| 74 | + int ival3 = Integer.parseInt(kb.nextLine()); |
| 75 | + |
| 76 | + System.out.printf("mid sayısı %d%n",Homework.getMid(ival1, ival2, ival3)); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +class Homework { |
| 81 | + public static void displaySigNum(int val) |
| 82 | + { |
| 83 | + System.out.printf("signum: %d%n",sigNum(val)); |
| 84 | + } |
| 85 | + |
| 86 | + public static void displayComparedNum(int min,int mid,int max) |
| 87 | + { |
| 88 | + |
| 89 | + char sigMinMid = '='; |
| 90 | + char sigMidMax = '='; |
| 91 | + if (min != mid) |
| 92 | + sigMinMid = '<'; |
| 93 | + if(mid != max) |
| 94 | + sigMidMax = '<'; |
| 95 | + System.out.printf("[%d] %c [%d] %c [%d]%n",min,sigMinMid,mid,sigMidMax,max); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + public static void cmpTreeNum(int ival1,int ival2,int ival3) |
| 100 | + { |
| 101 | + int min = Math.min(Math.min(ival1, ival2),ival3); |
| 102 | + int max = Math.max(Math.max(ival1,ival2),ival3); |
| 103 | + int mid = getMid(ival1,ival2,ival3); |
| 104 | + displayComparedNum(min,mid,max); |
| 105 | + } |
| 106 | + |
| 107 | + public static int getMid(int ival1,int ival2,int ival3) |
| 108 | + { |
| 109 | + int mid = -1; |
| 110 | + if (ival1 <= ival2 && ival2 <= ival3 || ival3 <= ival2 && ival2 <= ival1) |
| 111 | + mid = ival2; |
| 112 | + else if (ival2 <= ival1 && ival1 <= ival3 || ival3 <= ival1 && ival1 <= ival2) |
| 113 | + mid = ival1; |
| 114 | + else if (ival1 <= ival3 && ival3 <= ival2 || ival2 <= ival3 && ival3 <= ival1) |
| 115 | + mid = ival3; |
| 116 | + |
| 117 | + return mid; |
| 118 | + } |
| 119 | + |
| 120 | + public static int sigNum(int ival) |
| 121 | + { |
| 122 | + if(ival != 0) |
| 123 | + if (ival > 0) |
| 124 | + ival /=ival; |
| 125 | + else |
| 126 | + ival /=-ival; |
| 127 | + |
| 128 | + return ival; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +``` |
0 commit comments