Advertisement
GeorgeColon

George Colon Lab

Sep 15th, 2017
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. package homework;
  2. /*
  3.  * Name:George Colon
  4.  * Date:9/15
  5.  * Course Number: csc-220
  6.  * Course Name: Data Structures and Alg
  7.  * Problem Number: IsuglyNumbers
  8.  * Email: Gcolon6085@student.stcc.edu
  9.  * Short Description of the Problem
  10.  */
  11.  
  12. import java.util.Scanner;
  13.  
  14. import GeorgeLibrary.Library;
  15.  
  16. public class UglyNumberFinder {
  17.  
  18.     // **********************************************
  19.  
  20.     private static void process(Scanner sc, String args[]) {
  21.         System.out.print("Enter value: ");
  22.         int lowLimit = sc.nextInt();
  23.         int highLimit = sc.nextInt();
  24.         sc.nextLine(); // IMPORTANT!! Reset Scanner
  25.         isUgly(lowLimit, highLimit);
  26.     }
  27.  
  28.     // **********************************************
  29.  
  30.     private static boolean doThisAgain(Scanner sc, String prompt) {
  31.         System.out.print(prompt);
  32.         String doOver = sc.nextLine();
  33.         return doOver.equalsIgnoreCase("Y");
  34.     }
  35.  
  36.     // **********************************************
  37.  
  38.     public static void isUgly(int lowLimit, int highLimit) {
  39.         int count = 1;
  40.         System.out.println("        These are the ugly numbers from " + lowLimit + " to " + highLimit + " \n");
  41.         for (int i = lowLimit; i < highLimit; i++) {
  42.             int temp = i;
  43.             double sqrt = Math.sqrt(temp);
  44.             int x = (int) sqrt;
  45.             if (Math.pow(sqrt, 2) == Math.pow(x, 2)) {
  46.                 // This finds numbers that are prime in order to exclude them
  47.             } else {
  48.                 temp = i;
  49.                 while (temp % 2 == 0)
  50.                     temp = temp / 2;
  51.                 while (temp % 3 == 0)
  52.                     temp = temp / 3;
  53.                 while (temp % 5 == 0)
  54.                     temp = temp / 5;
  55.                 if (temp == 1) {
  56.                     System.out.printf("%10d) %-15d  |", count, i);
  57.                     if ((count) % 3 == 0) {
  58.                         System.out.println();
  59.                         System.out.println(repeatString("-", 90));
  60.  
  61.                     }
  62.                     count++;
  63.                 }
  64.             }
  65.         }
  66.     }
  67.  
  68.     // **********************************************
  69.  
  70.     public static String repeatString(String str, int count) {
  71.         String specFmt = String.format("%%%ds", count);
  72.         String countSpaces = String.format(specFmt, "-");
  73.         return countSpaces.replace(" ", str);
  74.     }
  75.  
  76.     // **********************************************
  77.     public static void main(String args[]) {
  78.         final String TITLE = "Ugly Number Finder";
  79.         final String CONTINUE_PROMPT = "\nDo this again? [y/N] ";
  80.         System.out.println("Welcome to " + TITLE);
  81.         Scanner sc = new Scanner(System.in);
  82.         do {
  83.             // long startTime = Library.StartTime();
  84.             process(sc, args);
  85.             // Library.endTime(startTime);
  86.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  87.         sc.close();
  88.         System.out.println("Thank you for using " + TITLE);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement