CSC152 2005F, Class 12: Object and Class Basics (1)
Admin:
* Convo tomorrow! Media! Whee!
* Cool talk today at 4:30 (food at 4:15): Visualization in statistics
* Homework due NOW! Not later!
* Beads
* Sending email
* Reading on numeric representation coming soon
Overview:
* What is a class?
* Key components of classes
* Example: Vectors in two space
* Java details
OOP: Define objects, create objects, tell objects to do things
Reminder (more or less): We define the general characteristics
of objects with *classes*. A class is a template for creating
objects.
Classes describe related objects
Classes we know already:
* String
* File
* PrintWriter
* BufferedReader
* Etc.
When defining a class, you need to think about
* Fields: The data we associate with individual objects
* Capabilities/Methods: The things these objects can do
(and, implicitly, the instructions for donig that stuff)
* Constructors: The instructions for building new copies of
these objects
* What data are necessary to build one of these objects?
* All these have to do with the objects themselves
* We also need to think about the characteristics of the class, itself
* It may have its own fields
* It may have its own methods
Let's ground this in an example: Vectors in two space
One representation of vectors: (X,Y) pair
* Fields are X (a double) and Y (a double)
integers provide huge range
but integers limit where in the plane you can describe
Another possible represntation: radius and theta
Another possible representation: Complex numbers
What kinds of things do we want to do with vectors?
* add: v1.add(Vector v2) returns a vector
* scale: v1.scale(double scaler)
* Could change the vector (mutator)
* Could return a new vector (observer)
* In scheme, we'd call the operations "scale!" and scale
* For the purposes of today's class, let's use the observer
v1.add(v2.scale(2))
* Determine the length/radius
radius: v1.radius() returns a double
* Take the dot product
v1.dotProduct(v2) returns a double
* Rotate
v1.rotate(angle)
* Others ...
To create a new vector, what information might we need?
* magnitude and direction
new Vector(5.0,2.0) meaning "create a vector of magnitude 5.0 with angle of 2.0 radians"
* x and y coordinates
new Vector(2.0,5.0) meaning "create a vector with x coordinate 2.0 and Y coordinate 5.0"
* Create the vector which is the identity for addition (i.e., the origin vector)
new Vector()
* Create a vector from a textual (string) description of the vector
* Very useful
new Vector("(3,5)")
new Vector("2 + 4i") ???
Are there fields and methods we care about for the class as a whole?
Hack that is common to Java: Given that we have two ambiguous
constructors, pick one and create a class method for the other
Suppose we choose that "new Vector(x,y)" means "create a vector from the origin to the point (x,y)"a
We add class method: createFromRadiusAndAngle(radius,angle)
You've seen this in Java already with the BigInteger.valueOf(int i) method
Once the design is done, you get to start coding
public class CLASSNAME
{
}
public class Vector
{
Field declarations look just like variable declarations (with a possible protection level)
double x;
double y;
Constructors have the form
public NAMEOFCLASS(NECESSARYPARAMS)
{
INSTRUCTIONS
}
Note: Parameters have the form TYPE NAME
public Vector(double initialx, double initialy)
{
x = initialx;
y = initialy;
}
public Vector(String description)
{
int openparen = 0;
int comma = description.indexOf(",");
x = Double.parseDouble(description.subString(openparen,comma));
...
}
}