Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎

2017-09-27 HW

posted Sep 27, 2017, 9:59 AM by Samuel Konstantinovich   [ updated Sep 28, 2017, 9:56 AM ]
Goal: You down with OOP?

The notes for the next few days are not my original work.
I will curate the Oracle website's notes on objects for you!
If you want the full set of Oracle notes you can find them here:

Object Oriented Programming (OOP)
This programming paradigm is very different from what you are used to. 
Four Tenets of OOP
  • Abstraction.
  • Encapsulation.
  • Inheritance.
  • Polymorphism.

Oversimplification: The use of Objects to organize data and functions is how we accomplish these four goals.


abstraction - a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.
encapsulation - a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.

Classes are blueprints for objects.
They contain 
-Fields (instance variables)
-Methods
-Constructors (special 'methods')


Example: 
(look at the overall idea, do not try to understand everything)
public class Bicycle {
        
    // the Bicycle class has
    // three fields
    public int cadence;
    public int gear;
    public int speed;
        
    // the Bicycle class has
    // one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
        
    // the Bicycle class has
    // four methods
    public void setCadence(int newValue) {
        cadence = newValue;
    }
        
    public void setGear(int newValue) {
        gear = newValue;
    }
        
    public void applyBrake(int decrement) {
        speed -= decrement;
    }
        
    public void speedUp(int increment) {
        speed += increment;
    }
        
}

Declaring Classes

You've seen classes already:

class MyClass {
    //method declarations like main, and your methods
    //so far always public and static
}


Now you've seen classes defined in the following way:

class MyClass {
    // fields
    //constructors
    // method declarations
}

There are several kinds of variables:

  • Member variables in a class—these are called fields.
  • Variables in a method or block of code—these are called local variables.
  • Variables in method declarations—these are called parameters.

Access Modifiers

The first (left-most) modifier used lets you control what other classes have access to a member field. For this course consider only public and private Other access modifiers exist like protected.

  • public modifier—the field is accessible from all classes.
  • private modifier—the field is accessible only within its own class.
In the spirit of encapsulation, it is common to make fields private. This means that they can only be directly accessed from the Bicycle class. We still need access to these values, however. This can be done indirectly by adding public methods that obtain the field values for us:

Here is what public variables would look like:

public class Point {
    //public means we can access x and y
    //from any other class that creates a point.
    public int x;
    public int y;

    // a constructor!
    public Point(int a, int b) {
	x = a;
	y = b;
    }
}

public class CreateObjectDemo {

    public static void main(String[] args) {
        Point p,q,r;
        p = new Point(1,2);
        q = new Point(3,4);
        r = q;
        r.x = 0; //public means we can access x and y from this class

        System.out.println("p: " + p.x + "," + p.y);
        System.out.println("q: " + q.x + "," + q.y);
        System.out.println("r: " + r.x + "," + r.y);
    }
}		




HW Coding Bat:
AP-1 (the first 8 problems by Friday)
scoresIncreasing 
scores100 
scoresClump 
scoresAverage 
wordsCount 
wordsFront 
wordsWithout  (CORRECTED the name sorry!)
hasOne
Comments