This is a question about Java The project name of this exerc
This is a question about Java.
The project name of this exercise is WaterState Problem Description
The purpose of this assignment is for you to write all of your own comments and code. You will also get some practice using enumeration.
The detailed description of this problem comes from the Programming Exercise P3.9 that is in the book (page 126).
\"Write a program that reads a temperature value and the letter C for Celsius or F for Fahrenheit. Print whether water is liquid, solid, or gaseous at the given temperature at sea level.\"
You are to write a program that will take a temperature and units(Celcius or Farenheit). This temperature and units has the temperature as a double followed by either a \'C\' or an \'F\' for Celcius and Farenheit, respectively. Notice, that there is no space between the temperature and the units. Your program will then output one of three matter states, SOLID, LIQUID or GAS.
Additionally, you should represent the state using an enumeration that contains the matter states SOLID, LIQUID and GAS. See special topic 5.4 (page 206) in the book.
Hints:
You can read in the entire input using Scanner.next().
You can use the length() method of the String object to figure out how many characters are in your input.
You can use the charAt() method of the String object to extract a character from your string.
You can print enumerations directly. For example, System.out.println(MatterState.SOLID) prints out SOLID.
This problem should be solved 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.
Using the test input, your output should look like:
Enter a temperature: -10F Water state: SOLID
Solution
import java.util.Scanner;
public class Temparature {
enum MatterState{
SOLID, LIQUID , GAS;
}
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);// initialing the Scanner class to read the input from the user
System.out.print(\"Enter a temperature:\");
String temperature= scan.next();// getting the temparature from the user
getWaterState(temperature);// invoking the method getWaterState with argument temperature.
}
private static void getWaterState(String temperature) {
int length=temperature.length();// getting the length of the temperature
char tempFormat=temperature.charAt(length-1);// getting the temp foramt i.e Celsius or Fahrenheit
int temp=Integer.parseInt(temperature.substring(0,length-1));// geting the value
switch (tempFormat) {// switch case to handle the variation
case \'C\':// case for Celsius
if(temp>100){// condition for GAS state
System.out.println(MatterState.GAS);
}else if(temp>0 && temp<=100){// condition for LIQUID state
System.out.println(MatterState.LIQUID);
}else{// condition for SOLID state
System.out.println(MatterState.SOLID);
}
break;
case \'F\':
if(temp>212){
System.out.println(MatterState.GAS);// condition for GAS state
}else if(temp>32 && temp<=212){
System.out.println(MatterState.LIQUID);// condition for LIQUID state
}else{
System.out.println(MatterState.SOLID);// condition for SOLID state
}
break;
default:
System.out.println(\"Invalid Temperature Entered\");// invalid use case
break;
}
}
}
-------------------output----------------
Enter a temperature:-10F
SOLID
Enter a temperature:120C
GAS
-------------------output----------------

