Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Make the following additions to the given code: import java.util.*; class Point{ int x,y,z; Point(){ this.x = 0; this.y = 0; this.z = 0; }

Make the following additions to the given code:

image text in transcribedimage text in transcribed

import java.util.*; class Point{ int x,y,z; Point(){ this.x = 0; this.y = 0; this.z = 0; } Point(int x, int y){ this.x = x; this.y = y; this.z = 0; } Point(int x, int y, int z){ this.x = x; this.y = y; this.z = z; } double distance(Point p){ double x = Math.pow(this.x-p.x,2); double y = Math.pow(this.y-p.y,2); double z = Math.pow(this.z-p.z,2); return Math.sqrt(x+y+z); } double distanceFromOrigin(){ double x = Math.pow(this.x,2); double y = Math.pow(this.y,2); double z = Math.pow(this.z,2); return Math.sqrt(x+y+z); } public String toString(){ return "("+x+","+y+","+z+")"; } Point translate(int x,int y){ Point p = new Point(this.x,this.y,this.z); p.x += x; p.y += y; return p; } boolean equals(Point p){ return (this.x == p.x) && (this.y == p.y) && (this.z == this.z); } Point translate(int x,int y, int z){ Point p = new Point(this.x,this.y,this.z); p.x += x; p.y += y; p.z += z; return p; } } class Line{ double length; Point start,end; Line(){ this.start = new Point(0,0); this.end = new Point(1,1); this.length = Math.sqrt(2); } Line(Point s, Point e){ try{ this.start = s; this.end = e; double x2 = Math.pow(s.x-e.x,2); double y2 = Math.pow(s.y-e.y,2); length = Math.sqrt(x2+y2); if (length 0."); } } catch (Exception ex) { System.out.println(ex.getMessage()); } } Point midPoint(){ Point p = new Point(); p.x = (this.start.x + this.end.x)/2; p.y = (this.start.y + this.end.y)/2; return p; } double slope(){ return (this.end.y - this.start.y)/(this.end.x-this.start.x); } public String toString(){ return "The line starts at ("+this.start.x+","+this.start.y+") and ends at ("+this.end.x+","+this.end.y+") The length is "+this.length; } boolean equals(Point p){ return p.equals(this.start) || p.equals(this.end); } } class LinePointTester{ public static void main(String[] args) { Line l = new Line(); System.out.println(l); System.out.println("Mid Point: "+l.midPoint()); System.out.println("Slope: "+l.slope()); Point p = new Point(2,3); System.out.println("Point: "+p); System.out.println("distance from origin: "+p.distanceFromOrigin()); } }

3. Now you will make a class Line. Should Line extend Point? Why? Should Point extend Line? Why? Should Line have attributes that are Points? Why? Line - start: Point - end : Point - length : double + Line 0 //makes line with start (0,0) and end (1,1) Line (s: Point, e: Point) + midpoint () : Point + slope(): double + toString(): String // "The line starts at (x,y) and ends at (x2,y2) The length is # + equals (Point p): boolean For a line we can assume we will only use the x and y coordinates. Length of a line is the same as the distance between its two endpoints. (Hint: This should be assigned using a method call to a method that should already exist at this point.) Midpoint of a line: M = 1+x2 Y1+Y2 22 Slope of a line: S = - X-X1 4. Now we are going to add an exception to the line class. A valid line must have a length > 0. In other words, the length of a line cannot be zero

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions