-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTask01.java
132 lines (108 loc) · 4.39 KB
/
Task01.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import javax.swing.*;
import java.util.Scanner;
public class Task01{
public static void main(String[] args) {
// Problem 1
Scanner scan = new Scanner(System.in);
System.out.println("Enter your height: ");
double a = scan.nextDouble();
System.out.println("Enter your weight:");
double b = scan.nextDouble();
double calculation = (b / (a * a));
String message = String.format("Your BMI is %f", calculation);
JOptionPane.showMessageDialog(null, message);
// Problem 2
System.out.println("Enter three numbers: ");
int num1 = scan.nextInt();
int num2 = scan.nextInt();
int num3 = scan.nextInt();
if (num1 == num2 && num1 == num3)
System.out.println("Not Working!");
else if(num1 != num2 && num1 != num3 && num2 != num3)
System.out.println("Not Working!");
else
System.out.println("Working!");
// Problem 3
if ((num1 == num2) && (num1 == num3))
System.out.println(num1 + " = " + num2 + " = " + num3);
else if (num1 == num2) {
if (num1 > num3)
System.out.println(num1 + " = " + num2 + " > " + num3);
else
System.out.println(num3 + " > " + num2 + " = " + num1);
} else if (num1 == num3) {
if (num1 > num2)
System.out.println(num1 + " = " + num3 + " > " + num2);
else
System.out.println(num2 + " > " + num1 + " = " + num3);
} else if (num2 == num3) {
if (num2 > num1)
System.out.println(num2 + " = " + num3 + " > " + num1);
else
System.out.println(num1 + " > " + num2 + " = " + num3);
} else if ((num1 > num2) && (num1 > num3)) {
if (num2 > num3)
System.out.println(num1 + " > " + num2 + " > " + num3);
else
System.out.println(num1 + " > " + num3 + " > " + num2);
} else if ((num2 > num1) && (num2 > num3)) {
if (num1 > num3)
System.out.println(num2 + " > " + num1 + " > " + num3);
else
System.out.println(num2 + " > " + num3 + " > " + num1);
} else if ((num3 > num1) && (num3 > num2)) {
if (num1 > num2)
System.out.println(num3 + " > " + num1 + " > " + num2);
else
System.out.println(num3 + " > " + num2 + " > " + num1);
}
// Problem 4
System.out.println("Enter three numbers: ");
int first = scan.nextInt();
int second = scan.nextInt();
int third = scan.nextInt();
if (((first + second) > third) && ((first + third) > second) && ((second + third) > first))
System.out.println("It's OK!");
else
System.out.println("It's not OK!");
// Problem 5
int middle = 0;
if (first > second && first > third) {
if (second > third)
middle = second;
else
middle = third;
}
if (second > first && second > third) {
if (first > third)
middle = first;
else
middle = third;
}
if (third > first && third > second) {
if (first > second)
middle = first;
else
middle = second;
}
System.out.println("Middle number is " + middle);
// Problem 6
System.out.println("Enter a,b and c to find the root of ax^2 + bx + c :");
double discriminant = (second * second) - (4 * first * third);
double root1 = 0, root2 = 0;
if (discriminant > 0) {
root1 = (-second + Math.sqrt(discriminant)) / (2 * first);
root2 = (-second - Math.sqrt(discriminant)) / (2 * first);
System.out.println("root1 = " + root1 + ", root2 = " + root2);
} else if (discriminant == 0) {
root1 = (-1.0)* (second) / (2 * first);
root2 = root1;
System.out.println("root1 = " + root1 + ", root2 = " + root2);
} else {
discriminant *= -1;
root1 = ((-1.0)*(second) + Math.sqrt(discriminant)) / (2 * first);
root2 = ((-1.0)*(second) - Math.sqrt(discriminant)) / (2 * first);
System.out.println("root1 = " + root1 + "i, root2 = " + root2 + "i");
}
}
}