Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Please could someone explain why the program outputs Point(10, 20);Point(0,20)

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have added the below comments to the program in the constructor, but I still misunderstood what the program is doing. Thanks for any help that you can give.


/* Given the following code
  What will be the result of compiling and executing Test class?
  A) Point(10, 20);Point(0,20)         CORRECT
  B) Point(0, 20);Point(10,20)
  C) Point(10, 20);Point(10,20)        WRONG ANSWER
  D) None of the other options                                             */


class Point {
   int x;
   int y;
   void assign(int x, int y) {
       x = this.x;                // Redundant as the current value of class instance variable which is 0, is assigned to x
       this.y = y;                // The standard way to initialise param using this keyword
   }

   public String toString() {
       return "Point(" + x + ", " + y + ")";
   }
}

public class _31_Question_This_Keyword_And_Constructors {
   public static void main(String[] args) {
       Point p1 = new Point();
       p1.x = 10;
       p1.y = 20;
       Point p2 = new Point();
       p2.assign(p1.x, p1.y);
       System.out.println(p1.toString() + ";" + p2.toString());
   }
}
 
Bartender
Posts: 4272
160
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the assign method.
 
Clemonte Johnstone
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Have a look at the assign method.



Please could you kindly elaborate.

I confused it for a constructor, but now I can see as you rightly say that its a method. However, this difference does not change the behaviour of the program.
 
Marshal
Posts: 71760
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How on earth could it be a constructor? Its name is nothing like the name of the class, for a start. You even marked the problem in your first post.
 
Rancher
Posts: 4792
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is the line in question.
That comment explains why you get the result you see.
 
Campbell Ritchie
Marshal
Posts: 71760
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . That comment explains . . .

The comment is a bit mild; that code isn't “redundant”, but it hides a logic error.
 
Clemonte Johnstone
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Dave Tolls wrote:. . . That comment explains . . .

The comment is a bit mild; that code isn't “redundant”, but it hides a logic error.


Thanks Campbell. Please could you elaborate
 
Campbell Ritchie
Marshal
Posts: 71760
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look what that line is assigning to what. What are the values of the different xs after that assignment? That sort of line is a classic mistake.
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic