I now have some of the basic and fundamental concepts behind me. I also have some basic syntax, semantics and pragmatics behind me. I believe my confusion is in not having a template "in my mind" of how and where to start after the problem domain has been defined. I have come up with a basic template that I will try and use as a guide for the next little bit. Cam, feel free to revise and add opinions.
1) Create object classes
-- Initialze and declare object properties
-- Create methods that define properties of that object
2) Create the main method
-- Define objects and their arguments
-- Create instance(s) of a method(multiple methods)
-- Pass instance(s) to the method(s)
3) Create method(s)
-- Perform data conditioning
-- return control back to calling method
2 comments:
1) Create object classes
-- Initialze and declare object properties
-- Create methods that define properties of that object
I'd add in here that you often need custom constructors too.
So, definte you class. Declare properties that the class HAS. In a class, you just declare them, you don't actually initialize them. The constructor allows the client to pass values that actually create instances and initialize values. Then, you have methods that work on the properties of the class.
Just remember our circle:
public class Circle {
int radius; //declare the has-a properties
/* code a constructor that initializes*/
public Circle(int r) {
radius = r;
}
/* provide methods that do stuff with props */
public int getArea() {
return 3*radius*radius;
}
}
That's the essence of a class there.
-- Define objects and their arguments
-- Create instance(s) of a method(multiple methods)
-- Pass instance(s) to the method(s)
I might reword a bit. In your programs you create instances:
Circle c = new Circle(5);
Circle d = new Circl(10);
You don't really create instances of methods. You invoke methods on existing instances. So you can invoke the getArea method on a circle:
System.out.println(d.getArea());
You can create other instances and invoke their methods, and sometimes those methods take arguments, which are often instances of classes you create.
CirclePrinter cp = new CirclePrinter();
cp.compareAreas(c, d);
Here, the instance of the fictitious CirclePrinter class has a method invoked. It's not an instance of a method, but a method being called on the instance named cp. The method is passed two circles as arguments.
Instances are certainly passed to methods.
Post a Comment