Sie sind auf Seite 1von 5

OOP Assignment

Name = Awais

Roll no. = 036

Questions & Answers

Q 1: . You’re probably wearing on your wrist one of the world’s most common types of objects — a watch. Discuss
how each of the following terms and concepts applies to the notion of a watch: object, attributes, behaviors, class,
inheritance (consider, for example, an alarm clock), abstraction, modeling, messages, encapsulation, interface and
information hiding.

Solution: The entire watch is an object that is composed of many other object. Watch attributes are time, color,
band, style, etc. The behaviors of the watch include setting the time and get- ting the time. A watch can be
considered a specific type of clock. With that in mind, it is possible that a class called Clock could exist from which
other classes such as watch and alarm clock could inherit the basic features in the clock. The watch is an abstraction
of the mechanics needed to keep track of the time. The user of the watch does not need to know the mechanics of
the watch in order to use it; the user only needs to know that the watch keeps the proper time. In this sense, the
mechanics of the watch are encapsulated inside the watch. The interface to the watch allows the user to set and
get the time. The user is not allowed to directly touch the internal mechanics of the watch. All interaction with the
internal mechanics is controlled by the interface to the watch. The data members stored in the watch are hidden
inside the watch and the member function provide the interface to the data.

Q 2: Some online email services save all email correspondence for some period of time. Suppose adisgruntled
employee of one of these online email services were to post all of the email correspondences for millions of people,
including yours, on the Internet. Discuss the issues.

Solution : As you can imagine, the amount of storage needed for the big Webmail services is truly massive.
Webmail uses a tiered, secure and highly distributed architecture. The front end handles the authentication and
user interface. The next tier manages the retrieval of messages and attachments from the server farms. The
messages that belong to a given account don't necessarily reside on the same server, and are not stored
contiguously. This tier is the only one that can "assemble" an email account. And, it will only do so for a properly
authenticated user. The back end - i.e., storage - is itself tiered. The content is compressed and only decompressed
upon retrieval by the middle tier. Passwords are encrypted. Without the password, the middle tier cannot get your
messages. There are hundreds of engineers working on a given service. They work on discrete parts of the overall
system and few have the ability to access the middle tier. The system itself is designed to be secure precisely to
prevent the scenario you describe. And, the security team is constantly monitoring for inappropriate behavior. This
doesn't get into the send/receive aspects of the service. Because SMTP sends messages in clear text, one might
install a sniffer to detect the headers for a given account and assemble messages that way. But again, this would
require the complicity of the entire mail server team, which won't happen. You should be far more concerned that
most email messages are out there in clear text on the Internet for all to read..

1
Q 3: As a programmer in industry, you may develop software that could affect people’s health or even their lives.
Suppose a software bug in one of your programs were to cause a cancer patient to receive an excessive dose during
radiation therapy and that the person is either severely injured or dies. Discuss the issues.

Solution: A lot of professions have risks. For example, A food manufacturer can introduce a harmful bacteria/virus
to their product. A distracted bus/taxi driver can also kill people. Besides, a programmer in an industry like health
care doesn't program alone (mostly). They work with a team of people like project managers, testers, etc. There's a
process of testing. In addition, I think hospitals have a trial run for these types of software solutions.

Q 4: Some scientists believe that carbon emissions, especially from the burning of fossil fuels, contribute
significantly to global warming and that this can be combatted if individuals take steps to limit their use of
carbonbased fuels. Organizations and individuals are increasingly concerned about their “carbonfootprints.”
Websites such as TerraPass www.terrapass.com/carbon-footprint-calculator/ and CarbonFotoprint
www.carbonfootprint.com/calculator.aspx

carbon footprint calculators. Test drive these calculators to determine your carbon footprint and list them on
paper.

Solution:

Q 5: By recent estimates, two-thirds of


the people in the United States are
overweight and about half of those are
obese. This causes significant increases in illnesses such as diabetes and heart disease. To determine whether a
person is overweight or obese, you can use a measure called the body mass index (BMI). The United States
Department of Health and Human Services provides a BMI calculator at www.nhlbisupport.com/bmi/. Use it to
calculate your own BMI.

2
Write code for following programs.

Q 1: Write a C++ program to check whether a number is even or odd using ternary operator.

Solution:

#include<iostream> using
namespace std;
int main(). { int n;
cout<<"Enter any number\n";
cin>>n;
n%2==0? cout<<"Number is even\n": cout<<" Number is odd"; }

Q 2: Write a C++ program to print pyramid as given below:


1

232

34543

4567654567898765

Solution:

#include <iostream>
using namespace std;

int main()
{
int rows, count = 0, count1 = 0, k = 0;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{ for(int space = 1; space <= rows-i; ++space)
{ cout << " ";
++count; } while(k != 2*i-1)
{ if (count <= rows-1)

3
{ cout << i+k << " ";
++count } else
{ ++count1;
cout << i+k-2*count1 << " ";
} ++k; }
count1 = count = k = 0;
cout << endl; } return 0;
}
Q 3: Write a C++ program to make a simple calculator to Add, Subtract, Multiply or Divide using switch
and case statements.

Solution:
#include<iostream>
using namespace std; int
main ()
{ int a,b,c,n;
float d;
cout<<"Enter two numbers"<<endl;
cin>>a>>b;
cout<<"Which operation do you want to perform"<<endl;
cout<<" 1 for addition\n 2 for subtraction \n 3 for multiplication\n 4 for division\n";
cin>>n;
switch(n)
{ case 1:
cout<<"Addition of"<<a<<" & "<<b<<" is : "<<a+b;
break;
case 2:
cout<<"Subtraction of"<<a<<" & "<<b<<" is: "<<a-b;
break;
case 3:
cout<<"Multiplication of "<<a<<" & "<<b<<" is: "<<a*b;
break;
case 4: if(b==0)
cout<<"Infinity";
else
{ d=(float) a/ (float) b;
cout<<"Division of "<<a<<" & "<<b<<
" is: "<<d;
break; } } }
Q 4: Write a C++ program to check whether a number is Prime or not.

Solution
#include<iostream> using
namespace std; void
prime_no(int);
int main()
{ int n;
cout<<"Enter any number\n";
cin>>n;
prime_no(n); } void
prime_no(int x)
{ int i,m=0, t=0,sum=0;
m=x/2;
for(i=2;i<m;i++) {
if(x%i==0)
{ cout<<x<<" is not Prime number";
t=1;

4
break; } }
if(t==0)
cout<<"\n a prime number"; }
Q 5: Write a C++ program to find the factorial of a number.

Solution:
#include<iostream>
using namespace std; int
main()
{ int a,b,fact=1;
cout<<"enter a num:";
cin>>a;
for(b=1;b<=a;b++)
{ fact=fact*b; }
cout<<"\nfact of "<<a<<" is "<<fact; return 0; }