public class Main public enum StateOfWater SOLIDLIQUID GAS

public class Main {

public enum StateOfWater {SOLID,LIQUID, GAS}

   public static void main(String[] args) {

       // TODO Auto-generated method stub

       StateOfWater result;      

       Scanner in = new Scanner(System.in);

       System.out.print(\"Enter a temperature: \");      

       String temprature = in.nextLine();      

       char letter = temprature.charAt(temprature.length()-1);

       double temp = Integer.parseInt(temprature.substring(0, temprature.length() -1 ));

       if (letter == \'C\')

       {

               if (temp <= 0){

          

               result = StateOfWater.SOLID;}

               else if (temp >= 100){

              result = StateOfWater.GAS;

               }

               else

               {result = StateOfWater.LIQUID;

              

               }

               System.out.print(\"Water state: \"+ result);

              

       }

       else if (letter == \'F\'){

           if ( temp<= 32){

               result = StateOfWater.SOLID;

              

           }

           else if (temp >= 212) {

               result = StateOfWater.GAS;

              

           }else {

               result = StateOfWater.LIQUID;

              

           }

           System.out.print(\"Water state: \"+ result);

       }          

   }

}

how to change this code by writing all your code in public static void main and public static String getWaterState(String temperature). You will want to get the user input inside of the main method and implement the algorithm inside of the getWaterState method that determines the matter state of the water based on the passed in temperature.

Solution

/**
* The Main that prompt user to enter the temeprtaure
* in either C or F along with temperature value
* and the prints the state of water at
* given temeprature.
* The output is modified so as to understand better.
* */
//Main.java
import java.util.Scanner;
public class Main {

   public enum StateOfWater {SOLID,LIQUID, GAS}
   public static void main(String[] args)
   {      
       StateOfWater result;      
       Scanner in = new Scanner(System.in);
       System.out.print(\"Enter a temperature: \");      
       String temperature = in.nextLine();
       result=getWaterState(temperature);
      
       System.out.println(\"At given temperature \"+temperature+
               \" the state of water is \"+result);
   }

   /**
   * The static method getWaterState that takes a string temperature
   * and returns the enum constant value of given temeprature
   * */
   public static StateOfWater getWaterState(String temperature)
   {
       char letter = temperature.charAt(temperature.length()-1);

       double temp = Integer.parseInt(temperature.substring(0, temperature.length() -1 ));
       StateOfWater result = StateOfWater.LIQUID;
       if (letter == \'C\')
       {
           if (temp <= 0)
           {
               result = StateOfWater.SOLID;}
           else if (temp >= 100)
           {
               result = StateOfWater.GAS;
           }
           else
           {
               result = StateOfWater.LIQUID;
           }      
       }
       else if (letter == \'F\')
       {
           if ( temp<= 32){
               result = StateOfWater.SOLID;
           }
           else if (temp >= 212)
           {
               result = StateOfWater.GAS;
           }
           else
           {
               result = StateOfWater.LIQUID;
           }
       }
       return result;          
   }
}//end of class

Sample Output:

Enter a temperature: 212F
At given temperature 212F the state of water is GAS

Enter a temperature: 100C
At given temperature 100C the state of water is GAS

public class Main { public enum StateOfWater {SOLID,LIQUID, GAS} public static void main(String[] args) { // TODO Auto-generated method stub StateOfWater result
public class Main { public enum StateOfWater {SOLID,LIQUID, GAS} public static void main(String[] args) { // TODO Auto-generated method stub StateOfWater result
public class Main { public enum StateOfWater {SOLID,LIQUID, GAS} public static void main(String[] args) { // TODO Auto-generated method stub StateOfWater result

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site