Hint:
here is the code of class Spices .
import java.util.Scanner; /** Class for data on endangered species. */ public class Species { private String name; private int population; private double growthRate; public void readInput( ) { Scanner keyboard = new Scanner(System.in); System.out.println("What is the species' name?"); name = keyboard.nextLine( ); System.out.println( "What is the population of the species?"); population = keyboard.nextInt( ); while (population < 0) { System.out.println("Population cannot be negative."); System.out.println("Reenter population:"); population = keyboard.nextInt( ); } System.out.println("Enter growth rate (% increase per year):"); growthRate = keyboard.nextDouble( ); } public void writeOutput( ) { System.out.println("Name = " + name); System.out.println("Population = " + population); System.out.println("Growth rate = " + growthRate + "%"); } /** Precondition: years is a nonnegative number. Returns the projected population of the calling object after the specified number of years. */ public int predictPopulation(int years) { int result = 0; double populationAmount = population; int count = years; while ((count > 0) && (populationAmount > 0)) { populationAmount = (populationAmount + (growthRate / 100) * populationAmount); count--; } if (populationAmount > 0) result = (int)populationAmount; return result; } public void setSpecies(String newName, int newPopulation, double newGrowthRate) { name = newName; if (newPopulation >= 0) population = newPopulation; else { System.out.println("ERROR: using a negative population."); System.exit(0); } growthRate = newGrowthRate; } public String getName( ) { return name; } public int getPopulation( ) { return population; } public double getGrowthRate( ) { return growthRate; } public boolean equals(Species otherObject) { return (name.equalsIgnoreCase(otherObject.name)) && (population == otherObject.population) && (growthRate == otherObject.growthRate); } }
Here is the code from Listing 12.5 on Page 821.
public class StringLinkedListSelfContained { private ListNode head; public StringLinkedListSelf( ) { head = null; } /** Displays the data on the list. */ public void showList( ) { ListNode position = head; while (position != null) { System.out.println(position.data); position = position.link; } } /** Returns the number of nodes on the list. */ public int length( ) { int count = 0; ListNode position = head; while (position != null) { count++; position = position.link; } return count; } /** Adds a node containing the data addData at the start of the list. */ public void addANodeToStart(String addData) { head = new ListNode(addData, head); } /** Deletes the first node on the list. */ public void deleteHeadNode( ) { if (head != null) head = head.link; else { System.out.println("Deleting from an empty list."); System.exit(0); } } /** Sees whether target is on the list. */ public boolean onList(String target) { return find(target) != null; } // Returns a reference to the first node containing the // target data. If target is not on the list, returns null. private ListNode find(String target) { boolean found = false; ListNode position = head; while ((position != null) && !found) { String dataAtPosition = position.data; if (dataAtPosition.equals(target)) found = true; else position = position.link; } return position; } public String[] toArray( ) { String[] anArray = new String[length( )]; ListNode position = head; int i = 0; while (position != null) { anArray[i] = position.data; i++; position = position.link; } return anArray; } private class ListNode { private String data; private ListNode link; public ListNode( ) { link = null; data = null; } public ListNode(String newData, ListNode linkValue) { data = newData; link = linkValue; } } }
No comments:
Post a Comment