Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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.
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 oneStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started