Wykład 3 programy

  1. P10Tablice.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

// P10Tablice.java

package p010tablice;

import java.util.*;

class Liczby

{

private int x=1,y=5;

public String toString()

{

String a = new String("(" + x + "," + y + ")");

return a;

}

}

public class P010Tablice

{

public static void main(String[] args)

{

int[] a;

int[] b = new int[5];

for(int i = 0; i<b.length; i++)

System.out.print("b[" + i + "]= " + b[i] + " ");

System.out.println();

for(int i = 0; i<b.length; i++)

b[i]=i*i;

for(int i = 0; i<b.length; i++)

System.out.print("b[" + i + "]= " + b[i] + " ");

System.out.println();

for(int element: b )

System.out.print("b = " + element + " ");

System.out.println();

Liczby[] c;

Liczby[] d = new Liczby[4];

for(int i = 0; i<d.length-1; i++)

{

d[i]=new Liczby();

System.out.print("d[" + i + "]= " + d[i]+ " ");

}

System.out.println();

for(int i =0; i<d.length; i++)

if(d[i] == null)

System.out.print("NIE ");

else

System.out.print("TAK ");

System.out.println();

for(Liczby element: d )

System.out.print("d = " + element + " ");

System.out.println();

Liczby[] e = {new Liczby(),new Liczby(),new Liczby()};

System.out.println("Rozmiar e = " + e.length);

for(int i = 0; i<e.length; i++)

System.out.println("e[" + i + "]= " + e[i] );

Liczby[] f;

f = new Liczby[]{new Liczby(), new Liczby()};

System.out.println("Rozmiar f = " + f.length);

for(int i = 0; i<f.length; i++)

System.out.println("f[" + i + "] = " + f[i]);

}

}

 

  1. 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();

}

}

  1. 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);

//}

  1. 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();

}

}

}


Wyszukiwarka

Podobne podstrony:
Wykład 4 programy
Wykład 8 programy
Ekonomia wykłady program PS1
Wykład 2 programy
wyklad c++, Programowanie
Wykład 1 + program wykładów RACH KOSZ projekt 2012
Jadczak R - Badania operacyjne Wykład 3, programowanie całkowitoliczbowe
Wykład 1 Program, literatura, warunki zaliczenia, definicje
Zadania dodatkowe, studia wsiz, semestr 1 2, programowanie LAB wyklad, Programowanie, BFryc, 1IID, Z
Laboratorium nr 2 tablice, studia wsiz, semestr 1 2, programowanie LAB wyklad, Programowanie, BFryc,
WYKŁAD 3 program badan prenatalnych
WYKŁAD 1 Program przedmiotu nauki
Wykład 1 + program wykładów PROGRAM WYKŁADÓW 2013
infa wykłady Programowanie w języku C# Informatyka wykład
Wykład 6 programy
wykład programypolitykizdrowotnej
Wykład 3 Programowanie Definicje
wyklad1, Programowanie, C++
Wykład z programowania obiektowego, Informatyka, Semsetr 2, Programowanie obiektowe
Delphi - Wykłady, Programowanie, Delphi

więcej podobnych podstron