472 Assignment3
472 Assignment3
UCE2023472
Problem statement:
A circle has a radius. Its area can be calculated. The area is a double
number. Its perimeter is a double number. Given two circles one can
find out which is large and which is small. Create two circles c1 and
c2 with radius 10 and 7 respec vely. Calculate the area and
perimeter of each. Compare two circles with each other and display
which is large and which is small.
Algorithm:
1. Start the program.
2. Import java.u l package to use Scanner class for taking input
from the user.
3. The ‘Calcula on’ class is defined with:
a. Class variables ‘pi’ and ‘radius’.
b. A constructor that takes the radius as a parameter and
ini alizes the ‘radius’ instance variable.
c. Methods to calculate the area and perimeter of the circle
using the entered radius, using appropriate formulae.
4. a. ‘Circle’ class is the ‘main’ class.
b. The ‘main’ method is the entry point of the program.
5. a. In ‘main’ method, we take user input by crea ng an object
(‘sc1’) of ‘Scanner’ class.
b. Prompt the user to choose the type of comparison (by area
or by perimeter) by entering 1 or 2.
6. We use switch case to call the method as per the comparison
user wants the program to do.
7. Case 1: Area comparison:
If the user chooses 1:
a. User is prompted to enter the radius of Circle 1.
b. An object of the ‘Calcula on’ class (‘circle1’) with the
entered radius, and the area of circle 1 is calculated.
c. User is prompted to enter the radius of Circle 2.
d. An object of the ‘Calcula on’ class (‘circle2’) with the
entered radius, and the area of circle 2 is calculated.
e. The areas of both the circles are compared, and the output is
displayed. (Which is larger and which is smaller or both
equal)
case 1: {
// Comparing circles based on area
System.out.print("Enter the radius for Circle 1: ");
double radius1 = sc1.nextDouble();
Calculation circle1 = new Calculation(radius1);
double area1 = circle1.area();
case 2: {
// Comparing circles based on perimeter
System.out.print("Enter the radius for Circle 1: ");
double radius1 = sc1.nextDouble();
Calculation circle1 = new Calculation(radius1);
double perimeter1 = circle1.perimeter();
Type 1 for comparing circles by area and type 2 for comparing circles by
perimeter2
Enter the radius for Circle 1: 5
Enter the radius for Circle 2: 9
Circle 2 is larger than circle 1. Perimeter of circle 1 is 31.42857143.
Perimeter of circle 2 is 56.571428574