Creating a circle with radius and area calculations

Report a typo

Create a Java class named Circle with two constructors. The default constructor should create a circle with a radius of 1, while the second constructor should accept a single parameter for radius value. Both constructors should initialize an area variable based on the radius. Create functions getRadius() and getArea() to return the circle's radius and area respectively. If the 'n' input is a positive number, create a circle with radius 'n', otherwise create a default circle. The area should be calculated as πr^2 and rounded off to nearest whole number for simplicity. Your main Java program should take single integer as input and print the circle's radius and rounded off area as output in the format ' '.

Sample Input 1:

1

Sample Output 1:

1 3

Sample Input 2:

5

Sample Output 2:

5 79
Write a program in Java 25
import java.util.Scanner;

// Define the Circle class here
class Circle {
// Declare the properties of the Circle class here


/* Define the default and parameterized constructors here. The constructors should initialize the
'radius' property with the given or default value and 'area' property with the value calculated as πr^2
rounded off to the nearest whole number. */


// Getters for the 'radius' and 'area' properties
public int getRadius() {
// Your code here
}

public int getArea() {
// Your code here
}
}

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();

// create a Circle object using 'n' if its positive else use the default constructor

// print the circle's radius and area
}
}

Create a free account to access the full topic