Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. public class Person
  2. {
  3. // instance variables - replace the example below with your own
  4. private String name;
  5. private int age;
  6. private String address;
  7.  
  8.  
  9.  
  10.  
  11. /**
  12. * Constructor for objects of class Person
  13. */
  14. public Person()
  15. {
  16. this.name = name;
  17. this.age = age;
  18. this.address = address;
  19. }
  20.  
  21.  
  22.  
  23. //Set Methods:
  24. public void setName () {
  25. this.name = name;
  26. }
  27.  
  28.  
  29. public void setAge () {
  30. this.age = age;
  31. }
  32.  
  33.  
  34. public void setAddress () {
  35. this.address = address;
  36. }
  37.  
  38.  
  39. //Get Methods:
  40. public String getName () {
  41. return name;
  42. }
  43.  
  44. public int getAge () {
  45. return age;
  46. }
  47.  
  48. public String getAddress () {
  49. return address;
  50. }
  51. }
  52.  
  53. public class Dog
  54. {
  55. // instance variables - replace the example below with your own
  56. private String name;
  57. private int age;
  58.  
  59.  
  60.  
  61. public Dog()
  62. {
  63. this.name = name;
  64. this.age = age;
  65. }
  66.  
  67.  
  68. //Set Methods:
  69. public void setName () {
  70. this.name = name;
  71. }
  72.  
  73. public void setAge () {
  74. this.age = age;
  75. }
  76.  
  77. //Get Methods:
  78. public String getName () {
  79. return name;
  80. }
  81.  
  82. public int getAge () {
  83. return age;
  84. }
  85.  
  86.  
  87. }
  88.  
  89. public class Main
  90. {
  91. //Blank
  92. }
  93.  
  94. public class Person
  95. {
  96. ArrayList<Dog> dogs=new ArrayList<Dog>(); //this will hold all the dogs that the Person has as pets
  97.  
  98. public void giveDog(Dog dog){
  99. dogs.add(dog)
  100. }
  101. .....
  102. .....
  103.  
  104. public class Dog
  105. {
  106. Person owner;
  107.  
  108. public void setOwner(Person owner){
  109. this.owner=owner;
  110. }
  111. .....
  112. .....
  113.  
  114. public class Person
  115. Set<Dog> dogs = new HashSet<Dog>();
  116.  
  117.  
  118. public void addDog(Dog dog){
  119. if(dogs.size()>20){
  120. throw new IllegalArgumentException("exceeded the limit: ");
  121. }
  122. dogs.add(dog);
  123. }
  124. }
  125.  
  126. public class Dog
  127. {
  128. Person person;
  129.  
  130. public void setPerson(Person person){
  131. this.person=person;
  132. }
  133. }
  134.  
  135. public class Person
  136. {
  137. private Dog myDog;
  138.  
  139. private String name;
  140. private int age;
  141. private String address;
  142. ...etc.
  143.  
  144. public class Dog
  145. {
  146. private Person myOwner;
  147.  
  148. private String name;
  149. private int age;
  150.  
  151. private Dog myDog;
  152.  
  153. private Dog[] dogArray = new Dog[20];
  154. OR
  155. private Collection<Dog> dogList = new ArrayList(20); //for example
  156.  
  157. Person person = new Person();
  158. Dog dog1 = new Dog();
  159. dog1.setAge(12);
  160.  
  161. Dog dog2 = new Dog();
  162. dog2.setAge(34);
  163.  
  164. person.addDog(dog1); //dog 1
  165. person.addDog(dog2); //dog 2
  166. person.listDogs(); //list of all dogs
  167.  
  168. public class Person {
  169. // instance variables - replace the example below with your own
  170. private String name;
  171. private int age;
  172. private String address;
  173. private ArrayList<Dog> dogs = new ArrayList<Dog>();
  174.  
  175.  
  176.  
  177. /**
  178. * Constructor for objects of class Person
  179. */
  180. public Person()
  181. {
  182. this.name = name;
  183. this.age = age;
  184. this.address = address;
  185. }
  186.  
  187. public void addDog(Dog dog) {
  188. this.dogs.add(dog);
  189. }
  190.  
  191. public void listDogs() {
  192. for(Dog item : this.dogs) {
  193. System.out.println(item.getAge());
  194. }
  195. }
  196.  
  197.  
  198.  
  199. //Set Methods:
  200. public void setName () {
  201. this.name = name;
  202. }
  203.  
  204.  
  205. public void setAge () {
  206. this.age = age;
  207. }
  208.  
  209.  
  210. public void setAddress () {
  211. this.address = address;
  212. }
  213.  
  214.  
  215. //Get Methods:
  216. public String getName () {
  217. return name;
  218. }
  219.  
  220. public int getAge () {
  221. return age;
  222. }
  223.  
  224. public String getAddress () {
  225. return address;
  226. }
  227. }
  228.  
  229. public class Dog {
  230.  
  231. // instance variables - replace the example below with your own
  232. private String name;
  233. private int age;
  234.  
  235.  
  236.  
  237. public Dog()
  238. {
  239. this.name = name;
  240. this.age = age;
  241. }
  242.  
  243.  
  244. //Set Methods:
  245. public void setName () {
  246. this.name = name;
  247. }
  248.  
  249. public void setAge (int age) {
  250. this.age = age;
  251. }
  252.  
  253. //Get Methods:
  254. public String getName () {
  255. return name;
  256. }
  257.  
  258. public int getAge () {
  259. return age;
  260. }
  261.  
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement