Part A: Alternating Numbers Write a program that prompts the user for four integers and displays the phrase “alternating

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Part A: Alternating Numbers Write a program that prompts the user for four integers and displays the phrase “alternating

Post by answerhappygod »

Part A: Alternating Numbers
Write a program that prompts the user for four integers anddisplays the phrase “alternating” if the numbers alternate in termsof their relationships with each other. For instance, if the secondnumber is less than the first, then the third must be greater thanthe second, and the fourth must be less than the third. Or if thesecond number is greater than the first, then the third must beless than the second, and the fourth must be greater than thethird. Display the phrase “not alternating” otherwise. Any twoadjacent 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: 1 5 7 4 not alternating
enter four integers: 3 8 -1 2 alternating
enter four integers: 9 8 10 4 alternating
enter four integers: -3 5 5 6 not alternating
You only need to write one class for Part A. You may place allof your code in the main method of that class. Include in yoursubmission document the output from at least three test cases:
1. Alternatingwherethesecondnumberislargerthanthefirst;2. Alternatingwherethesecondnumberissmallerthanthefirst;and 3.Notalternating.
Part B: Line Segments
Download the Point.java file, which is available for downloadwith this assignment on Desire2Learn. (This is the same class thatwas used in Module 5 and is also available for download there.) Youdo not need to change this file. You will use this class to helpcreate a LineSegment class and an associated test driver.
It is highly recommended that you use incremental programming tocomplete this program, where you code a little, try to compile andrun what you have so far, and repeat until done.
Write a complete LineSegment class. A LineSegment is defined byits two endpoints, each of which is represented by a Pointobject.
Create two constructors – one that receives two Point objects,and the other that receives the four x and y values required forthe constructor to create two Point objects.
Include an accessor method for each of the two endpoints. Alsoinclude the following three methods:
isParallelTo( ) – Returns true if this line segment is parallelto another line segment. The other line segment is given as aparameter to this method. Two line segments are parallel if theyhave the same slope. Be careful with vertical lines, where theslope is undefined because calculating such a slope would requiredivision by zero. Your method must handle vertical lines. (Hint:One way to do this is to perform your calculations in such a waythat no divisions are necessary. Another possible solution would beto use an if statement to detect when you have a vertical line,prior to attempting any slope calculations.) Can’t remember how tocalculate the slope of a line segment? Google it!
length( ) – Returns the length of this line segment. You can usethe distance method in the Point class to help with thiscalculation.
midpoint( ) – Returns a Point representing the midpoint of thisline segment. Can’t remember how to find the midpoint of a linesegment? Google it!
Add javadoc comments to the LineSegment class. Include a javadoccomment for the class, and for each of its instance variables andmethods (including theconstructors). Include @author, @param, and @return tags whereappropriate. You may wish to run javadoc to check that yourcomments are correctly
formatted. However, you do not need to submit the files obtainedby running the javadoc command.
In a separate LineTest class, create a test driver that testseach of the methods in your LineSegment class, including theaccessors 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) LineSegment 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 thefollowing:
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 consideredparallel).
For each test case for the isParallelTo( ) method, use anif-else statement in your test driver to print whether or not thetwo line segments were found to be parallel. All output shouldinclude enough information so you can identify which line segmentsare involved in each test case.
For both the midpoint and length methods, include test cases foreach of the following:
Line segment 1
Line segment 3
Line segment 4
Line segment 7It is okay to use the Point toString() method for displayingmidpoint results.
For each test case, you must print sufficient information toallow you to confirm that the test case performed as expected.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply