forked from Dshwn/Hacktoberfest_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program4
89 lines (86 loc) · 2.5 KB
/
Program4
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
/*
4) Define a class Battery having data field: level which indicate its energy level. When an
object is instantiated it has 100 units battery level. The class must implement the following
methods:
A default constructor
showLevel() method to show the current battery level.
sendMsg() method is used to consume 2 units of battery on each call
recvMsg() method is used to consume 1 unit of battery on each call
compute() method consumes 1.5 unit of battery on each call
recharge(minutes) method update the battery level , 1 unit per 2 minutes.
*/
import java.util.*;
class Battery
{
float level;
Battery()
{
level=100;
}
void showLevel()
{
System.out.println("Current level of battery is :"+level);
}
void sendMsg()
{
level=level-2;
System.out.println(" Message sent ");
}
void recvMsg()
{
level=level-1;
System.out.println(" Message received");
}
void compute()
{
level=level-1.5f;
System.out.println(" Task Computed");
}
void recharge(int minute)
{
float res=(float)minute/2;
level=level+res;
System.out.println(" Recharge done");
}
}
public class program4 {
public static void main(String[] args) {
Battery bat=new Battery();
int n;
while(true)
{System.out.println("\t\t\t\tMENU ");
System.out.println(" 1. Show level ");
System.out.println(" 2. Send Msg ");
System.out.println(" 3.Recieve Msg ");
System.out.println(" 4. Compute");
System.out.println(" 5. Recharge");
System.out.println(" 6. Exit ");
int choice;
System.out.println("Choice :");
Scanner inp=new Scanner(System.in);
choice=inp.nextInt();
switch(choice)
{
case 1:
bat.showLevel();
break;
case 2:
bat.sendMsg();
break;
case 3:
bat.recvMsg();
break;
case 4:
bat.compute();
break;
case 5:
System.out.println("Enter chargable minutes:");
n=inp.nextInt();
bat.recharge(n);
break;
case 6:
System.exit(0);
}
}
}
}