简体   繁体   中英

Creating a Circle object Error .Java

Getting an error when compiling my main and the error is coming from my class file. The object of this is to create a program that requires a user to create a circle then return to the user the area, diameter and circumference.

java.util.Scanner;

public class Driver {

    public static void main(String[] args)   {

    Scanner keyboard = new Scanner(System.in);

    double circumference;
    double radius = 5.7;
    double pi = 3.14159;
    double area = 0;

    Circle circleobject = new Circle();

    //Get radius
    System.out.println("What is the circle's Radius?");
    radius = keyboard.nextDouble();

    if (radius == 0.0) {
        radius = 5.7;
    }

    System.out.println("Creating a circle object.");
    //Creating the class object

    Circle circle = new Circle();

    System.out.println("Your circle's area:  " + Circle.getArea());
    System.out.println("Your circle's Diameter " + Circle.getDiameter());
    System.out.println("Your circle's Circumference " + Circle.getCircumference());
}

This is my class file

public class Circle {
    double circumference;
    double radius = 0.0;
    double pi = 3.14159;
    double area = 0;

    public void setRadius() {
        radius = radius;
    }

    public void radius() {
        radius = 0.0;
    }

    public void getArea(double area) {
        area = 0;
        area = pi * radius * radius;
    }

    public void getDiameter(double diameter) {
        diameter = radius * 2;
    }

    public void getCircumference(double circumference) {
        circumference = 2 * pi * radius;
    }   
}

This is the error that shows up when I compile the main.

Driver.java:38: error: method getCircumference in class Circle cannot be applied to given types;
System.out.println("Your circle's Circumference " + Circle.getCircumference());
                                                        ^
  required: double
  found: no arguments
  reason: actual and formal argument lists differ in length
  1 error

Your method

public void getCircumference(double circumference)
{

circumference = 2 * pi * radius;
}

should be rewritten as:

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

Using Java conventions, you should use getters and setters in the following way:

private xxx myAttribute;
// Here you can check if passed values are correct, for example if you expect a negative value or not.
public void setMyAttribute(xxx a) {
   this.myAttribute=a;
}

// It just returns the current value
public xxx getMyAttribute() {
   return myAttribute;
}

You should replace this method:

public void getCircumference(double circumference) {
    circumference = 2 * pi * radius;
}

By this :

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

Ie It should not take any parameters, and return the circumference as the result value.

Edit

You should modify your getArea and getDiameter as well :

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

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

Edit2

If I may say so, you should modify your code further. First, your Circle class should look like this:

  • PI is a static constant (the same for every instance of Circle)
  • You should add a constructor with a radius parameter, thus you can instantiate a new Circle providing the radius
  • Diameter, area and circomference does not need attributes in the class. They are directly computed from radius and PI
  • add a getRadius() method to provide public access to the radius value.

There is an example of what I would do:

public class Circle {

    private static final double PI = 3.14159;

    private double radius = 0.0;

    public Circle(double radius){
        setRadius(radius);
    }

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

    public double getRadius() {
        return radius;
    }

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

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

    public double getCircumference() {
        circumference = 2.0 * PI * radius;
    }
}

Thus, your main program would look like this:

java.util.Scanner;

public class Driver {

    public static void main(String[] args)   {

        Scanner keyboard = new Scanner(System.in);

        double radius;

        Circle circleobject = new Circle(radius);

        //Get radius
        System.out.println("What is the circle's Radius?");
        radius = keyboard.nextDouble();

        if (radius == 0.0) {
            radius = 5.7;
        }

        System.out.println("Creating a circle object.");
        //Creating the object

        Circle circle = new Circle(radius);

        System.out.println("Your circle's area:  " + circle.getArea());
        System.out.println("Your circle's Diameter " + circle.getDiameter());
        System.out.println("Your circle's Circumference " + circle.getCircumference());
    }
}

I removed some useless variables ( circumference , pi and area ), modified the way you instantiate the Circle object (using the new constructor) and modify the call to getArea() , getDiameter() , getCircumference() (you called them as static methods, but you need to call them on the instanciation circle since they are not static methods).

I suggest you to take a look at Understanding Instance and Class Members and Returning a Value from a Method Oracle's articles, and more generally to the whole Classes and Objects chapter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM