Last active
January 12, 2019 19:09
-
-
Save btforsythe/e63dfe60049712b80cd6026c991a9617 to your computer and use it in GitHub Desktop.
Simple code lesson created for newbies
This file contains hidden or 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
public class Dog { | |
private String name; | |
private int age; | |
public final static String FAMILY = "Canine"; | |
// constructors create objects (instances of classes) | |
public Dog(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
// 'void' methods don't return a value | |
public void bark() { | |
// "woof" is a String ~literal~ | |
System.out.println("woof"); | |
} | |
// this method returns a String literal | |
public String speak() { | |
// '+' is the String concatenation operator | |
return "My name is " + name; | |
} | |
public int equivalentHumanAge() { | |
// 7 is what we call a 'magic number'. that's bad. | |
// we'd usually create a constant | |
return 7*age; | |
} | |
} |
This file contains hidden or 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
public class DogTest { | |
public static void main(String[] args) { | |
Dog shortDog = new Dog("Short Dog", 14); | |
shortDog.bark(); | |
//when we tell him to speak, he responds | |
String response = shortDog.speak(); | |
System.out.println("Short Dog says [" + response + "]."); | |
int humanAge = shortDog.equivalentHumanAge(); | |
System.out.println("If I were a human, I'd be " + humanAge); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment