Switch and methods

1k Views Asked by At

I'm trying to do a temperature converter using different methods. Now, I'm a bit stuck at the moment. I'm creating a switch within a method, which has a char, a double and another char as parameters and those 3 represent the temp converting from, the actual temperature itself and the temperature the user want to convert to. I've been trying to create this switch but I haven't got any luck so far. Maybe I'm not making myself clear enough. Sorry!! I think it's obvious to say I'm a beginner and this is homework. I'm not asking to get the whole code, just a couple of hints to get back on track again. Thanks a lot! :-)

This is the method where i want to to the switch in, but the I need info from another method which I'll post below this one

public static double convertTemp( char uFrom, double temp, char uTo ){          

}

This method will do the actual conversion from celsius to kelvin or fahrenheit.

public static double convFromCelsius( double value, char unitTo ){


}

Thanks a lot again!

Here's what I got so far.

import java.util.Scanner;

class mainAssignment{

// Main Method
public static void main( String[] args ){

    char   scaleFrom = ' '; // From which temperature scale to convert from
    char   scaleTo   = ' '; // To which temperature scale to convert to 

    double tempFrom  = 0.0; // Temperature value to be converted
    double tempTo    = 0.0; // Temperature value converted 
    double result    = 0.0; // Result of the conversion 

    // Loop to repeat the menu until option chosen is "x"
    //  do {

        /* 
            Method to display the menu and store the scale from 
                which the temperature will be converted from
        */
        scaleFrom = displayMenu(scaleFrom);

        /* 
            Only asks user to input more information, 
            if scaleFrom is different than "x" ( x = Exit )
        */
        //if ( scaleFrom != 'x' ){

            /* 
                Method to get the temperature value to be 
                    converted and store the value entered by user
            */
            tempFrom = getTemp(tempFrom);

            /* 
                Method to get the scale to which the 
                    temperature value will be converted to
            */
            scaleTo = getUnitTo(scaleTo);

            // Method to convert the Temperature
            //result = convertTemp( scaleFrom, tempFrom, scaleTo );

            // Method to display the conversion to the screen
            //displayResult( scaleFrom, tempFrom, scaleTo, result );

        //}
    //} while ( scaleFrom != 'x' );
}

// Method to invoke the conversion of the temperature
public static double convertTemp( char uFrom, double temp, char uTo ){







}

//  Method to convert temperatures in Celsius to the other ones
public static double convFromCelsius( double value, char unitTo ){


}




//  Method to convert temperatures in Fahrenheit to the other ones
//public static double convFromFahrenheit( double value, char unitTo ){

    // body of the Method

//}return;




//  Method to convert temperatures in Kelvin to the other ones
//public static double convFromKelvin( double value, char unitTo ){

    // body of the Method

//}return;

public static char displayMenu (char scaleFrom){

    Scanner ui = new Scanner (System.in);

    System.out.println ("");
    System.out.println ("============================");
    System.out.println ("   Temperature Conversion");
    System.out.println ("=========== MENU ===========");
    System.out.println ("");
    System.out.println ("a. From Celsius");
    System.out.println ("b. From Fahrenheit");
    System.out.println ("c. From Kelvin");
    System.out.println ("");
    System.out.println ("x. Exit");
    System.out.println ("");
    System.out.println ("============================");
    System.out.println ("Enter an option: ");
    System.out.println ("");

    scaleFrom = ui.nextLine().charAt(0);
    return scaleFrom;
}

public static double getTemp (double getTemp){

    Scanner ui = new Scanner (System.in);

    System.out.println ("");
    System.out.println ("Please, enter the temperature you want to convert: ");
    System.out.println ("");

    getTemp = Double.parseDouble(ui.nextLine());    

    return getTemp;
}

public static char getUnitTo (char scaleTo){

    Scanner ui = new Scanner (System.in);

    System.out.println ("");
    System.out.println ("Please, choose the temperature you want to convert to:");
    System.out.println ("");
    System.out.println ("C = To Celsius  K = To Kelvin  F = To Fahrenheit");
    System.out.println ("");

    scaleTo = ui.nextLine().charAt(0);
    return scaleTo;
}

}//End of the class Main Assignment//

1

There are 1 best solutions below

0
On

I think you want something like:

public static double convertTemp( char uFrom, double temp, char uTo ){          
    switch (uFrom) {
        case 'c':
            return convertFromCelsius(temp, uTo);
        case 'f':
            return convertFromFahrenheit(temp, uTo);
        case 'k':
            return convertFromKelvin(temp, uTo);
        default:
            System.out.println("Unexpected unit");
            return 0;
    }
}

public static double convFromCelsius( double value, char unitTo ){
    switch (unitTo) {
        case 'f':
            // Code to convert from c to f
            return value + 30;
        case 'k':
            // Code to convert from c to k
            return value + 273;
        default:
            System.out.println("Unexpcted unit");
            return 0;
    }
}