Java Switch Case statement | Practice | GeeksforGeeks

Given an integer choice denoting the choice of the user and a list containing the single value R or two values L and B depending on the choice.
If the user's choice is 1, calculate the area of the circle having the given radius(R).  
Else if choice is 2, calculate the area of the rectangle with given length(L) and breadth(B).
 
Example 1:
Input: 
choice = 1, 
R = 5
Output: 78.53981633974483
Explaination: The choice is 1. 
So we have to calculate the area of the circle.
 
Example 2:
Input: 
choice = 2, 
L = 5, B = 10
Output: 50
Explaination: Here we have to calculate the 
area of the rectangle.
 
Your Task:
You do not need to read input or print anything. Your task is to complete the function switchCase() which takes choice and a list arr[], containing the single value R or the two values L and B, as input parameters. It should return area of the desired geomatrical figure.
Note: Use Math.PI for the value of pi.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ R, L, B ≤ 100 
+2
badgujarsachin835 months ago
static double switchCase(int choice, List<Double> arr){
        // code here
        double area=0;
        switch(choice){
            case 1:
                 area =   Math.PI*arr.get(0)*arr.get(0);
                 break;
            case 2:
                area=arr.get(0)*arr.get(1);
                break;
            
        }
        
        return area;
    }
-1
pankajkumarravi6 months ago
*********************** java Logic ***********
static double switchCase(int choice, List<Double> arr){    double area=0;    // code here            if (choice ==1)              area =   Math.PI*arr.get(0)*arr.get(0);// area of circle            else  // area of rectangle        area = arr.get(0)*arr.get(1);            return area;}
0
Kapil Kant Pal10 months ago
Kapil Kant Pal
class Solution{    static double switchCase(int choice, List<double> arr){        double x = 0.0;        double y = 0.0;
        switch(choice){
            case 1 :  x = Math.PI*arr.get(0)*arr.get(0);                     break;
            case 2 :  y = arr.get(0)*arr.get(1);                    break;
        }        if(choice == 1){                return x;                }else{                return y;            }
    }}
0
Shubham Bhugra2 years ago
Shubham Bhugra
/*package whatever //do not write package name here */
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) {//codeScanner sc = new Scanner(System.in);int t = sc.nextInt();
double Area1;
while(t-->0){    int  choice = sc.nextInt();
    switch(choice){        case 1:            int R = sc.nextInt();            Area1 =Math.PI*R*R;            System.out.println(Area1);            break;       case 2:            int L = sc.nextInt();            int B = sc.nextInt();            int Area2 = L*B;            System.out.println(Area2);            break;
    }}
}}
0
Vikash Malakar2 years ago
Vikash Malakar
/*package whatever //do not write package name here */
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) {Scanner s = new Scanner(System.in);int R,L,B;int Area2;double Area1;int T = s.nextInt();while(T-->0){    int choice = s.nextInt();    switch(choice)    {        case 1:            R = s.nextInt();            Area1 =Math.PI*R*R;            System.out.println(Area1);            break;
        case 2:            L = s.nextInt();            B = s.nextInt();            Area2 = L*B;            System.out.println(Area2);            break;
    }}}}
0
Rajesh Kumar2 years ago
Rajesh Kumar
class GFG {public static void main (String[] args) {    Scanner scan=new Scanner(System.in);        int test=scan.nextInt();        while(test-->0){            int choice=scan.nextInt();            switch(choice){                case 1:                    int R=scan.nextInt();                    double radius=Math.PI*R*R;                    System.out.println(radius);                    break;                case 2:                    int L=scan.nextInt();                    int W=scan.nextInt();                    System.out.println(L*W);                    break;            }        } }}
0
Larisa Frolova2 years ago
Larisa Frolova
Could anyone check my code please?
https://ide.geeksforgeeks.o...
My code works good. Only thing I can't figure out is how to make user's input of the length and breadth together in one variable (space separated)?
Thanks!
0
itskshitizsh3 years ago
itskshitizsh
Inform me if anything can be improved in below code! By the way it works perfectly./*package whatever //do not write package name here */
import java.util.*;import java.lang.*;import java.io.*;
class GFG {
    static Scanner scan;public static void main (String[] args) {//codescan = new Scanner(System.in);int t = scan.nextInt();while(t--!=0){    logicFun();}}
static void logicFun(){    int choice = scan.nextInt();    switch(choice)    {        case 1:            int radius = scan.nextInt();            System.out.println(""+Math.PI*(radius)*(radius));            break;        case 2:              int l = scan.nextInt();            int b = scan.nextInt();            System.out.println(""+(l)*(b));            break;        default:            break;    }}}
0
Mandar 3 years ago
Mandar 
Works correctly/*package whatever //do not write package name here */
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) {Scanner sc = new Scanner(System.in);int t = sc.nextInt();while(t > 0){    t--;    int val = sc.nextInt();    if(val == 1){        int r = sc.nextInt();        double area_circle = Math.PI*r*r;        System.out.print(area_circle);    }    else if(val==2){        int l = sc.nextInt();        int b = sc.nextInt();        long area_rect = l*b;        System.out.print(area_rect);    }  System.out.println();  }}}
0
Tanay Chauli
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.