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; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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);
}


}
}

Instructions
A farmer has an ArrayList of Pigs (fat pigs, if you are wondering). He wants to be able to sort the pigs by Welght and Age. He is a smart man and will not reinvent the wheel by writing a sorting algorithm. He knows that he can sort his ArrayList using the
Collections.sort() static method.
The problem is that Java is not that clever. Although Java knows the sorting algorithm it does not know how to compare two pigs =/
Your job is to help the farmer by implementing the Comparable interface (type-safe, we talked (will talk) about in Lecture 14) in the Pigs class. Plgs should be compared by welght ("smallest" first). If both plgs have the same welght, then compare them by
age (youngest first).
The starter code wll not compile until you implement the Comparable interface on the Pig class. You can/should play with the implementation of comparable to see how it affects the sorting.
Transcribed Image Text:Instructions A farmer has an ArrayList of Pigs (fat pigs, if you are wondering). He wants to be able to sort the pigs by Welght and Age. He is a smart man and will not reinvent the wheel by writing a sorting algorithm. He knows that he can sort his ArrayList using the Collections.sort() static method. The problem is that Java is not that clever. Although Java knows the sorting algorithm it does not know how to compare two pigs =/ Your job is to help the farmer by implementing the Comparable interface (type-safe, we talked (will talk) about in Lecture 14) in the Pigs class. Plgs should be compared by welght ("smallest" first). If both plgs have the same welght, then compare them by age (youngest first). The starter code wll not compile until you implement the Comparable interface on the Pig class. You can/should play with the implementation of comparable to see how it affects the sorting.
Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education