Question

In: Computer Science

Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...

Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below.

if (selection == 10)

System.out.println("You selected 10.");

else if (selection == 20)

System.out.println("You selected 20.");

else if (selection == 30)

System.out.println("You selected 30.");

else if (selection == 40)

System.out.println("You selected 40.");

else System.out.println("Not good with numbers, eh?");

2.

Write a Constructor for the TrafficLight class that sets stopLight value to “red”, waitLight to “yellow” and goLight to “green”?

Solutions

Expert Solution

Hey There,

- I understood your question and I have given the answer to the best of my knowledge.

- I have also put comments in the code so that you can also understand what I did in the code.

- If you need further explanations then do let me know in the comments box I will be more than happy to help you.

Answer:

Question 1:

- Below I have provided the switch case implementation of the given if else-if code.

- While using the switch case we use break after all the cases so that once the switch case matches it does not go to the next case.

- Switch Implementation code:

switch(selection){
    case 10: System.out.println("You selected 10.");
    break;
    case 20: System.out.println("You selected 20.");
    break;
    case 30: System.out.println("You selected 30.");
    break;
    case 40: System.out.println("You selected 40.");
    break;
    default: System.out.println("Not good with numbers, eh?");
}

Question 2:

- Constructor is a block of codes similar to the method. It is called when an instance of the class is created. It is mainly used to initialize the object.

- The name of constructor is same as the class name.

- Below I have provided the given constructor code as asked in the question:

// Declaring variables
String stopLight;
String waitLight;
String goLight;

// Defining constructor
TrafficLight(){
    stopLight = "red";
    waitLight = "yellow";
    goLight = "green";
}

- Below here I have also provided the whole code so that you can understand well.

- Here I have also used the display method that will print the values of stopLight, waitLight and goLight. By this we can confirm that our code is working correctly.

public class TrafficLight
{
    // Declaring variables
    String stopLight;
    String waitLight;
    String goLight;
    
    // Defining constructor
    TrafficLight(){
        stopLight = "red";
        waitLight = "yellow";
        goLight = "green";
    }
    
    // This function will print values of stopLight, waitLight and goLight
    void display(){
        System.out.println(stopLight);
                System.out.println(waitLight);
                System.out.println(goLight);
    }
    // main method
        public static void main(String[] args) {
            // Creating object of TrafficLight class
                TrafficLight t1 = new TrafficLight();
                
                // Calling display method
                t1.display();
        }
}

Screenshot of it working correctly:

Hope it helps:)


Related Solutions

Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
In Java What is the output produced by the following code? char letter = 'B'; switch...
In Java What is the output produced by the following code? char letter = 'B'; switch (letter) { case'A': case'a': System.out.println("Some kind of A."); case'B': case'b': System.out.println("Some kind of B."); break; default: System.out.println("Something else."); break; }
With C code Write a switch statement (not a complete program) which prints an appropriate message...
With C code Write a switch statement (not a complete program) which prints an appropriate message for a letter code entered. Use the following messages: If L is entered, output the message "Lakers" If C is entered, output the message "Clippers" If W is entered, output the message "Warriors" If any other character is entered, output the message "invalid code" Make sure to handle the case where the user enters in a small letter. That is, a capital or small...
a) i) The following VHDL code contains erroneous syntax. Re write the code in its corrected...
a) i) The following VHDL code contains erroneous syntax. Re write the code in its corrected format onto your answer sheet. You may assume that din is a 16-bit vector and that the ld, lr and cl inputs are 1-bit wide. lp: process(clk) signal reg : std_logic_vector(15 downt begin if cl=’1’ then reg := (others:=’0’); else if clk=’1’ and clkevent then if ld=’1’ reg <= din; end if; if lr=’1’ then reg := reg(14 downto 0) & “0 else reg...
Write the program that prints the product of 79 and 3.684. The output statement should be...
Write the program that prints the product of 79 and 3.684. The output statement should be descriptive, and should include the printing of all three variables (the two operands as well as the product). Write In C++
Use R to complete the following questions. You should include your R code, output and plots...
Use R to complete the following questions. You should include your R code, output and plots in your answer. 1. Two methods of generating a standard normal random variable are: a. Take the sum of 5 uniform (0,1) random numbers and scale to have mean 0 and standard deviation 1. (Use the properties of the uniform distribution to determine the required transformation). b. Generate a standard uniform and then apply inverse cdf function to obtain a normal random variate (Hint:...
Use R to complete the following questions. You should include your R code, output and plots...
Use R to complete the following questions. You should include your R code, output and plots in your answer. 1. Two methods of generating a standard normal random variable are: a. Take the sum of 5 uniform (0,1) random numbers and scale to have mean 0 and standard deviation 1. (Use the properties of the uniform distribution to determine the required transformation). b. Generate a standard uniform and then apply inverse cdf function to obtain a normal random variate (Hint:...
Analyze the following Verilog code and write down its output as pictured in the code. module...
Analyze the following Verilog code and write down its output as pictured in the code. module blocking; reg [0:7] A, B; initial begin: init1 A = last decimal digit of your ID; #1 A = A + 1; // blocking procedural assignment B = A + 1; $display("Output 1: A= %b B= %b", A, B ); A = last decimal digit of your ID; #1 A <= A + 1; B <= A + 1; #1 $display ("Output 2: A=...
1. Explain why the following statement is true: "All else the same, firms with relatively stable...
1. Explain why the following statement is true: "All else the same, firms with relatively stable sales are able to carry relatively high debt/asset ratios." and the explain how a firm might shift its capital structure so as to change its weighted average cost of capital (WACC). What would be the impact on the value of the firm?
Write an if-else statement for the following: If numDifference is less than -20, execute totalDifference =...
Write an if-else statement for the following: If numDifference is less than -20, execute totalDifference = -25. Else, execute totalDifference = numDifference. import java.util.Scanner; public class NumberDifference {    public static void main (String [] args) {       int totalDifference;       int numDifference;       Scanner scnr = new Scanner(System.in);       numDifference = scnr.nextInt(); // Program will be tested with values: -19, -20, -21, -22. //solution goes here//       System.out.println(totalDifference);    } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT