Class, objects and methods. Java

时间:2016-04-04 16:46:29

标签: java class object methods

I'm supposed to make a program which contains a class (circle) and an object in that class. The program should be able to read the radius of the object. And the class should contain methods for writing out circumference and area of the circle on the screen.

Here is what i have:

class Circle {  
     public static double area(double r){
        return Math.PI*r*r;
     }
     public static double circumference(double r){
         return 2*Math.PI*r;
     }

}

2 个答案:

答案 0 :(得分:1)

This is most likely a homework question, so I have some moral difficulty doing your homework for you.

That being said, I think you're on the right track with what you have so far. I'm not sure that making the methods static is the best choice (info on using static methods: Java: when to use static methods). Also you should examine what necessary information a circle needs in order to be instantiated, and then add that information into the circle's constructor (constructors: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html). Finally, the program needs a main method (http://www.cs.princeton.edu/courses/archive/spr96/cs333/java/tutorial/java/anatomy/main.html) that declares and instantiates a circle object, and then makes method calls against that object.

答案 1 :(得分:0)

    Here is the code...
    what you want exactly


 class Circle
{
   public static void main(String args[])
   {
      int radius = 3;
      double area = Math.PI * (radius * radius);
      System.out.println("The area of circle is: " + area);
      double circumference= Math.PI * 2*radius;
      System.out.println( "The circumference of the circle is:"+circumference) ;
   }