In this assignment, you will write a Java program that gets a list of ten (Fahre
ID: 3710034 • Letter: I
Question
In this assignment, you will write a Java program that gets a list of ten (Fahrenheit) temperatures from the user and then classifies them in terms of whether water would freeze/boil/stay a liquid. Recall that in Fahrenheit, water freezes at temperatures 32 and below; water boils at temperatures 212 and higher; and water remains a liquid at temperatures in-between those boundaries.
Requirements
This program should contain a single class (called Proj3) with a main method. Your program should compile and run in BlueJ.
Here are additional tips/requirements for your program:
You must store your temperatures in a size-10 array of ints
Use one for-loop to get the temperatures and store them in the array. Then use a second for-loop to find all temperatures at which water would freeze, a third for-loop to find all temperatures at which water would boil, and a fourth for-loop to find all temperatures at which water would remain a liquid.
You may assume that the user will enter whole numbers for each temperature (which may or may not be negative)
You can use System.out.println(); (with nothing int he parentheses) to print a blank line
The user might not enter any temperatures where water would freeze (or boil, or remain a liquid). You don't need to handle that differently -- your program just won't print any numbers under that heading.
Your program should look exactly like the example above when it runs, except for the values of the inputs/outputs.
Explanation / Answer
The required code is written below. This will fetch 10 temperature values (intgers) from user , find the temperatures at which water would freeze, boil or remain in liquid forms using 4 for loops as specified. Everything is explained in comments.Thank you.
// Proj3.java
import java.util.Scanner;
public class Proj3 {
// constants for storing freezing and boiling temperature values
static final int FREEZING_POINT = 32;
static final int BOILING_POINT = 212;
public static void main(String[] args) {
/**
* Defining a Scanner to read user input
*/
Scanner scanner = new Scanner(System.in);
/**
* Initializing an array of 10 ints for storing temperatures
*/
int temperatures[] = new int[10];
/**
* a loop for inputting the values
*/
for (int i = 0; i < temperatures.length; i++) {
System.out.printf("Enter temperature #%d: ", (i + 1));
temperatures[i] = scanner.nextInt();
}
/**
* a loop for finding the freezing temperatures
*/
System.out.println("Temperatures at which water would freeze:");
for (int i = 0; i < temperatures.length; i++) {
if (temperatures[i] <= FREEZING_POINT) {
System.out.println(temperatures[i]);
}
}
System.out.println("Temperatures at which water would boil:");
/**
* a loop for finding the boiling temperatures
*/
for (int i = 0; i < temperatures.length; i++) {
if (temperatures[i] >= BOILING_POINT) {
System.out.println(temperatures[i]);
}
}
System.out
.println("Temperatures at which water would remain in liquid form:");
/**
* a loop for finding the temperatures at which water would remain in
* liquid form
*/
for (int i = 0; i < temperatures.length; i++) {
if (temperatures[i] > FREEZING_POINT
&& temperatures[i] < BOILING_POINT) {
System.out.println(temperatures[i]);
}
}
}
}
/*OUTPUT*/
Enter temperature #1: 20
Enter temperature #2: 40
Enter temperature #3: 46
Enter temperature #4: 200
Enter temperature #5: 250
Enter temperature #6: 320
Enter temperature #7: -74
Enter temperature #8: 32
Enter temperature #9: 33
Enter temperature #10: 229
Temperatures at which water would freeze:
20
-74
32
Temperatures at which water would boil:
250
320
229
Temperatures at which water would remain in liquid form:
40
46
200
33