Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Ivor Horton's Beginning Java, Java 7 Edition
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 Edition section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 23rd, 2012, 04:03 PM
Authorized User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default Line object from two point objects and using objects as variables

Reference example in chapter 5 pages 188 and 190.

If I strip down the code for the Point class to:
Code:
 
public class Point  {
  double x;
  double y;
  public Point(double xVal, double yVal)  {
    x = xVal;
    y = yVal;
  }
 public Point(Point oldPoint)  {
    x = oldPoint.x;    
    y = oldPoint.y;    
  }
}
And strip the code of the Line class down to:
Code:
public class Line {
  Point start;   
  Point end;     

  public Line(Point start, Point end) {
    this.start = new Point(start);
    this.end = new Point(end);
  }
}
And compile the Line.java file, both the Line and Point source files compile. But if I remove this method:
Code:
public Point(Point oldPoint) {
    x = oldPoint.x;    
    y = oldPoint.y;    
  }
from the Point class source file, the Point source file compiles but the Line source file won't. The compiler says:
Line.java:6: error: constructor Point in class Point cannot be applied to given
types;
this.start = new Point(start);
^
required: double,double
found: Point
reason: actual and formal argument lists differ in length
Line.java:7: error: constructor Point in class Point cannot be applied to given
types;
this.end = new Point(end);
^
required: double,double
found: Point
reason: actual and formal argument lists differ in length

I understand the error messages but not why there is are errors. Just wondering what it is I am missing that is causing me to not understand what is wrong with the code this way.
Reply With Quote
  #2 (permalink)  
Old May 6th, 2012, 06:13 AM
Registered User
Points: 31, Level: 1
Points: 31, Level: 1 Points: 31, Level: 1 Points: 31, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Location: Mumbai,India
Posts: 9
Thanks: 0
Thanked 2 Times in 2 Posts
Default

This constructor is the constructor being calles when you write
this.start = new Point(start)

Code:
public Point(Point oldPoint) {
    x = oldPoint.x;    
    y = oldPoint.y;    
  }
If this is missing , how do you expect the compiler to compile ?
Its like writing address to the house that does not exist.

The compiler sees , OH! The only constructor in class Point is Point(double x,double y)
but who on earth is Point(Point oldPoint)??
so it says you need double,double instead of Point as an argument.

Thats why it says that actual arguments and formal arguments differ.
You deleted the constructor you are trying to call.

Hope that sums it up :)

Last edited by javaKid1337; May 6th, 2012 at 06:16 AM.. Reason: Missed a few points
Reply With Quote
The Following User Says Thank You to javaKid1337 For This Useful Post:
skf001 (May 15th, 2012)
  #3 (permalink)  
Old May 13th, 2012, 11:18 AM
Authorized User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default Thanks javakid but...

I understand what you were trying to explain and I really appreciate the fact that you took the time to help. Thankyou.

The problem I'm having with my example is that the constructor I removed from the Point class was the constructor included only to make new point objects out of already existing point objects. Let's say that I was only planning to make Point objects using the constructor that takes the double x and double y as arguements; why should I have to have the contructor that can make new objects from existing ones in order for the compiler to compile the Line and Point classes?

You said that the constructor being called by the keyword 'this' is the constructor that creates new objects from existing ones but I don't see that. The 'this' keyword is refering to the Point variable 'start' and point variable 'end' of the Line class.

Like I said, thanks for taking the time to respond. Any help is appreciated.
Reply With Quote
  #4 (permalink)  
Old May 14th, 2012, 01:43 AM
Registered User
Points: 31, Level: 1
Points: 31, Level: 1 Points: 31, Level: 1 Points: 31, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Location: Mumbai,India
Posts: 9
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Line.java:6: error: constructor Point in class Point cannot be applied to given
types;
this.start = new Point(start);
^
required: double,double
found: Point
reason: actual and formal argument lists differ in length


Based on this , I gave my answer. Now you said that you wanted to create ENTIRELY NEW POINT OBJECTS then you will have to pass 4 co-ordinate points because initially , the Point objects start and end have no x,y values stored in them.
so you should make a constructor like this

Code:
public Line(double x1,double y1,double x2,double y2) {
start = new Point(x1,y1);
end = new Point(x2,y2)
}
and you do have such a constructor , just that the values passed to them are not right. Thats what your compiler is saying - actual and formal arguments differ.
regarding calling the constructor using this , you can do it.
this(start,end)

this will call a constructor IN THE SAME CLASS that needs two Point objects as arguments

Last edited by javaKid1337; May 14th, 2012 at 01:52 AM..
Reply With Quote
The Following User Says Thank You to javaKid1337 For This Useful Post:
skf001 (May 15th, 2012)
  #5 (permalink)  
Old May 15th, 2012, 07:56 PM
Authorized User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default Now I get it

I see now where my thinking went wrong. You were absolutely right in your responses (which I misunderstood, even though I thought I understood them when I read them).

I missed, no matter how many times I looked at it, that the Point constructor that's called in the Line constructor...
Code:
Line(final Point start, final Point end) {
    this.start = new Point(start);
    this.end = new Point(end);}
...is calling the Point constructor that makes Point objects from existing ones. For some reason I kept thinking it was making brand new Point objects. (I don't know why I was thinking this, but it was why I couldn't see my mistake).

Like you said, to make new objects, the Line constructor that takes the four type double arguements would have to be called.

Once my brain realized this, everything - including, obviously, your correct and insightful responses - clicked into place.

Thanks again.

Addendum:
I think what was making me go wrong was that I was seeing the words 'start' and 'end' in the Point constructors in the Line constructor (ref. above code example) as refering to the variables in the Line object instead of what they were actually refering to, which were the start and end objects, already created, being passed as arguments, to the Line constructor. The fact that 'start' and 'end' are used as labels for the Line constructor parameters, for the names of two objects created in the TryGeometry class, and for the names of Point class variables in the Line class threw me off.

Last edited by skf001; May 15th, 2012 at 08:15 PM.. Reason: Addendum
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Value objects rayback_2 BOOK: Beginning Cryptography with Java 1 May 13th, 2008 05:04 PM
Storing objects in Session variables mega ASP.NET 1.0 and 1.1 Basics 8 November 16th, 2004 02:38 PM
Com Objects henryh MySQL 0 July 2nd, 2003 03:56 PM



All times are GMT -4. The time now is 03:08 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.