Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package com.fabby.test;
  2.  
  3. import java.security.SecureRandom;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class Main {
  10.  
  11.     protected static SecureRandom random = new SecureRandom();
  12.  
  13.     enum Type {
  14.         SQUARE {
  15.             public int operation(int a) {
  16.                 return a*a;
  17.             }
  18.         },
  19.         CUBE {
  20.             public int operation(int a) {
  21.                 return a*a*a;
  22.             }
  23.         };
  24.        
  25.         public abstract int operation(int a);
  26.     }
  27.    
  28.     private static void review(Type t, boolean find_roots, ArrayList<Integer> numbers, Scanner s) {
  29.         for(Integer i : numbers) {
  30.             int num = find_roots ? t.operation(i) : i;
  31.             int answer = find_roots ? i : t.operation(i);
  32.            
  33.             String question = "What is the " + t.name().toLowerCase() + "" + (find_roots ? " root" : "") +
  34.                     " of " + num;
  35.             System.out.println(question);
  36.            
  37.             int guess = s.nextInt();
  38.            
  39.             if (guess == answer) {
  40.                 System.out.println("You got it right! The answer was " + answer + ".");
  41.             } else {
  42.                 System.out.println("That's wrong! The answer was " + answer + ".");
  43.             }
  44.         }
  45.     }
  46.  
  47.     public static void main(String[] args) {
  48.         /*
  49.          * create our list, and populate it
  50.          */
  51.         ArrayList<Integer> nums = new ArrayList<>();
  52.  
  53.         for(int i = 1; i <= 40; i++) {
  54.             nums.add(i);
  55.         }
  56.        
  57.         /* randomly shuffle it */
  58.         Collections.shuffle(nums, random);
  59.  
  60.         System.out.println("Please enter squares or cubes:");
  61.  
  62.  
  63.         Scanner s = new Scanner(System.in);
  64.         Type type = s.next().contains("sq") ? Type.SQUARE : Type.CUBE;
  65.  
  66.         System.out.println("Enter 'Yes' to find roots, or 'no' to be given them.");
  67.         boolean find_roots = s.next().contains("y");
  68.    
  69.         review(type, find_roots, nums, s);
  70.        
  71.         s.close();
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement