By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Please help with my java hw that leaves me confused.

P: 3
im a java high school student and im just a beginner and my teacher gave me this OOP scenario that i thought about for so long but didnt know how to design a program for it.
The scenario is: "A Quadrilateral has LineSegments. A LineSegment is made up of 2 Points (with coordinates).
Then my task was to think of verbs that fit and could be added to this class and code methods for the class.
I thought about it and im not sure whether to use the Point class in the program for this or use a LineSegment class? Im so confused..
Can someone please help me. I will be so grateful if anybody can give me any clues.
Dec 30 '06 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
im a java high school student and im just a beginner and my teacher gave me this OOP scenario that i thought about for so long but didnt know how to design a program for it.
The scenario is: "A Quadrilateral has LineSegments. A LineSegment is made up of 2 Points (with coordinates).
Then my task was to think of verbs that fit and could be added to this class and code methods for the class.
I thought about it and im not sure whether to use the Point class in the program for this or use a LineSegment class? Im so confused..
Can someone please help me. I will be so grateful if anybody can give me any clues.
Start with the Point class, it is the one that does not require any other class(from the ones given) and then write code for the LineSegment and finally the Quadrilateral .
Post the code at each stage so that we can see if you are on the right path.
Dec 30 '06 #2

P: 3
public class LineSegment
{
private Point p1;
private Point p2;

public LineSegment()
{
p1 = new Point();
p2 = new Point();
}
........
That's what i have so far, I'm not sure if its right though, but the real problem for me is that how to use the LineSegment and Point classes for the Quadrilateral class.

public class Quadrilateral
{
private ----------------- _ im so confused about how to use these classes for the Quad. Should I use the point class in the Quad class, but then again, it says that the quad has LineSegments and the Points are part of the LineSegment class. im so confused.
Dec 30 '06 #3

10K+
P: 13,264
public class LineSegment
{
private Point p1;
private Point p2;

public LineSegment()
{
p1 = new Point();
p2 = new Point();
}
........
That's what i have so far, I'm not sure if its right though, but the real problem for me is that how to use the LineSegment and Point classes for the Quadrilateral class.

public class Quadrilateral
{
private ----------------- _ im so confused about how to use these classes for the Quad. Should I use the point class in the Quad class, but then again, it says that the quad has LineSegments and the Points are part of the LineSegment class. im so confused.
Once you get the Point and and LineSegment classes right, the other class will be easy to write as well. You started by writting LineSegment. Do you have the Point class? Otherwise the line class won't compile.
Dec 30 '06 #4

P: 3
This is the Point class that my teacher gave me:

public class Point
{
private int x;
private int y;

public Point(int anX, int aY)
{
x = anX;
y = aY;
}

public int getX()
{
return x;
}

public int getY()
{
return y;
}

public String toString()
{
return "[" + x + ", " + y + "]";
}
}

And I think the LineSegment class would be like this:?
public class LineSegment
{
private Point p1;
private Point p2;

public LineSegment()
{
p1 = new Point();
p2 = new Point();
}

// i decided to add my own method here using the distance formula

public double getDistance(p1, p2)
{
return (Math.sqrt((Math.pow(2, (p1.getX() - p2.getX())) + (Math.pow(2,
p1.getY() - p2.getY() )));
}
}

Okay, so i guess thses are the classes for Point and LineSegment, although im very insecure about the LineSegment class that i made up.
Now that i have those done, i honestly still dont know how to use these classes properly for the Quadrilateral class.
All I have so far is "public class Quadrilateral" and the rest is a blur b/c i dont know how to complete it. I dont know what to do and its driving me nuts.
Dec 30 '06 #5

10K+
P: 13,264
This is the Point class that my teacher gave me:

public class Point
{
private int x;
private int y;

public Point(int anX, int aY)
{
x = anX;
y = aY;
}

public int getX()
{
return x;
}

public int getY()
{
return y;
}

public String toString()
{
return "[" + x + ", " + y + "]";
}
}

And I think the LineSegment class would be like this:?
public class LineSegment
{
private Point p1;
private Point p2;

public LineSegment()
{
p1 = new Point();
p2 = new Point();
}

// i decided to add my own method here using the distance formula

public double getDistance(p1, p2)
{
return (Math.sqrt((Math.pow(2, (p1.getX() - p2.getX())) + (Math.pow(2,
p1.getY() - p2.getY() )));
}
}

Okay, so i guess thses are the classes for Point and LineSegment, although im very insecure about the LineSegment class that i made up.
Now that i have those done, i honestly still dont know how to use these classes properly for the Quadrilateral class.
All I have so far is "public class Quadrilateral" and the rest is a blur b/c i dont know how to complete it. I dont know what to do and its driving me nuts.
Going very much in right direction.

In the LineSegment class you did not provide a way of setting the points for that class. Can do this using setMethods or using a constructor
Expand|Select|Wrap|Line Numbers
  1.  public LineSegment(Point p1, Point p2) 
  2. {
  3. this.p1 = new Point();
  4. this.p2 = new Point();
  5. }
  6.  
Might also want to add getPoint1 and getPoint2 methods for that class.
Get distance sounds better as getLength().

The Quadrilateral class is now formed from the lines and points.

You can have several constructors for this class for all the different methods of forming a Quadrilateral

e.g
Expand|Select|Wrap|Line Numbers
  1.  
  2. public Quadrilateral(LineSegment side1, LineSegment side2, LineSegment side3, LineSegment side4)
  3.  
Jan 2 '07 #6

Ganon11
Expert 2.5K+
P: 3,652
In your LineSegment class, you have one default constructor that takes no arguments and sets both points (p1 and p2) to new Point objects, also using the default constructor. There are two problems here. First, there is no default constructor in the Point Class - you need to pass the constructor two integers representing the x and y values.

Next, when defining a LineSegment, you will want the endpoints to be defined, so it would make more sense to have a LineSegment constructor taking the x and y values of each point, rather than no constuctors.
Jan 2 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.