February 16th, 2013, 01:15 AM

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 09, y1 09, x2 09, y2 09.
I was able to draw the graph like so:
Code:
for(int y=9; y>=1; y) //the grid is only 09, but we need an extra row for the bottom numbers
{
for(int x = 1; x<=9; x++) //the grid is only 09, 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 (09)
}
}
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?
February 16th, 2013, 01:17 AM

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 09");
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 = ((x2x1)/(y2y1));
double yintercept = (y1slope*x1);
//slope calculator
if (((y2y1) == 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 09, but we need an extra row for the bottom numbers
{
for(int x = 1; x<=9; x++) //the grid is only 09, 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 (09)
}
}
else if (y == x)
System.out.print(y + "\n");
}
}
February 16th, 2013, 07:43 AM

Could you copy and paste here the program's current output?
Add some omments describing what the output should look like.
February 16th, 2013, 10:56 AM

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
but instead it does 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
February 16th, 2013, 11:00 AM

My hilariously bad attempt at adding in the asterisk
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 11:01 AM.
Reason: more info
February 16th, 2013, 11:44 AM

putting spaces everywhere where there will not be an asterisk
Use print(" ")
February 16th, 2013, 12:05 PM

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?
February 16th, 2013, 12:08 PM

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 12:12 PM.
February 16th, 2013, 12:17 PM

I don't understand how I'm supposed to do that
does my block of ifs need to be changed?
Thank you for your patience
February 16th, 2013, 12:28 PM

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"
There was no contact us link that I could find lol
This place is cool though.
February 21st, 2013, 03:39 PM

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 09");
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 = ((x2x1)/(y2y1));
double yintercept = (y1slope*x1);
//slope calculator
if (((y2y1) == 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)