Latest Activity In Study Groups

Join Your Study Groups

VU Past Papers, MCQs and More

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.

Uploading instructions

 

  • For clarity and simplicity, you are required to Upload/Submit only one .CPP file

Objective

The objective of this assignment is:

 

  • To give you the idea of practical implementation of class with attributes and functions

 

For any query about the assignment, contact at CS304@vu.edu.pk

 

GOOD LUCK

Problem Statement:

 

Consider the following attributes and functions of the class Time:

Attributes:

  • int hour
  • int minute
  • int seconds

Functions:

  • void setTime(int, int, int);
  • void show12Hour();
  • void show24Hour();

 

Write a program to instantiate the object of a class Time from the main() function and constructor is called of the class Time automatically to initialize the private data members of the class with 0. Time is printed on the screen in 12 hours format and 24 hours format as follows:

 

Initial Time:

 

Time display in 12 hours format: 12:00:00 AM

Time display in 24 hours format:  00:00

 

After that use the setTime() function to set the time  by passing the 3 attributes of time i.e setTime(13,55,23) and show the time again after setting:

 

After set:

 

Time display in 12 hours format: 01:55:23 PM

Time display in 24 hours format: 13:55

 

Now try to use setTime() function for setting invalid values i.e setTime(25,67,98) . Your program should not set invalid values of time and show finally the time as initial.

 

Time display in 12 hours format: 12:00:00 AM

Time display in 24 hours format: 00:00

 

 

 

 

 

Views: 6388

Attachments:

Replies to This Discussion

CS 304 1st Assignment 2015 Solution 

‪#‎include‬ <iostream>

using namespace std;

class Time
{
private:
int hour;
int minute;
int seconds;

public:
// default constructor
Time()
{
hour = 0;
minute = 0;
seconds = 0;
}

void setTime(int h, int m, int s)
{
if ( (h >= 0 && h < 24) &&
(m >= 0 && m < 60) &&
(s >= 0 && s < 60) )
{
hour = h;
minute = m;
seconds = s;
}
else
{
hour = 0;
minute = 0;
seconds = 0;
}
}

void show12Hour()
{
string unit = "";
string colon = ":";

cout "Time display in 12 hours format: ";
// setting units
if (hour > 12) {
unit = " PM";
} else if (hour <= 12 && hour > 0) {
unit = " AM";
}
// displaying hours with formatting
if ( (hour == 0 || hour == 12) ) {
cout 12 colon;
} else {
cout hour % 12 colon;
}
// displaying minutes with formatting
if (minute < 10 ) {
cout "0" minute colon;
} else {
cout "" minute colon;
}
// displaying seconds with formatting
if (seconds < 10 ) {
cout "0" seconds unit;
} else {
cout "" seconds unit ;
}
} // end of show24Hour method

void show24Hour()
{
string colon = ":";

cout "\nTime display in 24 hours format: ";
// displaying hours with formatting
if (hour < 10 ) {
cout "0" hour colon;
} else {
cout "" hour colon;
}
// displaying minutes with formatting
if (minute < 10 ) {
cout "0" minute;
} else {
cout "" minute ;
}
cout endl;
} // end of show24Hour method
}; // end of Time class

int main()
{
Time time;

cout "Initial time" endl;
time.show12Hour();
time.show24Hour();

time.setTime(13,55,23);
cout "After set" endl;
time.show12Hour();
time.show24Hour();

time.setTime(25,67,98);
cout "After setting invalid values" endl;
time.show12Hour();
time.show24Hour();

cin.get();
return 0;
}

there is some error n coding .... 

Sahrish MCS 2nd check mine attchment

#include<iostream>
using namespace std;

class Time
{
public:
Time()
{
hour = 00;
minute = 00;
seconds = 00;
}
void setTime(int h, int m, int s)
{
hour = h;
minute = m;
seconds = s;
}
void show12Hour()
{
int h, m, s;
h = hour;
m = minute;
s = seconds;
if(h>12 && h<24 && m>=00 && m<60 && s>=00 && s<60)
{
h -= 12;
couth":"m":"s" PM"endl;
}
else if (h>12 && h<24 && m >59 && s >= 00 && s < 60)
{
h -= 12;
m = 00;
cout h ":" m ":" s " PM " endl;
}
else if (h >12 && h < 24 && m >=00 && m <60 && s >59)
{
h -= 12;
s =00;
couth":"m":"s" PM"endl;
}
else if (h >12 && h < 24 && m >59 && s >59)
{
h -= 12;
s =00;
m =00;
couth":"m":"s" PM"endl;
}
else if (h>=01 && h<=12 && m>=00 && m <60 && s >=00 && s<60)
{
couth":"m":"s" AM"endl;
}
else if (h >= 01 && h <= 12 && m >59 && s >= 00 && s < 60)
{
m =00;
couth":"m":"s" AM"endl;
}
else if (h >= 01 && h <= 12 && m >=00 && m <60 && s >59)
{
s =00;
couth":"m":"s" AM"endl;
}
else if (h >= 01 && h <= 12 && m >59 && s >59)
{
m=00;
s=00;
couth":"m":"s" AM"endl;
}
else if ((h <= 0 || h > 24) && m >59 && s >59)
{
h = 12;
m =00;
s = 00;
couth":"m":"s" AM"endl;
}
else if ((h <= 0 || h > 24) && m <60 && s >59)
{
h = 12;
s = 00;
couth":"m":"s" AM"endl;
}
else if ((h <= 0 || h > 24) && m >59 && s<60)
{
h = 12;
m = 00;
couth":"m":"s" AM"endl;
}
else if ((h <= 0 || h > 24) && m >59 && s>59)
{
h = 12;
m = 00;
s = 00;
couth":"m":"s" AM"endl;
}
else if (h <= 0 && m <=0 && s <= 0)
{
h = 12;
m =00;
s = 00;
couth":"m":"s" AM"endl;
}
}
void show24Hour()
{
int h, m;
h = hour;
m = minute;

if(h > 24)
{
h = 00;
}
if(m > 59)
{
m = 00;
}
if (h < 01 && m < 01 )
{
h = 00;
m = 00;
}
couth":"mendl;
}

private:
int hour;
int minute;
int seconds;
};


int main()
{
Time t;
cout"Initial Time"endl;
cout"Time display in 12 hours format: ";
t.show12Hour();
cout"Time display in 24 hours format: ";
t.show24Hour();

t.setTime(13,55,23);
cout"Time display in 12 hours format: ";
t.show12Hour();
cout"Time display in 24 hours format: ";
t.show24Hour();

t.setTime(23,78,63);
cout"Time display in 12 hours format: ";
t.show12Hour();
cout"Time display in 24 hours format: ";
t.show24Hour();

t.setTime(25,67,98);
cout"Time display in 12 hours format: ";
t.show12Hour();
cout"Time display in 24 hours format: ";
t.show24Hour();
}

#include <iostream>
using namespace std;
class Time {
public:
Time(); // default constructor
void setTime(int,int,int);
void show24hour();
void show12hour();
private:
int hour;
int minute;
int second;
};
Time::Time() { hour = minute = second = 0; }


void Time::setTime(int h,int m,int s)
{
hour = (h >= 0 && h < 24) ? h : 0;
minute = (m >= 0 && m < 60) ? m : 0;
second = (s >= 0 && s < 60) ? s : 0;
}
// show time in 12 hour format
void Time::show12hour()
{
cout ((hour == 0 || hour == 12) ?12 : hour % 12) ;
cout ":" (minute < 10 ? "0" : "") minute ;
cout ":" (second < 10 ? "0" : "") second ;
cout (hour < 12 ? " AM" : " PM");
}
// show time in 24 hour format
void Time::show24hour()
{
cout (hour < 10 ? "0" : "") hour ":" ;
cout (minute < 10 ? "0" : "") minute ;

}


//main simple class Time
int main()
{

Time t; // instantiate object t of class Time
cout "initial time " endl;
cout "Time display in 12 hours format: ";
t.show12hour();
cout "\nTime display in 24 hours format:";
t.show24hour();

t.setTime(13,55,23);
cout "\nAfter set: ";
cout "\nTime display in 12 hours format: ";
t.show12hour();
cout "\nTime display in 24 hours format: ";
t.show24hour();
t.setTime(25,67,98);


cout "\nAfter Setting invalid Values:\n" ;

t.show12hour();
cout "\n" ;
t.show24hour();

cout endl;
return 0 ;
}

AOA.. Any one help me plzzzzzzzzzzz mera program compile ho jata ha bt output blink kr k ghaib ho jati ha koi help kr dy plzzz

Use 

system("PAUSE");

at the end of the program.

  • type in the end of program


cout endl; 
return 0 ;
}

its already exist.

 

Nahi aa rae out put

have u applied system("Pause");??

 

what is actual solution

there is some error Tariq bhai

RSS

Looking For Something? Search Below

Latest Activity

© 2022   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service