Question

In: Computer Science

A) Is it important to have a break after every case in the switch statement?) b)...

  1. A) Is it important to have a break after every case in the switch statement?)

b) Explain lain your answer by writing an example of a switch statement and showing the effect of including and excluding the break.

                                                                                                                                                              10 points

  1. A) String Name = KB.NextLine();

      Write a java statement to convert Name to upper case.

B) char Letter =KB.next().charAt(0);

          Write a java statement to convert Letter to lower case.

                                                                                                                                                       5 points

c) Write a while loop which allows the user to enter numbers between 1 and 15, the loops keep iterating as long as the total of the entered numbers is less than 1000.

                                                                                                                                                      15 points  

                                                                                                                                                                    

  1. Write an example of a trailing else, and explain the importance of using the trailing else statement.

5 points

  1. A) Write an example of an if-else and an example of a simple if statements.B) Write an example of nested if statement

15 points

THIS IS FOR JAVA PROGRAMMING. PLEASE ANSWER ASAP. THANK YOU

Solutions

Expert Solution

1.

It is important to have a break after every case in the switch statement so that there is no case of fall through i.e. if based on switch condition, a certain case is executed, if there is no break after it, the case below it will also get executed hence to avoid that, we use break.

import java.io.*;

class GFG {
        public static void main (String[] args) {
                int t = 3;
                switch(1){
                    case 1:
                        System.out.println("Case 1 is executed.");
                    case 2:
                        System.out.println("Case 2 is executed.");
                        break;
                    default:
                        System.out.println("End of switch.");
                        break;
                }
        }
}

As we see in the output, case 1 is executed based on switch condition but since it doesn't have a break, case 2 gets executed as well!

2.

A & B.

import java.util.*;
import java.io.*;

class GFG {
        public static void main (String[] args) {
                Scanner KB = new Scanner(System.in);
                // A
                String Name = KB.nextLine();
                Name = Name.toUpperCase();
                System.out.println(Name);
                
                // B
                char letter = KB.next().charAt(0);
                String l = ""+letter;
                l = l.toLowerCase();
                System.out.println(l);
        }
}

C.

import java.util.*;
import java.io.*;

class GFG {
        public static void main (String[] args) {
                int count = 0;
                int num;
                Scanner sc = new Scanner(System.in);
                while(count<1000){
                    num = sc.nextInt();
                    if(num>=1 && num<=15){
                        count++;
                        continue;
                    }else{
                        break;
                    }
                }
        }
}

3.

trailing else helps us to avoid repeating conditions for several cases like in the following example if we have to write a statement where if the user is less than 18, he can't vote, or else he can. The else part helps us to define all users above age of 18. We don't have to write new if statement for someone who is 19, 20, 21......so on

import java.util.*;
import java.io.*;

class GFG {
        public static void main (String[] args) {
                int age;
                Scanner sc = new Scanner(System.in);
                int num = sc.nextInt();
                if(num<18){
                    System.out.println("Not eligible for voting.");
                }else{
                    System.out.println("Eligible for voting.");
                }
        }
}

4.

If_else:

import java.util.*;
import java.io.*;

class GFG {
        public static void main (String[] args) {
                int age;
                Scanner sc = new Scanner(System.in);
                int num = sc.nextInt();
                if(num<18){
                    System.out.println("Not eligible for voting.");
                }else{
                    System.out.println("Eligible for voting.");
                }
        }
}

Simple If:

This code finds out whether entered number is an even number.

import java.util.*;
import java.io.*;

class GFG {
        public static void main (String[] args) {
                int age;
                Scanner sc = new Scanner(System.in);
                int num = sc.nextInt();
                if(num%2==0)
                    System.out.println("Number is even.")
        }
}

Nested_If:

This program simulates the divisibility test for 6 i.e. a number is divisible by 6 if it is divisible by both 2 and 3 as well.

import java.util.*;
import java.io.*;

