Created
May 5, 2015 05:29
-
-
Save adamchalmers/67954275945157330cef to your computer and use it in GitHub Desktop.
INFO1103 students: this demonstrates the basics of making a class.
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 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