-2

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.

  • 2
    Welcome to Stack Overflow! You have posted way to much code in your question, which makes it unclear to us (and to future readers) exactly where the problem is. Please reduce your problem code to 10 lines or less. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Joe C Aug 13 '17 at 12:52
  • my guess :you have created 2 varibles `c_area;` local and and instance one.remove local one.you set area value to local c_area but print instance one which is still 0(default) – Madhawa Priyashantha Aug 13 '17 at 13:01

4 Answers4

2

The problem is with this piece of code in your input class:

if(type.equals("circle"))
        {float c_area;
            c_area=3.14f*side1*side1;
        }

you are declaring c_area inside if making it local variable and not instance variable, Instead try this

if(type.equals("circle"))
        {
            this.c_area=3.14f*side1*side1;
        }

Hope it helps..

Ashish Raj
  • 170
  • 1
  • 1
  • 10
0

Fist of all it would be better for you to Use "Math.PI" intead of just taking 3.14f it´s more accurate;

In my Oppinoin you should use a static Method to calculate the "c_area" (with the Parametes (radius, Type) and imedietey return that. I don't see needs to Create an Object so you could delete your Constructor (cause it isn´t needed to work with this Object later.


If this is your whole coode I woult personally just create an Method to calculate the area.. and just don't use another class

CC RAT
  • 16
  • 1
0

he he program is perfectly fine and there is no coding mistake.

only thing is that there is typo in void area(String select){ } method

if(select.equals("cirlce")) should be if(select.equals("circle"))

circle spelling is wrong.

user2862544
  • 415
  • 3
  • 13
-1

Look at these 2 lines:

float c_area; c_area=3.14f*side1*side1;

You are declaring a variable named c_area inside the constructor, when you already have a variable named the same way in the class scope declaration. So the compiler ignore the class level variable, and calculates the constructor level variable instead. Of course, the class level variable doesn't change and stays zero by default. Simply remove float c_area; from the constructor and you're good

Notice: if you are new to Java, you should know that it follows certain code conventions. You should name your classes with UpperCase styling, and your variable names should be camelCased

MrMikimn
  • 721
  • 1
  • 5
  • 19