Hi, I need help with a java programming project. specifically Practice project n
ID: 3926973 • Letter: H
Question
Hi, I need help with a java programming project. specifically Practice project number 1 on page 476 from chapter 6 of the 7th edition of the book , java:an introduction to problem solving and programming. . It tells me to modify the class Species from the previous chapter by removing the setSpecies method and adding the following methods....
Five constructors: one of each instance variable, one with three parameters for the three instance variables, and a default constructor. Be sure that each constructor sets all of the instances variables
- Four methods names set that can reset values: one is the same as the method setSpecies in listing 5.16 and the other three each reset one of the instance variables
For reference I have to rename my modified species class SpeciesCh6. Also the original version of the Species class I have to modify can be found under the official textbook solution chegg has for this problem. Our teacher provided us a driver program to test the methods we added. I got the modified speciesch6 class to compile, but i get issues when I try to run it with the driver program.
Here is the code for my SpeciesCh6 class
import java.util.Scanner;
/**
* Class for data on endangered species.
*/
public class SpeciesCh6 {
private String name;
private int population;
private double growthRate;
/**
* "default constructor"
*/
public SpeciesCh6() {
this.setName("");
this.setPopulation(0);
this.setGrowthRate(0.0);
}
/**
* "One of each instance variable" -> growthRate
*/
public SpeciesCh6(double growthRate) {
this(); // "Be sure that each constructor sets all of the instances variables"
this.setGrowthRate(growthRate);
}
/**
* "One of each instance variable" -> population
*/
public SpeciesCh6(int population) {
this(); // "Be sure that each constructor sets all of the instances variables"
this.setPopulation(population);
}
/**
* "One of each instance variable" -> name
*/
public SpeciesCh6(String name) {
this(); // "Be sure that each constructor sets all of the instances variables"
this.setName(name);
}
/**
* "one with three parameters for the three instance variables"
*/
public SpeciesCh6(String name, int population, double growthRate) {
this.setName(name);
this.setPopulation(population);
this.setGrowthRate(growthRate);
}
public boolean equals(SpeciesCh6 otherObject) {
return (getName().equalsIgnoreCase(otherObject.getName()))
&& (getPopulation() == otherObject.getPopulation())
&& (getGrowthRate() == otherObject.getGrowthRate());
}
public double getGrowthRate() {
return growthRate;
}
public String getName() {
return name;
}
public int getPopulation() {
return population;
}
/**
* Precondition: years is a nonnegative number.
*
* Returns the projected population of the receiving object
*
* after the specified number of years.
*/
public int predictPopulation(int years) {
int result = 0;
double populationAmount = getPopulation();
int count = years;
while ((count > 0) && (populationAmount > 0)) {
populationAmount = (populationAmount + (getGrowthRate() / 100)
* populationAmount);
count--;
}
if (populationAmount > 0)
result = (int) populationAmount;
return result;
}
public void readInput() {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the species' name?");
setName(keyboard.nextLine());
System.out.println("What is the population of the species?");
setPopulation(keyboard.nextInt());
while (getPopulation() < 0) {
System.out.println("Population cannot be negative.");
System.out.println("Reenter population:");
setPopulation(keyboard.nextInt());
}
System.out.println("Enter growth rate (% increase per year):");
setGrowthRate(keyboard.nextDouble());
}
/**
* "the other three each reset one of the instance variables" -> growthRate
*/
public void setGrowthRate(double growthRate) {
this.growthRate = growthRate;
}
/**
* "the other three each reset one of the instance variables" -> name
*/
public void setName(String name) {
this.name = name;
}
/**
* "the other three each reset one of the instance variables" -> population
*/
public void setPopulation(int population) {
this.population = population;
}
/**
* "one is the same as the method setSpecies"
*/
public void setSpecies(String newName, int newPopulation,
double newGrowthRate) {
setName(newName);
if (newPopulation >= 0)
setPopulation(newPopulation);
else {
System.out.println("ERROR: using a negative population.");
System.exit(0);
}
setGrowthRate(newGrowthRate);
}
@Override
public String toString() {
return "Specie: " + getName() + ", Population: " + getPopulation()
+ ", Growth Rate: " + getGrowthRate();
}
public void writeOutput() {
System.out.println("Name = " + getName());
System.out.println("Population = " + getPopulation());
System.out.println("Growth rate = " + getGrowthRate() + "%");
}
}
Here is the code for the driver program provided to us(note: I can't modify the driver program, just the SpeciesCh6 class I had to make)
and also, here is a screenshot of the issues it gives me when I try to run the driver test. ANY help would greatly be appreciated
G File: SpeciesCh6,java CAUsers MaxDesktoplquiz 3 for cos260 Eile Edit View Build Project Settings Iools Window Help CSD Files Sort By Name 19 this.setGrowthRate (o.0 CaUsersMaxDesktoplhomework 4 Tor cos 2S C1UsersMaoDesktophomework 4 for cos 20 | SpeciesCh6.javaSpeciesCh6Driver.java GradesGraphTest.java Compile Messages jGRASP Messages Run UO Interactions -GRASP exec: Javac SpecieaCh6Driver.Java SpecieschéDriver.Java:97: error: symbol PersonCh6Test.java s1.set ("Yosarian", 55, 66.6) Clear Purchase·java PurchaseDemo.class aymbol: method aet (String,int, double) location: variable sl of type Speciesch6 Copy SpeciesCh6Driver.java: 108: error: cannot find symbol 31.set ("Hilo") ymbol: method get (String) location: variable 1 of type SpeciesCh6 -SpeciesCh6Driver.java : 119: error: cannot find ymbol a1.aet (77) ymbol: method set (int) location: variable 31 of type SpeciesCh6 SpecieaCheDriver.Java:130: error: cannot tind aymbol 81.get (88.8) ymbol mechod set (double) location: variable al of type Specieach6 4 errore jGRASP wedge2: exit code for proce55 GRASP: operation complete is 1 Browse FindDebugWorkbench Line:166 Col:2 Code:0 Top:19 10:34 AM 10/21/2016 I .." 4)Explanation / Answer
import java.util.Scanner;
/**
* Class for data on endangered species.
*/
public class SpeciesCh6 {
private String name;
private int population;
private double growthRate;
/**
* "default constructor"
*/
public SpeciesCh6() {
this.setName("");
this.setPopulation(0);
this.setGrowthRate(0.0);
}
/**
* "One of each instance variable" -> growthRate
*/
public SpeciesCh6(double growthRate) {
this(); // "Be sure that each constructor sets all of the instances variables"
this.setGrowthRate(growthRate);
}
/**
* "One of each instance variable" -> population
*/
public SpeciesCh6(int population) {
this(); // "Be sure that each constructor sets all of the instances variables"
this.setPopulation(population);
}
/**
* "One of each instance variable" -> name
*/
public SpeciesCh6(String name) {
this(); // "Be sure that each constructor sets all of the instances variables"
this.setName(name);
}
/**
* "one with three parameters for the three instance variables"
*/
public SpeciesCh6(String name, int population, double growthRate) {
this.setName(name);
this.setPopulation(population);
this.setGrowthRate(growthRate);
}
public boolean equals(SpeciesCh6 otherObject) {
return (getName().equalsIgnoreCase(otherObject.getName()))
&& (getPopulation() == otherObject.getPopulation())
&& (getGrowthRate() == otherObject.getGrowthRate());
}
public double getGrowthRate() {
return growthRate;
}
public String getName() {
return name;
}
public int getPopulation() {
return population;
}
/**
* Precondition: years is a nonnegative number.
*
* Returns the projected population of the receiving object
*
* after the specified number of years.
*/
public int predictPopulation(int years) {
int result = 0;
double populationAmount = getPopulation();
int count = years;
while ((count > 0) && (populationAmount > 0)) {
populationAmount = (populationAmount + (getGrowthRate() / 100)
* populationAmount);
count--;
}
if (populationAmount > 0)
result = (int) populationAmount;
return result;
}
public void readInput() {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the species' name?");
setName(keyboard.nextLine());
System.out.println("What is the population of the species?");
setPopulation(keyboard.nextInt());
while (getPopulation() < 0) {
System.out.println("Population cannot be negative.");
System.out.println("Reenter population:");
setPopulation(keyboard.nextInt());
}
System.out.println("Enter growth rate (% increase per year):");
setGrowthRate(keyboard.nextDouble());
}
/**
* "the other three each reset one of the instance variables" -> growthRate
*/
public void setGrowthRate(double growthRate) {
this.growthRate = growthRate;
}
/**
* "the other three each reset one of the instance variables" -> name
*/
public void setName(String name) {
this.name = name;
}
/**
* "the other three each reset one of the instance variables" -> population
*/
public void setPopulation(int population) {
this.population = population;
}
/**
* "one is the same as the method setSpecies"
*/
public void setSpecies(String newName, int newPopulation,
double newGrowthRate) {
setName(newName);
if (newPopulation >= 0)
setPopulation(newPopulation);
else {
System.out.println("ERROR: using a negative population.");
System.exit(0);
}
setGrowthRate(newGrowthRate);
}
@Override
public String toString() {
return "Specie: " + getName() + ", Population: " + getPopulation()
+ ", Growth Rate: " + getGrowthRate();
}
public void writeOutput() {
System.out.println("Name = " + getName());
System.out.println("Population = " + getPopulation());
System.out.println("Growth rate = " + getGrowthRate() + "%");
}
public void set(String newName, int newPopulation, double newGrowthRate)
{
setName(newName);
setPopulation(newPopulation);
setGrowthRate(newGrowthRate);
}
public void set(String newName)
{
setName(newName);
}
public void set(int newPopulation)
{
setPopulation(newPopulation);
}
public void set(double newGrowthRate)
{
setGrowthRate(newGrowthRate);
}
}
/**File name: SpeciesCh6Driver.java
Test each of SpeciesCh6's 5 constructors and four set methods.
*/
import java.util.*;
public class SpeciesCh6Driver
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
// Program loop control to repeat or stop
char ans;
do // Repeat entire test loop if user answered yes
{
System.out.println("Testing 1st constructor");
System.out.println(" - initialize all 3 parameters.");
System.out.println();
System.out.println("Name should be Crepek.");
System.out.println("Initial population should be 11.");
System.out.println("Growth rate should be 2.2.");
System.out.println();
SpeciesCh6 s1 = new SpeciesCh6("Crepek", 11, 2.2);
System.out.println("Values after constructor:");
s1.writeOutput();
System.out.println();
System.out.println("==============================");
System.out.println("Testing 2nd constructor");
System.out.println(" - specify only the name.");
System.out.println();
System.out.println("Name should be Foobar.");
System.out.println("Initial population should be 0.");
System.out.println("Growth rate should be 0.");
System.out.println();
SpeciesCh6 s2 = new SpeciesCh6("Foobar");
System.out.println("Values after constructor:");
s2.writeOutput();
System.out.println();
System.out.println("==============================");
System.out.println("Testing 3rd constructor");
System.out.println(" - specify only the population.");
System.out.println();
System.out.println("Name should be "No name, yet".");
System.out.println("Initial population should be 33.");
System.out.println("Growth rate should be 0.");
System.out.println();
SpeciesCh6 s3 = new SpeciesCh6(33);
System.out.println("Values after constructor:");
s3.writeOutput();
System.out.println();
System.out.println("==============================");
System.out.println("Testing 4th constructor");
System.out.println(" - specify just the growth rate.");
System.out.println();
System.out.println("Name should be "No name, yet".");
System.out.println("Initial population should be 0.");
System.out.println("Growth rate should be 44.4.");
System.out.println();
SpeciesCh6 s4 = new SpeciesCh6(44.4);
System.out.println("Values after constructor:");
s4.writeOutput();
System.out.println();
System.out.println("==============================");
System.out.println("Testing default constructor");
System.out.println(" - specify no parameters.");
System.out.println();
System.out.println("Name should be "No name, yet".");
System.out.println("Initial population should be 0.");
System.out.println("Growth rate should be 0.");
System.out.println();
SpeciesCh6 s5 = new SpeciesCh6();
System.out.println("Values after constructor:");
s5.writeOutput();
System.out.println();
System.out.println("==============================");
System.out.println("Test to set all parameters");
System.out.println();
System.out.println("Name should be Yosarian.");
System.out.println("Initial population should be 55.");
System.out.println("Growth rate should be 66.6.");
System.out.println();
s1.set("Yosarian", 55, 66.6);
s1.writeOutput();
System.out.println();
System.out.println("==============================");
System.out.println("Testing set name only");
System.out.println();
System.out.println("Name should be Milo.");
System.out.println("Initial population should be 55.");
System.out.println("Growth rate should be 66.6.");
System.out.println();
s1.set("Milo");
s1.writeOutput();
System.out.println();
System.out.println("==============================");
System.out.println("Testing set population only");
System.out.println();
System.out.println("Name should be Milo.");
System.out.println("Initial population should be 77.");
System.out.println("Growth rate should be 66.6.");
System.out.println();
s1.set(77);
s1.writeOutput();
System.out.println();
System.out.println("==============================");
System.out.println("Testing set growth rate");
System.out.println();
System.out.println("Name should be Milo.");
System.out.println("Initial population should be 77.");
System.out.println("Growth rate should be 88.8.");
System.out.println();
s1.set(88.8);
s1.writeOutput();
System.out.println();
System.out.println("==============================");
System.out.println();
System.out.println("Test again?(y/n)");
ans = keyboard.next().charAt(0);
}while ((ans == 'y') || (ans == 'Y'));
System.out.println("End of test.");
}
}
/*
output:
Testing 1st constructor
- initialize all 3 parameters.
Name should be Crepek.
Initial population should be 11.
Growth rate should be 2.2.
Values after constructor:
Name = Crepek
Population = 11
Growth rate = 2.2%
==============================
Testing 2nd constructor
- specify only the name.
Name should be Foobar.
Initial population should be 0.
Growth rate should be 0.
Values after constructor:
Name = Foobar
Population = 0
Growth rate = 0.0%
==============================
Testing 3rd constructor
- specify only the population.
Name should be "No name, yet".
Initial population should be 33.
Growth rate should be 0.
Values after constructor:
Name =
Population = 33
Growth rate = 0.0%
==============================
Testing 4th constructor
- specify just the growth rate.
Name should be "No name, yet".
Initial population should be 0.
Growth rate should be 44.4.
Values after constructor:
Name =
Population = 0
Growth rate = 44.4%
==============================
Testing default constructor
- specify no parameters.
Name should be "No name, yet".
Initial population should be 0.
Growth rate should be 0.
Values after constructor:
Name =
Population = 0
Growth rate = 0.0%
==============================
Test to set all parameters
Name should be Yosarian.
Initial population should be 55.
Growth rate should be 66.6.
Name = Yosarian
Population = 55
Growth rate = 66.6%
==============================
Testing set name only
Name should be Milo.
Initial population should be 55.
Growth rate should be 66.6.
Name = Milo
Population = 55
Growth rate = 66.6%
==============================
Testing set population only
Name should be Milo.
Initial population should be 77.
Growth rate should be 66.6.
Name = Milo
Population = 77
Growth rate = 66.6%
==============================
Testing set growth rate
Name should be Milo.
Initial population should be 77.
Growth rate should be 88.8.
Name = Milo
Population = 77
Growth rate = 88.8%
==============================
Test again?(y/n)
n
End of test.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.