Generally, this looks pretty good. There are a few points to make, but it's worth first saying that because this is such a simple problem and solution, some of these may seem a little pointless. I'll try to flag up when this is: even if they're pointless in this extremely simple case, things don't need to get much more complex before all of these are very important!
public class Method1
This is a bad name. A class isn't the same thing as a method, and also this name has no useful information.
public static float a, b, t;
What's t
? I'm guessing this was just a mistake, but don't leave variables lying around that aren't being used.
It's worth noting that generally, you should give variables more descriptive names than this, but in purely mathematical contexts like this, sometimes a
and b
really are as descriptive as you can be about the role of a variable!
public static float a, b, t;
public static Scanner scanner;
Given that this program has a single class, and in fact a single method, it doesn't really matter where you declare things. However, in general if you're only using a variable inside a method, you should declare it in that method rather than at the class level like you have.
Even if you were using it throughout the class, you should still keep it private, not public, unless you explicitly want to expose it to other classes.
if (a < b){
This isn't needed, since you've already checked if it's greater than or equal, anything that's left over must be less! You can use an if{...}else if{...}else{...}
here. Note that this one is more a matter of preference- arguably your version is more clearly readable than using else
s would be. Either way though, it's important to understand both options.
Finally a higher-level point. This example mixes together what I'd call presentation concerns and business logic.
Usually those terms wouldn't be applied to such a small, straightforward application, but essentially, presentation is how you display and get input from the user, and business logic is the actual logical work that your application is doing.
Since your entire application is so simple, this probably doesn't matter much, but it's a good idea to start early with thinking in terms of how you're going to separate out responsibilities. A single method shouldn't be taking responsibility both for presentation concerns (deciding how to ask for the numbers and print the response) and business logic (in this case, doing the number comparison). Refactoring these into two different methods your class might look like:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Type your first Value please.");
float a = scanner.nextFloat();
System.out.println("Type your second Value please.");
float b = scanner.nextFloat();
string result = compare(a,b);
System.out.println(result)
}
private static string compare(float a, float b) {
if (a == b) {
return "=";
}
if (a > b){
return ">";
}
if (a < b){
return "<";
}
}
Notice how now, if you do want to modify your program to be a valid submission to CodeChef, you don't need to modify compare
. It's only the input/output (presentation) that doesn't fit with CodeChef, the actual logical work your program is doing (the body of compare
) is completely independent. You could even build a GUI over it and you still wouldn't need to change the contents of that method.
That may not seem important since compare
is 9 lines of code, but imagine your program was doing something much more complex which requires 90 lines, or 900. Not having to modify every little bit of it just to do IO a little differently would be vital!