I got GUI, animal, reptile and mammal here, and i do not know if i need to write
ID: 3737058 • Letter: I
Question
I got GUI, animal, reptile and mammal here, and i do not know if i need to write ArrayList by myself. Can you help me to finish this lab? thanks
GUI:
package gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimalSortFrameStart extends JFrame implements ActionListener {
/**
* GUI component for getting the name
*/
private GetInputPanel namePanel = null;
/**
* GUI component for getting the weight
*/
private GetInputPanel weightPanel = null;
/**
* GUI component for getting the age
*/
private GetComboPanel agePanel = null;
/**
* GUI component for getting the length
*/
private GetComboPanel lengthPanel = null;
/**
* GUI component for getting the color
*/
private GetInputPanel colorPanel = null;
/**
* Button for adding a reptile to the collection
*/
private final JButton addReptileButton = new JButton("Add Reptile");
/**
* Button for adding a mammal to the collection
*/
private final JButton addMammalButton = new JButton("Add Mammal");
/**
* Button for displaying animals in the collection
*/
private final JButton displayAnimalsButton = new JButton("Display Animals");
/**
* Button for sorting animals in the collection by name
*/
private final JButton sortAgeButton = new JButton("Sort Animals by Age");
/**
* Button for sorting animals in the collection by kind
*/
private final JButton sortKindButton = new JButton("Sort Animals by kind");
/**
* A for displaying data
*/
private final JTextArea verifyArea = new JTextArea(25, 45);
/**
* Creates a new instance of AddAnimalFrame which is contains panels and other
* GUI components
*/
public AnimalSortFrameStart() {
super("Add and Sort Animals by name and by kind");
createGUI();
}
/**
* A method to create GUI components
*/
private void createGUI() {
Container c = this.getContentPane();
c.setLayout(new BorderLayout(5, 5));
JPanel inputPanel = new JPanel(); //contains GUI to input animals info
inputPanel.setLayout(new GridLayout(5, 1));
namePanel = new GetInputPanel(20, " Animal's Name: ");
inputPanel.add(namePanel);
weightPanel = new GetInputPanel(6, " Animal's Weight (lb): ");
inputPanel.add(weightPanel);
agePanel = new GetComboPanel(" Animal's Age (years):", 125);
inputPanel.add(agePanel);
lengthPanel = new GetComboPanel("Reptile's Length (cm)", 1000);
inputPanel.add(lengthPanel);
colorPanel = new GetInputPanel(20, "Mammal's skin/fur color");
inputPanel.add(colorPanel);
JPanel buttonPanel = new JPanel(); //contains buttons
buttonPanel.setLayout(new GridLayout(1, 5, 5, 5));
addReptileButton.setToolTipText("Press to add Reptile");
addMammalButton.setToolTipText("Press to add Mammal");
buttonPanel.add(addReptileButton);
buttonPanel.add(addMammalButton);
buttonPanel.add(displayAnimalsButton);
buttonPanel.add(sortAgeButton);
buttonPanel.add(sortKindButton);
c.add(inputPanel, BorderLayout.NORTH);
c.add(buttonPanel, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(verifyArea);
c.add(scrollPane, BorderLayout.SOUTH);
addReptileButton.addActionListener(this);
addMammalButton.addActionListener(this);
displayAnimalsButton.addActionListener(this);
sortAgeButton.addActionListener(this);
sortKindButton.addActionListener(this);
setVisible(true);
}
/**
* main() instantiates an object
*
* @param argv
*/
static public void main(String[] argv) {
AnimalSortFrameStart animalFrame = new AnimalSortFrameStart();
animalFrame.setSize(800, 650);
animalFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* Responds to the "Display" and "Add" buttons
*
* @param ev The button press event
*/
public void actionPerformed(ActionEvent ev) {
Object object = ev.getSource();
if (object == addReptileButton) {
} else if (object == addMammalButton) {
} else if (object == sortKindButton) {
} else if (object == sortAgeButton) {
} else {
}
}
/**
* A panel prompting for String input. It contains a label and a text field.
*/
class GetInputPanel extends JPanel {
private JTextField inputField; //used for the user input
/**
* Constructor sets up a label and the text field
*
* @param size the size of the input text field
* @param prompt the message specifying expected input
*/
public GetInputPanel(int size, String prompt) {
inputField = new JTextField(size);
JLabel label = new JLabel(prompt);
add(label);
add(inputField);
}
/**
* Gets the text from the text field
*
* @return Returns the text from the text field
*/
public String getText() {
return inputField.getText();
}
/**
* Converts the text field value into a number and displays an error message
* when inputed data contains non digit characters
*
* @return the integer represented by the user input
*/
public double getValue() {
double value = 0.0;
try {
value = Double.parseDouble(inputField.getText());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid characters in number",
"Input Error", JOptionPane.ERROR_MESSAGE);
}
return value;
}
}
/**
* This panel represents a panel with a label and a combo box.
*/
class GetComboPanel extends JPanel {
JLabel label; //explains the purpose of the combo box
JComboBox ageCombo; //used for the user input
/**
* Constructor sets up a panel with a label and a combo box.
*
* @param message the text indicating the purpose of the combo box
* @param numChoices the range of choices displayed in the combo box
*/
public GetComboPanel(String message, int numChoices) {
label = new JLabel(message);
String[] age = new String[numChoices];
for (int i = 0; i < age.length; i++) {
age[i] = i + 1 + "";
}
ageCombo = new JComboBox(age);
add(label);
add(ageCombo);
}
/**
* Gets the value from the combo box
*
* @return value selected from the combo box
*/
public int getValue() {
int a;
a = Integer.parseInt((String) ageCombo.getSelectedItem());
return a;
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Animals:
package animal;
import javax.swing.*;
import exceptions.*;
public class Animal implements Comparable{
/**
* give an initial name
*/
protected String name = null;
/**
* give an initial value of Weight
*/
protected double weight = 0.0;
/**
* give an initial value of age
*/
protected int age = 0;
/**
* @param name Name of the animal
* @param age age of animal
* @param weight Weight of animal
* @throws InvalidNameException
* @throws InvalidWeightException
*/
public Animal(String name, int age,double weight)throws InvalidWeightException
, InvalidNameException {
if (name.length() < 2) {
throw new InvalidNameException(
"The name must be at least two character");
}
this.name = name;
if (weight <= 0) {
throw new InvalidWeightException(
"The wight must be greater than zero");
}
this.weight=weight;
this.age = age;
}
/**
* @param name
* set the name
*/
public void setName(String name) {
this.name = name;
}
/**
* gets the name of the animal
* @return the string contains name
*/
public String getName() {
return name;
}
/**
* @param weight
* set the weight
*/
public void setWeight(double weight) {
this.weight = weight;
}
/**
* gets the weight of the animal
* @return the string contains weight
*/
public double getWeight() {
return weight;
}
/**
* @param age
* set the age
*/
public void setAge(int age) {
this.age = age;
}
/**
* gets the age of the animal
* @return the string contains age
*/
public int getAge() {
return age;
}
/**
* display the information about the animal in the textArea
* @param output a text area for display
*/
public void display(JTextArea output) {
output.append("name" + name + "weight" + weight + "age" + age);
}
/**
* @return a string containing contents of the object's fields
*/
@Override
public String toString() {
return (" Animal Name: " + name + " weight:" + weight
+ " age:" + age);
}
/**
* compare the name of animals
* @param b
* @return
*/
@Override
public int compareTo(Object b) {
Animal a = (Animal) b;
int result = age.compareTo(a.age);
if(result < 0){
return -1;
}
if(result >0){
return 1;
}
return 0;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Mammal:
package animal;
/**
*
*/
import exceptions.InvalidNameException;
import exceptions.InvalidWeightException;
import javax.swing.JTextArea;
public class Mammal extends Animal {
private String hairColor= null;
/**
*
* @param name
* @param weight
* @param age
* @param hairColor
* @throws InvalidWeightException
* @throws InvalidNameException
*/
public Mammal(String name, int age,double weight, String hairColor)
throws InvalidWeightException, InvalidNameException {
super(name, age, weight);
this.hairColor = hairColor;
}
/**
* @param hairColor
*/
public void setColor(String hairColor) {
this.hairColor = hairColor;
}
/**
* gets the color of the mammal
*
* @return the color of the mammal
*/
public String getColor() {
return hairColor;
}
/**
* Displays a Mammal in the textArea
*
* @param output a text area to display an information about the animal
*/
@Override
public void display(JTextArea output) {
super.display(output);
output.append(" hair/fur color: " + hairColor);
}
/**
* Returns a string representation of the Mammal object
*
* @return a string containing contents of the object's fields
*/
@Override
public String toString() {
return (super.toString() + " hair/fur color: " + hairColor);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Reptile:
package animal;
/**
*
*/
import exceptions.InvalidNameException;
import exceptions.InvalidWeightException;
import javax.swing.JTextArea;
public class reptile extends Animal {
private int length;
/**
*
* @param name
* @param weight
* @param age
* @param length
* @throws InvalidWeightException
* @throws InvalidNameException
*/
public reptile(String name, int age,Double weight, int length)
throws InvalidWeightException, InvalidNameException {
super(name, age, weight);
this.length = length;
}
/**
*
* @param length
*/
public void setLength(int length) {
this.length = length;
}
/**
*
* @return the length of reptile
*/
public int getLength() {
return this.length;
}
/**
*
* @param output a text area to display an information about the animal
*/
@Override
public void display(JTextArea output) {
super.display(output);
output.append("length" + length);
}
/**
*
* @return a string representation of the reptile object
*/
@Override
public String toString() {
return (super.toString() + "length" + this.length);
}
}
Explanation / Answer
I have removed comparator from Animal and created two Comparator Class, One to Compare by Kind of Animal and other By their age, the two exception class that you mentioned in the code were missing so I am adding them here. Completed your code fully, it runs seamlessly. Try it out.
========== Main AWT Class =============
package com.chegg;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.*;
public class AnimalSortFrameStart extends JFrame implements ActionListener {
/**
* ArrayList that holds all animal records throughout the Execution
*/
private static List<Animal> animalList=null;
/**
* GUI component for getting the name
*/
private GetInputPanel namePanel = null;
/**
* GUI component for getting the weight
*/
private GetInputPanel weightPanel = null;
/**
* GUI component for getting the age
*/
private GetComboPanel agePanel = null;
/**
* GUI component for getting the length
*/
private GetComboPanel lengthPanel = null;
/**
* GUI component for getting the color
*/
private GetInputPanel colorPanel = null;
/**
* Button for adding a reptile to the collection
*/
private final JButton addReptileButton = new JButton("Add Reptile");
/**
* Button for adding a mammal to the collection
*/
private final JButton addMammalButton = new JButton("Add Mammal");
/**
* Button for displaying animals in the collection
*/
private final JButton displayAnimalsButton = new JButton("Display Animals");
/**
* Button for sorting animals in the collection by name
*/
private final JButton sortAgeButton = new JButton("Sort Animals by Age");
/**
* Button for sorting animals in the collection by kind
*/
private final JButton sortKindButton = new JButton("Sort Animals by kind");
/**
* A for displaying data
*/
private final JTextArea verifyArea = new JTextArea(25, 45);
/**
* Creates a new instance of AddAnimalFrame which is contains panels and other
* GUI components
*/
public AnimalSortFrameStart() {
super("Add and Sort Animals by name and by kind");
createGUI();
}
/**
* A method to create GUI components
*/
private void createGUI() {
Container c = this.getContentPane();
c.setLayout(new BorderLayout(5, 5));
JPanel inputPanel = new JPanel(); //contains GUI to input animals info
inputPanel.setLayout(new GridLayout(5, 1));
namePanel = new GetInputPanel(20, " Animal's Name: ");
inputPanel.add(namePanel);
weightPanel = new GetInputPanel(6, " Animal's Weight (lb): ");
inputPanel.add(weightPanel);
agePanel = new GetComboPanel(" Animal's Age (years):", 125);
inputPanel.add(agePanel);
lengthPanel = new GetComboPanel("Reptile's Length (cm)", 1000);
inputPanel.add(lengthPanel);
colorPanel = new GetInputPanel(20, "Mammal's skin/fur color");
inputPanel.add(colorPanel);
JPanel buttonPanel = new JPanel(); //contains buttons
buttonPanel.setLayout(new GridLayout(1, 5, 5, 5));
addReptileButton.setToolTipText("Press to add Reptile");
addMammalButton.setToolTipText("Press to add Mammal");
buttonPanel.add(addReptileButton);
buttonPanel.add(addMammalButton);
buttonPanel.add(displayAnimalsButton);
buttonPanel.add(sortAgeButton);
buttonPanel.add(sortKindButton);
c.add(inputPanel, BorderLayout.NORTH);
c.add(buttonPanel, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(verifyArea);
c.add(scrollPane, BorderLayout.SOUTH);
addReptileButton.addActionListener(this);
addMammalButton.addActionListener(this);
displayAnimalsButton.addActionListener(this);
sortAgeButton.addActionListener(this);
sortKindButton.addActionListener(this);
setVisible(true);
}
/**
* main() instantiates an object
*
* @param argv
*/
static public void main(String[] argv) {
animalList=new ArrayList<Animal>();
AnimalSortFrameStart animalFrame = new AnimalSortFrameStart();
animalFrame.setSize(800, 650);
animalFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* Responds to the "Display" and "Add" buttons
*
* @param ev The button press event
*/
public void actionPerformed(ActionEvent ev) {
Object object = ev.getSource();
String name=namePanel.getText();
int age=agePanel.getValue();
double weight=weightPanel.getValue();
try {
if (object == addReptileButton) {
int length = lengthPanel.getValue();
Animal reptile = new Reptile(name, age, weight, length); //Create new Reptile Object
animalList.add(reptile); //Add Reptile to arrayList
} else if (object == addMammalButton) {
String hairColor=colorPanel.getText();
Animal mammal=new Mammal(name,age,weight,hairColor); //Create new Mammal Object
animalList.add(mammal); //Add Mammal to arrayList
} else if (object == sortKindButton) {
Collections.sort(animalList, (Comparator<? super Animal>) new KindComparator());
displayAnimals(); //Display after Sorting by Kind
} else if (object == sortAgeButton) {
Collections.sort(animalList,new AgeComparator());
displayAnimals(); //Display after sorting by Age in Ascending Order
} else if(object==displayAnimalsButton){
displayAnimals(); //Display Animal List
}
} catch (InvalidNameException e) {
e.printStackTrace();
} catch (InvalidWeightException e) {
e.printStackTrace();
}
}
/**
* Display List of animals
*/
private void displayAnimals(){
verifyArea.setText(null);
verifyArea.append("Name age Weight Color Length");
for (Animal animal : animalList) {
if (animal instanceof Mammal)
((Mammal)animal).display(verifyArea);
else
((Reptile)animal).display(verifyArea);
}
}
/**
* A panel prompting for String input. It contains a label and a text field.
*/
class GetInputPanel extends JPanel {
private JTextField inputField; //used for the user input
/**
* Constructor sets up a label and the text field
*
* @param size the size of the input text field
* @param prompt the message specifying expected input
*/
public GetInputPanel(int size, String prompt) {
inputField = new JTextField(size);
JLabel label = new JLabel(prompt);
add(label);
add(inputField);
}
/**
* Gets the text from the text field
*
* @return Returns the text from the text field
*/
public String getText() {
return inputField.getText();
}
/**
* Converts the text field value into a number and displays an error message
* when inputed data contains non digit characters
*
* @return the integer represented by the user input
*/
public double getValue() {
double value = 0.0;
try {
value = Double.parseDouble(inputField.getText());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid characters in number",
"Input Error", JOptionPane.ERROR_MESSAGE);
}
return value;
}
}
/**
* This panel represents a panel with a label and a combo box.
*/
class GetComboPanel extends JPanel {
JLabel label; //explains the purpose of the combo box
JComboBox ageCombo; //used for the user input
/**
* Constructor sets up a panel with a label and a combo box.
*
* @param message the text indicating the purpose of the combo box
* @param numChoices the range of choices displayed in the combo box
*/
public GetComboPanel(String message, int numChoices) {
label = new JLabel(message);
String[] age = new String[numChoices];
for (int i = 0; i < age.length; i++) {
age[i] = i + 1 + "";
}
ageCombo = new JComboBox(age);
add(label);
add(ageCombo);
}
/**
* Gets the value from the combo box
*
* @return value selected from the combo box
*/
public int getValue() {
int a;
a = Integer.parseInt((String) ageCombo.getSelectedItem());
return a;
}
}
}
============= Modified Animal Class ====================
package com.chegg;
import javax.swing.*;
public class Animal{
/**
* give an initial name
*/
protected String name = null;
/**
* give an initial value of Weight
*/
protected double weight = 0.0;
/**
* give an initial value of age
*/
protected int age = 0;
/**
* @param name Name of the animal
* @param age age of animal
* @param weight Weight of animal
* @throws InvalidNameException
* @throws InvalidWeightException
*/
public Animal(String name, int age,double weight)throws InvalidWeightException
, InvalidNameException {
if (name.length() < 2) {
throw new InvalidNameException(
"The name must be at least two character");
}
this.name = name;
if (weight <= 0) {
throw new InvalidWeightException(
"The wight must be greater than zero");
}
this.weight=weight;
this.age = age;
}
/**
* @param name
* set the name
*/
public void setName(String name) {
this.name = name;
}
/**
* gets the name of the animal
* @return the string contains name
*/
public String getName() {
return name;
}
/**
* @param weight
* set the weight
*/
public void setWeight(double weight) {
this.weight = weight;
}
/**
* gets the weight of the animal
* @return the string contains weight
*/
public double getWeight() {
return weight;
}
/**
* @param age
* set the age
*/
public void setAge(int age) {
this.age = age;
}
/**
* gets the age of the animal
* @return the string contains age
*/
public int getAge() {
return age;
}
/**
* display the information about the animal in the textArea
* @param output a text area for display
*/
public void display(JTextArea output) {
output.append(" "+name + " " + weight + " " + age);
}
/**
* @return a string containing contents of the object's fields
*/
@Override
public String toString() {
return (" Animal Name: " + name + " weight:" + weight
+ " age:" + age);
}
}
=============== Modified Mammal Class ====================
package com.chegg;
import javax.swing.JTextArea;
public class Mammal extends Animal {
private String hairColor= null;
/**
*
* @param name
* @param weight
* @param age
* @param hairColor
* @throws InvalidWeightException
* @throws InvalidNameException
*/
public Mammal(String name, int age,double weight, String hairColor)
throws InvalidWeightException, InvalidNameException {
super(name, age, weight);
this.hairColor = hairColor;
}
/**
* @param hairColor
*/
public void setColor(String hairColor) {
this.hairColor = hairColor;
}
/**
* gets the color of the mammal
*
* @return the color of the mammal
*/
public String getColor() {
return hairColor;
}
/**
* Displays a Mammal in the textArea
*
* @param output a text area to display an information about the animal
*/
@Override
public void display(JTextArea output) {
super.display(output);
output.append(" " + hairColor);
}
/**
* Returns a string representation of the Mammal object
*
* @return a string containing contents of the object's fields
*/
@Override
public String toString() {
return (super.toString() + " hair/fur color: " + hairColor);
}
}
============ Modified Reptile Class ====================
package com.chegg;
import javax.swing.JTextArea;
public class Reptile extends Animal {
private int length;
/**
*
* @param name
* @param weight
* @param age
* @param length
* @throws InvalidWeightException
* @throws InvalidNameException
*/
public Reptile(String name, int age,Double weight, int length)
throws InvalidWeightException, InvalidNameException {
super(name, age, weight);
this.length = length;
}
/**
*
* @param length
*/
public void setLength(int length) {
this.length = length;
}
/**
*
* @return the length of reptile
*/
public int getLength() {
return this.length;
}
/**
*
* @param output a text area to display an information about the animal
*/
@Override
public void display(JTextArea output) {
super.display(output);
output.append(" " + length);
}
/**
*
* @return a string representation of the reptile object
*/
@Override
public String toString() {
return (super.toString() + "length" + this.length);
}
}
============= Animal Kind Comparator =======================
package com.chegg;
import java.util.Comparator;
/**
* Compares Animal Object by Mammal or Reptile
*/
public class KindComparator implements Comparator<Animal> {
@Override
public int compare(Animal animal, Animal o2) {
if (animal instanceof Mammal) {
return -1;
} else if (animal instanceof Reptile) {
return 1;
} else return 0;
}
}
============== Animal Age Comparator =================================
package com.chegg;
import java.util.Comparator;
/**
* Compares Animal by Their Age in Ascending Order
*/
public class AgeComparator implements Comparator<Animal> {
@Override
public int compare(Animal o1, Animal o2) {
int age1=o1.getAge();
int age2=o2.getAge();
if (age1==age2)
return 0;
else if (age1>age2)
return 1;
else return -1;
}
}
================ InvalidNameException Class ==========================
package com.chegg;
public class InvalidNameException extends Exception {
public InvalidNameException(String s){
super(s);
}
}
================= InvalidWeightException Class =======================
package com.chegg;
public class InvalidWeightException extends Exception {
public InvalidWeightException(String s){
super(s);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.