P10Tablice.java
|
---|
P11Wewy.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
// P11Wewy.java package p11wewy; import java.util.*; import java.io.*; public class P11Wewy { public static void main(String[] args) throws IOException { InputStreamReader we = new InputStreamReader(System.in); BufferedReader in1 = new BufferedReader(we); Locale pl = Locale.getDefault(); String s; System.out.println("Podaj rozmiar tablicy: "); s = in1.readLine(); System.out.println("String " + s); System.out.println("Dlugosc tekstu= " + s.length()); Integer a = Integer.valueOf(s); int aa = a.intValue(); System.out.println("Wprowadzone z stdin = " + aa); // Od wersji 5 Scanner sc = new Scanner(System.in); System.out.println("I jeszcz raz: "); aa = sc.nextInt(); System.out.println("Druga wprowadzona = " + aa); int[] b = new int[aa]; for(int i=0; i< b.length; i++) { b[i]= i*i; System.out.println("b[" + i + "]= " + b[i]); } System.out.println("Czytanie z pliku " + args[0]); BufferedReader in2 = new BufferedReader(new FileReader(args[0])); Float c = Float.valueOf(in2.readLine()); float d = Float.parseFloat(in2.readLine()); /* Locale.setDefault(Locale.US); Scanner sc1 = new Scanner(new File(args[0])); float c = sc1.nextFloat(); float d = sc1.nextFloat(); Locale.setDefault(pl); */ Formatter frt = new Formatter(); frt.format("c = %.3f | %15.5f = d",c,d); System.out.println("Wczytane z pliku"); System.out.println(frt); System.out.println("Pisanie do pliku " + args[1]); PrintWriter out1 = new PrintWriter( new BufferedWriter(new FileWriter( args[1]))); //frt = new Formatter(); frt.format(Locale.US, " c = %+.6f d = %-15.1e|\n",c,d); out1.println(frt); Calendar cld = Calendar.getInstance(); out1.format("Czas %1$tY %1$tB %1$tA %1$tr",cld); System.out.format("Czas %1$tY %1$tB %1$tA %1$tr\n",cld); Locale.setDefault(Locale.US); System.out.format("Czas %1$tY %1$tB %1$tA %1$tr\n",cld); Locale.setDefault(pl); out1.close(); } } |
---|
P13Tablice.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
// P13Tablice.java package p13tablice; import java.util.*; import java.io.*; public class P13Tablice { public static void main(String[] args) { int[] a1 = {25,25,25}; int[] a2 = new int[3]; Arrays.fill(a2,25); for(int i=0; i< a2.length;i++) System.out.println("a1 " + a1[i] + " a2 " + a2[i]); System.out.println("Arrays " + Arrays.equals(a1,a2)); System.out.println("Zwykla " + a1.equals(a2)); Integer[] a3 = {new Integer(25),new Integer(25),new Integer(25)}; Integer[] a4 = new Integer[3]; Arrays.fill(a4,new Integer(25)); System.out.println("Arrays " + Arrays.equals(a3,a4)); System.out.println("Zwykla " + a3.equals(a4)); String[] a5 = {"ala","ala","ala"}; String[] a6 = new String[3]; Arrays.fill(a6,"ala"); System.out.println("Arrays " + Arrays.equals(a5,a6)); System.out.println("Zwykla " + a5.equals(a6)); int[] a7 = {15,3,5,16,8,4,5,9,25,45,2}; Arrays.sort(a7); for(int i=0; i< a7.length;i++) System.out.print(a7[i]+" "); System.out.println(); String[] a8 = {"gosia","ala","tola","ula","zosia","basia","kasia","maciek"}; Arrays.sort(a8); for(int i=0; i<a8.length;i++) System.out.print(a8[i]+" "); System.out.println(); Para[] a9={new Para(2,"ala"),new Para(1,"ola"),new Para(3,"ela")}; for(int i=0; i< a9.length; i++) System.out.print(a9[i]+" " ); System.out.println(); Arrays.sort(a9, new PorownajInt()); for(int i=0; i< a9.length; i++) System.out.print(a9[i]+" "); System.out.println(); Arrays.sort(a9, new PorownajString()); for(int i=0; i< a9.length; i++) System.out.print(a9[i]+" "); System.out.println(); Para aa = new Para(2,"ala"), bb=new Para(3,"tola"); PorownajInt xxx = new PorownajInt(); int k = xxx.compare(aa,bb); System.out.println("k="+k); } } class Para { private int x; private String y; Para(int a,String b){x=a;y=b;} public int dajInt(){return x;} public String dajString(){return y;} public String toString() { return x + "," + y ;} } class PorownajInt implements Comparator<Para> { public int compare(Para a, Para b) { int aa = a.dajInt(); int bb = b.dajInt(); return aa == bb ? 0 : ( aa < bb ? -1 : 1); } } class PorownajString implements Comparator<Para> { public int compare(Para a, Para b) { String aa = a.dajString(); String bb = b.dajString(); return aa.compareTo(bb); } } // przed wersją 5 //class PorownajInt implements Comparator { //public int compare(Object a, Object b){ //int aa = ((Para)a).dajInt(); //int bb = ((Para)b).dajInt(); //return aa == bb ? 0 : ( aa < bb ? -1 : 1); //} //} //class PorownajString implements Comparator{ //public int compare(Object a, Object b){ //String aa = ((Para)a).dajString(); //String bb = ((Para)b).dajString(); //return aa.compareTo(bb); //} |
---|
P14Tablice.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
// P14Tablice package p14tablice; import java.util.Arrays; class TablicaDrukuj { static void drukuj(String[][] x, String opis) { int n = x.length; System.out.println("String " + opis); for (int i = 0; i < n; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + " "); } System.out.println(); } } static void drukuj(int[][] x, String opis) { int n = x.length; System.out.println("int " + opis); for (int i = 0; i < n; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + " "); } System.out.println(); } } static void drukuj(DwieFloat[][] x, String opis) { int n = x.length; System.out.println("DwieFloat " + opis); for (int i = 0; i < n; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + " "); } System.out.println(); } } static void drukuj(float[][] x, String opis) { int n = x.length; System.out.println("float " + opis); for (int i = 0; i < n; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + " "); } System.out.println(); } } } class DwieFloat { private float x, y; DwieFloat(float a, float b) { x = a; y = b; } float dodajDwie() { return x + y; } float odejmijDwie() { return x - y; } @Override public String toString() { return "(" + x + "," + y + ")"; } } public class P14Tablice { public static void main(String[] args) { int[][] a1 = new int[3][4]; TablicaDrukuj.drukuj(a1, "a1"); int[][] a2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; TablicaDrukuj.drukuj(a2, "a2"); int[][] a22 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; TablicaDrukuj.drukuj(a22, "a22"); System.out.println("a2 == a22? => " + Arrays.equals(a2, a22)); for (int i = 0; i < a22.length; i++) { System.out.println("a2[" + i + "]== a22[" + i + "]? => " + Arrays.equals(a2[i], a22[i])); } a22[2][0] = 0; TablicaDrukuj.drukuj(a22, "a22"); for (int i = 0; i < a22.length; i++) { System.out.println("a2[" + i + "]== a22[" + i + "]? => " + Arrays.equals(a2[i], a22[i])); } int[][] a3 = new int[3][4]; Arrays.fill(a3[0], 1); Arrays.fill(a3[1], 2); Arrays.fill(a3[2], 3); TablicaDrukuj.drukuj(a3, "a3"); String[][] a4 = { {"ala", "bel", "cal"}, {"dil", "ela", "fel"} }; TablicaDrukuj.drukuj(a4, "a4"); DwieFloat[][] a5 = { {new DwieFloat(1f, 2f), new DwieFloat(3f, 4f)}, {new DwieFloat(5f, 6f), new DwieFloat(7f, 8f)}, {new DwieFloat(9f, 10f), new DwieFloat(11f, 12f)} }; TablicaDrukuj.drukuj(a5, "a5"); float[][] a51 = new float[a5.length][a5[0].length]; for (int i = 0; i < a51.length; i++) { for (int j = 0; j < a51[i].length; j++) { a51[i][j] = a5[i][j].dodajDwie(); } } TablicaDrukuj.drukuj(a51, "a51"); int[][] a6 = new int[5][]; for (int i = 0; i < a6.length; i++) { a6[i] = new int[i + 1]; } TablicaDrukuj.drukuj(a6, "a6"); Integer[][] a7 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; ParametrTablicePisz.pisz(a4, "a4 <String>"); ParametrTablicePisz.pisz(a5, "a5 <DwieFloat>"); ParametrTablicePisz.pisz(a7, "a7 <Integer>"); } } class ParametrTablicePisz { static <Tablica> void pisz(Tablica[][] x, String opis) { int n = x.length; System.out.println("Tablica " + opis); for (int i = 0; i < n; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + " "); } System.out.println(); } } } |
---|