Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person
- {
- // instance variables - replace the example below with your own
- private String name;
- private int age;
- private String address;
- /**
- * Constructor for objects of class Person
- */
- public Person()
- {
- this.name = name;
- this.age = age;
- this.address = address;
- }
- //Set Methods:
- public void setName () {
- this.name = name;
- }
- public void setAge () {
- this.age = age;
- }
- public void setAddress () {
- this.address = address;
- }
- //Get Methods:
- public String getName () {
- return name;
- }
- public int getAge () {
- return age;
- }
- public String getAddress () {
- return address;
- }
- }
- public class Dog
- {
- // instance variables - replace the example below with your own
- private String name;
- private int age;
- public Dog()
- {
- this.name = name;
- this.age = age;
- }
- //Set Methods:
- public void setName () {
- this.name = name;
- }
- public void setAge () {
- this.age = age;
- }
- //Get Methods:
- public String getName () {
- return name;
- }
- public int getAge () {
- return age;
- }
- }
- public class Main
- {
- //Blank
- }
- public class Person
- {
- ArrayList<Dog> dogs=new ArrayList<Dog>(); //this will hold all the dogs that the Person has as pets
- public void giveDog(Dog dog){
- dogs.add(dog)
- }
- .....
- .....
- public class Dog
- {
- Person owner;
- public void setOwner(Person owner){
- this.owner=owner;
- }
- .....
- .....
- public class Person
- Set<Dog> dogs = new HashSet<Dog>();
- public void addDog(Dog dog){
- if(dogs.size()>20){
- throw new IllegalArgumentException("exceeded the limit: ");
- }
- dogs.add(dog);
- }
- }
- public class Dog
- {
- Person person;
- public void setPerson(Person person){
- this.person=person;
- }
- }
- public class Person
- {
- private Dog myDog;
- private String name;
- private int age;
- private String address;
- ...etc.
- public class Dog
- {
- private Person myOwner;
- private String name;
- private int age;
- private Dog myDog;
- private Dog[] dogArray = new Dog[20];
- OR
- private Collection<Dog> dogList = new ArrayList(20); //for example
- Person person = new Person();
- Dog dog1 = new Dog();
- dog1.setAge(12);
- Dog dog2 = new Dog();
- dog2.setAge(34);
- person.addDog(dog1); //dog 1
- person.addDog(dog2); //dog 2
- person.listDogs(); //list of all dogs
- public class Person {
- // instance variables - replace the example below with your own
- private String name;
- private int age;
- private String address;
- private ArrayList<Dog> dogs = new ArrayList<Dog>();
- /**
- * Constructor for objects of class Person
- */
- public Person()
- {
- this.name = name;
- this.age = age;
- this.address = address;
- }
- public void addDog(Dog dog) {
- this.dogs.add(dog);
- }
- public void listDogs() {
- for(Dog item : this.dogs) {
- System.out.println(item.getAge());
- }
- }
- //Set Methods:
- public void setName () {
- this.name = name;
- }
- public void setAge () {
- this.age = age;
- }
- public void setAddress () {
- this.address = address;
- }
- //Get Methods:
- public String getName () {
- return name;
- }
- public int getAge () {
- return age;
- }
- public String getAddress () {
- return address;
- }
- }
- public class Dog {
- // instance variables - replace the example below with your own
- private String name;
- private int age;
- public Dog()
- {
- this.name = name;
- this.age = age;
- }
- //Set Methods:
- public void setName () {
- this.name = name;
- }
- public void setAge (int age) {
- this.age = age;
- }
- //Get Methods:
- public String getName () {
- return name;
- }
- public int getAge () {
- return age;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement