0

So as an assignment for a class i'm trying to make a simple circle class and then use it as an constructor. Here is my class code:

public class Circle
{
  private double radius;
  private double pi;

  public void setRadius(double rad)
  {
    radius = rad;
  }

  public double getRadius()
  {
    return radius;
  }

  public double getArea()
  {
    return pi * radius * radius;
  }

  public double getDiameter()
  {
    return radius * 2;
  }

  public double getCircumference()
  {
    return 2 * pi * radius;
  }
}

Here's the program that uses the class to make a constructor:

import java.util.Scanner; //scanner class for input

  public class CircleDemo
{
  public static void main(String[]args)
  {
    double radiusIn; //gets radius from input

    Scanner keyboard=new Scanner(System.in); //activates scanner class in program

    System.out.print("Enter the radius of a circle: ");
    radiusIn=keyboard.nextDouble();

    Circle circularObject= new Circle(radiusIn);

    System.out.println("The circle's area is" + circularObject.getArea());
    System.out.println("The circle's diameter is" + circularObject.getDiameter());
    System.out.println("The circle's circumference is" + circularObject.getCircumference());
  }
}

and i get the error: Error: constructor Circle in class Circle cannot be applied to given types; required: no arguments found: double reason: actual and formal argument lists differ in length

I don't see anything wrong with my code, but then again i'm using the sample that my teacher gave us.

2
  • 2
    Not related to the question (as answered already), but it would be better to use Math.PI instead of a private field pi, which hasn't even been set to the value of PI. Commented Feb 1, 2015 at 19:40
  • Nowhere are you giving pi a value to use mathematically. What do you think the output will be? Furthermore, does the value of pi ever change? Commented Feb 1, 2015 at 19:43

5 Answers 5

7

You are calling the constructor here

Circle circularObject= new Circle(radiusIn);

But there is no constructor defined in your Circle class which matches the argument you are passing in. Return to your Circle class and define the constructor which takes a double argument:

public Circle(double val)
{
    //Implementation
}

Note that if you do not provide an explicit constructor for a class, Java will automatically provide you with an empty-implementation, no argument, public constructor for you to use. Once you define your first explicit constructor, that default constructor is gone, meaning you would have to re-define it manually.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't have a constructor in your circle class

public Circle (something here) {}

2 Comments

It's not mandatory to have a constructor. Generally a good idea: yes. Required: no.
@FoggyDay In the snippet he posted though, Circle circularObject= new Circle(radiusIn); he is calling the class passing a value to the constructor, but he didn't write a constructor
0

Java provides a default constructor for every class you write. You cannot see it, but it is there and it will be called unless you write your own with any desired parameters.

Comments

0

One of the most used implementations of a parametered constructor is like this:

double radius;
public Circle(double radius){
    this.radius = radius;
}

you do not need to have the same name:

double radius;
public Circle(double i){
    radius = i;
}

but the first version is considered best practice and should be the one used. This is of course valid for any form of paramatered constructors:

String name;
int age;
public Person(String name, int age){
    this.name = name;
    this.age = age;
}

Comments

0

Please add the constructor for Circle. The modified code will be:

public class Circle 
{
  private double radius;
  private double pi;

  Circle(double x) 
  {
    this.radius = x;
  }

  public void setRadius(double rad) 
  {
    radius = rad;
  }

  public double getRadius() 
  {
    return radius;
  }

  public double getArea() 
  {
    return pi * radius * radius;
  }

  public double getDiameter() 
  {
    return radius * 2;
  }

  public double getCircumference() 
  {
    return 2 * pi * radius;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.