Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Exercise 98: how to write a main method with what I'm given

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Exercise 98: how to write a main method with what I'm given

    Here are the instructions:

     
    The program below includes the expanded version of the above definition of the APPoint class. In the space provided, complete the definition of the main method — replacing the two comments by appropriate code — so that two APPoint objects are created. The first should be assigned to the variable a and should represent the point with coordinate (25.3,–6.8). The second should be assigned to the variable b and should represent the point with coordinate (5,16.8).
     
      public class APPoint 
      { 
        private double myX; 
        private double myY; 
     
        public APPoint( double x, double y ) 
        { 
          myX = x; 
          myY = y; 
        } 
     
        public double getX()  
        { 
          return myX; 
        } 
     
        public double getY() 
        { 
          return myY; 
        } 
    public static void main( String[] args )
    {
      APPoint a = //...
      //...
     
      System.out.println( "a is (" + a.getX() + "," + a.getY() + ")" );
      System.out.println( "b is (" + b.getX() + "," + b.getY() + ")" );
    }
      }

    I have to replace the two commented lines with code with the definition of the "main" method. As always, I'm grateful for all help.

    Thanks, ghostheadx

    P.S. I just need a start. I could have the answer any time I wanted at the click of a button. Please remember that I have a "show solution" button, that I am purposely NOT clicking. That is because I want to be guided, as opposed to being fed the solution unless I take to long and fall behind. Nevertheless, all help appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    Are you asking how to code the call to a class's constructor?
    See the tutorial: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    I think yes, but I read the article you gave me and can't quite understand based on that what a constructor IS? Is it in that article? I don't see the term "constructor" in there, but thanks. Could you help explain what a constructor is and then I'll be glad to use that information? Thanks for helping.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    If you move around in the tutorial a bit you can find other articles:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    A constructor is the "method" that is called when an instance of a class is created with a new statement.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    So it's the method that creates the instance of a class? It's sort of like a special method, JUST for that class?

    --- Update ---

    What does this line in the article mean

    " If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor."

    I get what a superclass is. But what is an IMPLICIT superclass? Lol, I feel so stupid for asking that question. I just think knowing that word might help my understanding a little bit.

    --- Update ---

    I made it work. Here is my code:

     
    public static void main( String[] args )
    {
      APPoint a = new APPoint (25.3, -6.8);
      APPoint b = new APPoint (-5, 16.8);
     
      System.out.println( "a is (" + a.getX() + "," + a.getY() + ")" );
      System.out.println( "b is (" + b.getX() + "," + b.getY() + ")" );
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    It's sort of like a special method, JUST for that class?
    Yes.

    The Object class is the super class for all classes.

    I think the word implicit is used in its normal English language way.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    So, it's like a method JUST FOR creating ONE type of instance. It's the blueprints for a specific object, as opposed to one in the general category, i.e. the superclass.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    A specific constructor receives args to define an instance of the class.
    A class can have many constructors.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    Alright, so a constructor automatically makes a specific type of instance of a class?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    A constructor "constructs" an instance of a class.
    a constructor automatically makes a specific type of instance of a class?
    I'm not sure what "automatically" and "special type" mean. Leaving those out says this which is about correct:
    a constructor makes an instance of a class
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    Rephrasing and rearranging the words ghostheadx used, I will simply put: A constructor is automatically called when a new instance of its class is created.

  12. #12
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 98: how to write a main method with what I'm given

    //Creating Object To The Class APPoint using new() poerator which dynamically allocates space in heap
    APPoint a = new APPoint (25.3, -6.8);
    //when an object is created to the class then constructor declared in the class APPoint is invoked and perform its functionality i.e a constructor is automatically invoked when an object is created to the class.Also a constructor can be invoked even multiple objects created to the class and once for each object.
    APPoint b = new APPoint (-5, 16.8);

Similar Threads

  1. Replies: 15
    Last Post: May 2nd, 2013, 05:29 AM
  2. how to do write this method with more efficiency ?
    By romavolman in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 2nd, 2012, 11:23 AM
  3. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  4. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM

Tags for this Thread