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

Thread: Modify a class and test

  1. #1
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Modify a class and test

    We have two classes person and dog person object acts as an owner for dog and dog acts as pet for the person i.e a bidierctional association i have done this (see code) i modify the person class so that the person object can act for an owner for up to 20 dogs. Where im a bit lost is creating a test with main method to test if it works

    DOG
    /**
     * Write a description of class Dog here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Dog
    {
       private  String name;
       private int age;
     
       /**added for part c to create the  
         * bidirectional association between dog Object and person object
         */
       private Person owner;
     
        public Dog(String name,int age)
        {
           setName(name);
           setAge(age);
        }
     
           public void setName(String name)
        {
            this.name=name;
     
        }
     
        public void setAge(int age)
        {
            this.age=age;
        }
     
     
        /**added for part c to create the  
         * bidirectional association between dog Object and person object
         */
        public void setPerson(Person owner)
        {
            this.owner=owner;
     
        }
     
         public String getName()
        {
            return name;
        }
     
         public int getAge()
        {
            return age;
        }
        /**added for part c to create the  
         * bidirectional association between dog Object and person object
         */
           public Person getPerson()
        {
            return owner;
        }
     
          public String toString()
        {
            return  getName() +" "+ getAge();
        }
     
        public void print()
        {
            System.out.println(toString());
        }
     
    }

    PERSON
    /**
     * Write a description of class Dog here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Person
    {
       private  String name;
       private  String address;
       private int age;
       Dog dog[] = new Dog[20];
     
       /**added for part c to create the  
         * bidirectional association between dog Object and person object
         */
       private Dog pet;
     
        public Person(String name,String address,int age)
        {
           setName(name);
           setAddress(address);
           setAge(age);
     
        }
     
           public void setName(String name)
        {
            this.name=name;
     
        }
       public void setAddress(String address)
        {
            this.address=address;
     
        }
     
     
        public void setAge(int age)
        {
            this.age=age;
        }
     
     
        /**added for part c to create the  
         * bidirectional association between dog Object and person object
         */
        public void setDog(Dog pet)
        {
            this.pet=pet;
     
        }
     
         public String getName()
        {
            return name;
        }
     
         public int getAge()
        {
            return age;
        }
     
        public String getAddress()
        {
            return address;
        }
        /**added for part c to create the  
         * bidirectional association between dog Object and person object
         */
           public Dog getDog()
        {
            return pet;
        }
     
          public String toString()
        {
            return  getName() +" "+ getAge()+" "+ getAddress()+ getDog();
        }
     
        public void print()
        {
            System.out.println(toString());
        }
     
    }

    my attempt at test

    public class Test
    {
      public static void main(String[] args)
    	   {
     
    	    String name;
            String address;
            int age;
     
     
            Dog d= new Dog("patch",2);
            Dog y= new Dog("spot",6);
     
            Person owner=new Person ("harry","lavally",56);
     
     
    	   }
     
     
    }


  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: Modify a class and test

    Please use punctuation. Your unpunctuated stream of consciousness is hard to understand.
    Where im a bit lost is creating a test with main method to test if it works
    It's important for you and us to know what "it works" means before proper tests can be written. Sometimes tests are written to simply verify the operation of every method of each class in every possible way. You might start there, but we don't know if you've written every method necessary to satisfy the assignment's requirements. Once you've sorted that and then define specific cases that demonstrate that "it works," test to those as well.

  3. #3
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Modify a class and test

    Sorry want to create a new person and 2 dogs set that person to be the dogs owner

    --- Update ---

    i want to test that george owns the dogs


    public class Test
    {
      public static void main(String[] args)
    	   {
     
     
     
            Person george = new Person("Georges","221b Baker Street", 40);
     
            Dog sherlock = new Dog("Sherlock", 6);
     
            Dog goofy = new Dog("Goofy", 10);
     
     
     
    	   }
     
     
    }

  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: Modify a class and test

    Then george must be set as the owner using Dog.setPerson(), and to ensure that the owner is set correctly, the results of Dog.getPerson() will be compared to the Person object that was set as the owner in the first step.

  5. #5
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Modify a class and test

    the output for this gives sherlock 10 null
    if i change getPerson() to setPerson() i get an error.

    public class Test
    {
      public static void main(String[] args)
    	   {
     
    	    Dog dog[] = new Dog[20];
     
            Person george = new Person("Georges","221b Baker Street", 40);
     
            Dog sherlock = new Dog("Sherlock", 6);
     
            Dog goofy = new Dog("Goofy", 10);
     
     
     
            goofy.getPerson();
            sherlock.getPerson();
     
            sherlock.print();
            goofy.print();
            george.print();
     
    	   }
     
     
    }

  6. #6
    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: Modify a class and test

    How are the dog's owners (or owner) set? Or are they . . . . ?

    You'll have to show variations in your code and the errors received, because there are just too many possible ways you could be doing inappropriate things. The error message will help us help you find the error and understand why what you've done is resulting in an error.

    Edit: Your Dog.print() and Person.print() methods are odd. Now wrong, necessarily, but odd. Usually the toString() method will be overridden to present the object as desired when it is included in a print() statement, like:

    System.out.println( "The dog is: " + dogInstance );

    dogInstance.toString() will then be called to show the dog instance in the way specified.

  7. #7
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Modify a class and test

    only error i get is when i use set saying it is no a compatible type

  8. #8
    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: Modify a class and test

    Show the code that attempts to use the setPerson() method and results in the error. setPerson() requires a Person object as a parameter.

Similar Threads

  1. how to test a class ??
    By ajw1993 in forum Java Theory & Questions
    Replies: 5
    Last Post: March 11th, 2013, 06:53 AM
  2. How can I write a class or something so I can run and test the following code?
    By michael305rodri in forum Java Theory & Questions
    Replies: 3
    Last Post: October 18th, 2012, 10:46 PM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. Inheritance; Problem with Test class
    By Charlie.beat in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 8th, 2012, 10:59 PM
  5. Replies: 1
    Last Post: February 12th, 2012, 01:01 AM