The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - Plotting two points using nested loops
Discuss Plotting two points using nested loops in the Java Help forum on Dev Shed. Plotting two points using nested loops Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 16th, 2013, 12:15 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
|
Homework - 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?
|

February 16th, 2013, 12:17 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
|
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");
}
}
|

February 16th, 2013, 06:43 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Could you copy and paste here the program's current output?
Add some omments describing what the output should look like.
|

February 16th, 2013, 09:56 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
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, 10:00 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
|
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 10:01 AM.
Reason: more info
|

February 16th, 2013, 10:44 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | putting spaces everywhere where there will not be an asterisk |
Use print(" ")
|

February 16th, 2013, 11:05 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
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, 11:08 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | 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.
|

February 16th, 2013, 11:17 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
|
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, 11:28 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
|
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, 02:39 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 32
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
|
|
|
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)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|