Exp 4
Exp 4
CONSTRUCTOR
AIM
A class contains distance in meters and centimeters and another class contains distance in feets and
inches. Write a Java program to convert distance in meters and centimeters to feets and inches and
vice versa using constructors.
OBJECIVES
To implement Java program to create objects using constructors.
COURSE OUTCOME
CO1 Develop programs using Java class & object
MODULE OUTCOME
M1.03 Demonstrate the role of constructors
PROGRAM
import java.io.*;
class Meter
{
int mtr, cm;
Meter()
{
mtr = 0;
cm = 0;
}
Meter(int m, int c)
{
mtr = m;
cm = c;
}
Meter(Feet f)
{
int d;
class Feet
{
int ft, in;
Feet()
{
ft = 0;
in = 0;
}
Feet(int f, int i)
{
ft = f;
in = i;
}
Feet(Meter m)
{
int d;
void display()
{
System.out.println(ft + " Ft " + in + " Inches" );
}
OBSERVATIONS
Meter object with default constructor
0 M 0 CM
Enter distance in meters and centimeters
1
50
Given distance
1 M 50 CM
Converted to Feets and Inches
5 Ft 0 Inches
Enter distance in feets and inches
5
8
Given distance
5 Ft 8 Inches
Converted to Meters and Centimeters
1 M 70 CM
RESULT
Studied implementation Java program to create objects using constructors.