![]() |
| ||
| Java home work help (long post) This is a lab I am working on. I think that I am on the last step. My problem/question is highlighted in bold. 1. Create a constructor that allows you to specify the 2 endpoints of the line being created. Leave the original constructor alone. Your program should have 2 constructors now. 2. Create an accessor method that returns the color of the line. 3. Create a mutator method that lets you change the (x, y) coordinates of the endpoints to newly specified values. Don't just change values by some constant, allow new values to be entered for each coordinate. Don't forget to redraw the line after you change it. 4. Create an accessor method that returns the length of the line as an integer (not a double). 5. Create a method that prints out the midpoint of a line (print it out, this method returns nothing: void). Use the following format (i.e. if I use your program to create a line from (10, 20) to (100, 120) this method should print exactly as follows): The midpoint of the line from (10, 20) to (100, 120) is (55, 70). Hint: The midpoint remains unchanged if you swap the endpoints. 6. Create a method that finds the slope of a line. The method will print out the slope if it is defined, otherwise it will print out the x-value of the line. Print the slope as a fraction int/int (i.e. do not do the division. Note: Since the origin is in the upper left corner with x increasing to the right and y increasing down the slope will be different from what you would expect. What is different? Fix your code so that lines rising from left to right have a positive slope and lines falling from left to right have a negative slope.) If the slope is not defined the method should return false. If it is defined the method returns true. Here are several examples using different lines: For a line with endpoints (0,0) and (100, 200), acceptable outputs are: "Slope is : 200/-100" or "Slope is : -200/100" For a line with endpoints (100,0) and (0,200), acceptable outputs are: "Slope is : 200/100" or "Slope is : -200/-100" For a line with endpoints (0,100) and (200,100), the acceptable output is: "Slope is : 0" For a line with endpoints (100,0) and (100,200), the acceptable output is: "Slope is undefined: x = 100" My problem is that is seems that for my line there is no "anchor". If I start with X1,Y1 in the top left and X2,Y2 in the bottom right. My problem is that when I enter new X,Y coordinates if the X2,Y2 numbers are smaller than X1,Y2 it just seems to flip places with X1,Y1 essentially keeping the line going from top left to bottom right. I also have a sneaking suspicion that I didn't follow instruction three correctly. This is all ran using the program BlueJ for a beginner programming class. The area that I had to write into is blocked off by //------------------------ I am not supposed to change anything outside of the //--------------------'s Here is my code
|
| ||
| Re: Java home work help (long post) What I am looking for is a general direction and not necessarily the actual code snippet I may need. Basically how do I keep X1, Y1 and the top spot on a line and X2, Y2 as bottom. Like \ /. As it is now my program just draws \ no matter the input of X1, Y1 and X2, Y2. I can change the color, length, starting position. Move, slow move, etc but I cant get X1,Y1 to stay up top. Hope this helps explain my predicament better. |
| ||
| Re: Java home work help (long post) You're right - you didn't follow number 3 correctly. Your code is adding the new coordinates to the old when all it needs to do is set them x1 = xNew1; |
| ||
| Re: Java home work help (long post) You are right. For some reason though it wasn't actually adding xNew1 to the old value in x1 so I left it alone. As I said my biggest problem is my second post there. I will correct that += code for step three just for clarity sake though. Thanks! |
| ||
| Re: Java home work help (long post) Well, the draw method will draw between the two points you enter, so perhaps part of the issue is actually with the coords you are supplying, but perhaps you are looking for something like int left = Math.min(x1, x2);?? |
| ||
| Re: Java home work help (long post) The actual problem was with the coordinates I was given AND the picture examples provided. The pictures were \ / when they should have just been \\ for the values given. Thanks for your time and help though. Also my length of the line equation should have been public int getLength() |
| All times are GMT -4. The time now is 9:30 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC