230k views
0 votes
How to do simple class Circle with one field radius.Supply one constructor that makes the radius of the circle as a parameter.Supply one method getArea ,that computes and returns (not prints)the area of a circle.Provide the accessor method getRadius.How to create a small class CircleTest with a main method that prompts the user to enter an integer value for the radius,creates one Circle object with that radius,calls it getArea method,and prints out the returned value.

User OriHero
by
4.9k points

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class Circle {

private double radius;

//The constructor

public Circle(){

}

//Method to compute and return the area

public double getArea(double radius){

double area = Math.PI *(radius*radius);

return area;

}

//Accesor method (getRadius)

public double getRadius() {

return radius;

}

}

class CircleTest{

//The main method

public static void main(String[] args) {

//Scanner Class to receive user input

Scanner in = new Scanner(System.in);

System.out.println("Enter Radius");

double r = in.nextDouble();

//Make an instance of the class Circle

Circle circle = new Circle();

//Calculating the area

double calArea = circle.getArea(r);

//Printing out the area

System.out.println("The area is: "+calArea);

}

}

Step-by-step explanation:

  • This has been solved with Java Programming Language
  • Please follow the comments in the codes
  • As required, The class Circle is created with one member variable radius
  • A getter method is also created
  • A method getArea is also created that computes and returns the area of the circle
  • In the class CircleTest The scanner class is used to receive a user value for radius
  • An instance of Circle class is created
  • The method getArea is called with the instance of the class
  • The area is printed out
User SmRaj
by
4.6k points