### Thread: Plotting two points using nested loops

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6

#### Plotting two points using nested loops

I searched but my question is so specific that I can't find anything.

What I have to do: create a graph in the command line that plots two points, x1 0-9, y1 0-9, x2 0-9, y2 0-9.

I was able to draw the graph like so:

Code:
``` for(int y=9; y>=-1; y--) //the grid is only 0-9, but we need an extra row for the bottom numbers
{
for(int x = -1; x<=9; x++) //the grid is only 0-9, but we need an extra row for the side numbers
{
if(y==-1)
{
//this is the bottom row of numbers,so we can just print x.
if(x==-1){
System.out.print("  "); //we don't want to print -1, so we can just print a space to shift the rest of the line over.
} else {
System.out.print(x +" ");  //print the current column number (0-9)
}
}

else if (y == x)
System.out.print(y + "|\n");```
BUT
I have no idea how I'm supposed to use an asterisk to represent that point

How do I make it draw the points according to the (x1, y1), (x2, y2) while it's making the graph in that for loop?
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6

#### Here is the whole program

Code:
```import java.util.*;

public class Proj3 {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.print("Enter x1: ");
double x1 = s.nextDouble();
System.out.print("Enter y1: ");
double y1 = s.nextDouble();
System.out.print("Enter x2: ");
double x2 = s.nextDouble();
System.out.print("Enter y2: ");
double y2 = s.nextDouble();

while ((x1<0) || (y1<0) || (x2<0) || (y2<0) || (x1>9) || (y1>9) || (x2 > 9) || (y2 > 9)) {
System.out.println("Invalid entry. Enter only 0-9");

System.out.print("Enter x1: ");
x1 = s.nextDouble();
System.out.print("Enter y1: ");
y1 = s.nextDouble();
System.out.print("Enter x2: ");
x2 = s.nextDouble();
System.out.print("Enter y2: ");
y2 = s.nextDouble();
}

while (x1 == x2 && y1 == y2) {
System.out.println("Invalid entry. Points must be different ");

System.out.print("Enter x1: ");
x1 = s.nextDouble();
System.out.print("Enter y1: ");
y1 = s.nextDouble();
System.out.print("Enter x2: ");
x2 = s.nextDouble();
System.out.print("Enter y2: ");
y2 = s.nextDouble();

}

double slope = ((x2-x1)/(y2-y1));
double yintercept = (y1-slope*x1);
//slope calculator
if (((y2-y1) == 0))
System.out.println("Undefined slope. Divide by zero error.");
else
{
System.out.println("The slope is " + slope);
if (yintercept >0)
System.out.println("y = " + slope + "x +" + yintercept);
else
System.out.println("y = " + slope + "x -" + yintercept);
}//end else

for(int y=9; y>=-1; y--) //the grid is only 0-9, but we need an extra row for the bottom numbers
{
for(int x = -1; x<=9; x++) //the grid is only 0-9, but we need an extra row for the side numbers
{
if(y==-1)
{
//this is the bottom row of numbers,so we can just print x.
if(x==-1){
System.out.print("  "); //we don't want to print -1, so we can just print a space to shift the rest of the line over.
} else {
System.out.print(x +" ");  //print the current column number (0-9)
}
}

else if (y == x)
System.out.print(y + "|\n");
}
}```
3. Could you copy and paste here the program's current output?
Add some omments describing what the output should look like.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6
it's supposed to do this

Code:
```Enter x1: 0
Enter y1: 0
Enter x2: 2
Enter y2: 2
The slope is 1.0
y = 1.0x -0.0
9|
8|
7|
6|
5|
4|
3|
2|     *
1|
0|*___________________
0 1 2 3 4 5 6 7 8 9```

Code:
```Enter x1: 0
Enter y1: 0
Enter x2: 2
Enter y2: 2
The slope is 1.0
y = 1.0x -0.0
9|
8|
7|
6|
5|
4|
3|
2|
1|
0|
0 1 2 3 4 5 6 7 8 9```
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6

Code:
``` else if(x1 == 0 && y1 == 0)
{
System.out.print("*"); //print a *
}```
output:
Code:
```**********9|
*********8|
*********7|
*********6|
*********5|
*********4|
*********3|
*********2|
*********1|
*********0|
*********  0 1 2 3 4 5 6 7 8 9```
My instructor gave me a hint that I should be putting spaces everywhere where there will not be an asterisk. I don't know how to implement that though.
Last edited by darris; February 16th, 2013 at 10:01 AM. Reason: more info
6. putting spaces everywhere where there will not be an asterisk
Use print(" ")
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6
I don't mean to spam, but I have figured it out up to this point: (I deleted the x and y variables and replaced them with row and col because it's harder to get confused)
Code:
```for(int row = 9; row >= -1; row--)
{
for(int col = -1; col <= 9; col ++)
{
if((col == x1 && row == y1) || (col == x2 && row == y2))
{
if((y1 ==0) && (y2 == 0))
{
System.out.print("*-");
}
else
{
System.out.print("* ");
}

}
else if(col == -1 && row != -1)
{
System.out.print(row + " ");
}
else if(row == -1 && col != -1)
{
System.out.print(col + " ");
}
else if(row == -1 && col == -1)
{
System.out.print("  ");
}
else if ( col==0 && row >0)
{
System.out.print("| ");
}
else if(row == 0 && col > -1)
{
System.out.print("--");
}
else
{
System.out.print("  ");
}
}
System.out.println();
}```
Output is either
Code:
```Enter x1: 1
Enter y1: 0
Enter x2: 2
Enter y2: 2
The slope is 0.5
y = 0.5x --0.5
9 |
8 |
7 |
6 |
5 |
4 |
3 |
2 |   *
1 |
0 --* ----------------
0 1 2 3 4 5 6 7 8 9```
or
Code:
```Enter x1: 1
Enter y1: 0
Enter x2: 2
Enter y2: 2
The slope is 0.5
y = 0.5x --0.5
9 |
8 |
7 |
6 |
5 |
4 |
3 |
2 |   *-
1 |
0 --*-----------------
0 1 2 3 4 5 6 7 8 9```
But now my issue is making it an *- on only ONE point.
If (1,0) and (2,2) are my points, it only needs to print an *- at (1,0) and just * at (2,2)

So how can I change that?
8. my issue is making it an *- on only ONE point.
Only add on the trailing - when on the line with the -s
Last edited by NormR; February 16th, 2013 at 11:12 AM.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6
I don't understand how I'm supposed to do that
does my block of ifs need to be changed?
10. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6
Nevermind, I see what you're saying
Thank you so much

I have to say of this forum
I went to the official java forum first and it basically said "Registration denied, we don't like spammers. If you feel this is an error click the contact us link"

This place is cool though.
11. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
32
Rep Power
6

#### Solution

I suppose I should mark this as solved and post the solution for another future java noobie

Code:
```import java.util.*;
import java.text.DecimalFormat;

public class Proj3 {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#0.00");
do {
System.out.print("Enter x1: ");
double x1 = s.nextDouble();
System.out.print("Enter y1: ");
double y1 = s.nextDouble();
System.out.print("Enter x2: ");
double x2 = s.nextDouble();
System.out.print("Enter y2: ");
double y2 = s.nextDouble();

while ((x1<0) || (y1<0) || (x2<0) || (y2<0) || (x1>9) || (y1>9) || (x2 > 9) || (y2 > 9)) {
System.out.println("Invalid entry. Enter only 0-9");

System.out.print("Enter x1: ");
x1 = s.nextDouble();
System.out.print("Enter y1: ");
y1 = s.nextDouble();
System.out.print("Enter x2: ");
x2 = s.nextDouble();
System.out.print("Enter y2: ");
y2 = s.nextDouble();
}

while (x1 == x2 && y1 == y2) {
System.out.println("Invalid entry. Points must be different ");

System.out.print("Enter x1: ");
x1 = s.nextDouble();
System.out.print("Enter y1: ");
y1 = s.nextDouble();
System.out.print("Enter x2: ");
x2 = s.nextDouble();
System.out.print("Enter y2: ");
y2 = s.nextDouble();

}

double slope = ((x2-x1)/(y2-y1));
double yintercept = (y1-slope*x1);
//slope calculator
if (((y2-y1) == 0))
System.out.println("Undefined slope. Divide by zero error.");
else
{
System.out.println("The slope is " + df.format(slope));
if (yintercept >0)
System.out.println("y = " + slope + "x +" + df.format(yintercept));
else
System.out.println("y = " + df.format(slope) + "x -" + df.format(yintercept));
}//end else

for(int row = 9; row >= -1; row--)
{
for(int col = -1; col <= 9; col ++)
{
if((col == x1 && row == y1) || (col == x2 && row == y2))
{
if((row == 0))
{
System.out.print("*-");
}
else
{
System.out.print("* ");
}

}
else if(col == -1 && row != -1)
{
System.out.print(row + " ");
}
else if(row == -1 && col != -1)
{
System.out.print(col + " ");
}
else if(row == -1 && col == -1)
{
System.out.print("  ");
}
else if ( col==0 && row >0)
{
System.out.print("| ");
}
else if(row == 0 && col > -1)
{
System.out.print("--");
}
else
{
System.out.print("  ");
}
}
System.out.println();
}

System.out.println("Go again? Y/N");
}

while("Y".equals(s.nextLine()) ||"y".equals(s.nextLine()));
}
}```
(It also includes a loop so the user can do it as many times as she/he wants)