Thread: help with methods

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    help with methods

    i've been given an assignment, and an entire code. the only thing i have to do is code methods to make the program work. could someone give me an example of what one of these methods would be? i don't understand it. i have a ton to write, but i think once i understand what exactly a method is and how to use it, i can do the rest. this is the code that's given, and this is the assignment
    CSCI 240 - Spring 2010 - Program 11
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    //****************************************************************************
    //*
    //*                       The Time Class Definition
    //*
    //****************************************************************************
    
    class Time
    {
    public:
      Time();                     //default constructor
      Time(int, int, char);
        
      int toMilitary(int, int, char);
      void toStandard(int, int &, int &, char &);
    
      void display();
    
      void setTime(int, int, char);
      void setHours(int);
      void setMinutes(int);
      void setAMorPM(char);
      void setMilitary(int);
    
      int getHours();
      int getMinutes();
      char getAMorPM();
      int getMilitary();
    
    private:
      int hours;                  //hour value for standard time
      int minutes;                //minute value for standard time
      char AMorPM;                //indicates day or night
      int militaryTime;           //the military time
    };
    
    //****************************************************************************
    //*
    //*                             The main function
    //*
    //****************************************************************************
    
    int main()
    {
    Time t;
    
    //Test 1 -- default constructor
    cout << endl << "Test 1: Default constructor" << endl;
    t.display();
    
    
    //Test 2 -- setTime method
    
    cout << endl << endl << "Test 2: setTime method" << endl;
    t.setTime(1,29,'P');
    t.display();
    
    cout << endl;
    t.setTime(5,30,'P');
    t.display();
    
    cout << endl;
    t.setTime(15,15,'A');
    t.display();
    
    cout << endl;
    t.setTime(7,2,'A');
    t.display();
    
    cout << endl;
    t.setTime(1,64,'P');
    t.display();
    
    cout << endl;
    t.setTime(12,0,'P');
    t.display();
    
    cout << endl;
    t.setTime(1,23,'R');
    t.display();
    
    cout << endl;
    t.setTime(2,34,'A');
    t.display();
    
    
    //Test 3 -- toMilitary method
    
    cout << endl << endl << "Test 3: toMilitary method" << endl;
    cout << "5:42 AM is " << t.toMilitary(5,42,'A') << " in military time" << endl;
    cout << endl << "5:42 PM is " << t.toMilitary(5,42,'P') << " in military time" << endl;
    cout << endl << "12:00 AM is " << t.toMilitary(12,0,'A') << " in military time" << endl;
    cout << endl << "12:00 PM is " << t.toMilitary(12,0,'P') << " in military time" << endl;
    
    
    int hr, min;
    char p;
    //Test 4 -- toStandard method
    
    cout << endl << endl << "Test 4: toStandard method" << endl;
    t.toStandard(1720, hr, min, p);
    
    cout << "Military time 1720 is standard time hour = " << hr
         << "   minute = " << min << "   time of day = "  << p << "M" << endl;
    
    t.toStandard(0, hr, min, p);
    
    cout << "Military time    0 is standard time hour = " << hr
         << "   minute = " << min << "   time of day = "  << p << "M" << endl;
    
    t.toStandard(1200, hr, min, p);
    
    cout << "Military time 1200 is standard time hour = " << hr
         << "   minute = " << min << "   time of day = "  << p << "M" << endl;
    
    
    //Test 5 -- second constructor
    cout << endl << endl << "Test 5: second constructor" << endl;
    Time t2(1,23,'A');
    t2.display();
    
    
    //Test 6 -- set Methods
    cout << endl << endl << "Test 6: set Methods" << endl;
    cout << "The current time is: ";
    t2.display();
    
    t2.setHours(15);
    cout << endl << "  After using an invalid hour the current time is: ";
    t2.display();
    
    cout << endl << "  After using a valid hour the current time is: ";
    t2.setHours(4);
    t2.display();
    
    cout << endl;
    t2.setMinutes(-10);
    cout << endl << "  After using an invalid minute the current time is: ";
    t2.display();
    
    t2.setMinutes(10);
    cout << endl << "  After using a valid minute the current time is: ";
    t2.display();
    
    cout << endl;
    t2.setAMorPM('R');
    cout << endl << "  After using an invalid time of day the time is: ";
    t2.display();
    
    t2.setAMorPM('P');
    cout << endl << "  After using a valid time of day the time is: ";
    t2.display();
    
    cout << endl;
    t2.setMilitary(2400);
    cout << endl << "  After using an invalid military time the time is: ";
    t2.display();
    
    t2.setMilitary(1234);
    cout << endl << "  After using a valid military time the time is: ";
    t2.display();
    
    
    //Test 7 -- get Methods
    cout << endl << endl << "Test 7: get Methods" << endl;
    cout << "The current time in object t is: ";
    t.display();
    
    cout << endl << "  The hours value is " << t.getHours();
    cout << endl << "  The minute value is " << t.getMinutes();
    cout << endl << "  The time of day value is " << t.getAMorPM();
    cout << endl << "  The military time is " << t.getMilitary();
    
    cout << endl << endl << "The current time in object t2 is: ";
    t2.display();
    
    cout << endl << "  The hours value is " << t2.getHours();
    cout << endl << "  The minute value is " << t2.getMinutes();
    cout << endl << "  The time of day value is " << t2.getAMorPM();
    cout << endl << "  The military time is " << t2.getMilitary();
    
    
    
    cout << endl;
    system("pause");
    return 0;
    } // end of main
    
    
    //****************************************************************************
    //*
    //*                    Code your methods below this box
    //*
    //****************************************************************************

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    A method is a function.

    Basically you need to implement the meat of that class to make it preform the action.

    For example
    Code:
    void Time::setTime(int, int, char)
    {
    }
    That is how you would create the method for setTime. The insides are up to you to code, since this is a homework assignment.

    Feel free to ask if you have further questions.

    PS: It worries me that you don't know what a method is especially that your instructor is giving you assignments that deal with classes.
    Woop?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    ha, tell me about it. it worries me too. this is my last assignment fortunately. thanks for the tip, i'll get to work on this beast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. built-in methods
    By Aisthesis in forum C++ Programming
    Replies: 10
    Last Post: 12-22-2009, 11:07 PM
  2. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 02:52 AM
  3. Turn Based Stradegy System Methods
    By TylerMoyer in forum Game Programming
    Replies: 2
    Last Post: 07-30-2007, 10:45 PM
  4. Lesson #5 - Methods
    By oval in forum C# Programming
    Replies: 1
    Last Post: 05-04-2006, 03:09 PM
  5. Replies: 8
    Last Post: 07-27-2003, 01:52 PM