-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticInternetUser.java
More file actions
57 lines (41 loc) · 2.13 KB
/
StaticInternetUser.java
File metadata and controls
57 lines (41 loc) · 2.13 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
// 26. Static variable, static functions and static block
// Write a JAVA program that creates a class called 'InternetUsers' for browsers of a college, which contain two static variables 'count' with initial value=0 to keep track of number of internet users and 'happyCusfomers'. It also contain a final variable MAXTIME=2 hrs and string variable to store login name. class also contains a static method 'show' to display number of internet users and happy customers and a non static function 'compute' to increment and display the name of happyCustomers if customer is happy. Also create a static block in your program to display a welcome message like 'Let's start browsing' and display maximum allotted time for browsing, it also initializes the static variable happyCustomers =count.
public class StaticInternetUser {
//static variables
static int count = 0;
static int happyCustomers;
final static int MAXTIME = 2;
String loginName;
//static block to display welcome messages
static {
System.out.println("Let's start browsing!");
System.out.println("Maximum allotted time for browsing: " + MAXTIME + " hours");
happyCustomers = count; //intially happycustomer = 0
}
StaticInternetUser(String loginName) {
this.loginName = loginName;
count++;
}
// Static method to show number of internet users and happy customers
static void show() {
System.out.println("Number of Internet Users: " + count);
System.out.println("Happy Customers: " + happyCustomers);
}
// Non-static method (Normal method) to increment happyCustomers if the customer is happy
void compute(boolean isHappy) {
if (isHappy) {
happyCustomers++;
System.out.println(loginName + " is happy!");
} else {
System.out.println(loginName + " is not happy.");
}
}
public static void main(String[] args) {
StaticInternetUser user1 = new StaticInternetUser("Anees");
user1.compute(true);
StaticInternetUser.show();
StaticInternetUser user2 = new StaticInternetUser("Rigzin");
user2.compute(false);
StaticInternetUser.show();
}
}