I'm a bit of a beginner when it comes to Java, and I created this small code to help me grasp a few small subjects. It allows its user to convert a radical to a degree measure and a degree measure to a radical. This is my final code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class MainClass {
public static void dtr (BufferedReader dRead) throws IOException {
ArrayList<Integer> divisors = new ArrayList<Integer>();
ArrayList<Integer> gcd = new ArrayList<Integer>();
int d = Integer.parseInt(dRead.readLine());
for (int pd = 1; pd <= d; pd++) {
if (d % pd == 0) {
divisors.add(pd);
}
}
for (int index = 0; index < divisors.size(); ++index) {
if (180.0 % divisors.get(index) == 0) {
gcd.add(divisors.get(index));
}
}
int dem = (180 / gcd.get(gcd.size() - 1));
if ((d / (gcd.get(gcd.size() - 1))) == 1) {
System.out.print("Radical: pi / " + dem);
} else {
System.out.print("Radical: " + (d / (gcd.get(gcd.size() - 1))) + "pi / " + dem);
}
}
public static void rtd (BufferedReader rnRead, BufferedReader rdRead) throws IOException {
int rn = Integer.parseInt(rnRead.readLine());
int rd = Integer.parseInt(rdRead.readLine());
int dividend = rn * 180;
System.out.print("Degrees: " + (dividend / rd));
}
public static void main(String[] args) throws IOException {
System.out.println("Do you want to convert from radicals to degrees, or degrees to radicals?");
System.out.print("Use \"R\" for radials to degrees, and \"D\" for degrees to radicals: ");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
String userChoice = userInput.readLine();
if (userChoice.contains("D") || userChoice.contains("d")) {
System.out.print("Degrees: ");
BufferedReader dRead = new BufferedReader(new InputStreamReader(System.in));
dtr(dRead);
} else if (userChoice.contains("R") || userChoice.contains("r")) {
System.out.println("Radical numerator (omit pi): ");
BufferedReader rnRead = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Radical denominator: ");
BufferedReader rdRead = new BufferedReader(new InputStreamReader(System.in));
rtd(rnRead, rdRead);
} else {
System.out.println("Invalid response. Please restart the program.");
}
}
}
I know how bad some of this code is, but I just want to know if there is any way I could have optimized what this does, because I'd like to apply those concepts to everything that I'll make in the future.