-3
public class Circle {

    public static void main(String[] args) {

        int diameter; // (a) declare variable(s)
        Circle2 c = new Circle2(50); // (b) create a circle with diameter 50

        // (c) call to calculate perimeter 
        //     without putting codes here i still get the
        //     output
        // (d) call to calculate area

        // (e) display area and perimeter of the circle
        System.out.println("Area:" + c.calcArea());

        // (e) display area and perimeter of the circle
        System.out.println("Perimeter:" + c.calcPerimeter());

    }

}

class Circle2 {
    double diameter, radius;

    public Circle2() {
        diameter = 0.0;
        radius = 0.0;
    }

    public Circle2(double cDiameter) {
        // (f) construct a circle with a specified diameter and radius
        diameter = cDiameter;
        radius = diameter / 2;
    }

    public double calcPerimeter() {     
        // (g) calculate perimeter
        return (22 * diameter) / 7; 
    }

    public double calcArea() {
        // (h) calculate area
        return (22 * radius * radius) / 7;      
    }
}

This is an exam question which mean i cant add nor change anything. All i can do is to fill in the codes into question (a-h) I have tried many times to call are and perimeter but its not working

WJS
  • 36,363
  • 4
  • 24
  • 39
Kavina
  • 1
  • 1

1 Answers1

-1
  • a : you have to declare the variables which will be filled with methods :

    double perimeter;
    double area;
    
  • b : correct

  • c , d : you will fill the variables with methods :

    perimeter = c.calcPerimeter();
    area = c.caclArea();
    
  • e : you have to display them , you did it correct anyway because you've printed directly the values from method but the question wants u to print the variables filled

    System.out.println("Area:" + area);
    System.out.println("Perimeter:" + perimeter);
    
  • About Perimeter : you have to use this instead return Math.PI * 2 * radius

  • About Area : return Math.PI * radius * radius
Karam Mohamed
  • 843
  • 1
  • 7
  • 15