Results 1 to 13 of 13
  1. #1
    chang- is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default Please help. blue j test method

    hi there. i am new to this forum and was wondering if anyone could help me

    i have a test that i need to pass, but i am have no luck..

    the test code is

    Java Code:
     public void testCuttingGrassReducesGrassAndProducesHay() {
            
            Hay hay = field.cutGrass(100);
            assertNotNull(hay);
            assertEquals(100, hay.getUnits());
            assertEquals(0, field.getGrass());
        }
    the method i need do write it for is :

    Java Code:
    public Hay cutGrass(int grassToCut)
        {
    
        
            if( grassToCut <= 0){
               throw new IllegalArgumentException("Cannot cut less than one unit of grass"); // if grass cut if less then one, exception thrown
            } 
            if( grassToCut > 100){
                throw new IllegalArgumentException("Cannot cut more grass than field contains"); // if grass cut contains more then in field exception is thrown.
            } 
      
            if (month >= 12){        // if month is more then or equal to 12 the grass cut goes is set back to 0.
               grass =0;
            }
            
            report("cut " + grassToCut + " units of grass");
            
            
            
            
            return hay;
        }
    any help or if needed more info please ask..

    thanks
    Last edited by chang-; 06-02-2011 at 02:40 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    The cutGrass method must create an instance of the Hay class and return it. Since we have no idea what the Hay class looks like we cannot offer more advice.

    By the way the IDE (bluej) you use is irrelevant.

  3. #3
    chang- is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    the hay class just has one constructor in it
    Java Code:
    public Hay(int units)
         {
     
            super(units, 1);
            
            
        }
    sorry i dnt understand what you mean by blue j's is irrelvant?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    You mentioned that you are using bluej as if it has some impact on your code. It doesn't. You would have the same problem if you wrote your code in Eclipse, Netbeans, JBuilder, ItelliJ, notpad etc. So mentioning what IDE you use is of no importance to us.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    OK so now you have added code to the cutGrass method. What problem are you having? For starters I cannot see where you create an instance of the Hay class.

  6. #6
    chang- is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    im having troube on writing the instance that calls from the hay class. so far i have
    Java Code:
    hay = new Hay();
    i know its number of units should be in its parameter, but i dont know how to write it.

    oh ok sorry i didnt knw

  7. #7
    chang- is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    oh sorry i figured it out it was .
    Java Code:
     hay = new Hay(grassToCut);
    but when i run the test. the test says it expected 0 but got 100

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    Java Code:
       Hay hay = field.cutGrass(100);
    In your first post you passed a parameter to a method. The code passes 100 to the method cutGrass().
    A constructor is a special kind of method and you pass parameters to it the same way.

    Look at the constructor and see what type of data is required. Then make sure the number of units is that data type.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Don't think about this in Java terms, think about it in real terms. After you cut the grass of a lawn/field/etc how much grass would there be?

  10. #10
    chang- is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    more then 1 unit of grass?

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    What is the meaning of the parameter to the Hay constructor?
    How do you know what value to give to it? Is there any documentation for the class that tells you?
    No one here can know that number. It has to come from your professor or some part of the program.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Let me try another tack. If you lawn had 1000 units of grass and you cut 250 units and made it into hay, how much grass would your lawn still have?

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    I don't respond to private messages. If you have a question to ask then do it on the forum.
    hi would it be possible if i could send you my code, to give u a beter understanding.
    No. I believe people learn better if they write it themselves.
    i would sitll have 750 units of grass left..
    but i dont know how to write this as code
    It is simple math.
    Java Code:
    int x = 1000;
    int y = 250;
    x = .......;
    What would you replace the dots with so x had the final value of 750?

Similar Threads

  1. How to test a void method in Junit?
    By jRookie in forum New To Java
    Replies: 7
    Last Post: 09-01-2010, 07:47 AM
  2. Replies: 0
    Last Post: 04-05-2008, 12:38 AM
  3. Open Blue Lab 1.3.2
    By levent in forum Java Software
    Replies: 0
    Last Post: 08-01-2007, 04:22 PM
  4. Open Blue Lab 1.3.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-19-2007, 03:37 PM
  5. Open Blue Lab 1.2.10
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-19-2007, 08:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •