Hey everyone. I need a bit of advise on my code.

Here is a sample image.

^
20+
|
|
|
|
15+
|
|
|
|
10+
|
|
|
|
5+
|
|
|
|
+----+----+----+----+----+----+----+----+>
0 5 10 15 20 25 30 35 40

Upper Left: (5,10) Width: 8, Height: 4

********
********
********

EDIT: noticed the horizontal and vertical numbers are off on the forum here, but fine in program console.

Here is the code.

//program START


//create class for filename
public class DrawRectangle  

{
    
    //create main arugment to show string text
    public static void main(String[] args) 
        
    {
        
       
     
        //temporarly hard code values.
        int height = 4;
        int width = 8;
        int x = 5;
        int y = 10;

 
    //draw the whole graph and rectangle 
        System.out.println("\n\n   ^");
        
        //draw the verticle axis 
        for (int m = 20; m > 0; m--)
            
        {
		
	if (m == 5)
	  System.out.println("  " + m + "+");
	
         else if (m % 5 == 0)  
            System.out.println(m + "+");
	  
        else 
            System.out.println("    |");
        
        }
        
    
        System.out.print("    ");
	
        //draw the horizontal axis
        for (int n = 0; n <= 40; n++)
            
        {
           if (n % 5 == 0)
           
               System.out.print("+");
                
           else 
                System.out.print("-");
        }
        
        System.out.print(">");
        
        System.out.println();
        
        //create numbers for the horizontal axis
          for (int p = 0; p <= 40; p++)
            
        {
           if (p % 5 == 0)
           
               System.out.print("    " + p);
        }
        
        
        //draw the description
        System.out.println("\n\n Upper Left: (" + x + ","  + y + ") Width: " + width + ", Height: " + height + "\n\n");
        
  
		
	//printing the rectangle
	for (int hcount = 1; hcount <= height; hcount++)
            
	{
		System.out.println("");
        
		for(int wcount = 1; wcount <= width; wcount++)
            
		System.out.print("*");
	}
     
        
    } // main method end brace.
    
} // drawrectangle class end brace

//program END

So what is my problem?

Well I need the "*" rectangle to fit on the graph, on the PROPER coordinates.

Basically, I just need a starting point for the code highlighted in red to execute.

Now I just can't seem to figure it out.

I'm wondering... if x = n (the horizontal axis) and if y = m (the vertical axis). Then I'd assume the rectangle would execute at the correct coordinates?

I'm just a bit blank on how to execute the rectangle loop.

Thanks.

Recommended Answers

All 14 Replies

You need to print the vertical axis and the rectangle at the same time - you can't go back up the page.
So as you do the vertical axis, when you get to the line where the rectangle should start, start printing asterisks as well.
You can get the horizontal position by putting the right number of blanks before the asterisks.

Use Java GUI.

If you cannot use GUI then use two dimensional (jagged) character array. Fill it with blanks and '*' characters and print it.

You need to print the vertical axis and the rectangle at the same time - you can't go back up the page.
So as you do the vertical axis, when you get to the line where the rectangle should start, start printing asterisks as well.
You can get the horizontal position by putting the right number of blanks before the asterisks.

Thanks.

Yes, I have it on the bottom ONLY for coding purposes. I know it should print during the vertical axis print.

However, I can't seem to figure out WHEN it should print, or how I could match the x,y coordinates.

Eg. say the coordinates are 5,10.

Then it would need to print the first * at 5,10 on the graph.

I was wondering what could be done here?

The only thing I can think of at 3am right now :p is that if the x,y match the m(horizontal numbers),n (vertical numbers)??

You can convert between y numbers and m numbers (draw a picture and write in some typical y,m numbers and it will become clear).
The n numbers are irrelevant because you only have them when you draw the x axis, but the code in you red loop is very close to what you need (when you put it in the right place). FYI the print(...) method is just like println(...) except that it doesn't put a newline at the end, so you can use it to do two prints to the same line.
Having said all that, I think that adatapost's approach is much better - just set up a 2D char array as big as your final graph, fill it with blanks, and loop down or across it as many times as you want, putting asterisks or digits or whatever wherever you need them, then just print that out.

We are basically supposed to use if-else, while and for loops for this problem. No arrays or anything else.

I'm still unclear as to how I can print the stars at the CORRECT spot.

OK. So, as you loop down the page, on each line, first print the axis symbols/characters, as you do now but without the newline (ie print, not println) then look at the values of m and y to decide whether to print nothing or a block of asterisks, then print the newline.
Like I said, draw a picture and write in some typical y,m numbers. This will help you get the correct logic for when to print asterisks.

OK. So, as you loop down the page, on each line, first print the axis symbols/characters, as you do now but without the newline (ie print, not println) then look at the values of m and y to decide whether to print nothing or a block of asterisks, then print the newline.
Like I said, draw a picture and write in some typical y,m numbers. This will help you get the correct logic for when to print asterisks.

Thanks James.

I was working on it this morning and got almost everything to work. I just have a small issue with the vertical line due to print/println issues, which I need to resolve.

Are the mods here fucked in the head?

I have repeatedly asked the mods to ERASE my code.. I can't edit my post. I am tired of asking over and over again.

IT IS MY CODE and I WANT IT OFF!

Now please take it off.

holy fucking hell... how hard is it to edit and remove.

They are not going to do that. I've seen them refuse to do it before, and rightfully so. Anyway, by posting here or on any other site, you essentially give the site the right to display that work indefinitely. Are you afraid of failing a class or something?

They are not going to do that. I've seen them refuse to do it before, and rightfully so. Anyway, by posting here or on any other site, you essentially give the site the right to display that work indefinitely. Are you afraid of failing a class or something?

No, but the teacher uses this code for next term.

Someone could easily come here and STEAL the code and claim it as their own.

I don't want anyone STEALING my code.

From now on I am NEVER posting here.

Daniweb is the WORST place for help anyways. I got it resolved myself. Great.

OH btw to the mods. I DONT CARE if you give me a warning or infraction. I am DONE with this forum and WILL NOT recommend it to my fellow students or anyone in the future.

Someone could easily come here and STEAL the code and claim it as their own.

I don't want anyone STEALING my code.

From now on I am NEVER posting here.

Daniweb is the WORST place for help anyways. I got it resolved myself. Great.

I am DONE with this forum and WILL NOT recommend it to my fellow students or anyone in the future.

I don't know which part of your post is more ignorant. I'm still dumbfounded that you have the audacity to waste people's time (James in this case) and then tell them that their help was not worthwhile. And I'm also wondering why you would post on a forum which primarily deals in programming questions and programming help, and expect those posts and those people's efforts to help to be erased just because of your selfishness. Furthermore, this site has 500,000+ members, and nobody cares if some bratty kid who expects quick solutions - not guidance - to his problems doesn't recommend the site to his friends. I'm sure there will be no shortage of kids who expect their homework done for them. [/irony]

commented: I agree wholeheartedly. +17

There's really no need for you to shout & scream like that. The only code you posted was a few loops and prints that didn't work anyway (in fact that's why you asked for help). You never posted a working version that would be useful to anyone else.
I'm sorry my help was not useful to you, if you had let me know I could have saved quite a bit of time. Goodbye.

commented: Well stated. +17

Are you going to edit my post or not?

> Are you going to edit my post or not?

As per the site rules, no. Locked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.