Java. Reference attachment. Starter code below. public class Pig { private int age; private int weight; public Pig(int age, int weight) { this.age = age; this.weight = weight; }
Java. Reference attachment. Starter code below.
public class Pig {
private int age;
private int weight;
public Pig(int age, int weight) {
this.age = age;
this.weight = weight;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return String.format("Age: %d, weight: %d kg", age, weight);
}
}
//Do not modify this file.
import java.util.ArrayList;
import java.util.Collections;
public class PigsPigsPigs {
public static void main(String[] args) {
ArrayList<Pig> fatPigs = new ArrayList<>();
fatPigs.add(new Pig(4,300));
fatPigs.add(new Pig(6,278));
fatPigs.add(new Pig(4,100));
fatPigs.add(new Pig(3,500));
fatPigs.add(new Pig(6,5000));
fatPigs.add(new Pig(1,20));
fatPigs.add(new Pig(1,500));
fatPigs.add(new Pig(8,500));
Collections.sort(fatPigs);
for (Pig pig: fatPigs) {
System.out.println(pig);
}
}
}
Step by step
Solved in 4 steps with 1 images