473,378 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Please help with my java hw that leaves me confused.

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
6 1480
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
3,652 Expert 2GB
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

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

Similar topics

2
by: Clyde Ellul | last post by:
Hi there. I need to write a simple program that reads a GIF image from an input stream, resizes it, then writes it back to an output stream in the same format (GIF). (JPEG input/output is good...
16
by: Thomas G. Marshall | last post by:
This message is sent to these newsgroups because they are no longer valid: comp.lang.java comp.lang.java.api comp.lang.java.bugs comp.lang.java.misc comp.lang.java.setup comp.lang.java.tech
4
by: Don Grover | last post by:
I hope some one can help, I have a html table that is created with asp that has a row of repeated buttons down the side. these call a page passing a query string with invoice number. I need to...
14
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.