Menu
Topics Index
...
`

Print Distance between two points


If you need explanation Read this topic

If you need Answer Take test on this topic

Print Distance between two points
What will be the output of the following program? Assume that the inputs are point0(0, 3), point1(4, 0).
public class Distance {
    private final java.awt.Point point0, point1;
    public Distance(int x0, int y0, int x1, int y1) {
        point0 = new java.awt.Point(x0, y0);
        point1 = new java.awt.Point(x1, y1);
    }
    public void printDistance() {
        System.out.println(point0 + " and " + point1 + "\n" + point0.distance(point1));
    }
    public static void main(String[] args) {
        Distance dist = new Distance(intValue(args[0]), intValue(args[1]), intValue(args[2]), intValue(args[3]));
        dist.printDistance();
    }
    private static int intValue(String data) {
        return Integer.parseInt(data);
    }
}

A. [x=0,y=3] and [x=4,y=0]
0.0
B. Point[x=0,y=3] and Point[x=4,y=0]
25.0
C. Point[x=3,y=0] and Point[x=0,y=4]
5.0
D. (EMPTY)
E. Some other output
F. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is E... The Point class represents a location in a two-dimensional (x, y) coordinate space. In the program we are creating two pints and we are calling printDistance method on instance of distance class.distance method prints the distance between the two sets of specified coordinates. which is 5.0...so answer is Point[x=0,y=3] and Point[x=4,y=0]     and distance is 5.0

Posted by Shashanka Mogaliraju    2014-11-20 08:59:01


ans c.
The java command-line argument is an argument i.e. passed at the time of running the java program.
The arguments passed from the console can be received in the java program and it can be used as an input.
So, it provides a convenient way to check the behavior of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.                                                                         coming to this progrm Distance object created with  4 method called with command line arguments which  givn at run time of progrm .here java.awt .point class is used,this class holds the X and Y coordinates of a two-dimensional point.                                                                                                         results may vary depends on input.point 0 (x0=0,y0=3)and point1(x1=4,y1=0)  x,y coordinates get printd and thn distance method returns distance() which is equals to sqrt((x1-x0)^2+(y1-y2)^2)=sqrt(16+9)=5.0

Posted by Maheshwari Natarajan    2014-11-20 18:50:06


Ans is E..here we have the concept of the Cmd Line arguments...here we have to calculate the distance between the 2 points..

here we are passing these points through the Command Line Arguments means while running the java file..

Ex: java Distance 0 4 3 0

these values we receive as Strings...these all values will come as String array which is pointed by the arg[] in main()...

here we will convert these Strings into values using the parseInt() method in Integer class..which will return the int value..it may NumberFormatException if we pass invalid argument lik Alphabates..Ex: "abc"

here we create instance for the distance class by passing the 4 inputs which are received from the commandline argumetns...by calling intValue() these will become int values..

here we are creating the 2 Objects for the Point class by passing the received values...this will present in java.awt..

these will represent in values in the form of (x,y)
next we are calling printDis() there are printing the x,y values of the point objects..

next we are printing the distance between the these points...by calling the distance()

ans is :point1----(0,3)
          point2---(4,0)
         distance is 5.0
internal formula for dis

Sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))=(4)(4)*(-3)(-3)=Sqrt(25)=5

Posted by Uday Kumar    2014-11-21 12:00:12


This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Veerendra. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-23 01:55:07


© meritcampus 2019

All Rights Reserved.

Open In App