Beginner radian/degree converter program
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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 userChoice.contains("d"))
System.out.print("Degrees: ");
BufferedReader dRead = new BufferedReader(new InputStreamReader(System.in));
dtr(dRead);
else if (userChoice.contains("R")
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.
java beginner rational-numbers unit-conversion
New contributor
add a comment |Â
up vote
2
down vote
favorite
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 userChoice.contains("d"))
System.out.print("Degrees: ");
BufferedReader dRead = new BufferedReader(new InputStreamReader(System.in));
dtr(dRead);
else if (userChoice.contains("R")
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.
java beginner rational-numbers unit-conversion
New contributor
Regarding ` BufferedReader userInput ...` while in this example it is harmless no to close IO resources, it is good practice to do so in general. Check out try with resources statement: docs.oracle.com/javase/tutorial/essential/exceptions. /tryResourceClose.html. It is also common to reuse the same Reader and not creating a new one every time you need to perform a read operat
â David Soroko
1 hour ago
@DavidSoroko Sounds like the start of a review. Feel free to make it an answer.
â Mast
1 hour ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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 userChoice.contains("d"))
System.out.print("Degrees: ");
BufferedReader dRead = new BufferedReader(new InputStreamReader(System.in));
dtr(dRead);
else if (userChoice.contains("R")
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.
java beginner rational-numbers unit-conversion
New contributor
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 userChoice.contains("d"))
System.out.print("Degrees: ");
BufferedReader dRead = new BufferedReader(new InputStreamReader(System.in));
dtr(dRead);
else if (userChoice.contains("R")
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.
java beginner rational-numbers unit-conversion
java beginner rational-numbers unit-conversion
New contributor
New contributor
edited 2 hours ago
200_success
126k14146407
126k14146407
New contributor
asked 4 hours ago
avavstack
113
113
New contributor
New contributor
Regarding ` BufferedReader userInput ...` while in this example it is harmless no to close IO resources, it is good practice to do so in general. Check out try with resources statement: docs.oracle.com/javase/tutorial/essential/exceptions. /tryResourceClose.html. It is also common to reuse the same Reader and not creating a new one every time you need to perform a read operat
â David Soroko
1 hour ago
@DavidSoroko Sounds like the start of a review. Feel free to make it an answer.
â Mast
1 hour ago
add a comment |Â
Regarding ` BufferedReader userInput ...` while in this example it is harmless no to close IO resources, it is good practice to do so in general. Check out try with resources statement: docs.oracle.com/javase/tutorial/essential/exceptions. /tryResourceClose.html. It is also common to reuse the same Reader and not creating a new one every time you need to perform a read operat
â David Soroko
1 hour ago
@DavidSoroko Sounds like the start of a review. Feel free to make it an answer.
â Mast
1 hour ago
Regarding ` BufferedReader userInput ...` while in this example it is harmless no to close IO resources, it is good practice to do so in general. Check out try with resources statement: docs.oracle.com/javase/tutorial/essential/exceptions. /tryResourceClose.html. It is also common to reuse the same Reader and not creating a new one every time you need to perform a read operat
â David Soroko
1 hour ago
Regarding ` BufferedReader userInput ...` while in this example it is harmless no to close IO resources, it is good practice to do so in general. Check out try with resources statement: docs.oracle.com/javase/tutorial/essential/exceptions. /tryResourceClose.html. It is also common to reuse the same Reader and not creating a new one every time you need to perform a read operat
â David Soroko
1 hour ago
@DavidSoroko Sounds like the start of a review. Feel free to make it an answer.
â Mast
1 hour ago
@DavidSoroko Sounds like the start of a review. Feel free to make it an answer.
â Mast
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
Nowadays is common to omit the generic type on the right hand side of an assignment so instead of
ArrayList<Integer> divisors = new ArrayList<Integer>();
you can write
ArrayList<Integer> divisors = new ArrayList<>();
Since you are not using any ArrayList
specific functionality it is cleaner to declare divisors
as List
(same goes for gcd
)
List<Integer> divisors = new ArrayList<>();
The call gcd.get(gcd.size() deserves a separate method with a reasonable name.
Regarding BufferedReader userInput ...
while in this example it is harmless to not close IO resources, it is good practice to do so in general. Check out "try with resources statement": https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html.
It is also common to reuse the Reader and not create a new one every time you need to perform a read operation. With this in mind you will have something like this:
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: ");
try (BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)))
Note that now rtd
requires only one parameter. Because you print before reading user input, the user gets to see
Radical numerator (omit pi):
Radical denominator:
before she has an opportunity to provide any input. Those statements should move into rtd
interleaved with reading user input.
if (userChoice.contains("D") || userChoice.contains("d"))
can be simplified like so:
if (userChoice.toLowerCase().contains("d")) {
add a comment |Â
up vote
0
down vote
The Euclidean algorithm is a well-known
and efficient method to compute the greatest common divisor of two integers.
Compared to your approach, the Euclidean algorithm is faster, less code, and
does not require additional storage.
Using for example
// https://rosettacode.org/wiki/Greatest_common_divisor#Iterative_Euclid.27s_Algorithm
public static int gcd(int a, int b)
while (b > 0)
int c = a % b;
a = b;
b = c;
return a;
reducing the fraction simplifies to
int degrees = Integer.parseInt(dRead.readLine());
int commonDivisor = gcd(degrees, 180);
int numerator = degrees / commonDivisor;
int denominator = 180 / commonDivisor;
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Nowadays is common to omit the generic type on the right hand side of an assignment so instead of
ArrayList<Integer> divisors = new ArrayList<Integer>();
you can write
ArrayList<Integer> divisors = new ArrayList<>();
Since you are not using any ArrayList
specific functionality it is cleaner to declare divisors
as List
(same goes for gcd
)
List<Integer> divisors = new ArrayList<>();
The call gcd.get(gcd.size() deserves a separate method with a reasonable name.
Regarding BufferedReader userInput ...
while in this example it is harmless to not close IO resources, it is good practice to do so in general. Check out "try with resources statement": https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html.
It is also common to reuse the Reader and not create a new one every time you need to perform a read operation. With this in mind you will have something like this:
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: ");
try (BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)))
Note that now rtd
requires only one parameter. Because you print before reading user input, the user gets to see
Radical numerator (omit pi):
Radical denominator:
before she has an opportunity to provide any input. Those statements should move into rtd
interleaved with reading user input.
if (userChoice.contains("D") || userChoice.contains("d"))
can be simplified like so:
if (userChoice.toLowerCase().contains("d")) {
add a comment |Â
up vote
1
down vote
Nowadays is common to omit the generic type on the right hand side of an assignment so instead of
ArrayList<Integer> divisors = new ArrayList<Integer>();
you can write
ArrayList<Integer> divisors = new ArrayList<>();
Since you are not using any ArrayList
specific functionality it is cleaner to declare divisors
as List
(same goes for gcd
)
List<Integer> divisors = new ArrayList<>();
The call gcd.get(gcd.size() deserves a separate method with a reasonable name.
Regarding BufferedReader userInput ...
while in this example it is harmless to not close IO resources, it is good practice to do so in general. Check out "try with resources statement": https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html.
It is also common to reuse the Reader and not create a new one every time you need to perform a read operation. With this in mind you will have something like this:
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: ");
try (BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)))
Note that now rtd
requires only one parameter. Because you print before reading user input, the user gets to see
Radical numerator (omit pi):
Radical denominator:
before she has an opportunity to provide any input. Those statements should move into rtd
interleaved with reading user input.
if (userChoice.contains("D") || userChoice.contains("d"))
can be simplified like so:
if (userChoice.toLowerCase().contains("d")) {
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Nowadays is common to omit the generic type on the right hand side of an assignment so instead of
ArrayList<Integer> divisors = new ArrayList<Integer>();
you can write
ArrayList<Integer> divisors = new ArrayList<>();
Since you are not using any ArrayList
specific functionality it is cleaner to declare divisors
as List
(same goes for gcd
)
List<Integer> divisors = new ArrayList<>();
The call gcd.get(gcd.size() deserves a separate method with a reasonable name.
Regarding BufferedReader userInput ...
while in this example it is harmless to not close IO resources, it is good practice to do so in general. Check out "try with resources statement": https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html.
It is also common to reuse the Reader and not create a new one every time you need to perform a read operation. With this in mind you will have something like this:
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: ");
try (BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)))
Note that now rtd
requires only one parameter. Because you print before reading user input, the user gets to see
Radical numerator (omit pi):
Radical denominator:
before she has an opportunity to provide any input. Those statements should move into rtd
interleaved with reading user input.
if (userChoice.contains("D") || userChoice.contains("d"))
can be simplified like so:
if (userChoice.toLowerCase().contains("d")) {
Nowadays is common to omit the generic type on the right hand side of an assignment so instead of
ArrayList<Integer> divisors = new ArrayList<Integer>();
you can write
ArrayList<Integer> divisors = new ArrayList<>();
Since you are not using any ArrayList
specific functionality it is cleaner to declare divisors
as List
(same goes for gcd
)
List<Integer> divisors = new ArrayList<>();
The call gcd.get(gcd.size() deserves a separate method with a reasonable name.
Regarding BufferedReader userInput ...
while in this example it is harmless to not close IO resources, it is good practice to do so in general. Check out "try with resources statement": https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html.
It is also common to reuse the Reader and not create a new one every time you need to perform a read operation. With this in mind you will have something like this:
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: ");
try (BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)))
Note that now rtd
requires only one parameter. Because you print before reading user input, the user gets to see
Radical numerator (omit pi):
Radical denominator:
before she has an opportunity to provide any input. Those statements should move into rtd
interleaved with reading user input.
if (userChoice.contains("D") || userChoice.contains("d"))
can be simplified like so:
if (userChoice.toLowerCase().contains("d")) {
edited 50 mins ago
answered 1 hour ago
David Soroko
1713
1713
add a comment |Â
add a comment |Â
up vote
0
down vote
The Euclidean algorithm is a well-known
and efficient method to compute the greatest common divisor of two integers.
Compared to your approach, the Euclidean algorithm is faster, less code, and
does not require additional storage.
Using for example
// https://rosettacode.org/wiki/Greatest_common_divisor#Iterative_Euclid.27s_Algorithm
public static int gcd(int a, int b)
while (b > 0)
int c = a % b;
a = b;
b = c;
return a;
reducing the fraction simplifies to
int degrees = Integer.parseInt(dRead.readLine());
int commonDivisor = gcd(degrees, 180);
int numerator = degrees / commonDivisor;
int denominator = 180 / commonDivisor;
add a comment |Â
up vote
0
down vote
The Euclidean algorithm is a well-known
and efficient method to compute the greatest common divisor of two integers.
Compared to your approach, the Euclidean algorithm is faster, less code, and
does not require additional storage.
Using for example
// https://rosettacode.org/wiki/Greatest_common_divisor#Iterative_Euclid.27s_Algorithm
public static int gcd(int a, int b)
while (b > 0)
int c = a % b;
a = b;
b = c;
return a;
reducing the fraction simplifies to
int degrees = Integer.parseInt(dRead.readLine());
int commonDivisor = gcd(degrees, 180);
int numerator = degrees / commonDivisor;
int denominator = 180 / commonDivisor;
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The Euclidean algorithm is a well-known
and efficient method to compute the greatest common divisor of two integers.
Compared to your approach, the Euclidean algorithm is faster, less code, and
does not require additional storage.
Using for example
// https://rosettacode.org/wiki/Greatest_common_divisor#Iterative_Euclid.27s_Algorithm
public static int gcd(int a, int b)
while (b > 0)
int c = a % b;
a = b;
b = c;
return a;
reducing the fraction simplifies to
int degrees = Integer.parseInt(dRead.readLine());
int commonDivisor = gcd(degrees, 180);
int numerator = degrees / commonDivisor;
int denominator = 180 / commonDivisor;
The Euclidean algorithm is a well-known
and efficient method to compute the greatest common divisor of two integers.
Compared to your approach, the Euclidean algorithm is faster, less code, and
does not require additional storage.
Using for example
// https://rosettacode.org/wiki/Greatest_common_divisor#Iterative_Euclid.27s_Algorithm
public static int gcd(int a, int b)
while (b > 0)
int c = a % b;
a = b;
b = c;
return a;
reducing the fraction simplifies to
int degrees = Integer.parseInt(dRead.readLine());
int commonDivisor = gcd(degrees, 180);
int numerator = degrees / commonDivisor;
int denominator = 180 / commonDivisor;
answered 20 mins ago
Martin R
15k12261
15k12261
add a comment |Â
add a comment |Â
avavstack is a new contributor. Be nice, and check out our Code of Conduct.
avavstack is a new contributor. Be nice, and check out our Code of Conduct.
avavstack is a new contributor. Be nice, and check out our Code of Conduct.
avavstack is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206436%2fbeginner-radian-degree-converter-program%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Regarding ` BufferedReader userInput ...` while in this example it is harmless no to close IO resources, it is good practice to do so in general. Check out try with resources statement: docs.oracle.com/javase/tutorial/essential/exceptions. /tryResourceClose.html. It is also common to reuse the same Reader and not creating a new one every time you need to perform a read operat
â David Soroko
1 hour ago
@DavidSoroko Sounds like the start of a review. Feel free to make it an answer.
â Mast
1 hour ago