CMSC 131 quiz 2(Accurately solved)
What are the three logical operators? correct answers &&, ||, ! Write "truth-tables" for && and ||. correct answers A B A && B A || B -------------------------------------------------- T T T T T F F T F T F T F F F F Is the following boolean expression true or false?((3 < 5) && !(1 > 14) && (-5 < -15)) || ((6 == 6) && !(2 == 2)) correct answers False If s1 and s2 are variables representing Strings, what Java boolean expression is equivalent to "s1 is not the same as s2"? correct answers !s(s2) What statement must be included at the top of a file in order to use the Scanner in the file? correct answers import .Scanner; Write a java class called "UserInput". In the main method, declare three variables: an int, a float, and a String. Name the variables "age", "weight", and "name". Create a variable called "scan" of type Scanner, and set it equal to a new Scanner. (Use the syntax shown in class). Prompt the user to enter his/her age, weight, and name, then read these entries in using the scanner and set the variables accordingly. Then print the values of the three variables with appropriate labels. For example: "Name: Frank Age: 17 weight: 151.4". correct answers import .Scanner; public class UserInput { public static void main(String[] args) { int age; float weight; String name; Scanner scan = new Scanner(S); S("Enter your age: "); age = Int(); S("Enter your weight: "); Weight = Float(); S("Enter your name: "); name = (); Sln("Name: " + name + ", age: " + age + ", weight: " + weight); } } Write a java class called "FahrenheitToCelcius". The main method will ask the user to enter a temperature in Fahrenheit. (Use a variable of type "double" to store the value.) Then calculate the equivalent temperature in Celcius, and print out a message telling the user what you found. [Recall: C = (5/9)(F-32).] Hint: Be careful about doing arithmetic with integers! Check your program by entering 212 degrees. The output should be 100. correct answers import .Scanner; public class FahrenheitToCelcius { public static void main(String[] args) { Scanner scanner = new Scanner(S); Sln("Enter temp (F): "); double fahrenheit = Double(); double celcius = 5.0/9.0 * (fahrenheit - 32); Sln("That is " + celcius + " degrees C."); } } Modify the "FahrenheitToCelcius" question in the previous question so that the user can either go from F to C or vice versa. correct answers import .Scanner; public class FahrenheitToCelcius { public static void main(String[] args) { Scanner scanner = new Scanner(S); S("Enter 1 to go from F to C; 2 for C to F: "); int response = Int(); if (response == 1) { S("Enter temp (F): "); double fahrenheit = Double(); double celcius = 5.0/9.0 * (fahrenheit - 32); Sln("That is " + celcius + " degrees C."); } else { S("Enter
Written for
- Institution
- CMSC 131
- Course
- CMSC 131
Document information
- Uploaded on
- May 28, 2024
- Number of pages
- 4
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
-
what are the three logical operators
Also available in package deal