Skip to content

Instantly share code, notes, and snippets.

@adamchalmers
Created May 5, 2015 05:29
Show Gist options
  • Save adamchalmers/67954275945157330cef to your computer and use it in GitHub Desktop.
Save adamchalmers/67954275945157330cef to your computer and use it in GitHub Desktop.
INFO1103 students: this demonstrates the basics of making a class.
public class Pet {
// Instance variables
// Each pet object will have its own copies of
// these four variables
public String name;
public int age;
public String species;
public String[] nicknames;
private int numberOfNicknames = 0;
// Constructor
// This gets called whenever we make a new Pet
public Pet(String n, int a, String s) {
name = n;
age = a;
species = s;
nicknames = new String[10];
numberOfNicknames = 0;
}
// Cats go meow, dogs go woof
public void makeNoise() {
if (species.equals("Cat")) {
System.out.println("Meowww!");
} else if (species.equals("Dog")) {
System.out.println("Woof!");
} else {
System.out.println("???????");
}
}
// Adds a new nickname to the pet
public void giveNickname(String newNickname) {
nicknames[numberOfNicknames] = newNickname;
numberOfNicknames++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment