/* 6.914, Example 4.3 0) Classes are ways to create new types of objects. These objects can have attributes that are specific to them and methods that can effect changes in them. They are basically ways to create reusable objects with specified characteristics and abilities. Each class should have at least one constructor, which is a special type of method called when a new instance of a class is created. This constructor should basically give information about that instance to the new object. For instance, if you were creating an object of type Person, you could pass a name and a height in inches to the constructor. You declare a class using the following notation: class Person { int heightInInches; String name; Person (String n, int h){ name = n; height = h; } } Then, you create a new instances of that class by the following notation (which will look vaguely similar from your creation of colors): void draw{ Person myPerson0 = new Person("Bob", 70); Person myPerson1 = new Person("Alice", 62); } There will now be two objects of type Person in your program. Let's add some methods to class Person to allow you to get information about the person: class Person { int heightInInches; String name; Person (String n, int h){ name = n; heightInInches = h; } // these parts are new int getHeightInInches(){ return heightInInches; } String getName(){ return name; } } Now you can access that information about your Persons in draw(): void draw{ Person myPerson0 = new Person("Bob", 70); Person myPerson1 = new Person("Alice", 62); println("myPerson0 "+myPerson0.getName()+"'s height: "+myPerson0.getHeightInInches()); println("myPerson1 "+myPerson1.getName()+"'s height: "+myPerson1.getHeightInInches()); } You can also create methods that allow you to change attributes of your person: // these parts are new void setHeightInInches(int newHeight){ height = newHeight; } void setName(String newName){ name = newName; } Now you can change information about your person: void draw() { Person myPerson0 = new Person("Bob", 70); println("myPerson0 "+myPerson0.getName()+"'s height: "+myPerson0.getHeightInInches()); myPerson0.setName("Bobby"); println("myPerson0's new name: "+myPerson0.getName()); } Here are some other methods you could add to the Person class: void grow(){ heightInInches += 2; } void shrink(){ heightInInches -= 2; } boolean amIAHole(){ if (heightInInches < 0){ return true; } else { return false; } } One way to consolidate that last method would be this: boolean amIAHole(){ return (heightInInches < 0); } To call one of these methods, you would use myPerson0.grow(), myPerson0.shrink(), or boolean isHole = myPerson0.amIAHole(). Here's a class 1) Your classes can inherit from other classes. This means that they will get the attributes, methods, etc, belonging to the earlier classes. They can also have their own attributes/methods specific to the new, more specific class. To use the constructor from the superclass, pass back the new attributes to that constructer by using the super command. For instance, let's create a new class, MitStudent, that's an extension of Person. This class will have one new attribute: int numOfPsetsDue. Class MitStudent extends Person{ int numOfPsetsDue; MitStudent(n, h, psets){ super(n, h); numOfPsetsDue = psets; } void assignAnotherPset(){ if (isHosed()){ println("Oh no! I'm already hosed!"); } else { println("I'm hosed now..."); } numOfPsetsDue++; } boolean isHosed(){ return (psets > 0); } } If you write a method with the same name as a method in a superclass, the one in the subclass takes precedence. See the "WhatAmI" function below. An MitStudent IS a type of Person. If you had a function that took a Person as a parameter, you could pass an MitStudent, and the MitStudent would be treated only as a Person in that context. */ void setup(){ noLoop(); } void draw(){ Person myPerson0 = new Person("Bob", 70); MitStudent myStudent1 = new MitStudent("Eve", 59, 0); Person myPerson1 = myStudent1; println(myPerson0.getName()); println(myPerson0.whatAmI()); println(myPerson1.getName()); println(myPerson1.whatAmI()); } class Person { int heightInInches; String name; Person (String n, int h){ name = n; heightInInches = h; } int getHeightInInches(){ return heightInInches; } String getName(){ return name; } void setHeightInInches(int newHeight){ height = newHeight; } void setName(String newName){ name = newName; } void grow(){ heightInInches += 2; } void shrink(){ heightInInches -= 2; } boolean amIAHole(){ return (heightInInches < 0); } String whatAmI(){ return "Person"; } } class MitStudent extends Person{ int numOfPsetsDue; MitStudent (String n, int h, int psets){ super(n, h); numOfPsetsDue = psets; } void assignAnotherPset(){ if (isHosed()){ println("Oh no! I'm already hosed!"); } else { println("I'm hosed now..."); } numOfPsetsDue++; } boolean isHosed(){ return (numOfPsetsDue > 0); } String whatAmI(){ return "MIT Student"; } }