Introduction to Programming (22C:16, 22C:106)

Homework 4: Due Thursday, 2/27/97

Problem 1: [5 points]

 
	#include <iostream.h>
	#include "balloon.h"
	
	int main()
	{
	
		Balloon trip;
		int rise;	// how high to fly
		int duration;	// how long to cruise
		
		cout << "Welcome to the windbag emporium."<<endl;
		cout << "You'll rise up, cruise a while, then descend."<<endl;
	 	cout << "How high (in meters) do you want the balloon to rise? ";
		cin >> rise;
		
		cout << "How long (in seconds) do you want the balloon to cruise? ";
		cin >> duration;
		
		trip.Ascend(rise);	// ascend to specified height
		trip.Cruise(duration);	// cruise for specified time steps
		trip.Descend(rise/2);	// descend to half the initial altitude
		trip.Cruise(duration);	// cruise for specified time steps
		trip.Descend(0);	// descend to earth
		
		return 0;		

}

Problem 2: [15 points]


 
        1.  The Burn() function accomplishes two tasks.  First, it increases the
        value of myAltitude by ALT_CHANGE (ALT_CHANGE is a constant variable equal to
        10).  Second, the BurnMessage() function is called to print a message.
        
        

 
	2.	void Balloon::Burn()
		{
			if(myAltitude <= 50)
				myAltitude = myAltitude + ALT_CHANGE;
				
			else
				myAltitude = myAltitude + 15;
				
			BurnMessage();
		}


	3.  First, move the following function headers from the private to the public
	section of Balloon.h:

			void Burn();
			void Vent();
			void AltitudeMessage();
	
	Then adapt fly.cc so that it calls Burn() and Vent() instead of Ascend() and
	Descend().  Note that the GetAltitude() function returns the balloon's current height 
	(i.e. the value of the private variable, myAltitude).
	
	#include <iostream.h>
	#include "balloon.h"
	
	int main()
	{
	
		Balloon red_balloon;
		int rise;	// how high to fly
		int duration;	// how long to cruise
		
		cout << "Welcome to the windbag emporium."<<endl;
		cout << "You'll rise up, cruise a while, then descend."<<endl;
	 	cout << "How high (in meters) do you want the balloon to rise? ";
		cin >> rise;
		
		cout << "How long (in seconds) do you want the balloon to cruise? ";
		cin >> duration;
		
		cout << "***** (Height =  0) Ascending to "<< rise << " meters *****"<<endl;
		
		while(red_balloon.GetAltitude() < rise)
		{
			red_balloon.AltitudeMessage();
			red_balloon.Burn();
		}
		
		red_balloon.Cruise(duration);
		
		cout << "***** (Height = "<< rise << ") Descending to 0 meters *****"<<endl;
		
		while(red_balloon.GetAltitude() > 0)
		{
			red_balloon.AltitudeMessage();
			red_balloon.Vent();
		}
		
		return 0;		
	}