What's wrong with my code ? Why it is not calculating the area of cirlce correctly in case of circle as a shape? Every time it is calculating the area of circle equals to 0.Rest of the code is working fine.
package constructor;
import java.util.Scanner;
class input{
float c_area;
int s_area,r_area;
input(int side1,String type)
{
if(type.equals("circle"))
{
c_area=3.14f*side1*side1;
}
else{
s_area=side1*side1;
}
}
input(int l,int b){
r_area=l*b;
}
void area(String select){
if(select.equals("cirlce"))
{
System.out.println("Area is: "+c_area);
}
else if(select.equals("square")){
System.out.println("Area is: "+s_area);
}
else
{
System.out.println("Area is:"+r_area);
}
}
};
public class shape {
public static void main(String[] args) {
String name;
char ch;
Scanner obj=new Scanner(System.in);
do{
System.out.print("ENTER THE SHAPE TYPE:");
name=obj.next();
if(name.equals("circle"))
{
int radius;
System.out.print("Enter the radius: ");
radius=obj.nextInt();
input a=new input(radius,name);
a.area(name);
}
else if(name.equals("rectangle"))
{
int len,bre;
System.out.print("Enter LENGTH & BREADTH: ");
len=obj.nextInt();
bre=obj.nextInt();
input x=new input(len,bre);
x.area(name);
}
else if(name.equals("square"))
{
int side;
System.out.print("Enter side: ");
side=obj.nextInt();
input x=new input(side,name);
x.area(name);
}
System.out.println("continue: Y or N:");
ch=obj.next().charAt(0);
}while(ch=='y' || ch=='Y');
}
}
What's wrong with my code ? Why it is not calculating the area of cirlce correctly in case of circle as a shape? Every time it is calculating the area of circle equals to 0.Rest of the code is working fine.