Write a program that creates class Distance and assigns values to variable using constructor and this keyword.

 

AIM:  Write a program that creates class Distance and assigns values to variable using constructor and this keyword.

 

CODE:

import java.util.Scanner;

public class Distance{

            int dist, speed, time;

            Distance(int speed,int time){

                        this.speed = speed;

                        this.time = time;

            }

            void calcdist(){

                        this.dist = this.speed*this.time;

            }

            void display(){

                        this.calcdist();

                        System.out.println("\nDistance: "+this.dist);

            }

            public static void main(String args[]){

                       

                        Scanner k = new Scanner(System.in);

                       

                        System.out.print("\nEnter speed and time: ");

                        int speed = k.nextInt();

                        int time = k.nextInt();

                        Distance D = new Distance(speed, time);

                        D.display();

            }

}

OUTPUT:



Comments

Popular posts from this blog

Write a program in Java to perform I/O operations on a Text file.

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a program in Java to demonstrate use of final class.