Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1.  
  2. public class Dog {
  3.  
  4. private String name;
  5. private String color;
  6. private int age;
  7. private static Dog[] dogs = {new Dog("Betty", "Brown", 6), new Dog("Ivy", "Golden Brown", 16),
  8. new Dog("Aurora", "White", 5), new Dog("Bear", "Black", 13), new Dog("Tiny", "Brown", 10)};
  9.  
  10.  
  11. public Dog(String name, String color, int age) {
  12. setName(name);
  13. setColor(color);
  14. setAge(age);
  15. }
  16.  
  17. public static void main(String[] args) {
  18. sort();
  19. for (Dog d : dogs) {
  20. System.out.println(d.getName() + ", " + d.getAge() + "\t" + "(" + d.getColor() + ")");
  21. }
  22. }
  23.  
  24. public String getName() {
  25. return name;
  26. }
  27.  
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31.  
  32. public String getColor() {
  33. return color;
  34. }
  35.  
  36. public void setColor(String color) {
  37. this.color = color;
  38. }
  39.  
  40. public int getAge() {
  41. return age;
  42. }
  43.  
  44. public void setAge(int age) {
  45. this.age = age;
  46. }
  47.  
  48. private static void sort() {
  49. for (int i = 0; i < dogs.length -1; i++) {
  50. for (int j = i+1; j < dogs.length; j++) {
  51. if (dogs[i].getAge() > dogs[j].getAge()) {
  52. Dog swp = dogs[i];
  53. dogs[i] = dogs[j];
  54. dogs[j] = swp;
  55. }
  56. }
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement