0% found this document useful (0 votes)
32 views4 pages

Exp 4

The document describes a Java program that uses constructors to convert between distance measurements in meters/centimeters and feet/inches. It defines Meter and Feet classes with default and parameterized constructors. The main method creates Meter and Feet objects using these constructors, gets user input, displays conversions, and outputs the results, demonstrating the use of constructors to convert between the distance units.

Uploaded by

hejafa9465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Exp 4

The document describes a Java program that uses constructors to convert between distance measurements in meters/centimeters and feet/inches. It defines Meter and Feet classes with default and parameterized constructors. The main method creates Meter and Feet objects using these constructors, gets user input, displays conversions, and outputs the results, demonstrating the use of constructors to convert between the distance units.

Uploaded by

hejafa9465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NO 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;

d = f.ft * 30 + f.in * 25 / 10;


mtr = d / 100;
cm = d % 100;
}
void display()
{
System.out.println(mtr + " M " + cm + " CM" );
}

class Feet
{
int ft, in;

Feet()
{
ft = 0;
in = 0;
}

Feet(int f, int i)
{
ft = f;
in = i;
}

Feet(Meter m)
{
int d;

d = m.mtr * 100 + m.cm;


in = d * 10 / 25;
ft = in / 12;
in = in % 12;
}

void display()
{
System.out.println(ft + " Ft " + in + " Inches" );
}

public class Exp4


{
public static void main(String arg[]) throws IOException
{
Meter m1, m2, m3;
Feet f1, f2;
int x, y;

InputStreamReader in = new InputStreamReader(System.in);


BufferedReader b = new BufferedReader(in);

System.out.println("Meter object with default constructor");


m1 = new Meter();
m1.display();

System.out.println("Enter distance in meters and centimeters");


x = Integer.parseInt(b.readLine());
y = Integer.parseInt(b.readLine());
m2 = new Meter(x, y);
System.out.println("Given distance");
m2.display();
System.out.println("Converted to Feets and Inches");
f1 = new Feet(m2);
f1.display();

System.out.println("Enter distance in feets and inches");


x=Integer.parseInt(b.readLine());
y=Integer.parseInt(b.readLine());
f2 = new Feet(x, y);
System.out.println("Given distance");
f2.display();
System.out.println("Converted to Meters and Centimeters");
m3 = new Meter(f2);
m3.display();
}
}

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.

You might also like