-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbdullah.java
More file actions
93 lines (80 loc) · 3.47 KB
/
Abdullah.java
File metadata and controls
93 lines (80 loc) · 3.47 KB
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
import java.util.Scanner; // for input
// COMPLETED & TESTED
/* Scenario: A container is being filled with water at a changing rate.
Q1. Water Level in a Container (Volume over Time) */
class Container
{
private double current_volume, max_volume; // 2 data members
public Container (double m) // JAVA DOESN'T HAVE DEFAULT PARATERMIZED CONSTRUCTOR
{
max_volume = m;
}
public void add_water(double rate, double time) // to simulate water being added over time.
{
double choice; // CONSTANT OR VARYING RATE KAY LIYE
Scanner obj = new Scanner(System.in); // for entering choice
// PROMPT
// CHOICE FOR USER WHETHER HE WANTS CONSTANT OR VARYING RATE
do { // taakay user 1 or 2 kay ilawa kuch enter na kar sake
System.out.println(" WHAT DO YOU WANT? ");
System.out.println(" 1. CONSTANT RATE");
System.out.println(" 2. VARYING RATE");
choice = obj.nextInt();
} while (choice < 1 || choice > 2); // -1, 0, 3,4 ye wale numbers par loop hojaye dobara
System.out.println(); // SPACING
if (choice == 1) // CONSTANT RATE IT IS
{
for (int t = 1; t <= time; t++) // time changes all the time eg. 1 to 10
{
current_volume = current_volume + (rate * t); // calculation
if (current_volume > max_volume) // meaning overflow hojaye agar
{
break; // break from the FOR LOOP as no more water can be added
}
System.out.println(current_volume); // display of calculation after validation
// more efficient way
}
}
else // VARYING RATE IT IS THEN
{
for (double t = 1; t <= time; t++) // time changes all the time eg. 1 to 10
{
rate = (2*t) + 1; // rate = 2t + 1
current_volume = current_volume + (rate * t); // calculation
if (current_volume > max_volume) // meaning overflow hojaye agar
{
break; // breakf rom the FOR LOOP as no more water can be added
}
System.out.println(current_volume); // display of calculation after validation
// more efficient way
}
}
// CONTAINER will GET FULL whatever the type of rate is (varying, constant)
System.out.println("FULL");
} // end of add water function
// RETURN TYPE STRING HAI
public String get_rate_of_change(double v1, double v2, double t1, double t2)
{
String answer = " "; // assigning space for removing garbage value
// aik jo validation idhar lage gi is of dono times same nahi hone chahiye
if (t1 >= t2) // is the starting time >= ending time?
{
// yes, meaning ye rate of change kay liye suitable nai
// because of -ve and math error (10 - 10 -> 0)
answer = " Ending time should be greater than Starting Time" ;
}
else {
double rate = (v2 - v1) / (t2-t1);
answer = Double.toString(rate); // built in function for converting
// double to STRING
}
return answer;
}
}
public class Abdullah {
public static void main(String[] args) {
Container Areeb = new Container(20); // max = 20 L
Areeb.add_water(2,10); // you can also pass values here
System.out.println(Areeb.get_rate_of_change(2,8,1,2));
}
}