The knowledgebase for 1675 members.
We aren’t going to teach a full Java course but need to go over a few things about how Java works. If you want to take a full java course ask a mentor and we can point you towards resources!
Variables are pieces of data that hold a value. In Java every variable has a specific type. Types are either “primitive” or a class (more on classes below).
Examples of commonly use primitive variables:
boolean
- true or false, nothing else.int
- An interger, or whole number.double
- A number with a decimal part, like 3.14159
MyClass myObj = new MyClass();
.public class [name] {
.public [name]([arguments]) {
.NEW FILE Car.java
public class Car {
String model;
int miles;
int speed
//Constructor
public Car(String mo, int mi, int s) {
model = mo;
miles = mi;
speed = s;
}
// fullThrottle() method
public void fullThrottle() {
System.out.println("The car is going as fast as it can!");
}
// setSpeed() method
public void setSpeed(int s) {
speed = s;
}
// getSpeed() method
public void getSpeed() {
System.out.println("The car is going " + speed + " miles per hour.");
}
NEW FILE Main.java
public class Main {
// Inside main, call the methods on the myCar object
public static void main(String[] args) {
Car myCar = new Car("CRV", 100000, 0); // Create a myCar object and call the constructor
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.setSpeed(70); // Call the setSpeed() method
myCar.getSpeed(); // Call the getSpeed() method
}
}