Search This Blog

Sunday, February 13, 2011

Next assignment - space shuttle course alteration. JJ, Plot a line using two points

Master Jedi has asked I build a new class to plot a line using the classes we have been building regarding points, lines, rectangles, squares and triangles.  Hint - you need two sets of double parameters.  x1, x2 (starting point of line) and y1, y2 (ending point of line). 

2 comments:

LarryGutt said...

Correction - plotting a line is not a good choice of words. How about defining a line.

Cameron said...

Me: Hey Larry. Create a Class named Question that has three String properties.

Larry: Okay Cam...

public class Question {
String query;
String answer;
String option;
}

Me: Now, Larry, create a class named Point that has two ints.

Larry: Okay. Here you go!

public class Point {
int x;
int y;

}

Perfect. You're really getting this 'has-a' type stuff. Now create a class named Exam that has a Question.


Larry: Okay Cam:

public class Exam {
String question;
}

Me: No Larry, that Exam class you created has an object of type String. I want it to have an object of type Question.

Larry: Oh, you mean like this:

public class Exam {
int question;
}

Me: No! That is now an Exam class that has an object of type int. I want it to have an object of type Question!

Larry: Oh, you mean like this:

public class Exam{
SledgeHammer question;
}

Me: NO! Now the exam class has an object of type Sledgehammer. I want it to have an object of type Question.

Here's how you do it Larry:

public class Exam {
Question q;
}

See? Just like the Question class had three properties of type String, this Exam class has a property of type Question. Get it?

Larry: Oh, I get it.

Me: Great. Now, next, create a class named Line that has two points, a startPoint and an endPoint.

Larry: No problem. That's easy:

public class Line {
String startPoint;
String endPoint;
}

Me: No Larry! That's a line that has two objects of type String! I want it to have two objects of type Point.

Larry: Oh, of type Point! Now I get it. Like this, right, with two points:

public class Line{
int startPoint;
int endPoint;
}

Me: Larry! That Line class has two objects of type int. I want it to have two objects of type Point. The Point class has two objects of type int, the Line class has two objects of type Point.

Larry: Oh, like this:

public class Line {
Point startPoint;
Point endPoint;
}

Me: Yes! Exactly! The line class has two objects of type point! They are not ints, they are not Strings and they are not SledgeHammers. They are objects of type Point! The variable is declared as being of type Point, not String or int or boolean or anything like that.

Larry: Well, if that's what you wanted, why didn't you just say so?

Post a Comment