-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava
More file actions
88 lines (75 loc) · 2.49 KB
/
java
File metadata and controls
88 lines (75 loc) · 2.49 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
public class RobotController {
// Variables
private int batteryLevel;
private boolean isMoving;
private String robotName;
// Constructor
public RobotController(String robotName, int initialBatteryLevel) {
this.robotName = robotName;
this.batteryLevel = initialBatteryLevel;
this.isMoving = false; // Assuming the robot starts not moving
}
// Display the status of the robot
public void displayStatus() {
System.out.println("Robot Status:");
System.out.println("Name: " + robotName);
System.out.println("Battery Level: " + batteryLevel + "%");
System.out.println("Moving: " + isMoving);
System.out.println();
}
// Charge the robot's battery
public void chargeBattery(int amount) {
batteryLevel += amount;
// Cap the battery level at 100
if (batteryLevel > 100) {
batteryLevel = 100;
}
System.out.println("Battery charged. New battery level: " + batteryLevel + "%");
}
// Move the robot
public void moveRobot() {
if (batteryLevel >= 20) {
// Move the robot
isMoving = true;
System.out.println("Robot is moving.");
// Consume battery power
batteryLevel -= 10; // Change the value to whatever
System.out.println("Battery level after moving: " + batteryLevel + "%");
} else {
System.out.println("Low battery, unable to move.");
}
}
// Stop the robot
public void stopRobot() {
if (isMoving) {
isMoving = false;
System.out.println("Robot has stopped.");
} else {
System.out.println("Robot is not moving");
}
}
// Other methods...
public static void main(String[] args) {
// Example usage in the main method (you can customize this part)
RobotController myRobot = new RobotController("Bobot", 50);
myRobot.displayStatus();
myRobot.chargeBattery(30);
myRobot.displayStatus();
myRobot.moveRobot();
myRobot.displayStatus();
myRobot.stopRobot();
myRobot.displayStatus();
}
public void turnRobot(String direction) {
if (isMoving) {
System.out.println("turning" + direction);
}
else {
System.out.println ("robot is not moving");
}
}
public void checkSensors () {
System.out.println ("checking sensors");
//sensor check
}
}