Question: IN JAVA: Exercise 1.6: Create at least two different constructors for the XYPoint and XYLine classes. Add a function that calculates the length of a

IN JAVA: Exercise 1.6: Create at least two different constructors for the XYPoint and XYLine classes. Add a function that calculates the length of a line. Create an array with 10 random lines and print the length of each one.

IN JAVA: Exercise 1.6: Create at least two different constructors for the

class XYPoint double x; double y; public XYPoint(double x, double y) this.x-x // differentiate between parameter x and instance variabl this. , public XYPointO xMath.random() * 10; yMath.random() * 10; public class Main public static void main (String[] argv) Make a new instance of an XYPoint object XYPoint pnew XYPointO XYPoint p2 new XYPoint(5, 5); /Access its variables double d-Math.sqrt(p. X*p.x + . *p.y); System.out.println(d); System.out.println(d); Note * A class can have several different constructors with different parameters * Here we are using the this keyword to distinguish between the x variable that arrives as a parameter to the constructor and the instance variable x which is part of "this" new object being created What are the benefits of using a constructor? What could go wrong if we didn't have one? Exercise 1.6: Create at least two different constructors for the XYPoint and XYLine classes. Add a function that calculates the length of a line. Create an array with 10 random lines and print the length of each one

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!