Search This Blog
Saturday, February 19, 2011
New Project - Online Exam, Logic Diagram
Cam and I have taken some time out the last couple of days to chart the logic diagram for the online exam we are about to program and publish. I have had some differences of opinion in the logic flow and, as Cam stated, there is more than one way to code the program. I am trying to pay attention and grasp and understand his logic flow, which in itself is pretty easy. Towards the end, Cam's experience became apparent with the addition of certain classes to help organize this flow for multiplicity reasons later on. I will try and get a good copy, scan it and publish it here . . .
Subscribe to:
Post Comments (Atom)
9 comments:
How do you create a Square?
Square s1 = new Square();
The first half of that line of code says "I need a Square that I am going to refer to with the name s1."
The second half creates a Square by calling the default constructor. The code would assume that the Square class looks something like this:
public class Square{
int height;
int width;
public void getArea() {
return heigh * width;
}
}
Notice that there is no method named Square() in the class yet? That's because, if a class has no custom-coded constructor, you can call the 'default constructor' on it. Of course, the default is shit, because it initializes primitive types to zero, and reference types to null.
It often makes sense to have non-default constructors. That way, people can specify values for the properties of an instance when they create a class. So, if a Square has two properties of type int, what would the constructor look like?
public class Square{
int height;
int width;
public Square(int h, int w) {
height=h;
width = w;
}
public void getArea() {
return heigh * width;
}
}
Now, when someone creates a Square, they do it using the non-default constructor. Here's how it would look:
int mycrazyex = 10;
int iwonderwhy = 20;
Square s2 = new Square(mycrazyex, iwonderwhy);
That does it using variables. You could also use literals:
Square s2 = new Square(10,20);
What is the height of s2? What is the width of s2? You can probably tell just by looking at the constructor.
Here's the other neat thing. When you create a non-default constructor, you are insisting that people use it, as opposed to using the default one. So, when you create a non-default constructor, the default one is no longer available. So, the following code would not compile now:
Square s1 = new Square();
By adding in a non-default constructor, you are demanding that when people create a Square, they specify a height and width, and the compiler will help to enforce that for you.
Questions:
Why does it make sense for classes to have non-default constructors?
If a class has three instance properties, how many arguments do you think the constructor might have?
If a Point has an x and y coordinate, how many properties should a point have?
What would a non-default constructor for a point look like?
How would you crate a point using a non-default constructor?
If a Line has two Points, how many properties would a Line have?
What would a non-default constructor look like for the Line class?
How would you create an instance of a Line?
Question:
In the scenario described above, why can the separately named instances (Square s1 (default) and Square s2 (non-default)) of the constructor not exist simultaneously because of their individually named instances (s1 and s2)? I know you stated that once a non-default constructor is created, the default constructor has to be destroyed.
So, the instances are created by calling a constructor. Which constructor is valid to call? You need to open up the class for the Square to see what is coded in there at the time that you are creating your instances.
By the way, a developer can always code a default AND many non-default constructor, so both are available. When you have multiple methods with the same name but different method signatures, that's called method or constructor OVERLOADING.
Answers:
It makes sense to have non-default constructors for a number of reasons. First, the client is forced to pass in pertinent data. Second, the amount of code required is lessened. Third, by creating non-default constructors, different instances of the class can be passed to the constructor. If a class has three instance properties, three arguments should be passed to the constructor.
A point should have two properties. x1,y1. A point is a two dimensional location representation. A constructor for a point may look like this: LITERAL - Point p = new Point(3,4); OR:
public class Point{
int ex;
int why;
public Point(int x, int y){
ex = x;
why = y;
}
public void plotPoint(){
return x,y;
}
}
TO CREATE:
int ex;
int why;
Point p1 = new Point(ex, why);
I found an interesting read that, I believe, ties in with our online exam project:
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details.
To see why, consider that there might be 1,000 calls to a getX() method in your program, and each call assumes that the return value is of a particular type. You might store getX()'s return value in a local variable, for example, and that variable type must match the return-value type. If you need to change the way the object is implemented in such a way that the type of X changes, you're in deep trouble.
If X was an int, but now must be a long, you'll get 1,000 compile errors. If you incorrectly fix the problem by casting the return value to int, the code will compile cleanly, but it won't work. (The return value might be truncated.) You must modify the code surrounding each of those 1,000 calls to compensate for the change. I certainly don't want to do that much work.
One basic principle of OO systems is data abstraction. You should completely hide the way in which an object implements a message handler from the rest of the program. That's one reason why all of your instance variables (a class's nonconstant fields) should be private.
If you make an instance variable public, then you can't change the field as the class evolves over time because you would break the external code that uses the field. You don't want to search 1,000 uses of a class simply because you change that class.
This implementation hiding principle leads to a good acid test of an OO system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other OO features.
Holy crap, I just used an HTML tag to make the copied text italicized . . . . 8)
Yes, we must slowly indoctrinate you in HTML.
Just be careful with this method:
public void plotPoint(){
return x,y;
}
A void method doesn't return anything. So, here, you'd need to pick a return type. And of course, you can only return one thing from a method. It can be anything, but it can only be one thing. So, you can't just "return x,y" because you're trying to return two things. So, how would you do it? You'd have to return the actual point.
Indeed, all instance variables in Java should be made private, so nobody can see them. As we like to say, in the Java world, we don't expose our privates. :)
I believe this is how it is done?
public class Point{
int ex;
int why;
public Point(int x, int y){
ex = x;
why = y;
return Point;
}
}
Post a Comment