Last active
August 4, 2018 16:25
-
-
Save moniyax/a99516501b570ab851d8 to your computer and use it in GitHub Desktop.
Object things
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A class is used to mint new objects. The class here is used to create new car objects | |
// in the program. You may think of it as a car factory. | |
// Classes specify methods and attributes. | |
// Methods define operations that can be performed on (or by) an object: a car in this case. | |
// The methods defined in this Car class are "move" and "turn". See the comments written for | |
// each method for info on their functions. | |
// Attributes indicate well... attributes of objects. They help ho"ld the state of the object | |
// at any given instance. The attributes for the car are "location", "brand", "model", and "direction". | |
// Silly silly old Car | |
class Car { | |
// Them attributes (or fields, or properties, or instance variables or... whatchamakalit) | |
private int location; // Where is my ride?!!! | |
private String brand; // Who made that silly car eh? Who?! | |
private String model; // Oh what do the silly car makers call their silly car? | |
private int direction; // Where's it headed? In the Atlantic I hope. | |
// This is the constructor. A method used for initializing the attributes | |
// of a specific car when created. You don't invoke it; java invokes it | |
// automatically when you create a new car, like when you do 'new Car("BMX", "XXX2")' | |
public Car(String brand, String model) { | |
this.brand = brand; | |
this.model = model; | |
location = 0; | |
direction = 90; | |
} | |
// What happens when you push the throttle. Voom vava voom!!! | |
public void move(int dist) { | |
this.location += dist; | |
} | |
// What happens when you turn the steering. Remember to look left when turning right. *wink* | |
public void turn(int angle) { | |
direction = (direction + angle) % 360; | |
} | |
// OH! Turns out it has GPS! Still hate it nonetheless :/ | |
public String tellLocation() { | |
return String.format("The %s %s is %d miles/%d degrees off of base", brand, model, location, direction); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is where the real action is. Where we get to actually do stuff | |
// in our program. See classes are dumb, they do nothing aside creating | |
// kids eh... objects. Objects are what we actually use to interesting | |
// stuff like, of course, sending of your silly car(s) and then pretending that | |
// you actually care. | |
// The Program (sounds sci-fi eh?) | |
public class Program { | |
public static void main(String[] args) { | |
Car car = new Car("Audi", "880XE"); // Mint a new Audi ride | |
car.move(100); // Move it 100 miles off | |
System.out.println(car.tellLocation()); // Oh where's it? | |
car.move(30); // It moves another 30 miles | |
car.turn(280); // turning 280 degress | |
System.out.println(car.tellLocation()); // Oh where's it again? | |
Car honda = new Car("Honda", "Anaconda"); // Mint a new Honda ride | |
honda.move(80); // Move it 80 miles off | |
System.out.println(honda.tellLocation()); // Oh where's it? | |
} | |
// Final Output: | |
// The Audi 880XE is 100 miles/90 degrees off of base | |
// The Audi 880XE is 130 miles/10 degrees off of base | |
// The Honda Anaconda is 80 miles/90 degrees off of base | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment