Write a program to create a – “distance” class with methods where distance is computed in terms of feet and inches, how to create objects of a class and to see the use of this pointer
Write a program to create a – “distance” class with methods where distance is computed in terms of feet and inches, how to create objects of a class and to see the use of this pointer.
Program:
import java.util.Scanner;
class Dist
{
int feet,inches;
void getDistance(int feet, int inches)
{
this.feet=feet;
this.inches=inches;
}
void showDistance()
{
System.out.println("Feet= "+feet);
System.out.println("Inches= "+inches);
}
}
public class Distance
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the feet:");
int x=sc.nextInt();
System.out.println("Enter the inch:");
int y=sc.nextInt();
Dist obj=new Dist();
obj.getDistance(x, y);
obj.showDistance();
}
}
OutPut:
Enter the feet:
32
Enter the inch:
32
Feet= 32
Inches= 32
Post a Comment