Java class program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java class program

getting 0.0 as output which is wrong import java.util.*; class Point { int x, y; Point() { x=0; y=0; } void getdata(int a, int b) { x=a; y=b; } } class Line { Point p1 = new Point(); Point p2 = new Point(); Line() { p1.x=0; p1.y=0; p2.x=0; p2.y=0; } Line(int a, int b ,int c, int d) { p1.x=a; p1.y=b; p2.x=c; p2.y=d; } double length() { double ans; ans=Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)-(p2.y-p1.y)*(p2.y-p1.y)); return ans; } } class Triangle { Line l1 = new Line(); Line l2 = new Line(); Line l3 = new Line(); void perimeter() { System.out.print (l1.length()+l2.length()+l3.length()); } } class Program { public static void main(String [] args) { Scanner sc = new Scanner(System.in); Point p = new Point(); System.out.print("Enter two points\n"); int a = sc.nextInt(); int b = sc.nextInt(); p.getdata(a,b); Line l = new Line(); l.length(); Triangle t = new Triangle(); t.perimeter(); } } https://www.sololearn.com/compiler-playground/cnXOSNypDPUw

25th Sep 2022, 3:02 PM
Zane Vijay Falcao
Zane Vijay Falcao - avatar
1 Answer
+ 1
There are some mistakes Inside class Point change method name instead of this getdata use this: setPoint with this code it deals with objects : Line(Point pt1 , Point pt2) { p1 = pt1; p2 = pt2; } to present the result use this: System.out.println (l.length());
25th Sep 2022, 6:39 PM
SoloProg
SoloProg - avatar