Wednesday, June 27, 2012

A Beginner's Example

I know, I know... No posts in forever... I'm sorry and I really don't have any good excuses except that I've been lazy... but I'll try to be more consistent now.

If you are not a beginner, you certainly don't need to read the rest of this post. If you do continue please feel free to mock any stupid mistakes I may have made.

Anywho... I come from two wonderful parents who are both actually very into computers. Growing up we always had to have the latest and greatest in technology and I don't think we ever had less than 3 working computers and many different parts. My dad has always been into the security aspect of programs which has never been one of my interests. My mother actually was a programmer back in the day of COBOL and PASCAL, but since she's had kids she's been a stay at home mom and the programming languages and techniques are very, very different.

Recently she's wanting to get back into coding and asked me for some tips and advice on where to start. I suggested a book that I had used for my Java class and sent her a few examples I had quickly created that were simple. This is one of the examples I sent. It's simply a Dog class with a few variables and functions.

class Dog
{
//These are private member variables
//They can ONLY be accessed by the Dog 

   //classes functions
private String dogName;
private String dogSound;
private String breed;
 

   //This is what is called the Constructor
//This gets called when a new instance of 

   //the class is created
public Dog(String name, String noise)
{
dogName = name;
dogSound = noise;
}
public Dog(String name, String noise, String b)
{
dogName = name;
dogSound = noise;
breed = b;
}
 

   //This function is going to display the noise 
   //the dog makes when it barks
public void bark()
{
System.out.print(dogName + " says: " + dogSound);

      System.out.print(" " + dogSound + "!\n");
}
public String getBreed()
{
return breed;
}
}



The Dog Driver class just creates a couple of instances of the Dog class to demonstrate how to use it.

class DogDriver
{
public static void main (String args[])
{
//These to lines call the Constructor of the Dog 
      //class and create new instances of the Dog class
Dog chihuahua = new Dog("Joey", "Yip");
Dog germanShepard = new Dog("Max", "Woof");
Dog corgi = new Dog("Sam", "Moo", "Corgi");
Dog poodle = new Dog("Lulu", "Bark");

//These two lines call the bark function of the Dog 
      //class and make the Dogs bark
chihuahua.bark();
germanShepard.bark();
poodle.bark();
corgi.bark();

System.out.println(corgi.getBreed());
}
}


It's going to be an interesting experience trying to get her away from the linear concept and more comfortable with the object oriented approach, but I think it'll be a good way for me to brush up on my rusty techniques and a great review for me.

//END OF LINE