Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import static java.lang.System.out;
- import static java.lang.System.err;
- import static java.lang.System.in;
- import java.util.Scanner;
- enum FirstSecondValue {
- first(0),
- second(1);
- private final int witch;
- private FirstSecondValue(int witch) {
- this.witch = witch;
- }
- public int getWitch() {
- return witch;
- }
- }
- /**
- * Toy with the Fibonacci sequence. Find out about the Golden Ratio.
- * https://youtu.be/6nSfJEDZ_WM
- *
- * @author Aidan
- */
- final class FibonacciToy {
- static {
- GOLDEN_RATIO = (Math.sqrt(5) + 1d) / 2d;
- }
- private static final Double GOLDEN_RATIO;
- private final Integer numbers[]; // Previous, Current
- public FibonacciToy() {
- this.numbers = new Integer[]{1, 1};
- }
- public static Double getGoldenRatio() {
- return GOLDEN_RATIO;
- }
- public Double approximateGoldenRatio() {
- return (Double) ((double) (int) this.numbers[1] / (double) (int) this.numbers[0]);
- }
- public Integer get(FirstSecondValue fs) {
- return this.numbers[fs.getWitch()];
- }
- public void set(Integer value, FirstSecondValue fs) {
- this.numbers[fs.getWitch()] = value;
- }
- public void stepBackOnce() {
- final Integer tmp[] = new Integer[]{
- this.numbers[1] - this.numbers[0],
- this.numbers[0]
- };
- this.numbers[0] = tmp[0];
- this.numbers[1] = tmp[1];
- }
- public void stepForwardOnce() {
- final Integer tmp[] = new Integer[]{
- this.numbers[1],
- this.numbers[0] + this.numbers[1]
- };
- this.numbers[0] = tmp[0];
- this.numbers[1] = tmp[1];
- }
- public void stepForward(Integer steps) {
- for (Integer i = 0; i < steps; i++) {
- this.stepForwardOnce();
- }
- }
- public void stepBackward(Integer steps) {
- for (Integer i = 0; i < steps; i++) {
- this.stepBackOnce();
- }
- }
- public void stepForwardAndRun(Runnable r, Integer steps) {
- for (Integer i = 0; i < steps; i++) {
- this.stepForwardOnce();
- r.run();
- }
- }
- public void stepBackwardAndRun(Runnable r, Integer steps) {
- for (Integer i = 0; i < steps; i++) {
- this.stepBackOnce();
- r.run();
- }
- }
- }
- /**
- * Interact with a Fibonacci Sequence to find its Pisano Period.
- * https://youtu.be/Nu-lW-Ifyec
- *
- * @author Aidan
- */
- final class PisanoPeriod {
- private final FibonacciToy toy;
- private Integer modulo, periodicity;
- public PisanoPeriod(FibonacciToy toy) {
- this.toy = toy;
- this.modulo = 2;
- this.periodicity = 0;
- }
- public Integer getModulo() {
- return this.modulo;
- }
- public Integer getPeriodicy() {
- return this.periodicity;
- }
- public void setModulo(Integer modulo) {
- this.modulo = modulo;
- }
- // Pisano Period
- public void findPeriodicity() {
- final Integer save[] = new Integer[2];
- save[0] = this.toy.get(FirstSecondValue.first);
- save[1] = this.toy.get(FirstSecondValue.second);
- //
- Integer tmp[] = new Integer[2];
- Boolean hit01 = false;
- Integer period = 0;
- //
- while (true) {
- this.toy.stepForwardOnce();
- tmp[0] = this.toy.get(FirstSecondValue.first) % modulo;
- tmp[1] = this.toy.get(FirstSecondValue.second) % modulo;
- //
- if (tmp[0] == 0 && tmp[1] == 1) {
- if (hit01) {
- break;
- } else {
- hit01 = true;
- }
- }
- //
- if (hit01) {
- period += 1;
- }
- }
- this.toy.set(save[0], FirstSecondValue.first);
- this.toy.set(save[1], FirstSecondValue.second);
- //
- this.periodicity = period;
- }
- }
- /**
- * Access a thread-safe boolean.
- *
- * @author Aidan
- */
- final class LockedBoolean {
- private volatile Boolean x;
- private final Object l;
- public LockedBoolean(Boolean i) {
- this.l = new Object();
- this.setX(i);
- }
- public Boolean getX() {
- synchronized (this.l) {
- return this.x;
- }
- }
- public void setX(Boolean x) {
- synchronized (this.l) {
- this.x = x;
- }
- }
- public void flip() {
- this.setX(!this.getX());
- }
- }
- /**
- * Represent a user's place in the LUI.
- *
- * @author Aidan
- */
- enum Pages {
- MAIN,
- ALLOCATE,
- DEALLOCATE,
- MANIPUATE,
- M_GS_FIRST_NUMBER,
- M_GS_SECOND_NUMBER,
- M_STEP_FORWARD_ONCE,
- M_STEP_BACKWARD_ONCE,
- M_STEP_FORWARD,
- M_STEP_BACKWARD,
- M_FIND_PISANO_PERIOD,
- M_FIND_PISANO_PERIOD_GET_MOD,
- M_FIND_PISANO_PERIOD_SET_MOD,
- M_FIND_PISANO_PERIOD_FIND,
- M_FIND_PISANO_PERIOD_DISPLAY,
- M_FIND_PISANO_PERIOD_EXIT,
- M_GA_GOLDEN_RATIO,
- M_GA_GOLDEN_RATIO_APPROX,
- M_GA_GOLDEN_RATIO_EXACT,
- M_GA_GOLDEN_RATIO_DEGREE,
- M_GA_EXIT,
- M_EXIT,
- EXIT
- }
- /**
- * Demonstrate a well-structured LUI. https://youtu.be/SGZr06TpORE
- *
- * @author Aidan
- */
- public class ConsolePages {
- private static Boolean getTF(final String pmt, final String o1, final String o2) {
- final String tmp_pmt = "For " + pmt + ", " + o1 + " [0] or " + o2 + " [1]? ";
- Short input;
- while (true) {
- out.print(tmp_pmt);
- out.flush();
- try {
- input = new Scanner(in).nextShort();
- if (input == 0) {
- return true;
- }
- if (input == 1) {
- return false;
- }
- } catch (Exception e) {
- }
- }
- }
- private static Boolean getTF(final String pmt, final String o1, final String o2, final String o3) {
- final String tmp_pmt = "For " + pmt + ", " + o1 + " [0], " + o2 + " [1], " + o3 + " [2]? ";
- Short input;
- while (true) {
- out.print(tmp_pmt);
- out.flush();
- try {
- input = new Scanner(in).nextShort();
- if (input == 0) {
- return null;
- }
- if (input == 1) {
- return false;
- }
- if (input == 2) {
- return true;
- }
- } catch (Exception e) {
- }
- }
- }
- private static Integer getInt(String pmt) {
- final String tmp_pmt = pmt + ": ";
- Integer input;
- while (true) {
- out.print(tmp_pmt);
- out.flush();
- try {
- input = new Scanner(in).nextInt();
- if (input > 0) {
- return input;
- }
- } catch (Exception e) {
- }
- }
- }
- public static void main(String[] args) {
- FibonacciToy toy = null;
- Pages page = Pages.ALLOCATE;
- MainLoop:
- while (true) {
- switch (page) {
- case MAIN: {
- out.println(
- "\nMain Page:\n"
- + "1. Allocate Toy\n"
- + "2. Deallocate Toy\n"
- + "3. Manipulate Toy\n"
- + "4. Exit Program");
- out.flush();
- ManipulationLoop:
- while (true) {
- out.print("Option: ");
- out.flush();
- try {
- switch (new Scanner(in).nextShort()) {
- case 1: {
- page = Pages.ALLOCATE;
- break ManipulationLoop;
- }
- case 2: {
- page = Pages.DEALLOCATE;
- break ManipulationLoop;
- }
- case 3: {
- page = Pages.MANIPUATE;
- break ManipulationLoop;
- }
- case 4: {
- page = Pages.EXIT;
- break ManipulationLoop;
- }
- }
- } catch (Exception e) {
- }
- }
- break;
- }
- case ALLOCATE: {
- if (toy == null) {
- try {
- toy = new FibonacciToy();
- out.println("Toy allocation succeeded.");
- out.flush();
- } catch (Exception e) {
- err.println("Toy allocation failed: Memory is out.");
- err.flush();
- }
- } else {
- err.println("Toy is already allocated.");
- err.flush();
- }
- page = Pages.MAIN;
- break;
- }
- case DEALLOCATE: {
- if (toy != null) {
- toy = null;
- out.println("Toy deallocation succeeded.");
- out.flush();
- } else {
- err.println("Toy not yet allocated.");
- out.flush();
- }
- page = Pages.MAIN;
- break;
- }
- case MANIPUATE: {
- if (toy != null) {
- ManipulationMainLoop:
- while (true) {
- switch (page) {
- case MANIPUATE: {
- out.println(
- "\nManipulation Page:\n"
- + "1. Set/get First Fibonacci Number\n"
- + "2. Set/get Second Fibonacci Number\n"
- + "3. Step Forward/backward Once\n"
- + "4. Step Forward/backward Multiple Times\n"
- + "5. Find period Of Fibonacci Sequence\n"
- + "6. Get/approximate the Golden Ratio\n"
- + "7. Return to Main Page");
- out.flush();
- ManipulationSubMainLoop:
- while (true) {
- out.print("Option: ");
- out.flush();
- try {
- switch (new Scanner(in).nextShort()) {
- case 1: {
- page = Pages.M_GS_FIRST_NUMBER;
- break ManipulationSubMainLoop;
- }
- case 2: {
- page = Pages.M_GS_SECOND_NUMBER;
- break ManipulationSubMainLoop;
- }
- case 3: {
- page = Pages.M_STEP_BACKWARD_ONCE;
- break ManipulationSubMainLoop;
- }
- case 4: {
- page = Pages.M_STEP_BACKWARD;
- break ManipulationSubMainLoop;
- }
- case 5: {
- page = Pages.M_FIND_PISANO_PERIOD;
- break ManipulationSubMainLoop;
- }
- case 6: {
- page = Pages.M_GA_GOLDEN_RATIO;
- break ManipulationSubMainLoop;
- }
- case 7: {
- page = Pages.M_EXIT;
- break ManipulationSubMainLoop;
- }
- }
- } catch (Exception e) {
- }
- }
- break;
- }
- case M_GS_FIRST_NUMBER: {
- final Boolean gs = getTF("the first number", "set", "get");
- if (gs) {
- Integer input;
- while (true) {
- out.print("New value: ");
- out.flush();
- try {
- input = new Scanner(in).nextInt();
- break;
- } catch (Exception e) {
- }
- }
- if (input <= toy.get(FirstSecondValue.second)) {
- toy.set(input, FirstSecondValue.first);
- out.println("Succeeded.");
- out.flush();
- } else {
- err.println("The number " + input + " is greater than " + toy.get(FirstSecondValue.second) + '.');
- err.flush();
- }
- } else {
- out.println("The first number is " + toy.get(FirstSecondValue.first) + '.');
- out.flush();
- }
- page = Pages.MANIPUATE;
- break;
- }
- case M_GS_SECOND_NUMBER: {
- final Boolean gs = getTF("the second number", "set", "get");
- if (gs) {
- Integer input;
- while (true) {
- out.print("New value: ");
- out.flush();
- try {
- input = new Scanner(in).nextInt();
- break;
- } catch (Exception e) {
- }
- }
- if (input >= toy.get(FirstSecondValue.first)) {
- toy.set(input, FirstSecondValue.second);
- out.println("Succeeded.");
- out.flush();
- } else {
- err.println("The number " + input + " is less than " + toy.get(FirstSecondValue.first) + '.');
- err.flush();
- }
- } else {
- out.println("The second number is " + toy.get(FirstSecondValue.second) + '.');
- out.flush();
- }
- page = Pages.MANIPUATE;
- break;
- }
- case M_STEP_BACKWARD_ONCE: {
- final Boolean direction = getTF("one step, go", "backward", "forward");
- if (direction) {
- toy.stepBackOnce();
- out.println("Succeeded.");
- out.flush();
- page = Pages.MANIPUATE;
- } else {
- page = Pages.M_STEP_FORWARD_ONCE;
- }
- break;
- }
- case M_STEP_BACKWARD: {
- final Boolean direction = getTF("some steps, go", "backward", "forward");
- if (direction) {
- toy.stepBackward(getInt("Number of steps backward"));
- out.println("Succeeded.");
- out.flush();
- page = Pages.MANIPUATE;
- } else {
- page = Pages.M_STEP_FORWARD;
- }
- break;
- }
- case M_STEP_FORWARD_ONCE: {
- toy.stepForwardOnce();
- out.println("Succeeded.");
- out.flush();
- page = Pages.MANIPUATE;
- break;
- }
- case M_STEP_FORWARD: {
- toy.stepForward(getInt("Number of steps forward"));
- out.println("Succeeded.");
- out.flush();
- page = Pages.MANIPUATE;
- break;
- }
- case M_FIND_PISANO_PERIOD: {
- final PisanoPeriod period = new PisanoPeriod(toy);
- PisanoLoop:
- while (true) {
- switch (page) {
- case M_FIND_PISANO_PERIOD: {
- out.println(
- "\nPisano Period:\n"
- + "1. Get the modulus\n"
- + "2. Set the modulus\n"
- + "3. Find the period\n"
- + "4. Show the period\n"
- + "5. Return to the manipulation page"
- );
- out.flush();
- PisanoOptionLoop:
- while (true) {
- out.print("Option: ");
- out.flush();
- switch (new Scanner(in).nextShort()) {
- case 1: {
- page = Pages.M_FIND_PISANO_PERIOD_GET_MOD;
- break PisanoOptionLoop;
- }
- case 2: {
- page = Pages.M_FIND_PISANO_PERIOD_SET_MOD;
- break PisanoOptionLoop;
- }
- case 3: {
- page = Pages.M_FIND_PISANO_PERIOD_FIND;
- break PisanoOptionLoop;
- }
- case 4: {
- page = Pages.M_FIND_PISANO_PERIOD_DISPLAY;
- break PisanoOptionLoop;
- }
- case 5: {
- page = Pages.M_FIND_PISANO_PERIOD_EXIT;
- break PisanoOptionLoop;
- }
- }
- }
- break;
- }
- case M_FIND_PISANO_PERIOD_GET_MOD: {
- out.println("The modulus is " + period.getModulo() + '.');
- out.flush();
- page = Pages.M_FIND_PISANO_PERIOD;
- break;
- }
- case M_FIND_PISANO_PERIOD_SET_MOD: {
- while (true) {
- final Integer input = getInt("Set modulus to (2+)");
- if (input != 1) {
- period.setModulo(input);
- break;
- }
- }
- page = Pages.M_FIND_PISANO_PERIOD;
- break;
- }
- case M_FIND_PISANO_PERIOD_FIND: {
- LockedBoolean event = new LockedBoolean(Boolean.TRUE);
- Thread runner = new Thread(() -> {
- final char display[] = new char[]{'|', '\\', '-', '/'};
- Integer location = 0;
- while (event.getX()) {
- out.print("\r" + display[location]);
- out.flush();
- if (++location == 5) {
- location = 0;
- }
- try {
- Thread.sleep(1000L);
- } catch (InterruptedException ex) {
- }
- }
- out.println("\r \rDone.");
- out.flush();
- });
- runner.setDaemon(true);
- runner.setName("PROGRES");
- runner.start();
- period.findPeriodicity();
- event.flip();
- while (true) {
- try {
- runner.join();
- break;
- } catch (InterruptedException e) {
- }
- }
- page = Pages.M_FIND_PISANO_PERIOD;
- break;
- }
- case M_FIND_PISANO_PERIOD_DISPLAY: {
- out.println("For the modulus of " + period.getModulo() + ", the Pisano period is " + period.getPeriodicy() + '.');
- out.flush();
- page = Pages.M_FIND_PISANO_PERIOD;
- break;
- }
- case M_FIND_PISANO_PERIOD_EXIT: {
- break PisanoLoop;
- }
- }
- }
- page = Pages.MANIPUATE;
- break;
- }
- case M_GA_GOLDEN_RATIO: {
- GoldenRatioMainLoop:
- while (true) {
- switch (page) {
- case M_GA_GOLDEN_RATIO: {
- out.println(
- "\nGolden Ratio:\n"
- + "1. Approximate\n"
- + "2. Get it exactly\n"
- + "3. Compare the two\n"
- + "4. Return to the manipulation page"
- );
- out.flush();
- GoldenRatioOptionLoop:
- while (true) {
- out.print("Option: ");
- out.flush();
- try {
- switch (new Scanner(in).nextShort()) {
- case 1: {
- page = Pages.M_GA_GOLDEN_RATIO_APPROX;
- break GoldenRatioOptionLoop;
- }
- case 2: {
- page = Pages.M_GA_GOLDEN_RATIO_EXACT;
- break GoldenRatioOptionLoop;
- }
- case 3: {
- page = Pages.M_GA_GOLDEN_RATIO_DEGREE;
- break GoldenRatioOptionLoop;
- }
- case 4: {
- page = Pages.M_GA_EXIT;
- break GoldenRatioOptionLoop;
- }
- }
- } catch (Exception e) {
- }
- }
- break;
- }
- case M_GA_GOLDEN_RATIO_APPROX: {
- out.println("The approximation is currently " + toy.approximateGoldenRatio() + '.');
- out.flush();
- page = Pages.M_GA_GOLDEN_RATIO;
- break;
- }
- case M_GA_GOLDEN_RATIO_EXACT: {
- out.println("The Golden Ratio \u03C6 is exectly " + FibonacciToy.getGoldenRatio() + '.');
- out.flush();
- page = Pages.M_GA_GOLDEN_RATIO;
- break;
- }
- case M_GA_GOLDEN_RATIO_DEGREE: {
- final Integer percentage = (int) (toy.approximateGoldenRatio() / FibonacciToy.getGoldenRatio() * 100);
- out.println("The current approximation is " + percentage + "% accurate.");
- out.flush();
- page = Pages.M_GA_GOLDEN_RATIO;
- break;
- }
- case M_GA_EXIT: {
- break GoldenRatioMainLoop;
- }
- }
- }
- page = Pages.MANIPUATE;
- break;
- }
- case M_EXIT: {
- break ManipulationMainLoop;
- }
- }
- }
- } else {
- err.println("No toy present.");
- err.flush();
- }
- page = Pages.MAIN;
- break;
- }
- case EXIT: {
- break MainLoop;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement