Results 1 to 8 of 8

Thread: Java Program

  1. #1
    dbaby7 is offline Member
    Join Date
    Aug 2009
    Posts
    10
    Rep Power
    0

    Default 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!

  2. #2
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    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 04:24 PM.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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:
    Java Code:
    public double getMiddle(double x, double y){
         return (x+y)/2;
    }
    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.

    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!

  4. #4
    dbaby7 is offline Member
    Join Date
    Aug 2009
    Posts
    10
    Rep Power
    0

    Default

    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

  5. #5
    dbaby7 is offline Member
    Join Date
    Aug 2009
    Posts
    10
    Rep Power
    0

    Default

    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!

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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;

    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()
    }
    And for midPoint...
    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);
    }
    EDIT: MY note about Console is just that, while it is admittedly effective, those of us using IDE's can't use it properly.
    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!

  7. #7
    dbaby7 is offline Member
    Join Date
    Aug 2009
    Posts
    10
    Rep Power
    0

    Default

    Thanks very much for your help I've give that a go now!

  8. #8
    dbaby7 is offline Member
    Join Date
    Aug 2009
    Posts
    10
    Rep Power
    0

    Default

    I get this error when I try to name it as toString() thats why I originally renamed it

    Question1e.java:31: toString() in Point cannot override toString() in java.lang.
    Object; attempting to assign weaker access privileges; was public
    void toString()

Similar Threads

  1. New to Java, need help with program
    By javakid in forum New To Java
    Replies: 7
    Last Post: 12-05-2008, 05:16 AM
  2. Java Program
    By icedragon770 in forum Java Applets
    Replies: 26
    Last Post: 10-12-2008, 01:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM
  5. java program
    By nehasahu in forum New To Java
    Replies: 5
    Last Post: 07-12-2007, 12:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •