4. Run the following program with the three test values (20.05, 30.65, 40.50) Wh
ID: 3748081 • Letter: 4
Question
4. Run the following program with the three test values (20.05, 30.65, 40.50) Why do we need '(int)' in front of longSal in the switch statement? import java.util.scanne r; public class SwitchCastTest [ public static void main (String[] args) i // Create a Scanner named input Scanner input - new Scanner (System.in); // Create a double that will store the input double salary0.00; // Read in salary, use these test values in successive runs: 20.05, 30.65, 40.50 salary - input.nextDouble () long longSal - Math.round (salary); system. out . println("salary = " + salary) ; System. out.println ("longSal - " + longSal); // Use the switch statement to print appropriate message for the salary entered switch ((int)longSal) case 20: System.out.println ("Salary is about $20/hour."); case 30: System.out.println ("Salary is about $30/hour."); case 31: System.out.println ("Salary is about $31/hour."); case 40: System.out.println ("Salary is about $40/hour. ") case 41: System.out.println ("Salary is about $41/hour. "); default: System. out.println ("Salary is out of range."); break; break; break; break; break; break; input.close // Close the scanner objectExplanation / Answer
switch only accepts char, byte, short, int, String or an enum. switch does not accept a long. so, we can cast/convert it to an int so that switch statement accepts it. That's why you see a (int) front of longSal.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.