class GFG {
        public static void main (String[] args) {
                int age;
                Scanner sc = new Scanner(System.in);
                int num = sc.nextInt();
                if(num%2==0){
                    if(num%3==0){
                        System.out.println("The number is divisble by 6.")
                    }
                }
                    
        }
}

(Feel free to give an upvote or comment below for any doubts)


Related Solutions

If a switch statement contains no break statements at all, a. a syntax error will occur...
If a switch statement contains no break statements at all, a. a syntax error will occur b. each of the case clauses will be executed every time the switch statement is encountered c. this is equivalent to having the switch statement always take the default clause, if one is present d. this is not a syntax error but nothing within the switch statement will ever be executed e. None of these Which one of these options?
C++ Write a program using switch-case-break that will take an input number as for example 2...
C++ Write a program using switch-case-break that will take an input number as for example 2 and based on switch it will provide various powers of 2.
No Global variables No goto statement No break outside switch Write a menu driven C program...
No Global variables No goto statement No break outside switch Write a menu driven C program using functions and switch. Feel free to use “Empty Outlines” template from Canvas to design the functions as needed to build the code. Make sure to submit your work through Canvas. You can show me your code running in class when you are done. The program shows following menu to the user repetitively until user selects option 3 to exit. Circle Triangle Exit Based...
A Switch/case statement allows multiway branching based on the value of an integer variable. In the...
A Switch/case statement allows multiway branching based on the value of an integer variable. In the following example, the switch variable s is specified to be assumed to be one of three values ​​of [0, 2], and a different action for each case becomes: case 0: a = a - 1; break; case 1: a = a + 2; break; case 2: b = 3 * b; break; Shows how such statements can be compiled into MiniMIPS assembly instructions.
A switch/case statement allows multiway branching based on the value of an integer variable. In the...
A switch/case statement allows multiway branching based on the value of an integer variable. In the following example, the switch variables can assume one of the three values in [0, 2] and a different action is specified for each case. switch (s) {    case 0: a=a+1; break;    case 1: a=a-1; break;    case 2: b=2*b; break; } Show how such a statement can be compiled into MiniMIPS assembly instructions.
Q2. (a) WHAT IS BREAK - EVEN POINT? (b) EXPLAIN THE IMPORTANT MANAGERIAL USES OF BREAK-EVEN...
Q2. (a) WHAT IS BREAK - EVEN POINT? (b) EXPLAIN THE IMPORTANT MANAGERIAL USES OF BREAK-EVEN ANALYSIS.
Have a look at the code below. Write a switch statement to check for the mode...
Have a look at the code below. Write a switch statement to check for the mode type. When the designated mode is matched, display an appropriate string for that mode. The default case should display the statement, “We do not offer this mode of course delivery.” public class SwitchCourse {     enum CourseMode { ONLINE, RESIDENTIAL, HYBRID }     CourseMode myCourse = CourseMode.HYBRID;        public void myCourse()     {         switch (/*___*/)         {             case ONLINE:                 System.out.println("You...
Why won't the program run the code after the first switch statement. It shows Press c...
Why won't the program run the code after the first switch statement. It shows Press c to continue or q to quit, but does not give the user the option to fill it in. Please help to run the code fully. This program is to find the area of shapes the user decides and must give them an option to quit or restart the program. Please provide the explanation and code in JAVA here. Thank you. import java.util.Scanner; import java.lang.Math;...
I NEED THIS IN PSEUDOCODE: SWITCH/CASE 2 - Complete the pseudocode below by including a SWITCH...
I NEED THIS IN PSEUDOCODE: SWITCH/CASE 2 - Complete the pseudocode below by including a SWITCH statement that takes the user input (an integer between 26-30) and prints out a count up in its English equivalent. For example: if the user inputs “26”, the code will print “twenty-six”, “twenty-seven” on the next line, and so on up to “thirty”. If the user inputs “30”, the code will print “thirty” and finish. CREATE inputNum PRINT ("Please enter a number between 26-30...
Does break have to be used in each section of statements that follow a case in...
Does break have to be used in each section of statements that follow a case in a switch statement? Detail your answer. *In Java*
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT