Homework J2

Due: Friday, 18 February 2005 by 10 a.m.

Home | Resources | Homeworks | Slides | Labs | Contacts | Submit | Forums | Private


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.


Line class

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.

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. WARNING: 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.

  1. Print the legend
  2. Set up the input stream
  3. Prompt and extract a slope and y-intersect value
  4. Define a Line variable line1 whose Line is constructed using the acquired inputs.
  5. Define a Line variable line2 whose Line is default constructed.
  6. Prompt and extract another slope and intersect value
  7. Reset the slope and intersect of line2 using the values input from step 6
  8. Display line1 and line2 on separate lines.
  9. Prompt and extract an x-value
  10. Display, on separate lines, the corresponding y-values for line1 and line2.
  11. Define line3 to be a copy of the line2 Line
  12. Display the slope of line3
  13. Display the y-intersect of line3
  14. 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.


Sample Execution

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: 1
Enter the y-intersect for the first line: 2
Enter the slope for the second line: 3
Enter the y-intersect for the second line: 4
Line 1 is y=1.0x+2.0
Line 2 is y=3.0x+4.0
Please enter an x-value: 5
Given 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


Good programming practices

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.