|
Homework 4
1. Prime Number Seive - the purpose is to determing all of the numbers
between 1 and 100 that are prime.
- This program should create an array of size 100 (representing the
numbers from 0-99). It should be an array of bools, each space indicating
whether its index (the number of the space) is prime or non-prime. Start
by assuming all numbers are prime.
- To check if is number is not prime, you check to see if it is
divisible by
anything larger than 1 and smaller than itself.
- Write a loop that starts at 2 and goes up to 10
call the number your loop increments the index. we will
check each number in our array to see if it is divisible by this index.
when the loop is finished, we will have done all the necessary work to
determine which numbers are prime and which are not.
(note: 10 is the
square root of 100. if we cant to find all of the primes from 0-100, why
do we stop dividing at 10? you must answer this question in the comments
for your code).
- If a number is divisible by our index and is not equal to our index,
we know it is not prime. Mark the corresponding space in the array false.
- After the loop is done, go through the array in another loop. If the
space we are looking at is marked true, print out the index number (the
number of the space we are in, ie, the 5th space). This will print out a
list of every prime number from 1 to 100.
2. The Grasshopper class - slightly less offensive than cockroaches.

- Create a class called Grasshopper. it shold contain some variables
like hunger (how hungry our grasshopper is on a scale from 1 to 10),
reproduction (a boolean indicating whether or not our grasshopper has
mated), and health (also a 1 to 10 scale). The class should also have
several member methods. These should include hop() which makes the
grasshopper hop, and probably makes it hungrier (do whatever you like, but
be sure to do something), eat(), and mate() which do the corresponding
things.
- Make all of your member variables private and write accessor functions
to return their values.
- Write constuctor functions which let you set initial variables, as
well as a default constructor.
- Write a class Main, which contains a main method. In that method,
create in instance of a grasshopper named whatever you like ( be
creative). Print out the initial values of his/her variables, make it do
something, and print out the new values.
|