Java Ex 3
Java Ex 3
Aim
To distinguish the different types of constructors
Algorithm
Step 1: Define a class named Conversion.
Step 2: Declare two instance variables: iKm of type int to represent
kilometers and dMile of type double to represent miles.
Step 3: Define a no-argument constructor Conversion() within the class and
initialize iKm to 100.
Step 4: Define a one-argument constructor Conversion(int) within the class
and initialize iKm with the provided argument.
Step 5: Define a method miles() within the class to calculate and print the
equivalent miles for the value of iKm using the conversion factor (1 km =
0.621371 miles).
Step 6: In the main() method, create three objects: obj1 with no
arguments, obj2 with 500 as the argument, and obj3 using obj2 to initialize
it.
Step 7: Call the miles() method using two different objects.
Source Code
class Conversion
{
public int iKm;
public double dMile;
obj1.miles();
obj2.miles();
obj3.miles();
}
}