You are on page 1of 13

1: Create a distance class having two data members as feet and inches.

write
at least two constructor of the class and a suitable method to compare two
objects of the class to determine whether the objects are equal or not. result
should be displayed by main().
Solution:
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
float inch;
public:
Distance();
Distance(int a,float b);
void setDistance();
int getFeet();
float getInch();
void distanceSum(Distance d);
};
int main()
{
Distance D1,D2;
D1.setDistance();
D2.setDistance();
D1.distanceSum(D2);
return 0;
}
/*Function Definitions*/
Distance::Distance()
{
inch=feet=0;
}
Distance::Distance(int a,float b)
{
feet=a;
inch=b;
}
void Distance::setDistance()
{
cout<<"Enter distance in feet";
cin>>feet;
cout<<endl<<"Enter inches:";
cin>>inch;
}
int Distance::getFeet()
{
return feet;
}
float Distance::getInch()
{
return inch;
}
void Distance::distanceSum(Distance d)
{
cout<<"feet="<<d.feet+feet<<endl;
cout<<"inches="<<d.inch+inch<<endl;
}
2: write a class named array that contain an array of integers as data
members. the class contain the following members functions.
Solution:
#include<iostream.h>
#include<conio.h>
class num
{
int n;
public: void getdata();
void check();
};
void num::getdata()
{
cout<<"Enter a number:";
cin>>n;
}
void num::check()
{
if(n%2==0)
cout<<"::: EVEN :::";
else
cout<<"::: ODD :::";
}
void main()
{
num n1;
n1.getdata();
n1.check();
getch();
}
Alternative:
#include <iostream>
using namespace std;
int main() {
int input, remainder, even = 0, odd = 0;
int evenArray[even];
int oddArray[odd];
cout << "This program accepts integers until you enter 0.\nPlease enter a
value: ";
cin >> input;
while (input != 0) {
remainder = input % 2;
if (remainder == 0) {
evenArray[even] = input;
even++;
}
else {
oddArray[odd] = input;
odd++;
}
cout << "Enter another integer: ";
cin >> input;
}
cout << "\nThe number of evens is " << even << ".\n";
cout << "The even values are: ";
for(int i = 0; i < even; i++) {
cout << evenArray[i] << " ";
}
cout << endl;
cout << "The number of odds is " << odd << ".\n";
cout << "The odd values are: ";
for(int i = 0; i < odd; i++) {
cout << oddArray[i] << " ";
}
}

3. write a program that declare a Data class. it consist of only one data
member count write appropriate constructor on other member function in
order to validate the following statement in main() obj, ++obj
Solution.
#include <iostream>
using namespace std;
class test {
int code;
static int count;

public:
void setcode(void)
{
code = ++count;
}
void showcode(void)
{
cout << "object number :" << code << "\n";
}
static void showcount(void)
{
cout << "count:" << count << "\n";
}
};
int test::count;
int main()
{
test t1, t2;
t1.setcode();
t2.setcode();

test::showcount();

test t3;
t3.setcode();

test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}

4. write a program that have circle, square as classes derived from a common
class geometry and using these classes find area of circle, perimeter of circle
and area of square and perimeter of square
Solution:
#include "stdafx.h"
#include<cmath>
#include <iostream>

class shape
{
public:
virtual void displayArea()=0;
};

class circle :public shape


{
int radius;
public:
circle(int radius2) :radius(radius2){ }
void displayArea()
{
double area = 3.14*radius*radius;
std::cout << " \n Area circle" << area<<std::endl;
}
};

class triangle :public shape


{
double a,b,c;
public:
triangle(double a1, double b1, double c1): a(a1), b(b1),c(c1)
{ if (a + b > c && a + c > b && b + c > a)
std::cout << "The sides form a triangle" << std::endl;
else
std::cout << "The sides do not form a triangle. Correct me !" << std::endl;

void displayArea()
{

double s = (a + b + c) / 2;
double area = sqrt(s*(s - a)*(s - b)*(s - c));
std::cout << " \n Area triangle"<< area<<std::endl;
}
};
void main()
{
shape * p1[2];
p1[0]= new circle(20);

p1[1] = new triangle(5.6,8.1,10.3);


for (int i = 0; i < 2; ++i)
{
p1[i]->displayArea();
}

int y;
std::cin >> y;
}

5. write a program that have get and set function and find area of rectangle
and also check that given value is larger than 0.0 and less than 20.0
Solution:
public class Rectangle
{
private double length ;
private double width ;

public void setlength( double length)


{
if (length < 0 || length > 20)
{
throw new IllegalArgumentException ("For input length:0.0");

}
this.length = length;
}
public void setwidth(double width)
{
if ( width <0 || width > 20)
{
throw new IllegalArgumentException("For input width:0.0");
}
this.width = width;
}

public double getLength()


{
return length;
}

public double getWidth()


{
return width;
}

public double getPerimeter()


{
return (length + width)*2 ;
} public double getArea()
{
return length * width ;
}

import java.util.Scanner;

import javax.swing.JOptionPane;
public class Assign8Test {
public static void main ( String args[])
{
Rectangle r1 = new Rectangle();

String length, width;

length = JOptionPane.showInputDialog( "Enter Length (0.0 <L <20.0)");


width = JOptionPane.showInputDialog( "Enter Width (0.0 <W <20.0)");

double d1, d2;

d1 = Double.parseDouble( length );

d2 = Double.parseDouble( width );

r1.setlength( d1 );
r1.setwidth( d2 );

while (true)
{
JOptionPane.showMessageDialog (null, "Length = " + r1.getLength()+
"\nWidth = " + r1.getWidth()+ "\nPerimeter = " +
r1.getPerimeter()+ "\nArea = " + r1.getArea(), "Rectangle",
JOptionPane.PLAIN_MESSAGE);
try
{
Rectangle r2 = new Rectangle();
}
catch (IllegalArgumentException e)
{
JOptionPane.showMessageDialog (null,"Illegal Argument Exception:",
"Error", JOptionPane.ERROR_MESSAGE);
}
break;
}

6. Define a class from bank account that include the following data members
name of depositor Account name type of account balance amount in account
Solution:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class bank
{
char name[20];
int ano;
char atype[20];
float bal;
public:
void get(int no,char *n,char *t,float b)
{
strcpy(name,n);
ano=no;
strcpy(atype,t);
bal=b;
}
float deposit()
{
float amt;
cout<<“\nEnter amount: “;
cin>>amt;
bal=bal+amt;
return bal;
}
float withdrw()
{
float amt;
cout<<“\nHow many Rupees withdraw: “;
cin>>amt;
bal=bal-amt;
return bal;
}
void disp()
{
cout<<“\n\nAccount number: “<<ano;
cout<<“\n\nName: “<<name;
cout<<“\n\nAccount type: “<<atype;
cout<<“\n\nDeposit Amount: “<<deposit();
cout<<“\n\nAfter Withdraw Amount balnace: “<<withdrw();
}
};
void main()
{
int n;
char nm[20],t[20];
float a;
bank bk;
clrscr();
cout<<“\nEnter Account no.: “; cin>>n;
cout<<“\nEnter Name: “; cin>>nm;
cout<<“\nEnter account type: “; cin>>t;
cout<<“\nEnter balance amount: “;cin>>a;
bk.get(n,nm,t,a);
bk.disp();
getch();
}
7.Explain constructor overloading with the help of example.
Solution: In addition to overloading methods, we can also overload constructors in
java. Overloaded constructor is called based upon the parameters specified
when new is executed.
When do we need Constructor Overloading?
Sometimes there is a need of initializing an object in different ways. This can be done
using constructor overloading. For example, Thread class has 8 types of constructors. If
we do not want to specify anything about a thread then we can simply use default
constructor of Thread class, however if we need to specify thread name, then we may
call the parameterized constructor of Thread class with a String args like this:
Thread t= new Thread (" MyThread ");
Let us take an example to understand need of constructor overloading. Consider the
following implementation of a class Box with only one constructor taking three
arguments.
class Box
{
double width, height,depth;

// constructor used when all dimensions


// specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}

// compute and return volume


double volume()
{
return width * height * depth;
}
}
7. Phone number dealing
Solution:
class PhoneNumber implements Comparable<PhoneNumber> {

private Long number;

public PhoneNumber(Long number) {


this.number = number;
}

public Long getNumber() {


return this.number;
}

public boolean equals(Object object) {

if (getNumber() == null && object == null) {


return true; //or false its depend
}

return getNumber().equals(object);
}

public int compareTo(PhoneNumber that) {

if(that == null) {
return -1;
}
Long thisNumber = getNumber();
Long thatNumber = that.getNumber();

if (thisNumber == null && thatNumber == null) {


return 0; //or -1
}

if (thisNumber == null && thatNumber != null) {


return -1;
}

return thisNumber.compareTo(thatNumber);

@Override
public String toString() {
return String.format("%010d", getNumber());
}
}

You might also like