In: Computer Science
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
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
5 points
15 points
THIS IS FOR JAVA PROGRAMMING. PLEASE ANSWER ASAP. THANK YOU
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)