2015-09-22 HW04

posted Sep 22, 2015, 8:49 AM by Samuel Konstantinovich   [ updated Sep 24, 2015, 5:36 PM ]
HW: I am sending out a piazza invite to everyone! Check your stuy email.
Make a reply to the intro post.   
  

Class Notes:

Goal: You down with OOP? (yeah you know me)

1   Greeter ( as scheme or python )
   
2   Classes - a class is a blueprint for an object, we define the instance variables, and the methods. 

3   re-greeter as a class:
//Greeter.java is a class that acts as a template for creating new Greeters
public class Greeter{
    public String message;
    public void greet(){
        System.out.println(message);
    }
    public void greet(String name){
        System.out.println(message + " " + name);
    }
}

//we have a separate file Driver.java (or any other name) 
//This class has a main method and will create Greeter objects
public class Driver{
    public static void main(String[]args){
        //DECLARE
        Greeter g1,g2;
        //INSTANTIATE (initialize an object)
        g1 = new Greeter();
        g2 = new Greeter();

        //set the message
        g1.message = "Hey sup!";
        g2.message = "Hola!";

        //make g1 and g2 use their greet method
        g1.greet("Bob");
        g2.greet("Dylan");
        g1.greet("Annie");

    }
}


Comments