HW 3: Solutions

3.6
  1. ADT Calendar is Data The year and a logical variable leapyr that indicates if the year is a leap year. Due to a changing of the calendar in history we assume use of the current Gregorian calendar. Operations Constructor Initial Value: The Year. Process: Use the initial value to specify the value of the year and then call the method Leapyear to initialize the data value leapyr. NumDays Input: Pass a month mm in the range 1 to 12 and a day dd in the range 1 to days in the month. Invalid input results in an error. Preconditions: None. Process: Determine the number of days from Jan 1 to the specified date. Output: Return the number of days. Postconditions: None. Leapyear Input: None. Preconditions: None. Process: Retrieve the value of the leapyr. Output: Return the value of the leapyr. Postconditions: None. PrintDate(ndays) Input: None. Preconditions: None Process: Retrieve the value of the leapyr. Output: Return the value of the leapyr. Postconditions: None. PrintDate(ndays) Input: Variable ndays that indicates the number of days in the year from January 1. Preconditions: ndays is in range 1<=ndays<=365(366) Process: Print the date in format mm/dd/yy Output: None Postconditions: None. end ADT calendar
  2. #include <iostream.h>
    #include <stdlib.h>
    
    class Calendar
    {
    	private:
    		int year;
    		int leapyr;
    	public:
    		Calendar(int y);
    		int NumDays(int mm, int dd)const;
    		int Leapyear(void)const;
    		void PrintDate(int ndays)const;
    };
    Calendar::Calendar(int y):year(y)
    {
    	//a leap year if divisible by 4 and a century not divisible by 400
    	leapyr=(y%4==0)&&(y%100!=0||y%400==0)
    }
    
    int Calendar::NumDays(int mm,int dd)const
    {
    	int daysInMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31}
    	int ndays=0;
    	
    	//February has 29 days in a leap year.
    	if(leapyr)
    		daysInMonth[2]=29;
    
    	if(mm<1||mm>12)
    	{
    		cerr<<"Month is out of range 1-12" << endl;
    	exit(1);
    	}
    	if(dd<1||dd>daysInMonth[mm])
    	{
    		cerr << "Day is out of range 1-"<< daysInMonth[mm-1]
    		exit[1];
    	}
    	//for January ndays=dd
    	if(mm==1)
    		ndays=dd;
    	else
    	{
    		//if mm>1, compute number of days in preceding months,
    		//then add in dd.
    		for(int i=1;i<m;i++)
    			ndays+=daysInMonth[i];
    		ndays+=dd;	
    	}
    	return ndays;
    }
    
    int Calendar:: Leapyear(void)const
    {
    	return leapyr;
    }
    void Calendar::PrintDate(int ndays)const
    {
    	int mm=1,dd;
    
    	int daysInMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31}
    	int yearlength=365;
    
    	//February has 29 days in a leap year and year has 366 days
    	if(leapyr)
    	{
    		daysInMonth[2]=29;
    		yearLength=366;
    	}
    	if(ndays<1||ndays>yearLength)
    	{
    		cerr <<"Day is out of range1-"<<yearLength<<endl;
    		exit(1);
    	}
    
    	//if number of days<=31, month is January
    	if(ndays<=31)
    		mm=1;
    	else
    		//decrement ndays by the number of days in the succession of 
    		//months from January until ndays is less than the number of 
    		//days in a month. This is the month, and ndays is the number 
    		//of days into that month.
    		while(daysInMonth[mm]<ndays)
    			ndays-=daysInMonth[mm++];
    		cout<<"Date is"<<mm<<'/'<<ndays<<endl;
    }
    
    

3.7

  1. Student name, major, proposed year of graduation, gpa <not done>
  2. State capital, population, area, governor.
    class State
    {
    	private:
    		char StateName[16], capital[16],governor[28];
    		int population;
    		float area;		//measured in square miles
    	public:
    		State(char stname[], char cap[], char gov[], int pop, float ar);
    	//print name of the governor and the capital
    	void PrintGovernmentInfo(void);
    
    	//density is ratio of population to land area.
    	float Density(void);
    	....
    }
    
  3. A cylinder. Allow modification of both radius and height and include computation of the surface area and volume. <not done>
3.10
  1. < A test program is included >
    #include <iostream.h>
    #include <string.h>
    #include <strstream.h>
    #inlude <stdlib.h>
    
    class Date
    {
    	private:
    		//private members that specify the date.
    		int month, day, year;
    	public:
    		//constructors. Default date is January 1 1900.
    		Date(int m=1, int d=1, int  y=0);
    		Date(char *dstr);
    
    		// Output the date in format "month, day, year"
    		void PrintDate(void);
    		Date IncrementDate(int n) const;
    };
    
    //constructor. month day year given as string "mm/dd/yy"
    Date::Date(char *dstr)
    {
    	char inputBuffer[16];
    	char ch;
    
    	//copy the string to inputBuffer and declare an array based input stream.
    	strcpy(inputBuffer,dstr)
    	istrstream input(inputBuffer, sizeof(inputBuffer));
    	
    	//read from input stream.use ch to read the '/' characters.
    	input >> month >> ch >> day >> ch >> year;
    	year+=1900;
    }
    
    //print date with full month name.
    void Date:: PrintDate(void)
    {//allocate static array of month names. Index 0 is NULL string.
    	static char *Months[]={"","January","February","March",
    				"April", "May", "June", "July", 
    				"August","September", "October", "November",
    				"December"};
    	cout << Months[month] << " " << day << "," <<year;
    }
    int IsLeapYear(int y)
    {
    	if(y%4!=0||(y%100==0 && y%400!=0))
    		return 0;
    	else
    		return 1;
    }
    Date Date::IncrementDate(int n) const
    {
    	int tempday;
    	int daysInMonth[13]={0, 31, 28,31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	Date d(month, day, year-1900); // has same date as current object.
    	
    	if(n<0 || n>365)
    	{
    		cerr << "Increment" << n << "is out of range"" << endl;
    		exit(1);
    	}
    	if(month>1)		//current month after January
    	{
    		if(IsLeapYear(year))
    			daysInMonth[2]=29;	//next year is a leap year.
    	}
    	else
    		if(IsLeapYear(year))
    			daysInMonth[2]=29;
    		
    	tempday=day+n;
    
    	while(tempday>daysInMonth[d.month])
    	{
    		temday-=daysInMonth[d.month];
    		if(d.month!=12)			//current month not December
    			d.month++;
    		else
    		{
    			d.year++;
    			d.month=1;		//set month to January of next year.
    		}
    	}
    	d.day=tempday;
    	return d;
    }
    
    void main(void)
    {
    	Date d1(1, 10, 92), d2(1, 10, 93), d3(12, 1, 92), d4(12, 1, 91);
    	Date d;
    
    	d=d1.IncrementDate(50);		d.PrintDate();cout << endl;
    	d=d2.IncrementDate(50);		d.PrintDate();cout << endl;
    	d=d3.IncrementDate(91);		d.PrintDate();cout << endl;
    	d=d4.IncrementDate(91);		d.PrintDate();cout << endl;
    }
    
    /*
    <Run>
    February 29, 1992
    March 1, 1993
    March 2, 1993
    March 1, 1992
    
3.13 The function DateInterval must assume that the two calendar dates are given as (mm1, dd1) and (mm2,dd2) within the calendar year. These four (4) parameters are passed along with the Calendar object which can then determine the number of days into the year for each date.
int DayInterval(Calendar c, int mm1, int dd1, int mm2, int dd2)
{
	return c.NumDays(mm2,dd2)-c.NumDays(mm1,dd1);
}