Wednesday, July 14, 2010

Introduction to Java Part 3: Classes, Objects, and Methods - Java

Just some notes from a Computer Science class on Java
Account checking = new Account();
Account = class
Checking = object
Class:
-Blueprint for an object…definition for the object.
-Can be many objects for one class once blueprint is made
-Nouns and verbs
-Public classes can be called in other classes, Private classes cannot.
class Window
Nouns (Variables):
-Location
-Size
-Contents
-Color
Verbs (Methods):
-Close
-Change Size
-Move

Object:
-Made from the blueprint (class)

Methods:
-Verbs of a class
-Setters
public void deposit(int amount) //method
{
   balance += amount //variable
}
void = not returning anything. Not giving anything back.
-'void' is the return type. It means nothing is returned by this method. Alternately, it could say 'int' or 'string' or any type of variable.

savings.balance();
-Calls the method balance from the object savings

Private Variables/Attributes:
-Nouns of a class
Ex.
class Account
Nouns:
-balance
-interestRate
Verbs:
-deposit
-withdraw
-postInterest
-getBalance

Constructor:
Ex. Of constructors
public Account() //Default Values
{
   balance = 0;
   interest = 0;
}

public Account(int bal, int rate) //Paremeters: bal, rate
{
  balance = bal;
  interest =rate;
}

Parameters/Arguments:
Account savings = new Account(200,2);
200 & 2 = Parameters
-Set properties of a class

Accessor:
Checking.getBalance();
-Returns a value, doesn’t set anything
-Getters

0 comments:

Post a Comment