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: Linking objects, exception throwing and using boolean - how?

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

    Default Problem with method - not sure if it's correct

    I have been asked to create a method hasHad(aDrug). The method specification is: If the receiver is linked to aTreatment object that is linked to aDrug then returns true; otherwise returns false. The method I tried to come up with is:
    public boolean hasHad(Drug aDrug)
    {
           if (treatments.contains(aDrug))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    Drug is a class already created.
    aTreatment is used in another methoed in the same class as hasHad() - both in Patient class, and is used as such:

    public void addTreatment(Treatment aTreatment)

    Treatment is a class aready created.

    My porblem is I don;t kniow if what I have done is correct as the rest of the classes are incomplete so I can't test it. I need to be sure if its right - or wronf, in which case I need help to correct it, before I move on.

    Any help will be appreciated
    Last edited by steveby63; July 7th, 2014 at 07:24 AM.


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

    Default Re: Problem with method - not sure if it's correct

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Linking objects, exception throwing and using boolean - how?

    I have been asked to a method recordTreatment(aPatient, aDrug) with the following specification:
    (i) aPatient is not liked to ant Treatment object which is linked to aDrug; otherwise an exception is thrown.
    (ii) It the value of limit (a variable) is greater than the number of Treatment object linked to aDrug then a new
    Treatement object is created with date set to today's date and:
    (a) aTreatment is linked to aPatient
    (b) aTreatmen is linked to a Drug
    (c) true is returned
    otherwise false is returned.

    This method is in a coordinating class. Patient and Drug class are complete, a class specifiying the date has complete (is called M256Date).

    I understand an If statement is required (possibly more than one). My problem is getting all the specification together. This is my feable attempt (incomplete, and more than likely, wrong).

    public boolean recordTreatment(Patient aPatient, Drug aDrug)
    {
        if (!aPatient.equals.aTreatment)
        {
            throw new illegalArguemntException(“”);
        }
     
        if (limit > aDrug)
        {  
            Treatment aTreatment = new Treatment();
        return true;
        }
        else
        {
        return false;
        }
    }

    Any help will be appreciated.

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

    Default Re: Linking objects, exception throwing and using boolean - how?

    Threads merged. Please don't post multiple threads on essentially the same topic. If you decide to improve your original post - as you did, thank you - then please just add to the existing thread. If you decide the existing thread is in the wrong place, then report it using the triangle-exclamation and ask that it be moved.

    Good luck!

  5. #5
    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: Linking objects, exception throwing and using boolean - how?

    A useful technique when writing code to implement several tests is to add comments to a skeleton of the code describing what each test is going to do. The comments should be added before the code to implement the tests is written. Then when writing the code for one of the tests, the description for that test will be in front of you as you write the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. throwing Exception
    By Syahdeini in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2012, 06:05 PM
  2. Throwing Null Pointer Exception
    By SaurabhUpadhyay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 05:47 AM
  3. Throwing an Exception in paintComponent
    By jmack in forum Exceptions
    Replies: 1
    Last Post: January 31st, 2011, 08:12 AM
  4. Problem with throwing an exception...
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 23rd, 2011, 12:06 AM
  5. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 06:29 PM