Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.fabby.test;
- import java.security.SecureRandom;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- public class Main {
- protected static SecureRandom random = new SecureRandom();
- enum Type {
- SQUARE {
- public int operation(int a) {
- return a*a;
- }
- },
- CUBE {
- public int operation(int a) {
- return a*a*a;
- }
- };
- public abstract int operation(int a);
- }
- private static void review(Type t, boolean find_roots, ArrayList<Integer> numbers, Scanner s) {
- for(Integer i : numbers) {
- int num = find_roots ? t.operation(i) : i;
- int answer = find_roots ? i : t.operation(i);
- String question = "What is the " + t.name().toLowerCase() + "" + (find_roots ? " root" : "") +
- " of " + num;
- System.out.println(question);
- int guess = s.nextInt();
- if (guess == answer) {
- System.out.println("You got it right! The answer was " + answer + ".");
- } else {
- System.out.println("That's wrong! The answer was " + answer + ".");
- }
- }
- }
- public static void main(String[] args) {
- /*
- * create our list, and populate it
- */
- ArrayList<Integer> nums = new ArrayList<>();
- for(int i = 1; i <= 40; i++) {
- nums.add(i);
- }
- /* randomly shuffle it */
- Collections.shuffle(nums, random);
- System.out.println("Please enter squares or cubes:");
- Scanner s = new Scanner(System.in);
- Type type = s.next().contains("sq") ? Type.SQUARE : Type.CUBE;
- System.out.println("Enter 'Yes' to find roots, or 'no' to be given them.");
- boolean find_roots = s.next().contains("y");
- review(type, find_roots, nums, s);
- s.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement