Commit 4d404403 by Patryk Czarnik

gotowe przykłady "operatory"

parent 143504cc
package p07_operatory;
public class Bitowe1 {
public static void main(String[] args) {
byte x = 25;
byte y = -25;
byte shift = 2;
System.out.printf("x = %d y = %d %n", x, y);
System.out.println("Bez przycinania (w zakresie int):");
System.out.printf("x << %d = %d %n", shift, x << shift);
System.out.printf("y << %d = %d %n", shift, y << shift);
System.out.printf("x >> %d = %d %n", shift, x >> shift);
System.out.printf("y >> %d = %d %n", shift, y >> shift);
System.out.printf("x >>> %d = %d %n", shift, x >>> shift);
System.out.printf("y >>> %d = %d %n", shift, y >>> shift);
System.out.println();
System.out.println("Przycięte do byte: ");
System.out.printf("x << %d = %d %n", shift, (byte)(x << shift));
System.out.printf("y << %d = %d %n", shift, (byte)(y << shift));
System.out.printf("x >> %d = %d %n", shift, (byte)(x >> shift));
System.out.printf("y >> %d = %d %n", shift, (byte)(y >> shift));
System.out.printf("x >>> %d = %d %n", shift, (byte)(x >>> shift));
System.out.printf("y >>> %d = %d %n", shift, (byte)(y >>> shift));
}
}
package p07_operatory;
public class Bitowe2 {
public static void main(String[] args) {
int x = 0b00000000000000000000000000011001;
int y = -x;
int z = 0b11111111111111111111111111100111;
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println();
System.out.println("<<");
System.out.println(x << 2);
System.out.println(y << 2);
System.out.println(z << 2);
System.out.println(0b11111111111111111111111110011100); // -100
System.out.println();
System.out.println(">>");
System.out.println(x >> 2);
System.out.println(y >> 2);
System.out.println(0b11111111111111111111111111111001);
System.out.println();
System.out.println(">>>");
System.out.println(x >>> 2);
System.out.println(y >>> 2);
System.out.println(0b00111111111111111111111111111001);
System.out.println();
}
}
package p07_operatory;
public class Leniwosc1 {
public static void gorliwa() {
// opertor "gorliwy"
// zawsze oblicza obie strony wyrażenia logicznego, a dopiero potem daje wynik false / true
if(lewy() & prawy()) {
System.out.println("prawda");
} else {
System.out.println("fałsz");
}
}
public static void leniwa() {
// operator "leniwy"
// jeśli lewa strona rozstrzyga o ostatecznym wyniku, to prawa strona nie jest sprawdzana
if(lewy() && prawy()) {
System.out.println("prawda");
} else {
System.out.println("fałsz");
}
}
private static boolean lewy() {
System.out.print("lewy ");
return false;
}
private static boolean prawy() {
System.out.print("prawy ");
return true;
}
public static void main(String[] args) {
System.out.println("wersja gorliwa");
gorliwa();
System.out.println("\nwersja leniwa:");
leniwa();
}
}
package p07_operatory;
public class Leniwosc2 {
public static void main(String[] args) {
int x = 0, y = 0;
if(++x > 0 | y++ > 0) {
System.out.println("prawda");
} else {
System.out.println("fałsz");
}
System.out.println("x == " + x);
System.out.println("y == " + y);
System.out.println();
x = y = 0;
if(++x > 0 || y++ > 0) {
System.out.println("prawda");
} else {
System.out.println("fałsz");
}
System.out.println("x == " + x);
System.out.println("y == " + y);
System.out.println();
x = y = 0;
if(x++ > 0 || ++y > 0) {
System.out.println("prawda");
} else {
System.out.println("fałsz");
}
System.out.println("x == " + x);
System.out.println("y == " + y);
}
}
package p07_operatory;
public class Leniwosc3 {
public static void main(String[] args) {
int x = 0, y = 0;
if(x++ > 0 || y++ > 0) {
System.out.println("prawda");
} else {
System.out.println("fałsz");
}
System.out.println("x == " + x);
System.out.println("y == " + y);
System.out.println();
x = y = 0;
if(++x > 0 || ++y > 0) {
System.out.println("prawda");
} else {
System.out.println("fałsz");
}
System.out.println("x == " + x);
System.out.println("y == " + y);
}
}
package p07_operatory;
public class Leniwosc4 {
public static void main(String[] args) {
// z prawdopodobieństwem 0.33 na imie wpisz nulla
// z prawdopodobieństwem 0.33 na imie wpisz "Ala"
// z prawdopodobieństwem 0.34 na imie wpisz "Tadeusz"
double los = Math.random();
String imie;
if(los < 0.33) {
imie = null;
} else if(los < 0.66) {
imie = "Ala";
} else {
imie = "Tadeusz";
}
System.out.println("imie = " + imie);
// if(imie != null & imie.length() >= 5) {
// System.out.println("długie imię");
// } else {
// System.out.println("null albo krótkie imię");
// }
// jeśli imie będzie null, to w ogóle nie przejdziemy do drugiej części sprawdzenia
if(imie != null && imie.length() >= 5) {
System.out.println("długie imię");
} else {
System.out.println("null albo krótkie imię");
}
if(imie == null || imie.isEmpty()) {
System.out.println("Brak danych");
}
if(args.length >= 1 && "Ala".equalsIgnoreCase(args[0])) {
System.out.println("Ala ma kota");
}
}
}
package p07_operatory;
import java.util.Random;
public class Logiczne {
public static void main(String[] args) {
Random random = new Random();
int x = random.nextInt(1000);
int y = random.nextInt(1000);
System.out.println("Wylosowane liczby: " + x + " , " + y);
// koniunkcja, logiczne "i"
// alternatywa, logiczne "lub"
if(x > 500 && y > 500) {
System.out.println("Obie liczby > 500");
} else {
System.out.println("Któraś z liczb <= 500");
}
if(x > 500 || y > 500) {
System.out.println("Co najmniej jedna z liczb > 500");
} else {
System.out.println("Obie liczby <= 500");
}
// alternatywa rozłączna, "albo", technicznie "xor" (exclusive or)
if((x > 500) ^ (y > 500)) {
System.out.println("Dokładnie jedna z liczb > 500");
} else {
System.out.println("Obie liczby są > 500 albo obie liczby są <= 500");
}
// ! negacja
if(!(x % 3 == 0)) {
System.out.println("Nieprawda, że liczba jest podzielna przez 3");
}
}
}
package p07_operatory;
public class OperatorWarunkowy1 {
public static void main(String[] args) {
int x = 10;
String s;
s = x > 10 ? "A" : "B";
System.out.println(s);
// różwnoważne:
if(x > 10) {
s = "A";
} else {
s = "B";
}
s = x > 10 ? "A" : x > 5 ? "B" : "C";
System.out.println(s);
s = x > 10 ? "A" : (x > 5 ? "B" : "C");
System.out.println(s);
s = x > 8 ? x < 20 ? "A" : "B" : " C";
System.out.println(s);
s = x > 8 ? (x < 20 ? "A" : "B") : " C";
System.out.println(s);
s = x > 8 ? x < 20 ? "A" : "B" : x % 2 == 0 ? "C" : " D";
System.out.println(s);
s = x > 8 ? (x < 20 ? "A" : "B") : (x % 2 == 0 ? "C" : " D");
}
}
package p07_operatory;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class OperatorWarunkowy2 {
public static void main(String[] args) {
int[] liczby = { 100, 10, 20, 150, 7, 120 };
String wynik = IntStream.of(liczby)
.mapToObj(a -> a >= 100 ? "duże" : "małe")
.collect(Collectors.joining("; "));
System.out.println(wynik);
}
}
package p07_operatory;
public class OperatorWarunkowy3 {
public static void main(String[] args) {
// ilustracja zastosowań:
String s = Math.random() > 0.5 ? "ala" : null;
if(s != null && s.length() > 2) {
System.out.println("ok");
} else {
System.out.println("puste");
}
}
}
package p07_operatory;
public class PlusPlus {
public static void main(String[] args) {
int x;
int y;
x = 10;
y = ++x;
System.out.println("++x preinkrementacja");
System.out.println("x: " + x + " , y: " + y);
System.out.println();
x = 20;
y = x++;
System.out.println("x++ postinkrementacja");
System.out.println("x: " + x + " , y: " + y);
System.out.println();
x = 50;
System.out.println(x++);
System.out.println(++x);
System.out.println();
Integer a = 100;
a++;
System.out.println(a); // a wskazuje już na inny obiekt
System.out.println();
double d = 0.5;
System.out.println(d);
d++; // można inkrementować też float i double
System.out.println(d);
System.out.println();
y = x = 100;
System.out.println("x: " + x + " , y: " + y);
y = 33;
y = x += 5; // x zwiększam o 5, a to co wyjdzie, wpisuję także na y
System.out.println(" += 5");
System.out.println("x: " + x + " , y: " + y);
}
}
package p07_operatory;
public class PlusPlusZagadki {
public static void main(String[] args) {
int x, y, z;
x = 1;
x = x++ + ++x; // 1 + 3
System.out.println(x);
x = 1;
x = ++x + x++; // 2 + 2
System.out.println(x);
y = 1;
y += y++;
// y = (y + y++)
// y = 1 + 1
System.out.println(y);
z = 1;
z += z++ + ++z;
// z = z + z++ + ++z
// z = 1 + 1 + 3 = 5
System.out.println(z);
}
}
package p07_operatory;
public class PlusPlusZastosowania {
static int suma(int[] t) {
int suma = 0;
int i = 0;
while(i < t.length) {
suma += t[i++];
}
return suma;
}
public static void main(String[] args) {
int[] a = { 10, 20, 30, 40 };
System.out.println(suma(a));
}
}
package p07_operatory;
public class Przypisania {
public static void main(String[] args) {
int x, y, z;
z = y = x = 10;
System.out.printf("x=%d, y=%d, z=%d%n", x, y, z);
x = 10;
y = 50;
z = 100;
z += y = x += 3;
System.out.printf("x=%d, y=%d, z=%d%n", x, y, z);
}
}
package p07_operatory;
import javax.swing.JOptionPane;
public class ZastosowaniaLeniwychOperatorow {
public static void main(String[] args) {
String imie = JOptionPane.showInputDialog("Jak masz na imię?");
if(imie != null && imie.length() <= 3) {
JOptionPane.showMessageDialog(null, "Masz krótkie imię");
}
// Gdybym nie sprawdzał czy napis nie jest nullem,
// to w przypadku Cancel dochodziłoby do błedu NullPointerException (NPE)
// w momencie próby wywołania length()
// if (imie.length() <= 3) {
// JOptionPane.showMessageDialog(null, "Masz krótkie imię");
// }
// Gdybym zamiast "leniwego" && użył "gorliwego" &
// to także doszłoby do błędu, bo Java sprawdzałaby prawą stronę warunku nawet wtedy gdy lewa jest fałszem
// if(imie != null & imie.length() <= 3) {
// JOptionPane.showMessageDialog(null, "Masz krótkie imię");
// }
String jezyk = JOptionPane.showInputDialog("Jaki jest Twój ulubiony język programowania?");
if(jezyk != null && jezyk.equals("Java")) {
JOptionPane.showMessageDialog(null, "Brawo");
} else {
JOptionPane.showMessageDialog(null, "Źle");
}
// jeśli chodzi o tę konkretną sytuację, to pisząc w tej kolejności zapobiegamy wyjątkowi NPE
if("Java".equals(jezyk)) {
JOptionPane.showMessageDialog(null, "Brawo");
} else {
JOptionPane.showMessageDialog(null, "Źle");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment