Sri Krishna College of Engineering and Technology
Name :Dinesh Karthikeyan G M Email :23ee026@skcet.ac.in
Roll no:23ee026 Phone :9942227000
Branch :SKCET Department :EEE-A
Batch :2023_27 Degree :BE-EEE
2023_27_I_PS using CPP_IRC
Class_Practise_Home
Attempt : 1
Total Mark : 80
Marks Obtained : 76
Section 1 : Coding
1. Problem Statement:
Calendar
Create a class DateTime with the following member functions.
Hours, Minutes, Date, Month, and Year - Integer
Define a member function init() - to get the class variables
Define a member function display() - to print the class variables.
In the main method, create an object for the class and call the necessary
methods.
Answer
// You are using GCC
#include<iostream>
using namespace std;
class input
{
public:
int h,m,d,mo,y,s;
void myfunc()
{
if(d<=31 && mo<=12 && y<=9999 && h<24 && m<60)
{
s=(h*60)+m;
cout<<"Date:"<<d<<"-"<<mo<<"-"<<y<<endl;
cout<<"Time:"<<h<<" hrs "<<m<<" mins"<<endl;
cout<<"Total mins:"<<s;
}
else
{
cout<<"Invalid";
}
}
};
int main()
{
input a;
cin>>a.h>>a.m>>a.d>>a.mo>>a.y;
a.myfunc();
}
Status : Correct Marks : 10/10
2. Coding Contest
Sunrise Basket founder has decided to organize a fun event at your
college. The event coordinator has announced a coding contest for
creating the application for the Contest. The Best application would be
used for the fair and the developer gets a cash prize. You are a well-versed
and aspiring Programmer in your college. Many programmers have
enrolled themselves for the contest and you are one of them. Every
contestant is provided with a Schema diagram of the Fair. Get yourself
acquainted with Schema and brace yourself for the challenge!!!.
As a part of this, the Application requires a user prompt to create a new
Item type. Hence create an ItemType class with the following attributes.
name (String)deposit(double)costPerDay(double)Include appropriate
Getters and Setters for the class and also include a method "void display()"
to display the output shown in the sample output. In the main method get
input from the user and display it.
Example
Input
Fan
5000
300
Output
Name : Fan
Deposit Amount : 5000
Cost per day : 300
Explanation
Read the item type from the user and display the item name, amount and
per day amount.
Answer
// You are using GCC
#include<iostream>
using namespace std;
class item
{
public:
void display(string a)
{
cout<<"Name : "<<a<<endl;
}
void display1(double b)
{
cout<<"Deposit Amount : "<<b<<endl;
}
void display2(double c)
{
cout<<"Cost per day : "<<c<<endl;
}
};
int main()
{
string a;
double b,c;
cin>>a;
cin>>b;
cin>>c;
item i;
i.display(a);
i.display1(b);
i.display2(c);
}
Status : Correct Marks : 10/10
3. Calculate Total
Write a program to print the total rupees by creating class Money with two
attributes rupee(int) and paisa(int). Include getters and setters to initialize
value to the attributes. The naming convention for Getters and Setters
should be in Camel case. In the main method initialize the values for the
data members and print their sum.
Example
Input
50 85
42 65
Output
93.50
Explanation
85+65=150 (1rupee 50paisa)
50+42=92 . Totally 93.50 rupees.
Answer
// You are using GCC
#include<iostream>
using namespace std;
class money
{
private:
int rupee;
int paisa;
public:
void setr(int r){
rupee=r;
}
int getr(){
return rupee;
}
void setp(int p){
paisa=p;
}
int getp(){
return paisa;
}
};
int main(){
money m1,m2;
int rupee1,paisa1,rupee2,paisa2;
cin>>rupee1>>paisa1>>rupee2>>paisa2;
m1.setr(rupee1);
m1.setp(paisa1);
m2.setr(rupee2);
m2.setp(paisa2);
int totalpaisa = m1.getp() + m2.getp();
int carryrupee= totalpaisa/100;
int totalrupee=m1.getr()+m2.getr()+carryrupee;
totalpaisa%=100;
cout<<totalrupee<<"."<<(totalpaisa<10?"0":"")<<totalpaisa<<endl;
return 0;
}
Status : Correct Marks : 10/10
4. A Multiplication Game
John and Michael play the game of multiplication by multiplying an integer
p by one of the numbers 2 to 9. John always starts with p = 1 and multiply
it by 1, and passes the result to Michael. Then, Michael multiplies the
number by 2 and sends the result to John, then John multiplies by 3 and so
on. Before a game starts, they draw an integer N and the winner is the one
who first reaches p ≥ n.
Create a class that has two functions:
1) A function to perform the multiplication operation
2) The main()
Example
Input
10
Output
10 Michael wins
Explanation
Multiplied value gets greater than ten during fourth play, so Michael won
the game.
Answer
// You are using GCC
// You are using GCC
#include <iostream>
class MultiplicationGame {
private:
int currentValue;
int nextMultiplier;
public:
MultiplicationGame() {
currentValue = 1;
nextMultiplier = 1;
}
int performMultiplication() {
currentValue *= nextMultiplier;
nextMultiplier++;
return currentValue;
}
std::string playGame(int n) {
while (currentValue < n) {
performMultiplication();
}
return (nextMultiplier % 2 == 0) ? "John wins" : "Michael wins";
}
};
int main() {
int n;
std::cout << "";
std::cin >> n;
MultiplicationGame game;
std::string result = game.playGame(n);
std::cout << n << " " << result << std::endl;
return 0;
}
Status : Correct Marks : 10/10
5. Create a class named Address with the following member variables
and methods
street as Stringcity as Stringpincode as integercountry as
StringdisplayAddress() to display all the details.Create a main class named
AddressMain to include the Main method.In the main method, obtain the
details of the Address by creating an object for the Address class and
assign the values to the attributes. Call the method displayAddress() in the
Main class to display the values.
Note:
Use the same class names, attribute names, and method names.
Implement suitable getters and setters.
Example
Input
13,Rockfort Street
Chennai
654035
India
Output
Street: 13,Rockfort Street
City: Chennai
Pincode: 654035
Country: India
Explanation
Read user address and print the address given by the user.
Answer
// You are using GCC
#include<iostream>
#include<cstring>
using namespace std;
class address{
private:
string street;
string city;
int pincode;
string country;
public:
void sets(string s){
street=s;
}
string gets(){
return street;
}
void setc(string c){
city=c;
}
string getc(){
return city;
}
void setp(int p){
pincode=p;
}
void setco(string co){
country=co;
}
string getco(){
return country;
}
void display(){
cout<<"Street: "<<street<<endl;
cout<<"City: "<<city<<endl;
cout<<"Pincode: "<<pincode<<endl;
cout<<"Country: "<<country<<endl;
}
};
int main(){
address add;
string street;
getline(cin,street);
add.sets(street);
string city;
getline(cin,city);
add.setc(city);
int pincode;
cin>>pincode;
add.setp(pincode);
string country;
cin.ignore();
getline(cin,country);
add.setco(country);
add.display();
return 0;
Status : Correct Marks : 10/10
6. BO Classes
We can use a BO class for computational purposes. The Stall owners
wanted to calculate the total cost of a particular ItemType for the given
timeline. So add a feature in the application to calculate the total cost for
the given timeline. Create a class ItemType with the following attributes,
Add appropriate getter/setter, default and parameterized constructor.
public ItemType(String name, Double deposit, Double costPerDay). Get the
start date and end date (manipulate as Date object) from the stall owners
to calculate rent for the particular ItemType. Write a method calculateCost
in ItemTypeBO class.
Create a driver class Main to test the above classes.
Note: Strictly adhere to the Object-Oriented Specifications given in the
problem statement. All class names, attribute names and method names
should be the same as specified in the problem statement.
Display only 1 digit after decimal while displaying cost. Input date format is
dd/mm/yyyy.
Example
Input
Morsh
1000.00
50.00
12/10/2018
10/10/2018
Output
Morsh
1000.0
50.0
100.0
Explanation
50Rs per day So 100 rupees for two days.
Answer
// You are using GCC
// You are using GCC
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
class ItemType {
private:
std::string name;
double deposit;
double costPerDay;
public:
ItemType() {}
ItemType(const std::string& name, double deposit, double costPerDay) {
this->name = name;
this->deposit = deposit;
this->costPerDay = costPerDay;
}
// Getter and setter methods
std::string getName() const {
return name;
}
void setName(const std::string& name) {
this->name = name;
}
double getDeposit() const {
return deposit;
}
void setDeposit(double deposit) {
this->deposit = deposit;
}
double getCostPerDay() const {
return costPerDay;
}
void setCostPerDay(double costPerDay) {
this->costPerDay = costPerDay;
}
// Calculate the cost based on the given start and end date
double calculateCost(const std::string& startDateStr, const std::string&
endDateStr) const {
struct tm start_tm = {};
struct tm end_tm = {};
strptime(startDateStr.c_str(), "%d/%m/%Y", &start_tm);
strptime(endDateStr.c_str(), "%d/%m/%Y", &end_tm);
time_t start_time = mktime(&start_tm);
time_t end_time = mktime(&end_tm);
double diffInSeconds = difftime(end_time, start_time);
double diffInDays = diffInSeconds / (24 * 60 * 60);
return diffInDays * costPerDay;
}
};
class ItemTypeBO {
public:
// Calculate the cost for a given ItemType and date range
double calculateCost(const ItemType& itemType, const std::string& startDate,
const std::string& endDate) const {
return itemType.calculateCost(startDate, endDate);
}
};
int main() {
std::string name;
double deposit, costPerDay;
std::string startDateStr, endDateStr;
// Input values
std::cin >> name >> deposit >> costPerDay >> startDateStr >> endDateStr;
// Create an ItemType object
ItemType itemType(name, deposit, costPerDay);
// Create an ItemTypeBO object
ItemTypeBO itemTypeBO;
// Calculate and display the cost with 1 digit after the decimal
double cost = itemTypeBO.calculateCost(itemType, startDateStr, endDateStr);
std::cout << std::fixed << std::setprecision(1);
std::cout << itemType.getName() << std::endl;
std::cout << itemType.getDeposit() << std::endl;
std::cout << itemType.getCostPerDay() << std::endl;
std::cout << cost << std::endl;
return 0;
}
Status : Partially correct Marks : 6/10
7. Vishal is working in credit card section in a bank . He wants to write a
program to validate credit card number. Credit card numbers follow certain
patterns.
A credit card number must have between 13 and 16 digits. It must start
with:
4 for Visa cards5 for Master cards37 for American Express cards6 for
Discover cards
Use below steps to validate the card (For example, if card number is
4388576018402626)
Step 1. Double every second digit from right to left. If doubling of a digit
results in a two-digit number, add up the two digits to get a single-digit
number (like for 12:1+2, 18=1+8).
Step 2. Now add all single-digit numbers from Step 1.
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
Step 3. Add all digits in the odd places from right to left in the card number.
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
Step 4. Sum the results from Step 2 and Step 3.
37 + 38 = 75
Step 5. If the result from Step 4 is divisible by 10, the card number is valid;
otherwise, it is invalid.
Answer
// You are using GCC
#include<iostream>
#include<string>
using namespace std;
int main()
{
string cn;
cin>>cn;
int len=cn.length();
int sum1=0;
int sum3=0;
for(int i=len-2;i>=0;i-=2){
int digit=(cn[i]-'0')*2;
while(digit>0){
sum1+=digit%10;
digit/=10;
}
}
for(int i=len-1;i>=0;i-=2){
int digit = cn[i]-'0';
sum3+=digit;
}
int totalsum=sum1+sum3;
if(totalsum%10==0){
cout<<cn<<" is valid"<<endl;
}else{
cout<<cn<<" is invalid"<<endl;
}
return 0;
}
Status : Correct Marks : 10/10
8. Create class money with two attributes:
int rupee
int paisa
Include getters, setters and constructors.
Create a main class and initialize the values for the data members
Get two amounts and print their sum.
Answer
// You are using GCC
// You are using GCC
#include<iostream>
using namespace std;
class Money{
private:
int rupee;
int paisa;
public:
void setrupee(int r){
rupee=r;
}
int getrupee(){
return rupee;
}
void setpaisa(int p){
paisa=p;
}
int getpaisa(){
return paisa;
}
};
int main(){
Money money1,money2;
int rupee1,paisa1,rupee2,paisa2;
cin>>rupee1>>paisa1>>rupee2>>paisa2;
money1.setrupee(rupee1);
money1.setpaisa(paisa1);
money2.setrupee(rupee2);
money2.setpaisa(paisa2);
int totalp=money1.getpaisa()+money2.getpaisa();
int carryr=totalp/100;
int totalr=money1.getrupee()+money2.getrupee()+carryr;
totalp %=100;
cout<<totalr<<"."<<(totalp<10?"0":"")<<totalp<<endl;
return 0;
}
Status : Correct Marks : 10/10