blob: 1b8e64c70deac5ab564bcfa82e1bba2d8ba6740f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import java.util.Scanner;
import java.util.InputMismatchException;
class Cone {
double radius, height;
Cone(double r, double h) {
radius = r;
height = h;
}
double volume() {
return Math.PI * radius * radius * height / 3.0;
}
double surfaceArea() {
return Math.PI * radius * (radius + Math.sqrt(height * height + radius * radius));
}
void display() {
System.out.println("A cone with radius " + radius + " units and height " + height + " units has surface area " + surfaceArea() + " square units and volume " + volume() + " cubic units.");
}
}
class ConeCalculationsCLI {
public static void main(String args[]) {
if (args.length != 2) {
System.err.println("Usage: ConeCalculationsCLI radius height");
System.exit(1);
}
try {
var radius = Double.parseDouble(args[0]);
var height = Double.parseDouble(args[1]);
var c = new Cone(radius, height);
c.display();
} catch (NumberFormatException e) {
System.err.println("Invalid number given as input");
System.exit(1);
}
}
}
class ConeCalculationsScan {
public static void main(String args[]) {
try {
var sc = new Scanner(System.in);
System.out.print("Enter radius: ");
var radius = sc.nextDouble();
System.out.print("Enter height: ");
var height = sc.nextDouble();
var c = new Cone(radius, height);
c.display();
} catch (InputMismatchException e) {
System.err.println("Invalid number given as input");
System.exit(1);
}
}
}
|