CSC262 Assignment #1

Due Date: Wednesday, Jan 13


In this assignment you will gain practice creating and testing user defined 'classes'. There are 3 programs, each one fairly short, but requiring you to be familiar with these features of C++ classes:

1. Moth

Write a Moth class that models a moth flying along a straight line on which point light sources may be placed. When a moth is created it will have an initial position on the line, the distance from 0. When the moth moves toward a light source placed at some other position, the moth changes its position to be the point halfway to the the light source.

Data members

Data members should in general be private.

The only data member should be the position of the moth. This should be of type double

Constructors:

You should declare 2 public constructors:

	    Moth();
	    Moth(double initialPosition);
	  

If the first constructor is used to create a moth, it should initialize the moth's position to be 0.

If the second constructor is used to create a moth, it should initialize the moth's position to be the parameter initialPosition.

Member functions

Since the position data member is private you will need a public function to return the position value. (Called an accessor or 'get' member function). This doesn't change the position.

Write a member function that will move the moth toward a light source position. This will change the moth's position member. (A member function that changes member data is called a mutator.)

You should declare two public member functions:

	    double getPosition();
	    void moveToLight(double lightPosition);
	  

Files

You should write two files to implement the Moth class:

For example, your moth.h file should have this form:

	    #ifndef MOTH_H
	    #define MOTH_H

	    class Moth
	    {
	      ...
            };
	    #endif
	  

and your moth.cpp file should begin like this:

	    #include "moth.h"

	    // Implementation of constructors and function members
            Moth::Moth()
            {
	      ...
            }
	    Moth::Moth(double initialPosition)
	    {
	      ...
	    }

	    double Moth::getPosition()
	    {
	      ...
	    }

	    void Moth::moveToLight(double lightPosition)
	    {
	      ...
	    }
	  

Test Program

You will need to write a 3rd file, testMoth.cpp, with a main function to test the Moth class. The test program should create one moth using the no argument constructor and move it toward two different light positions, and check that the moth's position is as expected. Repeat this with a second moth created with the other constructor.

2. RoachPopulation

Implement a class RoachPopulation that simulates the growth of a roach colony. The constructor takes the size of the initial roach population. The breed member function simulates a period in which the roaches bread, which doubles their population. The spray method simulates spraying with insecticide, which reduces the population by 10%. The getRoaches member function returns the current number of roaches.

A test program (name it roachSimulation.cpp) should simulate a population that starts with 10 roaches. Breed, spray, and print the expected roach count and the actual roach count returned by your member function. Repeat this 3 more times. (i.e., for a total of 4 times).

3. Car

Implement a class Car with the following properties. A car has a certain fuel efficiency ( miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor when a Car value is created and the fuel level is initially 0. Also provide a no argument constructor that assigns the efficiency of 18 miles/gallon.

The Car class should also have these member functions:

	void addGas(double gallons);
	void drive(double miles);
	double gasInTank();
      

Also write a main function in a file carTest.cpp with the code outlined below to provide a simple test of your Car class:

	Car myHybrid(50);
	Car rentalCar();
	
	myHybrid.addGas(20);
	rentalCar.addGas(20);

	myHybrid.drive(100);
	rentalCar.drive(100);

	// Now get the amount of gas left for each car.
	// Print the expected amount of gas and the actual amount
	// calculated by your gasInTank member function.
      

What to submit.

Submit one compressed (e.g. zip file) with only the .h and .cpp files for the three programs.

For each file be sure to include a comment near the top of the file with

Submit your compressed file on the Course Online site for assign1.