Commit 98faea21 by Patryk Czarnik

rozwiązania zadań domowych

parent a91b0bb1
package domowe.r2.z03_funkcje;
import java.util.Scanner;
public class LiczbyPierwsze1 {
// Z definicji liczby pierwszej: to jest taka liczba naturalna, która ma dokładnie 2 dzielniki
// (1 i siebie samą, przy czym 1 NIE JEST uznawane za liczbę pierwszą).
// Ta wersja sprawdza ile jest dzielników; jeśli 2, to znaczy, że liczba pierwsza.
// Wada: długo działa dla dużych liczb. Niezależnie od tego czy liczba jest pierwsza, czy nie, i tak musi dojść do samego końca.
static boolean czyPierwsza(long liczba) {
long ileDzielnikow = 0;
for(long i = 1; i <= liczba; i++) {
if(liczba % i == 0) {
ileDzielnikow++;
}
}
System.out.println("ilość dzielników: " + ileDzielnikow);
return ileDzielnikow == 2; // tylko wtedy liczba jest pierwsza
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Aby zakończyć, wpisz 0");
while(true) {
System.out.print("Podaj liczbę: ");
long liczba = sc.nextLong();
if(liczba == 0) break;
// pomiar czasu
long p = System.currentTimeMillis();
boolean wynik = czyPierwsza(liczba);
long k = System.currentTimeMillis();
if(wynik) {
System.out.println("Liczba " + liczba + " jest pierwsza");
} else {
System.out.println("Liczba " + liczba + " nie jest pierwsza");
}
System.out.println("Sprawdzenie trwało " + (k-p) + " ms");
System.out.println();
}
}
}
package domowe.r2.z03_funkcje;
import java.util.Scanner;
public class LiczbyPierwsze2 {
// Ta wersja sprawdza czy liczba jest podzielna przez jakąkolwiek liczbę inną niż 1 i ona sama.
// Gdyby tak było, to nie jest pierwsza.
// Również "1" nie jest l.pierwszą.
// Gdy liczba nie jest pierwsza, to pętla jest przerywana szybiej (niż v1). spr np. 2147483648
// Jeśli jest pierwsza, to pętla musi dojść do końca, spr. np. 2147483647
static boolean czyPierwsza(long liczba) {
if(liczba == 1) {
return false;
}
for(long i = 2; i < liczba; i++) {
if(liczba % i == 0) {
return false; // skoro dzieli się przez jakąś liczbę pośrednią (nie 1 i nią samą), to nie jest pierwsza
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Aby zakończyć, wpisz 0");
while(true) {
System.out.print("Podaj liczbę: ");
long liczba = sc.nextLong();
if(liczba == 0) break;
// pomiar czasu
long p = System.currentTimeMillis();
boolean wynik = czyPierwsza(liczba);
long k = System.currentTimeMillis();
if(wynik) {
System.out.println("Liczba " + liczba + " jest pierwsza");
} else {
System.out.println("Liczba " + liczba + " nie jest pierwsza");
}
System.out.println("Sprawdzenie trwało " + (k-p) + " ms");
System.out.println();
}
}
}
package domowe.r2.z03_funkcje;
import java.util.Scanner;
public class LiczbyPierwsze3 {
// W tej wersji korzystam z tego, że jeśli liczba ma dzielnik (inny niż 1 i ona sama), to ten dzielnik
// musi być <= pierwiastka z tej liczby. Wystarczy więc iść w pętli do pierwiastka.
// Porównaj czas działania np. dla 2147483647 z wersją 2.
static boolean czyPierwsza(long liczba) {
if(liczba == 1) {
return false;
}
for(long i = 2; i <= Math.sqrt(liczba+1); i++) {
// wpisałem liczba+1, aby zmniejszyć ryzyko problemów z dokładnością doubli (dla dużych liczb)
// jednocześnie jest to poprawne dla początkowych liczb, bo sqrt(3) < 2
if(liczba % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Aby zakończyć, wpisz 0");
while(true) {
System.out.print("Podaj liczbę: ");
long liczba = sc.nextLong();
if(liczba == 0) break;
// pomiar czasu
long p = System.currentTimeMillis();
boolean wynik = czyPierwsza(liczba);
long k = System.currentTimeMillis();
if(wynik) {
System.out.println("Liczba " + liczba + " jest pierwsza");
} else {
System.out.println("Liczba " + liczba + " nie jest pierwsza");
}
System.out.println("Sprawdzenie trwało " + (k-p) + " ms");
System.out.println();
}
}
}
package domowe.r2.z03_funkcje;
import java.util.Scanner;
public class LiczbyPierwsze4 {
// W tej wersji osobno sprawdzam podzielność przez 2 jako "bardzo prawdopodobną sytuację"
// Dzięki temu w pętli mogę iść krokiem o 2 sprawdzając tylko liczby nieparzyste
// i jeszcze trochę (prawie dwukrotnie) przyspieszam działanie względem wersji 3.
static boolean czyPierwsza(long liczba) {
if(liczba == 1) {
return false;
}
if(liczba != 2 && liczba % 2 == 0) {
return false;
}
for(long i = 3; i <= Math.sqrt(liczba+1); i += 2) {
if(liczba % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Aby zakończyć, wpisz 0");
while(true) {
System.out.print("Podaj liczbę: ");
long liczba = sc.nextLong();
if(liczba == 0) break;
// pomiar czasu
long p = System.currentTimeMillis();
boolean wynik = czyPierwsza(liczba);
long k = System.currentTimeMillis();
if(wynik) {
System.out.println("Liczba " + liczba + " jest pierwsza");
} else {
System.out.println("Liczba " + liczba + " nie jest pierwsza");
}
System.out.println("Sprawdzenie trwało " + (k-p) + " ms");
System.out.println();
}
}
}
package domowe.r2.z2_funkcje_na_tablicach;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class FunkcjeNaTablicach {
static Integer max(int[] t) {
if(t.length == 0) {
return null;
}
int max = Integer.MIN_VALUE;
for (int e : t) {
if (e > max) {
max = e;
}
}
return max;
}
static Integer min(int[] t) {
if(t.length == 0) {
return null;
}
int min = Integer.MAX_VALUE;
for (int e : t) {
if (e < min) {
min = e;
}
}
return min;
}
static Integer min_v2(int[] t) {
if(t.length == 0) {
return null;
}
int min = t[0];
for (int i = 1; i < t.length; i++) {
if (t[i] < min) {
min = t[i];
}
}
return min;
}
static int roznicaMinMax_v1(int[] t) {
// wersja "lepsza" jeśli kryterium oceny jest działanie, wydajność - bo tylko jedno przejście przez dane
if(t.length == 0) {
return 0;
}
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int e : t) {
if (e < min) {
min = e;
}
if (e > max) {
max = e;
}
}
return max - min;
}
static int roznicaMinMax_v2(int[] t) {
// wersja "lepsza" jeśli kryterium oceny jest styl, struktura kodu - bo korzystamy z już wcześniej napisanych rzeczy
return t.length == 0 ? 0 : max(t) - min(t);
}
static void wypiszWieksze(int[] t, int x) {
for (int e : t) {
if (e > x) {
System.out.print(e + ", ");
}
}
System.out.println();
}
static Integer pierwszaWieksza(int[] t, int x) {
for (int e : t) {
if (e > x) {
return e;
}
}
return null;
}
static int sumaWiekszych(int[] t, int x) {
int suma = 0;
for (int e : t) {
if (e > x) {
suma += e;
}
}
return suma;
}
static int ileWiekszych(int[] t, int x) {
int ile = 0;
for (int e : t) {
if (e > x) {
ile += 1;
}
}
return ile;
}
static void wypiszPodzielne(int[] t, int x) {
for (int e : t) {
if (e % x == 0) {
System.out.print(e + "; ");
}
}
System.out.println();
}
static Integer pierwszaPodzielna(int[] t, int x) {
for (int e : t) {
if (e % x == 0) {
return e;
}
}
return null;
}
static int sumaPodzielnych(int[] t, int x) {
int suma = 0;
for (int e : t) {
if (e % x == 0) {
suma += e;
}
}
return suma;
}
static int ilePodzielnych(int[] t, int x) {
int ile = 0;
for (int e : t) {
if (e % x == 0) {
ile++;
}
}
return ile;
}
static Integer znajdzWspolny(int[] t1, int[] t2) {
for (int e1 : t1) {
for (int e2 : t2) {
if (e1 == e2) {
return e1;
}
}
}
return null;
}
static List<Integer> wszystkieWspolne(int[] t1, int[] t2) {
List<Integer> lista = new ArrayList<>();
for (int e1 : t1) {
for (int e2 : t2) {
if (e1 == e2) {
lista.add(e1);
}
}
}
return lista;
}
}
package domowe.r2.z2_funkcje_na_tablicach;
import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;
import java.util.function.IntPredicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/** Ta wersja wykorzystuje strumienie (od Java 8) i gotowe rozwiązania, jeśli są dostępne. */
public class WersjaStrumieniowa {
// Tu i w kilku dalej zmieniam typ na OptionalInt, bo to bardziej pasuje do nowoczesnego stylu niż Integer.
static OptionalInt max(int[] t) {
return IntStream.of(t).max();
}
static OptionalInt min(int[] t) {
return IntStream.of(t).min();
}
static Integer min_v2(int[] t) {
// Gdybyśmy bardzo chcieli zwrócić Integer, a nie OptionalInt
OptionalInt opt = IntStream.of(t).min();
if(opt.isPresent()) {
return opt.getAsInt();
} else {
return null;
}
}
static int roznicaMinMax(int[] t) {
return max(t).orElse(0) - min(t).orElse(0);
}
static void wypiszWieksze(int[] t, int x) {
IntStream.of(t)
.filter(e -> e > x)
.forEachOrdered(e -> System.out.print(e + ", "));
System.out.println();
}
static OptionalInt pierwszaWieksza(int[] t, int x) {
return IntStream.of(t)
.filter(e -> e > x)
.findFirst();
}
static int sumaWiekszych(int[] t, int x) {
return IntStream.of(t)
.filter(e -> e > x)
.sum();
}
static long ileWiekszych(int[] t, int x) {
return IntStream.of(t)
.filter(e -> e > x)
.count();
}
// przykłady z podzielnością wyszłyby bardzo podobne do 4 powyższych, dlatego wprowadzam pewne urozmaicenia
private static IntPredicate jestPodzielnaPrzez(int x) {
return (int e) -> e % x == 0;
}
static void wypiszPodzielne(int[] t, int x) {
IntStream.of(t)
.filter(jestPodzielnaPrzez(x))
.forEachOrdered(System.out::println);
}
static OptionalInt pierwszaPodzielna(int[] t, int x) {
return IntStream.of(t)
.filter(jestPodzielnaPrzez(x))
.findFirst();
}
static int sumaPodzielnych(int[] t, int x) {
return IntStream.of(t)
.filter(jestPodzielnaPrzez(x))
.reduce(0, (a, e) -> a+e); // istnieje też sum
}
static int ilePodzielnych(int[] t, int x) {
return IntStream.of(t)
.filter(jestPodzielnaPrzez(x))
.reduce(0, (a, e) -> a+1); // istnieje też count
}
static OptionalInt znajdzWspolny(int[] t1, int[] t2) {
return IntStream.of(t1)
.flatMap(e1 -> IntStream.of(t2).filter(e2 -> e1 == e2))
.findAny();
}
static List<Integer> wszystkieWspolne(int[] t1, int[] t2) {
return IntStream.of(t1)
.flatMap(e1 -> IntStream.of(t2).filter(e2 -> e1 == e2))
.boxed()
.collect(Collectors.toList());
}
}
package domowe.r2.z3_funkcje_konwersje;
/* Tego nie było w naszych zestawach, ale też warto zobaczyć: klasa z metodami statycznymi, czyli "funkcjami". */
public class KonwersjeJednostek {
static double cale_na_cm(double cale) {
return cale * 2.54;
}
static double cm_na_cale(double cm) {
return cm / 2.54;
}
static double mile_na_km(double mile) {
return mile * 1.609344;
}
public static double km_na_mile(double km) {
return km / 1.609344;
}
public static double far_na_celc(double far) {
// to dawało złe wyniki z poowdu dzielenia całkowitego 5/9 == 0
// return 5/9 * (far - 32);
// przykładowe dobre rozwiązania
// return 5.0 / 9.0 * (far - 32.0);
return 5. / 9. * (far - 32.);
// return (far - 32) / 1.8;
// Jeśli zacznę od zmiennej typu double to będzie OK (Java liczy od lewej do prawej)
// return (far - 32) * 5 / 9;
}
public static double celc_na_far(double cel) {
//źle return 32 + 9/5 * cel;
// return 32.0 + 9./5. * cel;
// return 32 + 1.8 * cel;
return cel * 9 / 5 + 32;
}
public static void main(String[] args) {
System.out.println(cale_na_cm(0));
System.out.println(cale_na_cm(10));
System.out.println(cale_na_cm(17));
System.out.println();
System.out.println(mile_na_km(100));
System.out.println();
System.out.println(km_na_mile(100));
System.out.println(km_na_mile(160));
System.out.println();
System.out.println(far_na_celc(0)); // -17.7
System.out.println(far_na_celc(100)); // 37.7
System.out.println();
System.out.println(celc_na_far(0)); // 32
System.out.println(celc_na_far(37)); // około 100
System.out.println(celc_na_far(100)); // 212
}
}
package domowe.r2.z3_funkcje_konwersje;
import java.util.Scanner;
// Jak uzyć definicji z innej klasy będąc w tym smaym pakiecie?
// Po prostu uzyć nazwy klasy
public class ProgramKonwertujacy {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("Podaj nazwę operacji");
System.out.println(" m2k - mile na kilometry");
System.out.println(" k2m - kilometry na mile");
System.out.println(" f2c - Fahrenheity na Celcjusze");
System.out.println(" c2f - Celcjusze na Fahrenheity");
String operacja = sc.nextLine();
System.out.println("Podaj argument:");
double arg = sc.nextDouble();
double wynik = 0;
switch(operacja) {
case "m2k" :
wynik = KonwersjeJednostek.mile_na_km(arg);
break;
case "k2m" :
wynik = KonwersjeJednostek.km_na_mile(arg);
break;
case "f2c" :
wynik = KonwersjeJednostek.far_na_celc(arg);
break;
case "c2f" :
wynik = KonwersjeJednostek.celc_na_far(arg);
break;
}
System.out.println("Wynik wynosi " + wynik);
}
}
package domowe.r2.z3_funkcje_konwersje;
import static domowe.r2.z3_funkcje_konwersje.KonwersjeJednostek.*;
import java.util.Scanner;
// Jak użyć definicji z innej klasy?
// Można "zaimportować statycznie".
public class ProgramKonwertujacy2 {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
petla:
while(true) {
System.out.println("Podaj nazwę operacji");
System.out.println(" m2k - mile na kilometry");
System.out.println(" k2m - kilometry na mile");
System.out.println(" f2c - Fahrenheity na Celcjusze");
System.out.println(" c2f - Celcjusze na Fahrenheity");
System.out.println(" k - Koniec programu");
String operacja = sc.nextLine().trim().toLowerCase();
System.out.println("Podaj argument:");
double arg = sc.nextDouble();
sc.nextLine();
double wynik = 0;
switch(operacja) {
case "k":
break petla;
case "m2k" :
wynik = mile_na_km(arg);
break;
case "k2m" :
wynik = km_na_mile(arg);
break;
case "f2c" :
wynik = far_na_celc(arg);
break;
case "c2f" :
wynik = celc_na_far(arg);
break;
}
System.out.println("Wynik wynosi " + wynik);
System.out.println();
}
System.out.println("papa");
}
}
package domowe.r3.ogloszenia;
import java.util.Objects;
public class AdresPocztowy {
private String ulica_numer;
private String kodPocztowy;
private String miasto;
public AdresPocztowy(String ulica_numer, String kodPocztowy, String miasto) {
this.ulica_numer = ulica_numer;
this.kodPocztowy = kodPocztowy;
this.miasto = miasto;
}
public String getUlica_numer() {
return ulica_numer;
}
public void setUlica_numer(String ulica_numer) {
this.ulica_numer = ulica_numer;
}
public String getKodPocztowy() {
return kodPocztowy;
}
public void setKodPocztowy(String kodPocztowy) {
this.kodPocztowy = kodPocztowy;
}
public String getMiasto() {
return miasto;
}
public void setMiasto(String miasto) {
this.miasto = miasto;
}
@Override
public String toString() {
return ulica_numer + " " + kodPocztowy + " " + miasto;
}
@Override
public int hashCode() {
return Objects.hash(kodPocztowy, miasto, ulica_numer);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AdresPocztowy other = (AdresPocztowy) obj;
return Objects.equals(kodPocztowy, other.kodPocztowy) && Objects.equals(miasto, other.miasto)
&& Objects.equals(ulica_numer, other.ulica_numer);
}
}
package domowe.r3.ogloszenia;
public class OgloszeniaPrzyklad {
public static void main(String[] args) {
Sprzedawca jan = new Sprzedawca("Jan", "Kowalski", "321321321", "jan@kowalski.pl", new AdresPocztowy("Jasna 14a", "01-234", "Warszawa"));
Sprzedawca adam = new Sprzedawca("Adam", "Malinowski", "123123123", null, null);
System.out.println(jan);
System.out.println(adam);
System.out.println();
Ogloszenie[] ogloszenia = {
new Ogloszenie("Sprzedam lunetę do oglądania planet", 1950, jan),
new OgloszenieSamochodowe("Sprzedam Golfa", 15000, jan, "VW", "Golf", 2010, 169000, RodzajNapedu.BENZYNA),
new OgloszenieMieszkaniowe("Apartament Lux", 550000, adam, "Warszawa", "Mokotów", 22.5, 3, 1),
};
for(Ogloszenie ogloszenie : ogloszenia) {
System.out.println(" * " + ogloszenie);
}
}
}
package domowe.r3.ogloszenia;
import java.util.Objects;
public class Ogloszenie {
private String tytul;
private int cena;
private Sprzedawca sprzedawca;
public Ogloszenie(String tytul, int cena, Sprzedawca sprzedawca) {
this.tytul = tytul;
this.cena = cena;
this.sprzedawca = sprzedawca;
}
public String getTytul() {
return tytul;
}
public void setTytul(String tytul) {
this.tytul = tytul;
}
public int getCena() {
return cena;
}
public void setCena(int cena) {
this.cena = cena;
}
public Sprzedawca getSprzedawca() {
return sprzedawca;
}
public void setSprzedawca(Sprzedawca sprzedawca) {
this.sprzedawca = sprzedawca;
}
@Override
public String toString() {
return tytul + ", cena " + cena + " zł, sprzedawca: " + sprzedawca;
}
@Override
public int hashCode() {
return Objects.hash(cena, sprzedawca, tytul);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ogloszenie other = (Ogloszenie) obj;
return cena == other.cena && Objects.equals(sprzedawca, other.sprzedawca) && Objects.equals(tytul, other.tytul);
}
}
package domowe.r3.ogloszenia;
import java.util.Objects;
public class OgloszenieMieszkaniowe extends Ogloszenie {
private String miasto, dzielnica;
private double metraz;
private int pietro, liczbaPokoi;
public OgloszenieMieszkaniowe(String tytul, int cena, Sprzedawca sprzedawca, String miasto, String dzielnica,
double metraz, int pietro, int liczbaPokoi) {
super(tytul, cena, sprzedawca);
this.miasto = miasto;
this.dzielnica = dzielnica;
this.metraz = metraz;
this.pietro = pietro;
this.liczbaPokoi = liczbaPokoi;
}
public String getMiasto() {
return miasto;
}
public void setMiasto(String miasto) {
this.miasto = miasto;
}
public String getDzielnica() {
return dzielnica;
}
public void setDzielnica(String dzielnica) {
this.dzielnica = dzielnica;
}
public double getMetraz() {
return metraz;
}
public void setMetraz(double metraz) {
this.metraz = metraz;
}
public int getPietro() {
return pietro;
}
public void setPietro(int pietro) {
this.pietro = pietro;
}
public int getLiczbaPokoi() {
return liczbaPokoi;
}
public void setLiczbaPokoi(int liczbaPokoi) {
this.liczbaPokoi = liczbaPokoi;
}
@Override
public String toString() {
return "OgloszenieMieszkaniowe: " + super.toString() + ", miasto= " + miasto + ", dzielnica=" + dzielnica + ", metraz=" + metraz
+ ", pietro=" + pietro + ", liczbaPokoi=" + liczbaPokoi;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + Objects.hash(dzielnica, liczbaPokoi, metraz, miasto, pietro);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
OgloszenieMieszkaniowe other = (OgloszenieMieszkaniowe) obj;
return Objects.equals(dzielnica, other.dzielnica) && liczbaPokoi == other.liczbaPokoi
&& Double.doubleToLongBits(metraz) == Double.doubleToLongBits(other.metraz)
&& Objects.equals(miasto, other.miasto) && pietro == other.pietro;
}
}
package domowe.r3.ogloszenia;
import java.util.Objects;
public class OgloszenieSamochodowe extends Ogloszenie {
private String marka, model;
private int rocznik;
private long stanLicznika;
private RodzajNapedu naped;
public OgloszenieSamochodowe(String tytul, int cena, Sprzedawca sprzedawca, String marka, String model, int rocznik,
long stanLicznika, RodzajNapedu naped) {
super(tytul, cena, sprzedawca);
this.marka = marka;
this.model = model;
this.rocznik = rocznik;
this.stanLicznika = stanLicznika;
this.naped = naped;
}
public String getMarka() {
return marka;
}
public void setMarka(String marka) {
this.marka = marka;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getRocznik() {
return rocznik;
}
public void setRocznik(int rocznik) {
this.rocznik = rocznik;
}
public long getStanLicznika() {
return stanLicznika;
}
public void setStanLicznika(long stanLicznika) {
this.stanLicznika = stanLicznika;
}
public RodzajNapedu getNaped() {
return naped;
}
public void setNaped(RodzajNapedu naped) {
this.naped = naped;
}
@Override
public String toString() {
return "OgloszenieSamochodowe: " + super.toString() + ", marka=" + marka + ", model=" + model + ", rocznik=" + rocznik + ", stanLicznika="
+ stanLicznika + " napęd=" + naped;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + Objects.hash(marka, model, rocznik, stanLicznika, naped);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
OgloszenieSamochodowe other = (OgloszenieSamochodowe) obj;
return Objects.equals(marka, other.marka) && Objects.equals(model, other.model) && rocznik == other.rocznik
&& stanLicznika == other.stanLicznika && naped == other.naped;
}
}
package domowe.r3.ogloszenia;
public enum RodzajNapedu {
BENZYNA,
ON,
LPG,
ELEKRTYK,
HYBRYDA;
}
package domowe.r3.ogloszenia;
import java.util.Objects;
public class Sprzedawca {
private String imie, nazwisko;
private String telefon;
private String email;
private AdresPocztowy adres;
public Sprzedawca(String imie, String nazwisko, String telefon, String email, AdresPocztowy adres) {
this.imie = imie;
this.nazwisko = nazwisko;
this.telefon = telefon;
this.email = email;
this.adres = adres;
}
public String getImie() {
return imie;
}
public void setImie(String imie) {
this.imie = imie;
}
public String getNazwisko() {
return nazwisko;
}
public void setNazwisko(String nazwisko) {
this.nazwisko = nazwisko;
}
public String getTelefon() {
return telefon;
}
public void setTelefon(String telefon) {
this.telefon = telefon;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public AdresPocztowy getAdres() {
return adres;
}
public void setAdres(AdresPocztowy adres) {
this.adres = adres;
}
@Override
public String toString() {
return imie + " " + nazwisko
+ (telefon != null ? (", tel. " + telefon) : "")
+ (email != null ? (", email " + email) : "")
+ (adres != null ? (", adres " + adres) : "");
}
@Override
public int hashCode() {
return Objects.hash(adres, email, imie, nazwisko, telefon);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Sprzedawca other = (Sprzedawca) obj;
return Objects.equals(adres, other.adres) && Objects.equals(email, other.email)
&& Objects.equals(imie, other.imie) && Objects.equals(nazwisko, other.nazwisko)
&& Objects.equals(telefon, other.telefon);
}
}
package domowe.r3.ogloszenia;
import java.time.LocalDate;
import java.util.List;
public class Szkolenie {
private String tytul;
private int liczbaGodzin;
private int cena;
private List<LocalDate> dniSzkoleniowe;
public Szkolenie(String tytul, int liczbaGodzin, int cena, List<LocalDate> dniSzkoleniowe) {
this.tytul = tytul;
this.liczbaGodzin = liczbaGodzin;
this.cena = cena;
this.dniSzkoleniowe = dniSzkoleniowe;
}
public String getTytul() {
return tytul;
}
public void setTytul(String tytul) {
this.tytul = tytul;
}
public int getLiczbaGodzin() {
return liczbaGodzin;
}
public void setLiczbaGodzin(int liczbaGodzin) {
this.liczbaGodzin = liczbaGodzin;
}
public int getCena() {
return cena;
}
public void setCena(int cena) {
this.cena = cena;
}
public List<LocalDate> getDniSzkoleniowe() {
return dniSzkoleniowe;
}
public void setDniSzkoleniowe(List<LocalDate> dniSzkoleniowe) {
this.dniSzkoleniowe = dniSzkoleniowe;
}
@Override
public String toString() {
return "Szkolenie [tytul=" + tytul + ", liczbaGodzin=" + liczbaGodzin + ", cena=" + cena + ", dniSzkoleniowe="
+ dniSzkoleniowe + "]";
}
}
package domowe.r3.ogloszenia;
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;
public class SzkolenieProgram {
public static void main(String[] args) {
Szkolenie kursJavy = new Szkolenie("Java XL", 80, 4500,
List.of(LocalDate.of(2023, 3, 7), LocalDate.of(2023, 3, 8), LocalDate.of(2023, 3, 9)));
System.out.println(kursJavy);
Locale locale = new Locale("pl", "PL");
for (LocalDate date : kursJavy.getDniSzkoleniowe()) {
//System.out.println(date + " to dzień tygodnia " + date.getDayOfWeek());
System.out.println(date + " to dzień tygodnia " + date.getDayOfWeek().getDisplayName(TextStyle.FULL, locale));
}
}
}
package domowe.r3.pracownik;
public class Pracownik {
private int stawka;
private int sumaGodzin = 0;
private int sumaNadgodzin = 0;
public Pracownik(int stawka) {
this.stawka = stawka;
}
public void praca(int godziny) {
if(godziny > 8) {
sumaGodzin += 8;
sumaNadgodzin += (godziny - 8);
} else {
sumaGodzin += godziny;
}
}
public int wyplata() {
try {
return sumaGodzin * stawka + sumaNadgodzin * stawka * 2;
} finally {
sumaGodzin = 0;
sumaNadgodzin = 0;
}
}
}
package domowe.r3.pracownik;
public class PracownikPremia extends Pracownik {
private int sumaPremii = 0;
public PracownikPremia(int stawka) {
super(stawka);
}
public void premia(int kwota) {
sumaPremii += kwota;
}
@Override
public int wyplata() {
try {
return super.wyplata() + sumaPremii;
} finally {
sumaPremii = 0;
}
}
}
package domowe.r3.z3_fraction;
public class Fraction extends Number implements Comparable<Fraction> {
private final long nom;
private final long denom;
public static final Fraction ZERO = Fraction.of(0);
public static final Fraction ONE = Fraction.of(1);
public static final Fraction HALF = Fraction.of(1, 2);
public static final Fraction PERCENT = Fraction.of(1, 100);
private Fraction(long nominator, long denominator) {
this.nom = nominator;
this.denom = denominator;
}
public static Fraction of(long nominator, long denominator) {
if(denominator == 0) {
throw new ArithmeticException("denominator zero");
}
if(denominator < 0) {
denominator = -denominator;
nominator = -nominator;
}
return new Fraction(nominator, denominator);
}
public static Fraction of(long whole) {
return new Fraction(whole, 1);
}
public long getNominator() {
return nom;
}
public long getDenominator() {
return denom;
}
@Override
public String toString() {
return String.format("%d/%d", nom, denom);
}
// equals i hashCode napisałem sam, ale to absolutnie nie znaczy, że to są najlepsze implementacje na świecie ;)
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != Fraction.class) {
return false;
}
Fraction that = (Fraction)obj;
return this.nom == that.nom && this.denom == that.denom;
}
@Override
public int hashCode() {
long mix = 1009 * denom + nom;
return (int)mix ^ (int)(mix >> 32);
}
@Override
public int compareTo(Fraction other) {
return Long.compare(this.nom * other.denom, other.nom * this.denom);
}
public Fraction shortened() {
if(nom == 0) {
return Fraction.ZERO;
}
final long gcd = Utils.gcd(nom, denom);
if(gcd == 1) {
return this;
}
return new Fraction(nom / gcd, denom / gcd);
}
public Fraction repricodal() {
return Fraction.of(denom, nom);
}
public Fraction negated() {
if(nom == 0) {
return this;
}
return new Fraction(-nom, denom);
}
public Fraction add(Fraction other) {
return Fraction.of(this.nom * other.denom + other.nom * this.denom, this.denom * other.denom).shortened();
}
public Fraction sub(Fraction other) {
return this.add(other.negated());
}
public Fraction mul(Fraction other) {
return Fraction.of(this.nom * other.nom, this.denom * other.denom).shortened();
}
public Fraction mul(long number) {
return Fraction.of(this.nom * number, this.denom).shortened();
}
public Fraction div(Fraction other) {
return this.mul(other.repricodal());
}
public Fraction div(long number) {
return Fraction.of(this.nom, this.denom * number).shortened();
}
public Fraction pow(int exp) {
return Fraction.of(Utils.pow(this.nom, exp), this.denom).shortened();
}
@Override
public double doubleValue() {
return (double)nom / (double)denom;
}
@Override
public float floatValue() {
return (float)nom / (float)denom;
}
@Override
public long longValue() {
return nom / denom;
}
@Override
public int intValue() {
return (int)longValue();
}
}
package domowe.r3.z3_fraction;
final class Utils {
private Utils() { }
/** Największy wspólny dzielnik. Implementacja wg poprawionego algorytmu Euklidesa (z modulo zamiast odejmowania).
* @param a liczba całkowita
* @param b liczba całkowita
* @return największy wspólny dzielnik <var>a</var> i <var>b</var>
*/
static long gcd(long a, long b) {
if (a == 0 || b == 0) {
throw new IllegalArgumentException("gcd argument zero");
}
while (b != 0) {
long old_b = b;
b = a % b;
a = old_b;
}
return a;
}
/** Najmniejsza wspólna wielokrotność.
* @param a liczba całkowita
* @param b liczba całkowita
* @return najmniejsza dodatnia wspólna wielokrotność <var>a</var> i <var>b</var>
*/
static long lcm(long a, long b) {
if (a == 0 || b == 0) {
throw new IllegalArgumentException("lcm argument zero");
}
a = Math.abs(a);
b = Math.abs(b);
return a / gcd(a, b) * b;
}
/** Potęgowanie liczb całkowitych.
* @param base podstawa
* @param exp wykładnik
* @return <var>base</var> do potęgi <var>exp</var>
*/
public static long pow(long base, int exp) {
long result = 1L;
for (int i = 1; i <= exp; i++) {
result *= base;
}
return result;
}
}
package turtle;
public class NarysujKwadrat {
public static void main(String[] args) {
Turtle t = new Turtle();
t.forward(100);
t.left(90);
t.forward(100);
t.left(90);
t.forward(100);
t.left(90);
t.forward(100);
}
}
package turtle;
public class NarysujKwadratPetla {
public static void main(String[] args) {
Turtle t = new Turtle();
for(int i = 1; i <= 4; i++) {
t.forward(100);
t.left(90);
}
}
}
package turtle;
import javax.swing.JOptionPane;
public class NarysujWielokat {
public static void main(String[] args) {
final int OBWOD = 2000;
int n = Integer.parseInt(JOptionPane.showInputDialog("Podaj liczbę boków"));
double kat = 360.0 / n;
double bok = (double)OBWOD / n;
// za pomocą żółwia narysuj wielokąt foremny o tylu wierzchołkach / bokach
Turtle.setCanvasSize(OBWOD / 2, OBWOD / 2);
Turtle t = new Turtle(-bok/2, -OBWOD/(Math.PI * 2));
t.penColor("blue");
t.width(5);
for(int i = 0; i < n; i++) {
t.forward(bok);
t.left(kat);
}
}
}
package turtle;
import javax.swing.JOptionPane;
public class NarysujWielokat_v0 {
public static void main(String[] args) {
int n = Integer.parseInt(JOptionPane.showInputDialog("Podaj liczbę boków"));
Turtle t = new Turtle();
double kat = 360.0 / n;
for(int i = 0; i < n; i++) {
t.forward(100);
t.left(kat);
}
}
}
package turtle;
import java.awt.Color;
public class Zolw1 {
public static void main(String[] args) {
Turtle t = new Turtle();
t.forward(100);
t.left(90);
t.forward(100);
t.penColor(Color.GREEN);
t.left(45);
t.forward(74);
t.left(90);
t.forward(74);
t.left(45);
t.penColor(Color.BLACK);
t.forward(100);
}
}
package domowe.r3.pracownik;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class PracownikTest {
private Pracownik p1;
@BeforeEach
void setUp() {
p1 = new Pracownik(100);
}
@Test
void testPracaWyplata() {
p1.praca(4);
p1.praca(6);
int wynik = p1.wyplata();
assertEquals(1000, wynik);
}
@Test
void testNadgodziny() {
p1.praca(10);
p1.praca(12);
p1.praca(4);
int wynik = p1.wyplata();
assertEquals(3200, wynik);
}
// Dobra praktyka testów jednostkowych: dużo małych testów; każdy aspekt działania opisywać oddzielnym testem
// W oddzielnych teście napiszemy, że po wypłaceniu pieniędzy licznik się "zeruje"
@Test
void testReset() {
p1.praca(5);
p1.praca(12);
p1.wyplata();
// w tym teście nie interesuje mnie wynik pierwszej wypłaty, bo tym zajmował się inny test
// interesuje mnie tylko to, czy druga wypłata daje wynik 0
int kolejnaWyplata = p1.wyplata();
assertEquals(0, kolejnaWyplata);
}
@Test
void testInnaStawka() {
Pracownik p2 = new Pracownik(150);
p2.praca(5);
p2.praca(10);
int wynik = p2.wyplata();
assertEquals(2550, wynik);
}
@Test
void testPremia() {
PracownikPremia pp = new PracownikPremia(100);
pp.praca(10);
pp.premia(111);
pp.praca(4);
pp.premia(333);
int wynik = pp.wyplata();
assertEquals(2044, wynik);
// tutaj wyjątkowo nie piszę kolejnego testu, tylko drugą asercję
int kolejnaWyplata = pp.wyplata();
assertEquals(0, kolejnaWyplata);
}
}
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