Hi! this is a lengthy post/I hope its comprehensible

basically this program should calculate and display the area of a circle.....but it shows area as 0.0
for every value of radius entered.......I would really appreciate it if someone helps!

import java.util.Scanner;

public class circledriver
{
    public static void main(String args[])
    {
        
        System.out.println("Welcome to Circle\n");

       
        Scanner scan = new Scanner(System.in);
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {
            System.out.print("Enter Circle ");
            String select = sc.next();  
            sc.nextLine();  

            System.out.print("Enter Radius");
            double Radius= scan.nextDouble();  
            sc.nextLine();  

          
            circleshape p = circleDB.getshape(select, Radius);
            System.out.println();
            if (p != null)
                System.out.println(p.toString());
            else
                System.out.println("No CircleShape matches this selection.\n");

          
            System.out.print("Continue? (y/n): ");
            choice = sc.nextLine();
            System.out.println();
        }
    }
}

This is a circledriver program......Basically the user has to select circle and enter the radius....then circleDB has to select the selection and create a circle object....

public class circleDB
{
    public static circleshape getshape(String select,double Radius)
    {
        circleshape p = null;

        if (select.equalsIgnoreCase("circle") )
        {
            circle c = new circle();
            if (select.equalsIgnoreCase("circle"))
            {
                c.setSelection(select);
                c.setRadius(Radius);
            
            p = c;
            }
      
        }
        return p;
    }
}

now circleDB calls superclass circleshape where radius is defined and subclass circle which has area defined

import java.text.NumberFormat;

public class circleshape
{
    private String Selection;
    double Radius;
    public static int count = 0;

    public circleshape()
    {
        Selection= "";
        Radius= 0;
    }

    public void setSelection(String Selection)
    {
        this.Selection = Selection;
    }

    public String getSelection()
    {
        return Selection;
    }

    public void setRadius(double Radius)
    {
        this.Radius = Radius;
    }
 

    public double getRadius()
    {
        return Radius;
    }

    public String toString()
    {
        return "Selection:        " + Selection+ "\n" +
               "Radius:       " + Radius + "\n";
    }

    public static int getCount()
    {
        return count;
    }
}

and this is the subclass circle

public class circle extends circleshape
{
	 double Area;

	public circle()
	{
		super();
		Area= 0.0;
		count++;
	}

	public void setArea(double Area)
	{
		Radius=super.getRadius();
                Area=3.14*Radius*Radius;
	}

	public double getArea(){
		return Area;
	}

	public String toString()
	{
		return super.toString() +
			"Area:" + Area; 
	}
}

Recommended Answers

All 3 Replies

Use constructors with radius parameter. In constructor of circle.class calculate area.

hey p.setArea is not called.then how will area be calculated?(in main)
Besides,if you are calculating area from radius,why should pass a parameter area to setarea in circleshape?

Comments on circledriver class:
You don't need two Scanner Objects. You can use the same Scanner Object for reading in different types of data. Also, the standard is to capitalize the class name as far as I know, so it should be called CircleDriver.

Comments on DB class

if (select.equalsIgnoreCase("circle") )
        {
            circle c = new circle();
            if (select.equalsIgnoreCase("circle"))
            {
                c.setSelection(select);
                c.setRadius(Radius);
            
            p = c;
            }
      
        }

The second if statement is unnecessary since you already know, at that point, that it will return true.


As far as your area

public void setArea(double Area)
{
Radius=super.getRadius();
Area=3.14*Radius*Radius;
}

Why super.getRadius() and not just getRadius()? They should be calling the same method anyway. Since this class extends the other one, it inherited the getRadius method, and the Radius instance variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.