Skip to content

Commit c1315c1

Browse files
Create 21.1 Class and Cont.java
1 parent 29b193f commit c1315c1

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

21.1 Class and Cont.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Write a program to make "Circle" class consisting of following: -- radius --> of double datatype -- parameterized constructor to initialize radius variable -- getArea() --> returns area of Circle of double datatype (formula: 3.14 * radius * radius)
3+
4+
Create array of "n" Circle objects (where "n" is no. of objects specified by user at run-time) and display the area of those Circle-objects whose area is greater than 30.0.
5+
6+
Input Format
7+
8+
Program should take the inputs in following sequence: 1) In First input line, no. of circle objects to create. i.e. value of "n". 2) In remaining input lines, enter radius value of "n" Circle objects. For example, if no. of Circle-objects to be created are 2, then user-inputs should be as follows: 2 3.2 2.5
9+
10+
Constraints
11+
12+
1) No. of Circle objects range between 1 to 10, i.e. 1 <= n <= 10 2) All radius value should be positive, i.e. 0.1 <= radius <= 50.0
13+
14+
Output Format
15+
16+
If no. of Circle-objects "n" is less than 1, then "Invalid input" should be displayed and no other input should be taken. Treat any negative value of radius as 0.0 and then display area of all those circle objects with value greater than 30.0, such as follows: 32.1536
17+
18+
Sample Input 0
19+
20+
2
21+
3.2
22+
2.5
23+
Sample Output 0
24+
25+
32.1536
26+
*/
27+
28+
import java.util.*;
29+
30+
class Circle
31+
{
32+
double radius = 0.0;
33+
Circle(double r)
34+
{
35+
this.radius = r;
36+
}
37+
double getArea()
38+
{
39+
return (3.14*this.radius*this.radius);
40+
}
41+
}
42+
43+
44+
public class Solution {
45+
46+
public static void main(String[] args) {
47+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
48+
Scanner sc = new Scanner(System.in);
49+
int n = sc.nextInt();
50+
if(n>=1 && n<=10)
51+
{
52+
Circle []c = new Circle[n];
53+
for(int i=0;i<n;i++)
54+
{
55+
double r = sc.nextDouble();
56+
if(r<=0.1 || r>=50.0)
57+
r = 0.0;
58+
c[i] = new Circle(r);
59+
}
60+
for(int i=0;i<n;i++)
61+
{
62+
if(c[i].getArea()>30.0){
63+
System.out.println(c[i].getArea());
64+
}
65+
}
66+
}
67+
else
68+
{
69+
System.out.print("Invalid input");
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)