Java Programming Write code to evaluate the physical state of multiple types of
ID: 3742694 • Letter: J
Question
Java Programming
Write code to evaluate the physical state of multiple types of matter at a given temperature (and sea level pressure). You will need to replace line 3 in this repl with your actual code.
Your program should
prompt the user with this string: "Enter a type of matter (water, hydrogen, mercury):"
read a String type of matter from System.in
prompt the user with this string: "Enter a decimal number as temperature (degrees C):"
read a decimal temperature from System.in
depending on the inputs, output a String describing the state of matter at that temperature using the following rules
For water:
Temp is 0 or below --> "solid"
Temp is between 0 and 100 (but not equal) --> "liquid"
Temp is 100 or above --> "gas"
For hydrogen:
Temp is 259.16°C or below --> "solid"
Temp is between 259.16°C and 252.879°C (but not equal) --> "liquid"
Temp is 252.879°C or above --> "gas"
For mercury:
Temp is 38.829°C or below --> "solid"
Temp is between 38.829°C and 356.619°C (but not equal) --> "liquid"
Temp is 356.619°C or above --> "gas"
For any other types of matter:
Output "I don't know about that material." with newline.
Explanation / Answer
import java.io.*;
import java.util.*;
public class Solution {
static String temperature(String str, float num) {
String res;
switch(str){
case "water": if(num <= 0)
res = "solid";
else if (num<100)
res = "liquid";
else
res = "gas";
break;
case "hydrogen": if(num <= -259.16)
res = "solid";
else if (num < -252.879)
res = "liquid";
else
res = "gas";
break;
case "mercury": if(num <= -38.829)
res = "solid";
else if (num < 356.619 )
res = "liquid";
else
res = "gas";
break;
default :
res = "I don't know about that material";
};
return res;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str, result;
float num;
System.out.println("Enter a type of matter (water, hydrogen, mercury):");
str = in.next();
System.out.println("Enter a decimal number as temperature (degrees C):");
num = in.nextFloat();
result = temperature(str, num);
System.out.println(result);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.