Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 16th, 2013, 12:15 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old February 16th, 2013, 12:17 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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");
    }
}

Reply With Quote
  #3  
Old February 16th, 2013, 06:43 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Could you copy and paste here the program's current output?
Add some omments describing what the output should look like.

Reply With Quote
  #4  
Old February 16th, 2013, 09:56 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old February 16th, 2013, 10:00 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old February 16th, 2013, 10:44 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
putting spaces everywhere where there will not be an asterisk

Use print(" ")

Reply With Quote
  #7  
Old February 16th, 2013, 11:05 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #8  
Old February 16th, 2013, 11:08 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #9  
Old February 16th, 2013, 11:17 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #10  
Old February 16th, 2013, 11:28 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #11  
Old February 21st, 2013, 02:39 PM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Plotting two points using nested loops

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap