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 2 of 2

Thread: First programming class, and I don't understand the run-time error I am getting! (writing classes and methods exercise)

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default First programming class, and I don't understand the run-time error I am getting! (writing classes and methods exercise)

    This assignment is basically to understand how to write classes and methods. I have written my code from scratch, and it compiles and it runs. My methods that get the perimeter and area do so accurately, but my access methods that just getWidth and getLength always return values of 0.

    Does anyone know why this might be the case? Btw, when you look at my code, I'm just going straight from the text, so if you think "why did she do that?" it's me just doing my best to understand from the textbook.

    Edit: My code had my real name in it, so I'm removing it now that I have figured it out. Thank you! I don't know how to delete/resolve messages here, but I appreciate the help.
    Last edited by claireballoon; April 6th, 2014 at 05:24 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: First programming class, and I don't understand the run-time error I am getting! (writing classes and methods exercise)

    Welcome to the forum! Thank you for taking the time to learn to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    And, ironically, your perimeter() and area() methods are wrong. One of the purposes of a class' constructor is to initialize the object's instance variables. Your constructor signature is correct, your class has the instance variables or fields it needs, and you call the constructor properly, but the constructor's empty body does nothing.

    Remedy: Modify the constructor so that the parameters passed to it are used to initialize the object's instance variables. Modify the area() and perimeter() methods so that they do not take parameters but use the object's instance variables to calculate the answer that is returned.

    Once you've corrected the class, you will likely want to rethink how the main() method tests it.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    claireballoon (April 6th, 2014)

Similar Threads

  1. Some of my methods don't run properly
    By pyler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 5th, 2013, 10:55 AM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. Don't understand void methods, need help!
    By alex067 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 9th, 2012, 07:02 AM
  4. Employee Class Exercise... Code will not run!!!!
    By jbarcus81 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: January 26th, 2012, 03:39 PM
  5. [SOLVED] Experiencing a run time error, don't know what the problem is
    By scooty199 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2010, 10:21 AM

Tags for this Thread