Computer Science
Design a class 'AddDist' with the following specifications: class AddDist
Data members/Instance variables
int km, mts, cm
Member methods:
void getdist( ) : to accept a distance in km, mts and cm. void showdist( ) : to display the distance in km, mts and cm.
Write a main function to create an object of class 'Add_Dist' and call the member methods.
Objects & Classes
13 Likes
Answer
import java.util.Scanner;
class Add_Dist {
int km;
int mts;
int cm;
void get_dist() {
Scanner in = new Scanner(System.in);
System.out.println("Enter a distance in km, mts and cm");
System.out.print("Enter km: ");
km = in.nextInt();
System.out.print("Enter mts: ");
mts = in.nextInt();
System.out.print("Enter cm: ");
cm = in.nextInt();
}
void show_dist() {
System.out.println("Distance in km = " + km);
System.out.println("Distance in mts = " + mts);
System.out.println("Distance in cm = " + cm);
}
public static void main(String args[]) {
Add_Dist obj = new Add_Dist();
obj.get_dist();
obj.show_dist();
}
}
Answered By
9 Likes
Related Questions
An object is referred to as an instance of a class. Explain.
Class and Object are inter-related. Justify this statement.
Design a class 'Cuboid' with the following specifications:
class Cuboid
Data members/Instance variables
int len, br, ht, vol;
Member methods:
void input( ) : to accept length, breadth and height of a cuboid.
void calculate( ) : to calculate volume of a cuboid.
void display( ) : to print volume of cuboid.
Write a main function to create an object of class 'Volume' and call the member methods.Write a class template 'Calculator' with the following specifications:
Class : Display
Data members/Instance variables
int a,b,c;
Member Methods:Name Purpose void Accept() to accept the values of a and b. void Max() to find greater of the two number 'a' and 'b' and store the result in c. Display the result. void Min() to find smaller of the two number 'a' and 'b' and store the result in c. Display the result. void Diff() to store the difference between 'a' and 'b' and store the result in c. Display the result. Write a main class to create an object of class 'Display' and call the member methods.