-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTV.java
82 lines (68 loc) · 1.87 KB
/
TV.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
public class TV {
//state (attributes)of the tv
private boolean isOn;
private int volume;
private int channel;
// constructor to initialize the tv object
public TV() {
isOn=false;
volume=50;
channel=1;
}
//behavior (methods) of the TV
//method to turn the TV on
public void turnOn() {
isOn=true;
System.out.println("Tv is now ON");
}
//Method to turn the TV off
public void turnOff() {
isOn=false;
System.out.println("TV is now OFF");
}
//method to change the channel
public void changeChannel(int newChannel) {
if(isOn) {
channel =new channel;
System.out.println("channel changed to "+ channel);
}
else {
System.out.println("tv is off .please turn it on first");
}
}
// method to increase the volume
public void increaseVolume() {
if(isOn) {
volume ++;
System.out.println("volume increased to "+volume);
}
else {
System.out.println("Tv is off. please turn it on first ");
}
}
public void decreaseVolume() {
if (isOn) {
volume--;
System.out.println("Volume decreased to " + volume);
} else {
System.out.println("TV is off. Please turn it on first.");
}
}
// Method to display the current status of the TV
public void displayStatus() {
System.out.println("TV is " + (isOn ? "ON" : "OFF"));
System.out.println("Channel: " + channel);
System.out.println("Volume: " + volume);
}
}
public static void main(String[] args) {
TV myTV = new TV(); // Create a new TV object
myTV.turnOn(); // Turn the TV on
myTV.changeChannel(5); // Change the channel
myTV.increaseVolume(); // Increase volume
myTV.displayStatus(); // Display the current status
myTV.turnOff(); // Turn the TV off
myTV.changeChannel(3); // Try to change channel while off
}
}
}