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; }
C++: Write correct C++ code for a nested if-else if-else structure that will output a message...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message based on the logic below. A buyer can pay immediately or be billed. If paid immediately, then display "a 5% discount is applied." If billed, then display "a 2% discount is applied" if it is paid in 30 days. If between 30 and 60 days, display "there is no discount." If over 60 days, then display "a 3% surcharge is added to the bill."...
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...
It's Java; the code should be the same as the output sample below; please, thank you....
It's Java; the code should be the same as the output sample below; please, thank you. Note: create a single-file solution that has multiple class definitions. Also, note that the static main() method should be a member of the Vehicle class. Create a class called Vehicle that has the manufacturers name (type String), number of cylinders in the engine (type int), and owner (type Person given next). Then, create a class called Truck that is derived from Vehicle and has...
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:...
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. 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT