## Homework J2## Due: Friday, 18 February 2005 by 10 a.m. |

The purpose of this homework is to gain further familiarity with classes and objects. In particular, we have developed a class named Line for you to use. As you might have guessed, class Line supports the representation of lines. In addition to its role in this homework, it is important that you understand the class Line specification - it is likely you will need to develop your implementation of the class in some future venue.

You will demonstrate your familiarity with classes by developing and
submitting a program LineDemo.java to accomplish some line manipulations. To do
so, you will need a copy of the Line.class file
(right-click on the link, and select the save option on your browser). It
is *IMPORTANT* for JCreator that your LineDemo.java and
our Line.class file be stored in the *SAME* folder.

Class Line represents a line using the form *y* = *mx* + *
b*. It's assumed that everybody in this class has a basic understanding of
this representation. If you feel you need review, or feel you don't understand
them well, please ask one of the course staff. Recall that *m* is the
slope, and *b* is the *y*-intersect of the given line. Although
this
representation does not allow us to represent vertical lines - we are using it
for its simplicity and student familiarity.

Note: some textbooks refer to *b* as the *y*-intercept, others as
the *y*-intersect (the difference being the 'cept' vs. 'sect'). We
are using the latter in this assignment.

Class Line has the following methods, all of which are public. Remember all of these behaviors are available for you to use once you download Line.class.

`Line()`The default constructor configures a new Line representation of the form 0

*x*+0; that is, a horizontal line along the*x*-axis.

`Line(double theSlope, double theIntersect)`The specific constructor configures a new Line representation using the passed values as the slope and

*y*-intersect.

`double getSlope()`This accessor method returns the slope (i.e. the

*m*value) of its Line.

`double getYIntersect()`This accessor method returns the

*y*-intersect (i.e. the*b*value) of its Line.

`void setSlope(double theSlope)`This mutator method sets the slope (i.e. the

*m*value) of its Line.

`void setYIntersect (double theIntersect)`This mutator method sets the

*y*-intersect (i.e. the*b*value) of its Line.

`double getYValue(double x)`This method returns the

*y*-value for the point on its Line associated with x-coordinate*x*.

`double computeIntersection (Line that)`This method returns the

*x*-value of the intersection point of two non-parallel lines -- its line and Line parameter that.

`Line createCopy ()`This method creates a new Line that is a copy of its Line.

Because Line also provides a method toString(), Line objects can be displayed with a System.out.println() statement. For example, the following code segment:

Line line1 = new Line (1,2); System.out.println ("linel: " + line1);

prints out:

linel: y=1.0x+2.0

The purpose of this assignment is to implement LineDemo.java based following
the below instructions. No
code is being given for this assignment. : The class **MUST** be
public and named LineDemo, and it
**MUST** be in a file called LineDemo.java, where the 'L' and the 'D' are the
only capital letters. If the file is not named LineDemo.java, the CS101 submit
tool will have a hissy fit and reject the submission. If the class is not LineDemo, then the program
will not compile and it will effect your grade severely. If the class is
not public, we will be unable to run the program and this too will effect your
grade severely.

Right. That being said, this is what the LineDemo class must do. Your class LineDemo
is to define a single method `main()` - no other methods are required for
this homework. Remember method `main()` must be defined as `public static void
main (String[] args)`. *Each* of
the provided Line methods will be used at *least once* various in
implementing the following design for LineDemo.

- Print the legend
- Set up the input stream
- Prompt and extract a slope and
*y*-intersect value - Define a Line variable
`line1`whose Line is constructed using the acquired inputs. - Define a Line variable
`line2`whose Line is default constructed. - Prompt and extract another slope and intersect value
- Reset the slope and intersect of
`line2`using the values input from step 6 - Display
`line1`and`line2`on separate lines. - Prompt and extract an
*x*-value - Display, on separate lines, the corresponding
*y*-values for`line1`and`line2`. - Define
`line3`to be a copy of the`line2`Line - Display the slope of
`line3` - Display the
*y*-intersect of`line3` - Compute and display the
*x*-value of the point where lines`line1`and`line2`intersect

The Line.class file does not handle parallel lines. Therefore, when you
are testing your program make sure you enter different slopes for lines `line1` and `line2`;
otherwise the program will misbehave.
Our testing program will not consider parallel lines.

There is a **lot **that this program must do. The best way to
approach it is to do each of the above steps in order - make sure you
have the prior step right before you proceed to the next step. In this way,
the problem of writing the entire program is broken down into 14
sub-parts each of which can easily be handled.

Below is a sample execution run of the program. The five values in red underline are those that were input by the user. Your program needs to produce a similar output.

This program shows the usage of the Line class Enter the slope for the first line:1Enter the y-intersect for the first line:2Enter the slope for the second line:3Enter the y-intersect for the second line:4Line 1 is y=1.0x+2.0 Line 2 is y=3.0x+4.0 Please enter an x-value:5Given an x value of 5.0, the y-value for line 1 is 7.0 Given an x value of 5.0, the y-value for line 2 is 19.0 The slope of line 3 is 3.0 The y-intersect of line 3 is 4.0 Lines 1 and 2 intersect at x-value of -1.0

Recall the good
programming practices listed in HW J1. * All homeworks
and lab quizzes must include these whenever appropriate.* Not
including them will effect the grading of the assignment.

Submit only LineDemo.java when you are done. There is no need to submit the Line.class file, as we already have a copy.