So our teacher did a pretty bad job of explaining what objects are, so basically I have no idea how to use them, their format, what to input, etc. etc.
Our assignment is to basically create a point from (1,1) to (20,20), have the user guess a point, and see how far/close it is. Using objects.
His "Point" class is
Spoiler
class Point
{
// Field Summary
int x;
int y;
// Constructor Summary
Point(int x, int y)
{
//look on webpage
}
// Method Summary
public boolean equals(Object obj) // checks if two point objects hold equivalent data
{
Point other = (Point)obj;
return (other.x == x)&&other.y == y;
}
void move(int newX, int newY) // changes the (x,y) data of a point object
{
// look on website
}
public String toString() // returns character data that can be printed
{
return "("+x+" ,"+y+")";
}
}
Assume that the "//look on webpage" pretty much shows nothing useful. I'm supposed to put something there.
Steps he told us to do to complete the objective:
1. Copy paste the above file into jcreator (the program we use to code).
2. Insert some stuff in the //look on webpage part (aka fix the Constructor Summary).
3. Decompile.
4. Create another file and code and decompile and stuff to complete the objective.
So...how do I do this? Like I said, I have literally no idea to do this. What is the point of all of those summaries? What does void move mean? And to use it do I use point(dot)...what?
I'm going to read the textbook later when I get home in an attempt to grasp some understanding, but I have some downtime at the moment and I thought "Hey, this is a great moment to ask Southperry!"
I am assuming that you mean "compile" when you say "decompile". I am guessing that those summaries are just supposed to be descriptions of what the method does. You didn't ask about "public boolean equals" or "public String toString()" so I'm assuming that you're comfortable with the concept of functions that return a value. "void move" is just a function that doesn't return any value.
Is it supposed to be "public void move" or is it supposed to not have an access specifier?
You would call move like so:
Point point = new Point(5, 5); // Create the Point object
point.move(10, 15); // move it
I am assuming that you mean "compile" when you say "decompile". I am guessing that those summaries are just supposed to be descriptions of what the method does. You didn't ask about "public boolean equals" or "public String toString()" so I'm assuming that you're comfortable with the concept of functions that return a value. "void move" is just a function that doesn't return any value.
Is it supposed to be "public void move" or is it supposed to not have an access specifier?
You would call move like so:
Point point = new Point(5, 5); // Create the Point object
point.move(10, 15); // move it
I've had at least 3 sources telling me stuff about Objects (the Sun tutorial, my cousin and my instructor), but the idea never really clicked until I figured it out for and by myself, so I'm not sure how much help I'll be, but I'll relay my understanding of OOP here.
subject to conceptual derp
So you have an object. In general, imagine it like a block of clay that you can stick stuff onto; it is nothing until you define it to be something.
By defining, I mean giving it characteristics, behaviors and a name/type. Characteristics are probably what's referred to as fields, behaviors would be methods and the type is just that: type.
So imagine an object called a... line. What properties does it have? Well, length, that's it. What does it do? Idk, not much, just hanging around chilling.
Now imagine a... car, classic.
What properties does a car have? Well, tons. Let's go with the easy stuff: horse power, mileage, number of seats, number of wheels, number of doors, etc...
What does it do? Well it surely can move, at least. More specifically, it can speed up, slow down, turn left/right, reverse, jump, toot its horn, flash lights, etc...
What's its type? Well, car, duh.
So that's a Car class (declared like so: public class Car {}), a Car object is there when you make an instance (an example, a unit, a single identity, whatever clicks for you) of that model. If you've heard of Plato's theory of Forms, the class is like the form of the car while each car object you make is a particular.
(Then you can have subclasses of the class Car, like a Sedan class where num_doors is declared to be 4, for example. But this is for later.)
So, you have a class Point. What does it spit out? Well, a point. What properties does a point have? Probably its coordinate (x and y, as in your code). What does it do? I don't know... move to a new position? So the action of moving is defined as changing its property of coordinates.
E.G: pointA would have x = 2 and y = 5. point A.move(2,3) would change pointA's x and y into 4 and 8 (2 + 2 and 5 + 3). Syntax is given above by Spaz.
The action of moving changes pointA's states directly and do not return any information, thus it is declared void (no return type).
For comparison, a method that might return something for the Point class is distanceFromOrigin(), which takes the object's (pointA's) x and y information, do voodoo magic to it then return the distance from the origin.
Spaz Wrote:I am assuming that you mean "compile" when you say "decompile". I am guessing that those summaries are just supposed to be descriptions of what the method does. You didn't ask about "public boolean equals" or "public String toString()" so I'm assuming that you're comfortable with the concept of functions that return a value. "void move" is just a function that doesn't return any value.
Is it supposed to be "public void move" or is it supposed to not have an access specifier?
You would call move like so:
Point point = new Point(5, 5); // Create the Point object
point.move(10, 15); // move it
1) Oops, meant compile.
2) I have no idea what those are, either D=.
3) It's supposed to not have an access specifier, I guess. I have no idea, really. I just copy pasted.
So say I have variable pointA. To use this class, would I do something like, pointA.equals(....I'm guessing Point goes here)? And pointA.toString(...I'm also guessing Point goes here?)
Kalovale Wrote:I've had at least 3 sources telling me stuff about Objects (the Sun tutorial, my cousin and my instructor), but the idea never really clicked until I figured it out for and by myself, so I'm not sure how much help I'll be, but I'll relay my understanding of OOP here.
subject to conceptual derp
So you have an object. In general, imagine it like a block of clay that you can stick stuff onto; it is nothing until you define it to be something.
By defining, I mean giving it characteristics, behaviors and a name/type. Characteristics are probably what's referred to as fields, behaviors would be methods and the type is just that: type.
So imagine an object called a... line. What properties does it have? Well, length, that's it. What does it do? Idk, not much, just hanging around chilling.
Now imagine a... car, classic.
What properties does a car have? Well, tons. Let's go with the easy stuff: horse power, mileage, number of seats, number of wheels, number of doors, etc...
What does it do? Well it surely can move, at least. More specifically, it can speed up, slow down, turn left/right, reverse, jump, toot its horn, flash lights, etc...
What's its type? Well, car, duh.
So that's a Car class (declared like so: public class Car {}), a Car object is there when you make an instance (an example, a unit, a single identity, whatever clicks for you) of that model. If you've heard of Plato's theory of Forms, the class is like the form of the car while each car object you make is a particular.
(Then you can have subclasses of the class Car, like a Sedan class where num_doors is declared to be 4, for example. But this is for later.)
So, you have a class Point. What does it spit out? Well, a point. What properties does a point have? Probably its coordinate (x and y, as in your code). What does it do? I don't know... move to a new position? So the action of moving is defined as changing its property of coordinates.
E.G: pointA would have x = 2 and y = 5. point A.move(2,3) would change pointA's x and y into 4 and 8 (2 + 2 and 5 + 3). Syntax is given above by Spaz.
The action of moving changes pointA's states directly and do not return any information, thus it is declared void (no return type).
For comparison, a method that might return something for the Point class is distanceFromOrigin(), which takes the object's (pointA's) x and y information, do voodoo magic to it then return the distance from the origin.
Well, thanks, that makes a lot more sense than what my teacher tried to teach us X_X. Now I just need to figure out how to use this class Point. Starting to read the textbook, finally.
Corn Wrote:So say I have variable pointA. To use this class, would I do something like, pointA.equals(....I'm guessing Point goes here)? And pointA.toString(...I'm also guessing Point goes here?)
Point pointA = new Point(5, 6);
Point pointB = new Point(5, 6);
Point pointC = new Point(15, 1);