patternjavaModerate
Entering two integers and returning a decimal fraction
Viewed 0 times
decimaltworeturningfractionandenteringintegers
Problem
After a couple of small programs in Python we are now asked to do some basic stuff in Java. I don't really know where to start to improve this and whether it's even necessary.
This is an exercise, so the resulting message for when the denominator is zero should not be taken too seriously.
This is an exercise, so the resulting message for when the denominator is zero should not be taken too seriously.
import java.io.*;
/**
* Class: GetRationalNumber
* Takes two integers (numerator and denominator) and
* return them as a decimal fraction.
*/
class GetRationalNumber {
public static void main(String[] args) throws IOException {
int p, q;
BufferedReader userInput = new BufferedReader(
new InputStreamReader(System.in)
);
System.out.println("Numerator: ");
p = Integer.parseInt(userInput.readLine());
System.out.println("Denominator: ");
q = Integer.parseInt(userInput.readLine());
if(q == 0) {
System.out.println(
"Divison by zero can cause serious trouble to the universe!"
);
System.exit(0);
}
System.out.println("Result:\n" + (double) p / (double) q);
}
}Solution
import java.io.*;It's often a bad practice to import everything in a package. this will clutter your IDE's Ctrl + Space functionality, and makes it harder for other people to see exactly which things you are using from a package.
/**
* Class: GetRationalNumberI like the Javadoc, but it's totally overkill to specify the class name in the javadoc. The class name is already displayed in the javadoc functionality itself, you don't need to duplicate that when you write javadoc.
int p, q;Make it a habit ASAP to avoid one-letter variable names like these.
numerator and denominator would be much better.Possible improvement: Catch
NumberFormatException (or use a regex in advance to make sure that it is a number, but that might a bit over your head right now).System.out.println(
"Divison by zero can cause serious trouble to the universe!"
);I would write this on one line:
System.out.println("Divison by zero can cause serious trouble to the universe!");System.exit(0);The zero here indicates the exit status, which can be used by scripts and other stuff. If a program terminates normally, that is exit status zero. Is a possible division by zero really a normal program termination? In my opinion it is not. I would do
System.exit(1); or System.exit(42); or something.Overall, there's nothing object-oriented about what you have done, but I guess you haven't gotten that far yet, huh? :) Your code is quite fine for being beginner code. Now take it to the next level :)
Code Snippets
import java.io.*;/**
* Class: GetRationalNumberSystem.out.println(
"Divison by zero can cause serious trouble to the universe!"
);System.out.println("Divison by zero can cause serious trouble to the universe!");System.exit(0);Context
StackExchange Code Review Q#70725, answer score: 14
Revisions (0)
No revisions yet.