Page 183 - CA_Blue( J )_Class9
P. 183

6                Scanner sc=new Scanner(System.in);
                   7                   double ac = 0.0, r;

                   8                   int ch, l,b,s,as=0,ar=0;

                   9                   System.out.println("Enter c for circle / s for square / r for
                          rectangle ");
                  10                   ch=sc.next().charAt(0);     // Syntax to input a character from console
                  11      switch(ch)

                  12      {
                  13              case 'c' :

                  14              case 'C': System.out.println("Enter radius of circle ");

                  15                          r=sc.nextDouble();
                  16                          ac=22.0/7.0 * r * r;

                  17                          System.out.println("Area of Circle " +ac);
                  18                          break;

                  19              case 's' :

                  20              case 'S': System.out.println("Enter side of a square ");
                  21                          s=sc.nextInt();

                  22                          as=s * s;

                  23                          System.out.println("Area of Square " +as);
                  24                          break;

                  25              case 'r' :

                  26              case 'R': System.out.println("Enter length of a rectangle ");
                  27                          l=sc.nextInt();

                  28                          System.out.println("Enter breadth of a rectangle ");
                  29                          b=sc.nextInt();

                  30                         ar=l * b;

                  31                         System.out.println("Area of Rectangle " +ar);
                  32                         break;

                  33              default:  System.out.println("Wrong Choice");
                  34      }

                  35  }}








                                                                                     Conditional Constructs in Java  181
   178   179   180   181   182   183   184   185   186   187   188