Define a class whose objects are records on animal species. The class will have
ID: 3622648 • Letter: D
Question
Define a class whose objects are records on animal species. The class will have instance variables for the species name, population, and growth rate. The growth rate is a percentage which can be positive or negative and can exceed 100 per cent. Include a suitable collection of constructors, mutator methods, and accessor methods. Also, include a toString method and an equals method. Also, include a boolean valued method named endangered that returns true when the growth rate is negative and returns false otherwise. Write a test program (or programs) that tests each method in your class definition.You should implement the following methods:
public Species()
public Species(String name)
public Species(String name, long pop, double growthRate)
public Species(Species other)
public void setName(String name)
public String getName()
public void setGrowthRate(double growthRate)
public double getGrowthRate()
public void setPopulation(long population)
public long getPopulation()
public boolean equals(Object obj)
public boolean endangered()
You may also find it useful to define a method such as:
private void setSpeciesInfo(String name, long pop, double growthRate)
to simplify implementation of the constructors.
/**
Class representing an animal species
*/
class Species {
/**
Species name
*/
private String name;
/**
Species population
*/
private long population;
/**
Growth rate for the species
*/
private double growthRate;
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
// --------------------------------
// --------- END USER CODE --------
// --------------------------------
/**
Returns a string describing the species
*/
public String toString() {
return name + " (population of " + population + " with growth rate " +
growthRate + "%)";
}
}
/**
Test class that exercises the methods of the Species class
*/
public class SpeciesDemo {
public static void main(String[] args) {
// Test the no-arg constructor, setter methods, and toString()
Species rabbit = new Species();
rabbit.setName("Rabbit");
rabbit.setPopulation(1000);
rabbit.setGrowthRate(75.0);
System.out.println(rabbit);
// Test the getter methods
System.out.println("Rabbit name : " + rabbit.getName());
System.out.println("Rabbit population: " + rabbit.getPopulation());
System.out.println("Rabbit growthRate: " + rabbit.getGrowthRate());
// Test the other constructors
System.out.println();
Species esquilax = new Species("Esquilax", 1000, 50.0);
System.out.println(esquilax);
Species spottedOwl = new Species("Spotted Owl");
System.out.println(spottedOwl);
spottedOwl.setPopulation(200);
spottedOwl.setGrowthRate(-50.0);
System.out.println(spottedOwl);
Species spottedOwlCopy = new Species(spottedOwl);
System.out.println(spottedOwlCopy);
// Test the equals method
System.out.println();
System.out.println(
"Spotted Owl and Copy of Spotted Owl are same object? " +
(spottedOwl == spottedOwlCopy));
System.out.println(
"Spotted Owl and Copy of Spotted Owl are equal? " +
spottedOwl.equals(spottedOwlCopy));
System.out.println(
"Rabbit and Spotted Owl are equal? " +
rabbit.equals(spottedOwl));
// Test the endangered method
System.out.println();
System.out.println("Rabits are endangered? " + rabbit.endangered());
System.out.println(
"Spotted Owls are endangered? " + spottedOwl.endangered());
}
}
Explanation / Answer
Dear, Modified code /** Class representing an animal species */ class Species { /** Species name */ private String name; /** Species population */ private long population; /** Growth rate for the species */ private double growthRate; public Species() { } public Species(String nm) { name=nm; } public Species(String nm, long pop, double gRate) { name=nm; population=pop; growthRate=gRate; } public Species(Species other) { name=other.name;; population=other.population; growthRate=other.growthRate; } public void setName(String nm) { name=nm; } public String getName() { return name; } public void setGrowthRate(double gRate) { growthRate=gRate; } public double getGrowthRate() { return growthRate; } public void setPopulation(long pop) { population=pop; } public long getPopulation() { return population; } public boolean equals(Species obj) { if((this.population==obj.population)&&(this.growthRate==obj.growthRate)&&(name.equals(obj.name))) return true; else return false; } public boolean endangered() { if(growthRateRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.