Make your own free website on Tripod.com

Lab 1 : 23/2/99

Ubahsuai class Date untuk melaksanakan pemeriksaan sintak ke atas nilai awal ahli data         month, day dan year. Juga tambah ahli fungsi nextDay untuk menambahkan day dengan 1. Object Date mesti sentaiasa dalam keadaan konsisten. Tulis satu main aturcara yang menguji fungsi nextDay yang akan mencetak tarikh sepanjang gelungan. Ujian anda mestilah berdasarkan perkara berikut :

  1. penambahan kepada bulan berikutnya
  2. penambahan kepada tahun berikutnya

Gabungkan class Time yang telah diubahsuai( dari assignment 3) dan class Date yang telah diubahsuai (dari soalan di atas ) dengan menambahkan satu kelas iaitu DateAndTime. Ubahsuai fungsi tick supaya boleh panggil fungsi nextDay jika masa ditambah kepada hari berikutnya. Ubahsuai fungsi printStandard dan printMilitary untuk mencetak tarikh yang mempunyai penambahan masa. Tulis satu main aturcara untuk menguji kelas baru DateAndTime. Khususkan ujian anda terhadap penambahan masa kepada hari berikutnya (next day).
 
Lab 1 mesti dihantar setelah tamat waktu lab anda.



 class Date

            #include <iostream.h>
 
            //simple Date class
            class Date {
                public :
                    Date( int =1, int = 1, int = 1990 )              // default constructor
                    void print();
                private :
                    int month;
                    int day;
                    int year;
            };

            // simple Date constructor with no range checking
            Date :: Date( int m, int d, int y ){
                month = m;
                day = d;
                year = y;
            }

            // Print the Date in the form mm-dd-yyyy
            void Date :: print(){
                cout << month << '-' << day << '-' << year;
            }

            main() {
                Date d1(7, 4, 1993), d2;    // d2 defaults to 1/1/1990
                cout << "d1=";
                d1.print();
                cout << "\nd2 = ";
                d2.print();
                d2 = d1;                            // assignment by memberwise copy
                cout << "\n\nAfter default memberwise copy, d2= ";
                d2.print();
                cout << endl;
                return 0;
            }
 



class Time yang belum diubahsuai (pengubahsuaian bergantung kepada assignment 3 anda)

            //Member function definitions for Time class
#include "time3.h"
#include <iostream.h>

//constructor function to initialize private data
//calls member function setTime to set variables
//Default values are 0 (see class definition).

Time::Time(int ht, int min, int sec)
 { setTime(hr,min, sec);}

//Set the values of hour, minute, and second
void Time::setTime(int h, int m, int s)
{
 setHour(h);
 setMinute(m);
 setSecond(s);
}

//Set the hour value
void Time::setHour(int h)
 { hour = (h >= 0 && h < 24) ? h :0;}

//Set the minute value
void Time::setMinute(int m)
 {minute = (m >= 0 && m < 60) ? m : 0;}

//Set the second value
void Time::setSecond(int s)
 {second = (s >= 0 && s< 60) ? s:0;}

//Get the hour value
int Time::getHour() { return hour;}

//Get the minute value
int Time::getMinute() {return minute;}

//Get the second value
int Time::getSecond() {return second;}

//Print time is military format
void Time::printMilitary()
{
 cout<< (hour < 10 ? "0" : "" ) << hour << ":"
   << (minute < 10 ? "0" : "") << minute;
}

//Print time in standard format
void Time::printStandard()
{
 cout << (( hour ==0 || hour == 12)?12 : hour %12)
   << ":" << (minute < 10 ? "0" : "") << minute
   << ":" << (second < 10 ? "0" : "") << second
   << (hour < 12 ? " AM" : " pm" );
}

===================================================

//Demonstrating the Time class set and get functions
#include <iostream.h>
#include "time3.h"

void incrementatMinutes( Time &, const int );

int main()
{
 Time t;

 t.setHour(17);
 t.setMinute(34);
 t.setSecond(25);

 cout << "Result of setting all valid values:\n"
   << " Hour: " << t.getHour()
   << " Minute: " << t.getMinute()
   << " Second: " << t.getSecond();

 t.setHour(234);  //invalid hour set to 0
 t.setMinute(43);
 t.setSecond(6374); //invalid second set to 0

 cout << "\n\nResult of attempting to set invalid hour and "
   << " second:\n Hour: " << t.getHour()
   << " Minute: " << t.getMinute()
   << " Second: " << t.getSecond() << "\n\n";

 t.seTime(11,58,0);
 incrementMinute(t,3);

 return 0;
}

void incrementMinutes( Time &tt, const int count)
{
 cout << "incrementing minute " << count
   << " time:\nStart time: ";
 tt.printStandard();

 for (int i=0; i < count; i++) {
  tt.setMinute( (tt.getMinute() + 1) % 60);

  if (tt.getMinute() ==0)
   tt.setHour(( tt.getHour() + 1) % 24);

  cout << "\nminute + 1: ";
  tt.printStandard();
 }
 cout << endl;
}