Question
Lab Assignment description: Use an inheritance to create three java classes that represent Animal, Mammal and Reptile taxonomic classes Use the provided SimpleGUI.java application to
Lab Assignment description:
Use an inheritance to create three java classes that represent Animal, Mammal and Reptile taxonomic classes
Use the provided SimpleGUI.java application to create animal objects (mammals and reptiles) and store them in the array. The array size is 10. In case the user wants to add more than 10 animals, your program should display an error message indicating that there in no room. Examine the provided SimpleGUI java application and familiarize yourself with different GUI components. The application provides GUI components for entering animals information. Modify SimpleGUI application by writing a code to provide a functionality for the "Add Mammal", "Add Reptile" and "Display Animals" buttons. Note: You may use the provided SimpleGUI application or you can write your own application to create animals, to store animals inside the array and to display their attributes.
Create InvalidWeightException and InvalidNameException classes for invalid weight and invalid name.
I have finished three jave classes, I do not know how to store them in the array. Use try...catch form.
the SimpleGUI.java is blow:
package gui;
import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField;
/** * SimpleGUI.java * This program uses variety of GUI objects and it allows the user to interact * with the objects. */ public class SimpleGUI extends JFrame implements ActionListener { /** GUI component for getting the name*/ private GetInputPanel namePanel; /** GUI component for getting the weight */ private GetInputPanel weightPanel; /**GUI component for getting the age */ private GetComboPanel agePanel; /** GUI component for getting the number of legs */ private GetComboPanel lengthPanel; /** GUI component for getting the color */ private GetInputPanel colorPanel; /** Button for adding a reptile to the collection */ private JButton addReptileButton; /** Button for adding a reptile to the collection */ private JButton addMammalButton; /**Button for displaying animals in the collection */ private JButton displayAnimalsButton; /**A temporary GUI component for validating data */ private JTextArea verifyArea; /** * Main method launching the application. * @param args */ public static void main(String[] args) { SimpleGUI frame = new SimpleGUI(); frame.setSize(450, 650); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
/** * Constructor creates the SimpleGUI frame with a title */ public SimpleGUI() { super("Animal GUI Components"); createGUI(); }
/** * method createGUI creates GUI objects and adds them to the frame */ private void createGUI() { Container c = this.getContentPane(); c.setLayout(new BorderLayout(5, 5)); // 5 pixels hor and vert gap
JPanel inputPanel = new JPanel(); //contains various input panels 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, 3, 5, 5));
addReptileButton = new JButton("Add Reptile"); addMammalButton = new JButton("Add Mammal"); displayAnimalsButton = new JButton("Display Animals");
addReptileButton.setToolTipText("Press to add Reptile"); addMammalButton.setToolTipText("Press to add Mammal");
buttonPanel.add(addReptileButton); buttonPanel.add(addMammalButton); buttonPanel.add(displayAnimalsButton);
verifyArea = new JTextArea(20, 35); JScrollPane p = new JScrollPane(verifyArea); c.add(inputPanel, BorderLayout.NORTH); c.add(buttonPanel, BorderLayout.CENTER); c.add(p, BorderLayout.SOUTH);
addReptileButton.addActionListener(this); addMammalButton.addActionListener(this); displayAnimalsButton.addActionListener(this); setVisible(true); }
/** * Responds to the "Display" and "Add" buttons * @param ev Returns the command string associated with this action. */ @Override public void actionPerformed(ActionEvent ev) { Object object = ev.getSource(); verifyArea.setText(""); if (object == addReptileButton) { verifyArea.append(" Add Reptile button presed "); verifyArea.append("name:" + namePanel.getText() + " age: " + agePanel.getValue() + " weight: " + weightPanel.getValue() + " length: "); } else if (object == addMammalButton) { verifyArea.append(" Add Mammal button pressed "); verifyArea.append("name:" + namePanel.getText() + " age: " + agePanel.getValue() + " weight: " + weightPanel.getValue()); } else if (object == displayAnimalsButton) { verifyArea.append(" Display button pressed "); } } }
/** * A panel for a string and double value input. * It contains a label and a text field. */ class GetInputPanel extends JPanel { private final 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); add(new JLabel(prompt)); 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 the number", "Input Error", JOptionPane.ERROR_MESSAGE); } return value; } }
/** * This panel represents a panel with a label and a combo box. */ class GetComboPanel extends JPanel { private final JComboBox combo; //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) { String[] data = new String[numChoices]; for (int i = 0; i < data.length; i++) { data[i] = i + 1 + ""; } combo = new JComboBox<>(data); add(new JLabel(message)); //explains the purpose of the combo box add(combo); }
/** * Gets the value from the combo box * @return value selected from the combo box */ public int getValue() { int a; a = Integer.parseInt((String) combo.getSelectedItem()); return a; } }
My 3 classes is below:
parent class:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package animals;
import exceptions.*; import javax.swing.JTextArea;
/** * */ public class Animal {
protected String name; protected double weight; protected int age;
public Animal(String name, double weight, int age) 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; }
public void setName(String name) { this.name = name; }
public String getName() { return name; }
public void setWeight(double weight) { this.weight = weight; }
public double getWeight() { return weight; }
public void setAge(int age) { this.age = age; }
public int getAge() { return age; }
public void display(JTextArea output) { output.append("name" + name + "weight" + weight + "age" + age); }
@Override public String toString() { return (" Animal Name: " + name + "\t weight:" + weight + "\t age:" + age); } }
taxonomic class 1:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package animals;
import exceptions.InvalidNameException; import exceptions.InvalidWeightException; import javax.swing.JTextArea;
/** * */ public class Mammal extends Animal {
private String hairColor;
/** * * @param name * @param weight * @param age * @param hairColor */ public Mammal(String name, double weight, int age, String hairColor) throws InvalidWeightException,InvalidNameException { super(name, weight, age); this.hairColor = hairColor; }
public void setColor(String hairColor) { this.hairColor = hairColor; }
public String getColor() { return hairColor; }
@Override public void display(JTextArea output) { super.display(output); output.append("\t hair/fur color: " + hairColor); }
@Override public String toString() { return (super.toString() + "\t hair/fur color: " + hairColor); } }
taxonomic class 2:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package animals;
import exceptions.InvalidNameException; import exceptions.InvalidWeightException; import javax.swing.JTextArea;
/** * */ public class reptile extends Animal {
private int length;
public reptile(String name, Double weight, int age, int length) throws InvalidWeightException, InvalidNameException { super(name, weight, age); this.length = length; } public void setLength(int length) { this.length = length;} public int getLength() { return this.length; } @Override public void display(JTextArea output) { super.display(output); output.append("length" + length); } @Override public String toString() { return(super.toString() + "length" + this.length); } }
Two exception classes:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package exceptions;
/** * */ public class InvalidNameException extends Exception {
public InvalidNameException(String message) { super(message); }
}
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package exceptions;
/** * */ public class InvalidWeightException extends Exception {
public InvalidWeightException(String message) { super(message); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started