0% found this document useful (0 votes)
19 views8 pages

472 Assignment3

The document describes a Java program that takes user input to compare two circles by either area or perimeter. It defines Circle and Calculation classes, calculates area and perimeter using formulas, and outputs which circle is larger.

Uploaded by

sejal.s.patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

472 Assignment3

The document describes a Java program that takes user input to compare two circles by either area or perimeter. It defines Circle and Calculation classes, calculates area and perimeter using formulas, and outputs which circle is larger.

Uploaded by

sejal.s.patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PS-J Assignment 3

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)

8. Case 2: Perimeter comparison:


If the user chooses 2:
f. User is prompted to enter the radius of circle 1.
g. An object of the ‘Calcula on’ class (‘circle1‘) with the
entered radius, and the perimeter of the circle 1 is
calculated.
h. User is prompted to enter the radius of Circle 2.
i. An object of the ‘Calcula on’ class (‘circle2’) with the
entered radius, and the perimeter of circle 2 is calculated.
j. The perimeters of both the circles are compared, and the
output is displayed. (Which is larger and which is smaller or
both equal)

9. End the program.


Flowchart:
Program:
// Importing the java.util package to use the Scanner class for user input
package Assi3;
import java.util.*;

// Definition of the Calculation class


class Calculation {
// Class variables for pi and radius
double pi = 3.142857143;
double radius;

// Constructor that takes radius as a parameter and initializes the


instance variable
public Calculation(double r) {
this.radius = r;
}

// Method to calculate the area of the circle


double area() {
double a = pi * radius * radius;
return a;
}

// Method to calculate the perimeter of the circle


double perimeter() {
double p = 2 * pi * radius;
return p;
}
}

// Main class definition for the Circle program


public class Circle {
// Main method where the program execution begins
public static void main(String[] args) {
// Creating Scanner object to take user input
Scanner sc1 = new Scanner(System.in);

// Displaying a prompt for the user to choose the comparison type


System.out.print("Type 1 for comparing circles by area and type 2
for comparing circles by perimeter");

// Reading the user's choice


int choice = sc1.nextInt();

// Switch statement to handle user's choice


switch (choice) {

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();

System.out.print("Enter the radius for Circle 2: ");


double radius2 = sc1.nextDouble();
Calculation circle2 = new Calculation(radius2);
double area2 = circle2.area();
// Comparing the areas and displaying the result
if (area1 > area2) {
System.out.print("Circle 1 is larger than circle 2.
Area of circle 1 is " + area1 + ". Area of circle 2 is " + area2);
} else if (area1 < area2) {
System.out.print("Circle 2 is larger than circle 1.
Area of circle 1 is " + area1 + ". Area of circle 2 is " + area2);
} else
System.out.print("Both are equal." + area1);
break;
}

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();

System.out.print("Enter the radius for Circle 2: ");


double radius2 = sc1.nextDouble();
Calculation circle2 = new Calculation(radius2);
double perimeter2 = circle2.perimeter();

// Comparing the perimeters and displaying the result


if (perimeter1 > perimeter2) {
System.out.print("Circle 1 is larger than circle 2.
Perimeter of circle 1 is " + perimeter1 + ". Perimeter of circle 2 is " +
perimeter2);
} else if (perimeter1 < perimeter2) {
System.out.print("Circle 2 is larger than circle 1.
Perimeter of circle 1 is " + perimeter1 + ". Perimeter of circle 2 is " +
perimeter2);
} else
System.out.print("Both are equal." + perimeter1);
break;
}
}
}
}
Output:
Type 1 for comparing circles by area and type 2 for comparing circles by
perimeter1
Enter the radius for Circle 1: 4
Enter the radius for Circle 2: 7
Circle 2 is larger than circle 1. Area of circle 1 is 50.285714288. Area of
circle 2 is 154.000000007

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

You might also like