I've a assignment question,

A point on two dimensional plane can be represented by two numbers: an x coordinate and y coordinate. Eg (4,5). The sum of two point can be defined as a new point whose x coordinate is the sum of the x coordinates of two points similarly y coordinate is the sum of the y coordinates of two points.

and i've create the following code,

``````class Dimensions {
int x_coordinate;
int y_coordinate;
public void print(){
System.out.println ("(x,y) = " + "(" + x_coordinate + "," + y_coordinate + ")");
}
public Dimensions (int i, int j){
x_coordinate = i;
y_coordinate = j;
}
}
class abc {
public static void main (String args[]){
Dimensions first = new Dimensions (5,10);
Dimensions second = new Dimensions (10,10);
first.print();
second.print();
}
}``````

it is working fine and printing two x-coordinated and two y-coordinates but how can i add two points of x-coodinate and two points of y-coordinate ??
i want to sum it in class Dimensions and return the value in main

3
Contributors
14
Replies
15
Views
8 Years
Discussion Span
Last Post by Xufyan
Featured Replies
• 1
Ezzaral 2,714   8 Years Ago

Create a method in your Dimensions class to add a Dimension to the instance and return a new Dimension with the result[code]public Dimension add(Dimension d){ ... add dimensions and return the result ... }[/code]

how can i add two points of x-coodinate and two points of y-coordinate ??

Can you explain what you are trying to do.
Say you have two points given by x1, y1 and x2, y2
What does "adding" these two points mean? Can you give an example?

yes...
i want to do this,

x1+x2 and y1+y2 and then print the result as a third co-ordinate (x1+x2,y1+y2)

Seems possible. What is your problem?
Can you show the code you are having a problem with and any error messages you get.

print the result

What does that mean? Can you show an example given point1 and point2,
add them and show what you want as the printed result?

The example code is already mentioned in my First post,
that code gives following output,

(x,y) = (5,10)
(x,y) = (10,10)

i want following output,

(x,y) = (5,10)
(x,y) = (10,10)
(x,y) = (sum of x coordinates of both points,sum of y coordinates of both points)

i don't have any idea of doing this that is why i don't have any code to get the desired output.

Would the last line of the output be:
(x,y) = (15,15)

Yes that must be the output line,
but if i change the x and y coordinates of 1st and 2nd point then the third coordinate should be automatically changed , it should not remain static i.e (15,15)
for example,
if i change first point to (5,5) and second point to (3,3) then third point should become (8,8)

You would need a "listener" feature in the first point that would tell all dependent points that it has been changed. Each dependent point would have to call the point that it is dependent on and register as a listener to be called when the values of the first point are changed so that it can change its values.

This seems a bit complicated for a beginning programmer assignment.

what is Listener ??
can you give me an example ?

For example in pseudo code:
Dimensions first = new Dimensions(3, 4);
Dimensions second = new Dimensions(4,5);
first.addDimChangeListener(second); // add listener for changes to first
...
first.setDims(6, 6); // Change the values in first.
// first's setDim method would call second's listener method to tell it that first's values have changed

Your assignment can't be using this technique.
Can you explain what you meant when you said:

if i change the x and y coordinates of 1st and 2nd point then the third coordinate should be automatically changed

Edited by NormR1: n/a

I mean if i store coordinates using Keyboard input.
and Sorry i didn't understand the listener and our teacher didn't taught it yet so we are not allowed to use it in a program

Create a method in your Dimensions class to add a Dimension to the instance and return a new Dimension with the result

``````public Dimension add(Dimension d){
... add dimensions and return the result ...
}``````
Votes + Comments
thnX :)

how to add both coordinates separation ??? i mean , first add both x coordinates and then both y coordinates

Just like you wrote it above (x1+x2, y1+y2).

Thanks :) Problem Solved

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.