Riešenia (skupina C, 3. týždeň)

Streda

package sk.paz1a.practicals.task03;

import java.awt.Color;

import sk.upjs.jpaz2.*;

public class SmartTurtle extends Turtle {

        public void mystery() {
                Color c = null;
                for (int i = 0; i < 256; i++) {
                        c = new Color(i, 100, 100);
                        this.setPenColor(c);
                        this.setPosition(i, 0);
                        this.moveTo(i, 150);
                }
        }

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

        }

        public double concentricCircles(double radius) {

                int counter = 0;
                double totalArea = 0;

                while (radius >= 0.5) {
                        if (counter % 3 == 0) {
                                this.setFillColor(Color.RED);
                        } else if (counter % 3 == 1) {
                                this.setFillColor(Color.BLUE);
                        } else {
                                this.setFillColor(Color.GRAY);
                        }

                        totalArea = totalArea + Math.PI * radius * radius;
                        this.dot(radius);

                        radius = radius * 0.8;
                        counter++;

                }
                return totalArea;
        }

}
package sk.paz1a.practicals.task03;

import sk.upjs.jpaz2.*;

public class Launcher {

        public static void main(String[] args) {
                WinPane sandbox = new WinPane();

                SmartTurtle franklin = new SmartTurtle();
                sandbox.add(franklin);

                ObjectInspector oi = new ObjectInspector();
                oi.inspect(franklin);
                oi.inspect(sandbox);

        }
}
 
package sk.paz1a.practicals.task03;

import sk.upjs.jpaz2.Turtle;

public class ScientificTurtle extends Turtle {

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

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

        public long factorial(int n) {
                long result = 1;

                for(int i = n; i>1; i-- ) {
                        result=result*i;
                }
                return result;
        }

}
 

Štvrtok

package sk.paz1a.practicals.task03;

import java.awt.Color;

import sk.upjs.jpaz2.*;

public class SmartTurtle extends Turtle {

        public void mystery() {
                Color c = null;
                for (int i = 0; i < 256; i++) {
                        c = new Color(i, 100, 100);
                        this.setPenColor(c);
                        this.setPosition(i, 0);
                        this.moveTo(i, 150);
                }
        }

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

        }

        public double concentricCircles(double radius) {

                int counter = 0;
                double totalArea = 0;

                while (radius >= 0.5) {
                        if (counter % 3 == 0) {
                                this.setFillColor(Color.RED);
                        } else if (counter % 3 == 1) {
                                this.setFillColor(Color.BLUE);
                        } else {
                                this.setFillColor(Color.GRAY);
                        }

                        totalArea = totalArea + Math.PI * radius * radius;
                        this.dot(radius);

                        radius = radius * 0.8;
                        counter++;

                }
                return totalArea;
        }

}
package sk.paz1a.practicals.task03;

import sk.upjs.jpaz2.*;

public class Launcher {

        public static void main(String[] args) {
                // create new "sandbox" - a place where turtles can live
                AnimatedWinPane sandbox = new AnimatedWinPane();

                // create new turtle and add it to the "sandbox"
                SmartTurtle franklin = new SmartTurtle();
                sandbox.add(franklin);

                // create new object inspector
                ObjectInspector oi = new ObjectInspector();
                // ask the inspector to inspect "franklin" and "sandbox"
                oi.inspect(franklin);
                oi.inspect(sandbox);

                // you can put other initialization commands here
                ScientificTurtle albert = new ScientificTurtle();
                oi.inspect(albert);

                albert.isPrime(12);

                int a = 13;
                int b = 15;

                // Klasický výpočet násobením
                long vysledok = 1;
                for (int i = 0; i < b; i++) {
                        vysledok = vysledok * a;
                }
                System.out.println(vysledok);

                // Výpočet s využitím Math.pow a pretypovania
                vysledok = (long) Math.pow(a, b);
                System.out.println(vysledok);
        }
}
 
package sk.paz1a.practicals.task03;

import sk.upjs.jpaz2.Turtle;

public class ScientificTurtle extends Turtle {

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

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

        public long factorial(int n) {
                long result = 1;

                for (int i = n; i > 1; i--) {
                        result = result * i;
                }
                return result;
        }

        public int countDivisors(int n) {
                int counter = 0;
                for (int i = 0; i < n; i++) {
                        if (n % (i + 1) == 0) {
                                counter++;
                        }
                }
                return counter;
        }

        public int countDigits(int n) {
                int numberOfDigits = 0;
                while (n > 0) {
                        numberOfDigits++;
                        n = n / 10;
                }
                return numberOfDigits;

        }

        public boolean isPrime(int n) {
                int numberOfDivisors = 0;
                for (int i = 1; i <= n; i++) {
                        if (n % i == 0) {
                                numberOfDivisors++;

                        }

                }
                if (numberOfDivisors == 2) {
                        return true;
                }
                return false;

        }

        public boolean isPrime2(int n) {

                return this.countDivisors(n) == 2;
        }

        public int findGreatestDigit(int n) {
                int greatestDigit = 0;
                while (n > 0) {
                        if(greatestDigit<n%10) {
                                greatestDigit = n%10;

                        }
                        // alebo:
//                      greatestDigit = Math.max(greatestDigit, n % 10);
                        n = n / 10;
                }
                return greatestDigit;
        }

}