Java

OOP GUIDE / WORK

 

CIRCLE CLASS 3

 

Topics

  • Instance methods
  • toString() method

 

 

TASK – PART 1 – STARTING CODE

 

Copy and paste the following Circle class into your IDE to use it in this problem.

 

 

public class Circle

{

   public double radius;

 

   public Circle(double r)

   {

      radius = r;

   }

 

   public double area()

   {

      double a = Math.PI * radius * radius;

      return a;

   }

}

 

 

 

TASK – PART 2 – ADDING TO THE CIRCLE CLASS

 

a)    Add a method that calculates and returns the circumference of the circle.  You must first determine a good method name, then the method header and then implement it.

 

b)    Add another method that calculates and returns the diameter of the circle.

 

c)     Add a toString() method that returns a string of the form:

Circle: Radius=2

 

TASK – PART 3 – TESTING THE CIRCLE CLASS


Create the CircleTester class with a main method.  Inside main, create a few Circle instances and call all methods to test them.