Created
October 23, 2014 13:56
-
-
Save Twister915/2135ee1c4bf976739dbb to your computer and use it in GitHub Desktop.
The assignment said to make at least three comments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Scanner; | |
public final class DigitToolsApplication extends Application { | |
public static void main(String[] args) { | |
new DigitToolsApplication().run(); | |
} | |
@Override | |
protected void runCycle(Scanner scanner) { | |
//Ask for input | |
printInline("Please type a number: "); | |
String nextLine = scanner.nextLine(); | |
//Try to convert the input (which is a string) to an Integer so that we can manipulate it | |
Integer i; | |
try { | |
//Try to get the value of the string | |
i = Integer.valueOf(nextLine); | |
} catch (NumberFormatException e) { | |
//But in the event that the string is invalid, we will throw an exception (error). | |
throw new RuntimeException("Could not read passed digit", e); | |
} | |
//Now, we setup our variables to track the data we want | |
Integer count, sum = 0, average, reverse = 0; | |
//we want to also know if this is a perfect square | |
boolean isPerfectSquare = false; | |
//Along with some lists to store the digits and divisors | |
List<Integer> divisors = new ArrayList<>(); | |
List<Integer> digits = new ArrayList<>(); | |
//Now, we create a temporary copy of "i" (which is the number we're manipulating), and start separating out the digits | |
//This loop creates a copy of I, repeats as long as I > 0, and divides i by 10 after each loop (moving the decimal place) | |
for (int workingI = i; workingI > 0; workingI = workingI/10) { | |
//Now we get the remainder of division by 10 | |
//12 % 10 = 2 and the last digit in 12 is 2 thus we have the last digit | |
digits.add(workingI % 10); | |
//Now, we let the for loop cut off that last digit by dividing by 10 (see last statement in for loop above) | |
} | |
//Now that we have the digits in a list, we can get the size of that list as our digit count | |
count = digits.size(); | |
//And we can iterate through all the digits to calculate the sum. | |
for (Integer i1 : digits) {//For every digit, add it to sum | |
sum += i1; | |
} | |
//Now that we have both the sum and the count, we can divide the two to get the average | |
average = sum/count; | |
//Next part, let's get the reverse | |
//we go in reverse through the digits array list using a for loop starting at count-1 | |
//count-1 is the last index in the list of digits | |
//Then, we use the N variable to keep track of how many times we've run the for loop | |
//So, for each iteration, we add one to N (the number of times this for loop has run) and | |
//remove one from index, so that we can traverse the array in reverse | |
//this will continue until index < 0 (the first index) | |
for (int index = count-1, n = 0; index >= 0; index--, n++) { | |
//Now we add 10 to the power of N times the digit in question | |
reverse += (int)Math.pow(10, n)*digits.get(index); | |
} | |
//Lastly, we need to calculate the divisors. We simply try every whole number between 1 and the square root of i | |
//we use the square root because that is the "midpoint" where all divisors on either end will match up. | |
//Imagine a number line going from 1 to i. In the middle of this number line is Math.sqrt(i) | |
//Any divisor on the left side of that midpoint will have a partner on the right side that is equal to i/x | |
//For example, all numbers have a divisor of 1. The "pair" for this divisor is i, and you can get that by doing i/1 | |
for (int x = 1, sqrt = (int) Math.ceil(Math.sqrt(i)); x <= sqrt ; x++) { | |
//If the number evenly divides, it is a divisor. | |
if (i % x == 0) { | |
//If we have found a divisor, we add it to our divisor collection. | |
divisors.add(x); | |
//Then, to avoid adding i itself, we check if x == 1. If so, we do not add i/x (the matching divisor) to the array | |
//otherwise, we add the matching divisor | |
//We also want to avoid adding the same number twice. This is useful only for perfect squares, like 25, where x = 5; 25/5 = 5, thus 5 will be added twice | |
int matchingPair = i/x; | |
if (x != 1 && !divisors.contains(matchingPair)) divisors.add(matchingPair); | |
//also, although we likely know this is true given that the above statement failed (only two cases in which if fails, when x == 1, or when i/x = x, or x*x = i ;) | |
//we want to track if this is a perfect square. | |
else if (x == sqrt) isPerfectSquare = true; | |
} | |
} | |
//Lastly, since we added divisor matches next to their original divisors, the array will be out of order. We want to sort it | |
//Java will automatically sort by least-->greatest in an array containing numbers. | |
Collections.sort(divisors); | |
//Now we need to check if the number is perfect, so we're going to calculate the sum of all the divisors. | |
int divisorSum = 0; | |
//For each divisor, add it to the sum. | |
for (Integer divisor : divisors) { | |
divisorSum += divisor; | |
} | |
//And now we can do the output | |
print(">Number " + i, | |
"\t>Sum " + sum, | |
"\t>Average " + average, | |
"\t>Reverse " + reverse, | |
"\t>Divisors " + divisors, | |
//This checks if the condition is true, and if so uses the first value. Otherwise, it will use the second. | |
"\t>Is perfect " + (divisorSum == i ? "Yes" : "No"), | |
"\t>Is perfect Square " + (isPerfectSquare ? "Yes" : "No")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment