cresjoy cresjoy - 4 months ago 7
Java Question

If statements in the methods or in the main?

Hi Currently I am on chapter 5 in my book and we studied

if
statements, and
.equals
, and some other basic things. I noticed that in all of my problems
and in the examples in the book we usually tend to put
if
statements inside those methods within a class. Here is an exercise I did which isn't completely
finished but the picture is clear. I just ask the user to convert between some units. However when I was thinking on how to approach this problem, I didn't want to use if statements in my methods since I wanted the if statements to be about user input, and from my knowledge we should always put the
Scanner
in the main(well this is what we have been doing all the time so far). And because I wouldn't be able to access those scanners from the main I used
if
statements in the main.

Is this bad practise, or is it generally just better to do whatever given the situation? I can see the main getting ugly, in large programs if we put too many
things in there. I only coded one test case below, but you can see how ugly it would get if I did the other combinations.

Heres my code

import java.util.Scanner;
class Conversion{
public double mmToCm(double value){
value = value / 10.0;
return value;
}
public double cmToMm(double value){
value = value * 10.0;
return value;
}
public double cmToMeter(double value){
value = value / 100.0;
return value;
}
public double meterToCm(double value){
value = value * 100.0;
return value;
}
public double mmToMeter(double value){
value = value / 1000.0;
return value;
}
public double meterToMM(double value){
value = value * (1000.0);
return value;
}
}


public class test{
public static void main(String [] args){
Conversion convertThisVal = new Conversion();
Scanner typeOne = new Scanner(System.in);
Scanner typeTwo = new Scanner(System.in);
Scanner valueOne = new Scanner(System.in);
System.out.println("Convert From: ");
String newTypeOne = typeOne.next();
System.out.println("To: ");
String newTypeTwo = typeTwo.next();
System.out.println("Enter the values!");
double newValueOne = valueOne.nextDouble();

if (newTypeOne.equalsIgnoreCase("mm") && newTypeTwo.equalsIgnoreCase("cm"))
{
System.out.println("cm:" + convertThisVal.mmToCm(newValueOne));
}
}
}

Answer

to create a good structured application which is build using separated components and by separated i mean logically not physically, you need to think of each component as an independent party, which means, the conversion logic should be separated from the input logic (by input i mean getting input data)

let's talk about input data a little bit, it could be from Scanner as in your case, and it could be from a database, and it could be from a web-request parameters. that lead us to think about conversion logic as separated unit, that takes some parameters (IN) regardless of the source of these parameters, and based on specific logic (some if statements, some other logic) it returns a specific output that will be used later in the process

your conversion class may contain a method that will decide which other conversion method to call based on input parameters.

ex:add this method to your Conversion class

public double convert(String newTypeOne, String newTypeTwo, double newValueOne){
    if (newTypeOne.equalsIgnoreCase("mm") && newTypeTwo.equalsIgnoreCase("cm")){
        return mmToCm(newValueOne);
    }else if (...){
        return ...;
    }
    :
    : ... and so on
}

as you can see, we made this method to decide which conversion to do based on parameters, which originally obtained using Scanner from user.

so your main will look like this:

public static void main(String [] args){
    Conversion convertThisVal = new Conversion();
    Scanner typeOne = new Scanner(System.in);
    Scanner typeTwo = new Scanner(System.in);
    Scanner valueOne = new Scanner(System.in);
    System.out.println("Convert From: ");
    String newTypeOne = typeOne.next();
    System.out.println("To: ");
    String newTypeTwo = typeTwo.next();
    System.out.println("Enter the values!");
    double newValueOne = valueOne.nextDouble();

    System.out.println("cm:" + convertThisVal.convert(newTypeOne,newTypeTwo, newValueOne ));
}

all new conversion types will be added in convert() keeping the caller method unaware or involved in any logic needed to convert the values.

just pass whatever parameters you need to the destination method and let it decide what to do.

this way you will have a conversion component that can be used in any other system, as i told you earlier, (for database, a webservice, ...etc)

hope this helps you.

Comments