473,769 Members | 8,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help with my java hw that leaves me confused.

3 New Member
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 1500
r035198x
13,262 MVP
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
jukebox239
3 New Member
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 MVP
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
jukebox239
3 New Member
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((Mat h.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 MVP
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((Mat h.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 Recognized Expert Specialist
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
11257
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 too, but GIF is preferrable). JAI can do this sort of but can't write to GIF format. Also, it looks too complicated for what I need; I'm a bit in a hurry... Is there a simple way of doing what I want? Is there a free,
16
3961
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
1847
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 get a Java Yes/No dialog up and on yes call the hyperling query string. I can do this with a single hyperlink but not passing on the query string and invnumber Here is my Response Write link
14
5693
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 places on application code. Take, for instance, this C++ construct: class foo { char *m_name;
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.