Results 1 to 8 of 8
Thread: Java Program
- 08-18-2009, 02:37 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 10
- Rep Power
- 0
Java Program
Hi guys
Im completely stumped with this program Im not really sure whats going on I hope one of you can help! The question Im doing is:
(e)Write a class P o i n t to represent 2-dimensional points (where the point
coordinates are of type double). The class should contain the two
standard constructors, i.e. one with no arguments, and one with an
argument for each instance variable. It should also include instance
methods get for reading the coordinates of a point from the keyboard,
and t o s t r i n g for computing a string representation of the point in a
form typified by (3.14,2.71).
(f)Add to class P o i n t of part (e) an instance method called m i d p o i n t
which returns an object of type P o i n t representing the midpoint of two
points, where one of the points is supplied as a parameter and the
other is ,the current point (i.e. the point provided by the local instance
variables). Note that m i d p o i n t returns a new P o i n t object.
This is what I currently have
class Point
{
double x,y;
Point(double xval, double yval)
{
x = xval; y = yval;
}
Point midpoint(Point p)
{
;
}
void get()
{
System.out.println("Enter co-ordinates:");
x = Console.readDouble();
y = Console.readDouble();
}
void toSentence()
{
String points = (x + "," + y);
System.out.println(points);
}
}
class Question1e
{
public static void main(String[] args)
{
Point p = new Point(2.0,4.5);
}
}
I know I've left a lot out but I've tried different things and just cant figure it out!
Any help would be greatly appreciated!
- 08-18-2009, 03:22 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 18
- Rep Power
- 0
For the midpoint you just need the arithmetic mean of the coordinates.
(ie. to get the x-coordinate of the midpoint, add together the x-coordinates of the two end points, and divide by 2)
I think this should do it:
Java Code:Point midpoint(Point p) { double ex = (p.x + x)/2; double wy = (p.y + y)/2; return new Point(ex,wy); }Last edited by DiamondDog; 08-18-2009 at 03:24 PM.
- 08-18-2009, 03:31 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
I'll try to guide you to the correct solutions, but as it is a school assignment, I'm not going to give out full solutions.
Before I start, you need to rename toSentence() to toString();
For your midPoint method, you need an algorithm for getting the middle of two points. This is very simple, as its just getting the average of your doubles and putting them in a point. Try using this method:
call this with the x/y coordinates from your Points, create a new point with the values returned, and you've got the Object to return.Java Code:public double getMiddle(double x, double y){ return (x+y)/2; }
The other thing you're missing is a Point() constructor (the no-arg constructor). It should set x and y to 0.0. you could do that explicitly, or through this(0,0) (same as calling a super-constructor, but for calling other constructors of the class)
And note that, for anyone using an IDE, at least Eclipse, your get() method wont work. (IDE's tend not to support Console). Of course, any other method is more difficult to do, and if you've been taught to use Console, feel free. Just remember that for a lot of developers, running code using Console is a pain, because we have to run it through the command line, which we can do, but don't necessarily want to. Just for future reference in your posts, I at least would replace Console methods with my own to avoid command line. (Part of that being my computer can never find javac or java...) And I make no promises to switch it back. Leave it for now, but remember that when you distribute source code with Console, it's less portable, as it only works through shell/command line, not so well with IDEs.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 08-18-2009, 03:32 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 10
- Rep Power
- 0
Thanks very much for your help that makes sense but I dont understand how I feed the user input into that? The get method is supposed to read in input...Console is a class that reads user input
Thanks
- 08-18-2009, 03:39 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 10
- Rep Power
- 0
Hi Singing Boyo,
Its not actually for an assignment Im studying for a test next week and could never get my head around this! Yes sorry I know there are probably other ways to read information other than console but we have not actually been taught any at this stage so please excuse this.
So you're saying I need to make a Point as in
Point()
{
x = 0.0;
y = 0.0;
}
Is this correct? Im trying to get this question fully correct myself as it goes towards a ful question in the exam so I really appreciate everyones help!
Thank you!
- 08-18-2009, 05:10 PM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Hmm.. if its not an assignment, then I can give you some more code.
Assuming your get method correctly sets the variables, doing this (and this is why you need the no-arg constructor) should get you a Point object;
And for midPoint...Java Code:public static void main(String[] args){ Point p = new Point();//if you don't have the Point() constructor, use p = new Point(0.0,0.0); p.get(); System.out.println(p); //this calls p.toString(), which is why you needed to change toSentence to toString() }
EDIT: MY note about Console is just that, while it is admittedly effective, those of us using IDE's can't use it properly.Java Code:public Point midPoint(Point other){ Point ret;//point to be returned //use the getMiddle method I posted earlier double retx = getMiddle(x,other.x); double rety = getMiddle(y,other.y); ret = new Point(retx,rety); } //or without the getMiddle method public Point midPoint(Point other){ Point ret;//point to be returned double retx = (x+other.x)/2; double rety = (y+other.y)/2; ret = new Point(retx,rety); }If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 08-18-2009, 05:51 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 10
- Rep Power
- 0
Thanks very much for your help I've give that a go now!
- 08-18-2009, 05:55 PM #8
Member
- Join Date
- Aug 2009
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
New to Java, need help with program
By javakid in forum New To JavaReplies: 7Last Post: 12-05-2008, 04:16 AM -
Java Program
By icedragon770 in forum Java AppletsReplies: 26Last Post: 10-12-2008, 12:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM -
java program
By nehasahu in forum New To JavaReplies: 5Last Post: 07-12-2007, 11:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks