Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Dog {
- private String name;
- private String color;
- private int age;
- private static Dog[] dogs = {new Dog("Betty", "Brown", 6), new Dog("Ivy", "Golden Brown", 16),
- new Dog("Aurora", "White", 5), new Dog("Bear", "Black", 13), new Dog("Tiny", "Brown", 10)};
- public Dog(String name, String color, int age) {
- setName(name);
- setColor(color);
- setAge(age);
- }
- public static void main(String[] args) {
- sort();
- for (Dog d : dogs) {
- System.out.println(d.getName() + ", " + d.getAge() + "\t" + "(" + d.getColor() + ")");
- }
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- private static void sort() {
- for (int i = 0; i < dogs.length -1; i++) {
- for (int j = i+1; j < dogs.length; j++) {
- if (dogs[i].getAge() > dogs[j].getAge()) {
- Dog swp = dogs[i];
- dogs[i] = dogs[j];
- dogs[j] = swp;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement