Cvičenia: 3. týždeň - riešenia úloh

import java.awt.Color;
import sk.upjs.jpaz2.*;

public class MojaKorytnacka extends Turtle {

        public void nahodnaFarba() {
                int r = (int) (Math.random() * 256);
                int g = (int) (Math.random() * 256);
                int b = (int) (Math.random() * 256);
                Color nf = new Color(r, g, b);
                this.setPenColor(nf);
        }

        public double min(double cislo1, double cislo2) {
                if (cislo1 <= cislo2)
                        return cislo1;
                else
                        return cislo2;
        }

        public void sustredneKruhy(double polomer) {
                int pocitadlo = 1;
                while (polomer >= 1) {

                        if (pocitadlo % 3 == 1) {
                                this.setFillColor(Color.red);
                        }
                        if (pocitadlo % 3 == 2) {
                                this.setFillColor(Color.blue);
                        }
                        if (pocitadlo % 3 == 0) {
                                int r = (int) (Math.random() * 256);
                                this.setFillColor(new Color(r, r, r));
                        }

                        this.dot(polomer);
                        polomer = polomer * 0.8;
                        pocitadlo++;
                }
        }

        public long mocnina(int n, int k) {
                long vysledok = 1;
                for (int i = 0; i < k; i++) {
                        vysledok = vysledok * n;
                }
                return vysledok;
        }

        public long faktorial(int n) {
                int vysledok = 1;
                for (int i = 2; i <= n; i++) {
                        vysledok *= i;
                }
                return vysledok;
        }

        public int pocetDelitelov(int n) {
                n = Math.abs(n);
                int pocet = 2; // kazde cislo je delitelne 1 a samym sebou
                for (int i = 2; i < n; i++) {
                        if (n % i == 0) {
                                pocet++;
                        }
                }
                return pocet;
        }

        public boolean jePrvocislo(int n) {
                return n >= 2 && pocetDelitelov(n) == 2;
        }

        public int pocetCifier(int n) {
                int pocet = 1;
                while (n > 9) {
                        pocet++;
                        n /= 10;
                }
                return pocet;
        }

        public boolean maCifru(byte c, int n) {
                if (n < 10) {
                        return n == c;
                }

                while (n > 0) {
                        if (n % 10 == c) {
                                return true;
                        }
                        n /= 10;
                }
                return false;
        }

        public boolean nerastuceCifry(int n) {
                if (n < 10) {
                        return true;
                }

                int cifra = n % 10;
                n /= 10;

                while (n > 0) {
                        if (n % 10 < cifra) {
                                return false;
                        }

                        cifra = n % 10;
                        n /= 10;
                }
                return true;
        }

        public int NSD(int a, int b) {
                a = Math.abs(a);
                b = Math.abs(b);
                int mensie = Math.min(a, b);
                for (int i = mensie; i >= 2; i--) {
                        if (a % i == 0 && b % i == 0)
                                return i;
                }
                return 1;
        }

        public int NSN(int a, int b) {
                // a * b = NSD(a, b) * NSN(a, b)
                return (a * b) / NSD(a, b);
        }
}
import sk.upjs.jpaz2.Turtle;

public class Znakarka extends Turtle {

        public int pocetVyskytov(String s, char znak) {
                int pocet = 0;
                for (int i = 0; i < s.length(); i++) {
                        if (s.charAt(i) == znak) {
                                pocet++;
                        }
                }
                return pocet;
        }

        public boolean jePalindrom(String s) {
                if (s.length() <= 1) {
                        return true;
                }

                for (int i = 0; i < s.length() / 2; i++) {
                        if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
                                return false;
                        }
                }
                return true;
        }
}