-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaIntro5.java
More file actions
40 lines (32 loc) · 1.25 KB
/
JavaIntro5.java
File metadata and controls
40 lines (32 loc) · 1.25 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
public class JavaIntro5 {
public static void main(String args[]) {
motor m1 = new motor(0); //new is used to create an instance of the class called an object
motor m2 = new motor(1);
System.out.println("m1 channel: " + m1.getChannel());
System.out.println("m2 channel: " + m2.getChannel());
m1.setSpeed(1);
m2.setSpeed(-1);
System.out.println("m1 speed: " + m1.getSpeed());
System.out.println("m2 speed: " + m2.getSpeed());
}
}
//Simple motor class
class motor{
//classes can have fields that store data
int channel; //which port is the motor connected too
double speed = 0; //how fast should the motor be going -1 to 1
motor(int c){ //This is the constructor, it doesn't have a return type or void
channel = c; //channel doesn't get a value set until the consttuctor is run
}
int getChannel(){ //this is a "getter" method it returns a value
return channel;
}
double getSpeed(){
return speed;
}
void setSpeed(double s){ //this is a "setter" method it sets a value
speed = s;
}
//a complete motor class would have methods that actually control the motor
//most of those methods are already created for us in FRC
}