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

Thread: exercise 140

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

    Talking exercise 140

    Hi, I'm having trouble with this exercise, but only a part of it. I don't know how to define "abs." Here are the instructions:

     
    The purpose of this exercise is to add to the IntClass class below a method, abs, that takes as its only argument an IntClass object and returns a new IntClass object representing the absolute value of the argument.
     
    Define the desired method as a class method.
     
    Define the desired method as an instance method.
     
    In each case, use the main method to test your code.

    There's two boxes of editable code. I'm having trouble with the first one, the one with "abs." I think I'll be able to figure out the second box if I figure out the first box. I'm just having trouble understanding the instructions and am in need of a summary please.

    Here is more code:

     
    my code goes here
     
    }

    Here's another box of code that they want me to fill out:

     
    public class MainClass
    {
      public static void main( String[] args )
      {
     
    my code goes here...
     
      }
    }

    Thx, ghosthead


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: exercise 140

    my code goes here
    You have left out the code. That comment is pretty useless. If you have questions about your code, you will have to post it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    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: exercise 140

    I don't know how to define "abs."
    What about the given instructions do you not understand? You surely know how to write a method signature and an empty method body, so what specifically are you confused about in this case?

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

    Default Re: exercise 140

    Here's my new code. I needed some review on method signatures. Now, I realize this was a bad place to get it because I should find it myself and I'm rudely asking you guys this, but I made the code work:

    box 1:

     
    public IntClass abs(IntClass a){ 
        if (a.myN >= 0)
            return a;
        else
            return new IntClass(-a.myN);
    }

    box 2:

     
    public class MainClass
    {
      public static void main( String[] args )
      {
          IntClass b = new IntClass(-5);
          System.out.println((b.abs(b)).getN());
      }
    }

    It worked perfectly. My teacher helped me. Lol, sorry for asking you guys this. I'll try to ask a better question next time.

  5. #5
    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: exercise 140

    You're not being rude, but you learned a lot more by figuring it out yourself or with your teacher's help. That's what they are for.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 11:31 PM
  4. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 09:40 PM
  5. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM

Tags for this Thread