Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Point { /** The location of the point in the X direction. */ private double x; /** The location of the point in

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
public class Point
{
/** The location of the point in the X direction. */
private double x;
/** The location of the point in the Y direction. */
private double y;
/** Constructs a Point from given x and y values.
@param xIn The location of the Point in the X direction.
@param yIn The location of the Point in the Y direction.
*/
public Point(double xIn, double yIn)
{ x = xIn;
y = yIn;
}
/** Returns the x value for this Point.
@return The x value for this Point.
*/
public double getX()
{ return x;
}
/** Returns the y value for this Point.
@return The y value for this Point.
*/
public double getY()
{ return y;
}
/** Calculates the distance between this Point and
another Point using the pythagorean theorem
@param other The other Point.
@return The distance between this Point and
another Point.
*/
public double distance (Point other)
{ double dx = x - other.getX();
double dy = y - other.getY();
return Math.sqrt(dx*dx + dy*dy);
}
// What is the Math class?
// Math.sqrt is a static method
// Find & use the Java API documentation
// ==> Use 'javac -version'
// ==> Then Google: jdk (your version number) api
// Bookmark that page!
/** Returns a String representation of the x and y values
for this Point.
@return A String representation of the x and y values
for this Point.
*/
public String toString()
{ return "Point[x=" + x + ", y=" + y + "]";
}
/** Changes the x value for this Point.
@param xIn The new x value.
*/
public void setX(double xIn)
{ x = xIn;
}
/** Changes the y value for this Point.
@param yIn The new y value.
*/
public void setY(double yIn)
{ y = yIn;
}
}

Part A: Alternating Numbers Write a program that prompts the user for four integers and displays the phrase "alternating" if the numbers alternate in terms of their relationships with each other. For instance, if the second number is less than the first, then the third must be greater than the second, and the fourth must be less than the third. Or if the second number is greater than the first, then the third must be less than the second, and the fourth must be greater than the third. Display the phrase "not alternating" otherwise. Any two adjacent numbers that are equal means not alternating. Below is sample output from four separate runs of the program. User input is shown here in blue italics. Enter four integers: 1574 not alternating enter four integers: 3812. alternating enter four integers: 98104 alternating enter four integers: 3556 not alternating You only need to write one class for Part A. You may place all of your code in the main method of that class. Include in your submission document the output from at least three test cases: 1. Alternating where the second number is larger than the first; 2. Alternating where the second number is smaller than the first; and 3. Not alternating. Part B: Line Segments Download the Point.java file, which is available for download with this assignment on Desire2Learn. (This is the same class that was used in Module 5 and is also available for download there.) You do not need to change this file. You will use this class to help 2. Alternating where the second number is smaller than the first; and 3. Not alternating. Part B: Line Segments Download the Point.java file, which is available for download with this assignment on Desire2Learn. (This is the same class that was used in Module 5 and is also available for download there.) You do not need to change this file. You will use this class to help create a LineSegment class and an associated test driver. It is highly recommended that you use incremental programming to complete this program, where you code a little, try to compile and run what you have so far, and repeat until done. Write a complete LineSegment class. A LineSegment is defined by its two endpoints, each of which is represented by a Point object. Create two constructors - one that receives two Point objects, and the other that receives the four x and y values required for the constructor to create two Point objects. Include an accessor method for each of the two endpoints. Also include the following three methods: isParallelTo() - Returns true if this line segment is parallel to another line segment. The other line segment is given as a parameter to this method. Two line segments are parallel if they have the same slope. Be careful with vertical lines, where the slope is undefined because calculating such a slope would require division by zero. Your method must handle vertical lines. (Hint: One way to do this is to perform your calculations in such a way that no divisions are necessary. Another possible solution would be to use an if statement to detect when you have a vertical line, prior to isParallelTo() - Returns true if this line segment is parallel to another line segment. The other line segment is given as a parameter to this method. Two line segments are parallel if they have the same slope. Be careful with vertical lines, where the slope is undefined because calculating such a slope would require division by zero. Your method must handle vertical lines. (Hint: One way to do this is to perform your calculations in such a way that no divisions are necessary. Another possible solution would be to use an if statement to detect when you have a vertical line, prior to attempting any slope calculations.) Can't remember how to calculate the slope of a line segment? Google it! length() - Returns the length of this line segment. You can use the distance method in the Point class to help with this calculation. midpoint( ) - Returns a Point representing the midpoint of this line segment. Can't remember how to find the midpoint of a line segment? Google it! Add javadoc comments to the LineSegment class. Include a javadoc comment for the class, and for each of its instance variables and methods (including the constructors). Include @author, @param, and @return tags where appropriate. You may wish to run javadoc to check that your comments are correctly formatted. However, you do not need to submit the files obtained by running the javadoc command. In a separate LineTest class, create a test driver that tests each of the methods in your LineSegment class, including the accessors for the two endpoints, as well as both constructors. Use the following line segments as test data: Line Segment 1: (0.0,0.0) to (0.0,5.0) Line Segment 2: (2.0,5.0) to (2.0.7.0) Line Segment 3: (1.0,5.0) to (6.0,5.0) Line Segment 4: (2.0,5.0) to (7.3,10.1) Line Segment 5: (0.0,3.0) to (10.6,7.2) Line Segment 6: (0.0,3.0) to (10.6,3.0) Line Segment 7:(2.0,5.0) to (2.0,5.0) For the isParallelTo method, include test cases for each of the following: - Line segments 1 and 2. - Line seaments 1 and 3. For the isParallelTo method, include test cases for each of the following: - Line segments 1 and 2. - Line segments 1 and 3. - Line segments 4 and 5. - Line segments 4 and 6. - Line segment 4 compared with itself (yes, this is considered parallel). For each test case for the isParallelTo( ) method, use an if-else statement in your test driver to print whether or not the two line segments were found to be parallel. All output should include enough information so you can identify which line segments are involved in each test case. For both the midpoint and length methods, include test cases for each of the following: - Line segment 1 - Line segment 3 - Line segment 4 - Line segment 7 It is okay to use the Point toString() method for displaying midpoint results. For each test case, you must print sufficient information to allow you to confirm that the test case performed as expected

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

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

Explain exothermic and endothermic reactions with examples

Answered: 1 week ago

Question

Write a short note on rancidity and corrosiveness.

Answered: 1 week ago