Assignment Requirements 1. Define six variables of appropriate data type and assign them the following values: 35.5,30.5
Posted: Thu Jul 14, 2022 2:13 pm
Source code:
class temperature{public static void main(String args[]) //main method{Scanner sc=new Scanner(System.in); //creating an object forscannerdouble temp1 = 35.5;double temp2 = 30.5;double temp3 = 22.2;double temp4 = 16.1;double temp5 = 7.3;double temp6 = -1;
int tempF = 0;String advisory = " ";System.out.println( " Assignment 7 - Functions and TypeConversion\n\n");
tempF = fahrenheit(temp1);advisory = weatherStatement(tempF);System.out print("The temperature is " + temp1 + "C or " +tempF + " F. " + advisory + "\n" ;
tempF = fahrenheit(temp2);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp2 + "C or " + tempF +"F." + advisory + "\n");
tempF = fahrenheit(temp3);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp3 + "C or " + tempF +"F." + advisory + "\n");
tempF = fahrenheit(temp4);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp4 + "C or " + tempF +"F." + advisory + "\n");
tempF = fahrenheit(temp5);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp5 + "C or " + tempF +"F." + advisory + "\n");
tempF = fahrenheit(temp6);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp6 + "C or " + tempF +"F." + advisory + "\n");
}public static int fahrenheit(double myTemp){ int intTempF = (int)Math.round(myTemp * 9.0 /5.0 + 32); return intTempF;}public static String weatherStatement(int tempF) { if (tempF >= 95) { return "A heat advisory hasbeen issued."; } else if (tempF >= 85) { return "Pleasant butwarm."; } else if (tempF >= 70) { return "Very pleasant weathertoday."; } else if (tempF >= 50) { return "Pleasant butcool."; } else if (temp >= 33) { return "Cold weather."; } else { return "A freeze warning hasbeen issued."; }}}
Assignment Requirements 1. Define six variables of appropriate data type and assign them the following values: 35.5,30.5,22.2,16.1,7.3,−1 (notice this last v is negative one) 2. Write a function that will accept a decimal value representing the Celsius temperature, convert the value to Fahrenheit, and retum Fahrenheit value as a rounded integer value. a. Hint 1: The formula to convert from C to F is, TempF=TempC∗9/5+32 b. Hint 2: An easy way to round infeger values is to add 5 to the decimal value before converting it to integer. 3. Write a function that will accept an integer value representing the temperature in Fahrenheit. The function will check the value and determine the correct weather statement to return as a string value. The criteria for these statements are as follows:
class temperature{public static void main(String args[]) //main method{Scanner sc=new Scanner(System.in); //creating an object forscannerdouble temp1 = 35.5;double temp2 = 30.5;double temp3 = 22.2;double temp4 = 16.1;double temp5 = 7.3;double temp6 = -1;
int tempF = 0;String advisory = " ";System.out.println( " Assignment 7 - Functions and TypeConversion\n\n");
tempF = fahrenheit(temp1);advisory = weatherStatement(tempF);System.out print("The temperature is " + temp1 + "C or " +tempF + " F. " + advisory + "\n" ;
tempF = fahrenheit(temp2);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp2 + "C or " + tempF +"F." + advisory + "\n");
tempF = fahrenheit(temp3);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp3 + "C or " + tempF +"F." + advisory + "\n");
tempF = fahrenheit(temp4);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp4 + "C or " + tempF +"F." + advisory + "\n");
tempF = fahrenheit(temp5);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp5 + "C or " + tempF +"F." + advisory + "\n");
tempF = fahrenheit(temp6);advisory = weatherStatement(tempF);System.out.print("The temperature is " + temp6 + "C or " + tempF +"F." + advisory + "\n");
}public static int fahrenheit(double myTemp){ int intTempF = (int)Math.round(myTemp * 9.0 /5.0 + 32); return intTempF;}public static String weatherStatement(int tempF) { if (tempF >= 95) { return "A heat advisory hasbeen issued."; } else if (tempF >= 85) { return "Pleasant butwarm."; } else if (tempF >= 70) { return "Very pleasant weathertoday."; } else if (tempF >= 50) { return "Pleasant butcool."; } else if (temp >= 33) { return "Cold weather."; } else { return "A freeze warning hasbeen issued."; }}}
Assignment Requirements 1. Define six variables of appropriate data type and assign them the following values: 35.5,30.5,22.2,16.1,7.3,−1 (notice this last v is negative one) 2. Write a function that will accept a decimal value representing the Celsius temperature, convert the value to Fahrenheit, and retum Fahrenheit value as a rounded integer value. a. Hint 1: The formula to convert from C to F is, TempF=TempC∗9/5+32 b. Hint 2: An easy way to round infeger values is to add 5 to the decimal value before converting it to integer. 3. Write a function that will accept an integer value representing the temperature in Fahrenheit. The function will check the value and determine the correct weather statement to return as a string value. The criteria for these statements are as follows: