Jump to content

I'm having trouble with a simple program

- - - - -

  • Please log in to reply
2 replies to this topic

#1
elihylton

elihylton

    Newbie

  • Members
  • Pip
  • 2 posts
I'm creating a class please help! Below is how far i gotten.


That is the data. The methods breed and spray will change the population value. Breed doubles the population, and spray reduces it by 10%.

breed and spray are NOT data. They are methods that do a job with not input help.

Change your class.

1) The only data is the number of roaches. Call it roaches.
2) The first constructor will set the data to 0
3) The second constructor will take one input only from the user and use it to set the value of roaches
4) The method getRoaches does not need inputs. It just returns the number of roaches.
5) The method breed does not take inputs (it is void) it just reduces the value of roaches by 10%
6)
function toString() {
[native code]
}
() returns the state, which is the value of the data you will call roaches.

remember that a method that starts with get ==> will return something but takes not inputs.
for example public int getPrice() { return price; }
the method returns price which has a type of int

you should use methods getRoaches(), spray(), breed() and
function toString() {
[native code]
}
()


That's the instructions - and it seems tedious! By the way I'm using DR.Java



public class Roaches

{
private int sizeofroach;

Roaches()
{
int breed = 0;
}
Roaches(int size)
{
size = size - 10%;
}
public int breed()
{
return breed;
}
public getSpray()
{
return spray = size;
}
public void getRoaches()
{
return getRoaches = getRoaches * 2;
}
public String toString()
{
return "Roaches count: = " + getRoaches;
}

}

#2
elihylton

elihylton

    Newbie

  • Members
  • Pip
  • 2 posts
Project Specifications
1. The class RoachPopulation keeps track of the size of the population of roaches.
2. The constructor takes the size of the initial roach population.
3. Overload the constructor to allow for a default initial value.
4. Provide a method breed to simulate a period where the population doubles.
5. Provide a method spray to simulate spraying with insecticide. When we call this method, the population will automatically be reduced by 10%.
6. Provide a method getRoaches() that returns the current value for the population of roaches.
7. Provide a program called RoachSimulation to simulate a kitchen that starts out with some roaches.
8. Breed, spray and then print the roach count. Repeat several times.
9. Please use comments to display your name, date and assignment number.
10. Provide documentation comments for your class RoachPopulation. Make sure JavaDoc produces a usable documentation file.

#3
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
RoachPopulation:
package net.codecall;

public class RoachPopulation {
    private int roaches;

    public RoachPopulation(int roaches) {
	this.roaches = roaches;
    }

    public RoachPopulation() {
	this(0);
    }

    public int getRoaches() {
	return roaches;
    }

    public void breed() {
	roaches *= 2;
    }

    public void spray() {
	roaches *= 0.9;
    }
}
RoachSimulation:
package net.codecall;

public class RoachSimulation {
    /**
     * @param args
     */
    public static void main(String[] args) {
	RoachPopulation population = new RoachPopulation(10);

	for (int i = 0; i < 5; i++) {
	    System.out.println(String.format("Iteration %d", i + 1));
	    System.out.println(String.format("  Initial number of roaches: %d", population.getRoaches()));

	    population.breed();
	    population.spray();

	    System.out.println(String.format("  After breed and spray: %d", population.getRoaches()));
	}
    }
}
I hope you can add comments yourself :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users