In: Computer Science
Please find the code below with if statements:
-----------------------------------------------------------------------------------
import java.util.Scanner;
public class Area
{ static String ch;
Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
Main ob=new Main();
ob.input();
if(ch=="C")
{
ob.circ();
}
if(ch=="S")
{
ob.squa();
}
if(ch=="R")
{
ob.rect();
}
}
void input()
{
System.out.println("Enter 'C' to calculate Area of Circle");
System.out.println("Enter 'S' to calculate Area of Square");
System.out.println("Enter 'R' to calculate Area of
Rectangle");
ch=sc.nextLine();
}
void circ()
{
System.out.println("Enter the Radius");
int
r=sc.nextInt();
double
ar1=3.14*r*r;
System.out.println("Area
of circle is :"+ar1);
}
void squa()
{
System.out.println("Enter the Side");
int
s=sc.nextInt();
int ar2=s*s;
System.out.println("Area
of Square is :"+ar2);
}
void rect()
{
System.out.println("Enter the Length");
int
l=sc.nextInt();
System.out.println("Enter the Breadth");
int
b=sc.nextInt();
int ar3=l*b;
System.out.println("Area
of Rectangle is :"+ar3);
}
}
----------------------------------------------------------------------------------------------------------
Please find the Switch statement code below:
----------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Area
{ static String ch;
Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
Main ob=new Main();
ob.input();
switch(ch)
{
case "C":
ob.circ();
break;
case "S":
ob.squa();
break;
case "R":
ob.rect();
break;
default:
System.out.println("Wrong choice entered, Re-enter choice");
ob.input();
break;
}
}
void input()
{
System.out.println("Enter 'C' to calculate Area of Circle");
System.out.println("Enter 'S' to calculate Area of Square");
System.out.println("Enter 'R' to calculate Area of
Rectangle");
ch=sc.nextLine();
}
void circ()
{
System.out.println("Enter the Radius");
int
r=sc.nextInt();
double
ar1=3.14*r*r;
System.out.println("Area
of circle is :"+ar1);
}
void squa()
{
System.out.println("Enter the Side");
int
s=sc.nextInt();
int ar2=s*s;
System.out.println("Area
of Square is :"+ar2);
}
void rect()
{
System.out.println("Enter the Length");
int
l=sc.nextInt();
System.out.println("Enter the Breadth");
int
b=sc.nextInt();
int ar3=l*b;
System.out.println("Area
of Rectangle is :"+ar3);
}
}