This weeks homework... (java) got the code, program runs right, one small problem...

Status
Not open for further replies.
Well, this weeks assignment is pretty crazy... first off, we had to make a program that would allow you to input a number, and then it would display a square made up of "*" with the dimensions of whatever number you inputted... (i.e. "4" would make a square * * * * and 4 lines down)

Code:
import javax.swing.JOptionPane;

public class SquareDraw {

	public static void main( String[] args) {

		int dimen =
		  Integer.parseInt( JOptionPane.showInputDialog(
		    "Enter the size of the square.") );

		int control1, control2;
		
		control1 = dimen;

		while( control1 > 0 ) {

		control2 = dimen;

		while( control2 > 0 ) {
		
		  System.out.print( "* " );
		  control2 --;
		}
		
		System.out.println();
		
		control1--;

		}

		System.exit( 0 );
}
}

Just noticed that for the assigment the square must be displayed using JOptionPane.showMessageDialog...can soemone show me how to do this the simple way? I tried, but for some reason I just can't get it to work.

Next Problem is to make a program to display a triangle like the square above.. I figure I need to set the loop to remove a star for every count-- on the height loop... but how do I do that? Adding a star is easy, taking one away is confusing :) I am supposed to use the same code above and just alter it.

Thanks for any help
Anthony
 

Heewa

Smack-Fu Master, in training
68
I don't know Java at all, but I think I get what you're saying about the first part. You need to create a string instead of outputing it right away. Something like:

Code:
string S[D*(D+1)];  //To accound for the extra newline character
int D;

input dimention into D

for(i=0; i<D; i++)     
{
 for(j=0; j<D; j++)    
  S[i*(D+1)+j] = '*';      
 S[i*(D+1)+D] = newline;
}

output S


As for the triangle bit, only a slight modification is needed:

Code:
for(i=0; i<D; i++)
{
 for(j=0; j<i+1; j++)
  S[i*(D+1)+j] = '*';   //Stars
 for(j=i+1; j<D; j++)
  S[i*(D+1)+j] = ' ';   //The rest are spaces
 S[i*(D+1)+D] = newline;
}

I haven't actually checked this code, and it's sorta early still, so it might be a little messed up, but the general idea is right...I hope!
 
Next Problem is to make a program to display a triangle like the square above.. I figure I need to set the loop to remove a star for every count-- on the height loop... but how do I do that? Adding a star is easy, taking one away is confusing :) I am supposed to use the same code above and just alter it.
Easiest way to do this is to not take one away, but add one fewer asterisks. Because your control2 loop is where the asterisks are being added, you will need to change it. How? Well, you want to output as many stars as the row you are currently in... like so:
*
* *
* * *
* * * *
So the number of stars we display depends on the row we are in... and the row we are in is represented by control1. So, we need to change the way that control2 loops:
Code:
	int control1, control2;
	control1 = 0;

	while( control1 < dimen ) {
		control2 = 0;
		while( control2 < control1 ) {
			System.out.print( "* " );
			control2++;
		}
		
		System.out.println();
		control1++;
	}
You'll note that control2 now begins at 0, and increments until it reaches the size of control1 (the row you are in.) control1 also now loops in this fashion. This is the most standard form of looping... unless your instructor specifically said to use a decrement loop, I would use an increment loop. (Of course, as your skill increases, you'll be able to decide which is more appropriate for the situation at hand -- if your triangle was supposed to start big at the top and end up small at the bottom, you'd want to decrement control1 as you did before.)

In the preview frame, I just noticed Heewa's code -- a cursory glance reveals it should work for C++, but will not work as a copy & paste into Java. You also do not need to create a string before outputting. My own disclaimer: I don't program Java, I program C++, so there could be some minor syntax errors, but there shouldn't be anything major.

- Rob
 

Heewa

Smack-Fu Master, in training
68
Rob: You also do not need to create a string before outputting.

The reason I got the impression that you did need to create it before outputing was:

Xynie: Just noticed that for the assigment the square must be displayed using JOptionPane.showMessageDialog

Again, I'm not familiar with Java, but it looks like it's one function call that pops up something (window, dialog..) that displays some text. Once it's up, you can't modify it like you would in a console. If you could, his code would have been fine with a square. But, I'm just guessing here..
 
For those learning for actually following my progress in learning Java... this is how I got the assignments to work...

First off, the convert to JOptionPane assignment...

Code:
import javax.swing.JOptionPane;

public class SquareDraw {

	public static void main( String[] args) {

		int dimen =
		  Integer.parseInt( JOptionPane.showInputDialog(
		    "Enter the size of the square.") );

		String lines = "";		
		
		int control1, control2;
		
		control1 = dimen;

		while( control1 > 0 ) {

		control2 = dimen;

		while( control2 > 0 ) {
		
		  lines = lines + "* ";
                  
                  control2 --;
		}
		
		lines = lines + "\n";
		
		control1--;

		}

		JOptionPane.showMessageDialog(null, lines);	

		System.exit( 0 );
}
}

The next assignment was this, please note the triangle cannot be a right triangle like my quote, must be an equilateral triangle.

1) Write a program application called SquareOrTriangle that takes two (2) user inputs: i) a shape and ii) a "size".

The shape can be either "square" or "triangle" (CASE-INSENSITIVE). The user should be made aware of the fact that there are only two choices -- namely, either "square" or "triangle" -- and the program should prompt the user for input until he or she inputs an acceptable shape (as many prompts as it takes...hint).

The meaning of the "size" that the user enters depends on what shape we're dealing with. If the shape that the program has just taken as input is "square", the "size" is the length of the sides of the square. If the shape is "triangle", then "size" means the length of the BASE of the triangle.

Once the program has collected this information from the user, it should create the appropriate shape and display it to the user in a JOptionPane.showMessageDialog(...) box. You MUST use JOptionPane for all input and output.

Here's how the program should behave.

Program: Enter a shape (either 'square' or 'triangle').
User: circle
Program: Enter a shape (either 'square' or 'triangle').
User: rhombus
Program: Enter a shape (either 'square' or 'triangle').
User: Oval
... [This goes on for a while]
Program: Enter a shape (either 'square' or 'triangle').
User: sQUarE
Program: Enter the "size" of this square.
User: 5
Program: Here's your square.
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
OR...Had I entered "triangle"...

User: tRIANgle
Program: Enter the "size" of this triangle.
User: 5
Program: Here's your triangle
* * * * *
* * * *
* * *
* *
*
You can draw your triangles either way, upside-down or right-side-up. You should also note that the program did not discriminate between "sQUarE" and "square" (and similarly for "triangle").

My code, which tested out...

Code:
import javax.swing.JOptionPane;

public class SquareOrTriangle {

	public static void main( String[] args) {

		String shape = JOptionPane.showInputDialog(
		    "Enter a shape (either 'square' or 'triangle').");
                
                while (!(shape.equalsIgnoreCase( "square") || shape.equalsIgnoreCase( "triangle"))){ 
                    
                  shape = JOptionPane.showInputDialog(
		    "Enter a shape (either 'square' or 'triangle').");  
                }
                  if  (shape.equalsIgnoreCase( "square" )) {	

       	          int size = Integer.parseInt( JOptionPane.showInputDialog(
		    "Enter the size of the square.") );

		  String square = "";		
		
		  int control1, control2;
		
		  control1 = size;

		  while( control1 > 0 ) {

		    control2 = size;

		  while( control2 > 0 ) {
		
		    square = square + "* ";
                  
                    control2 --;
		}
		
		    square = square + "\n";
		
		    control1--;

		}

		JOptionPane.showMessageDialog(null, square);
		System.exit( 0 );	
		}
                else    {
                        int size = Integer.parseInt( JOptionPane.showInputDialog(
		   	 "Enter the size of the triangle.") );

		 	String triangle = "";
                        
                        String spacer = " ";
		
			   int control1 = size;

                           int control2;
                           
                           while ( control1 > 0) {
                           
                               control2 = control1;
                               
                           while( control2 > 0 ) {
	                                                 
                            triangle = triangle + "* ";
                            
                            control2--;
                        }   
                          
			    triangle = triangle + "\n" + spacer;
                            
                            spacer = spacer + " ";
		
			    control1--;
                           }
                                                     
			    JOptionPane.showMessageDialog(null, triangle);
			    System.exit( 0 );
			}
 
                
                         	   		
}
}

Thanks for looking! I am learning... glad i got these workin... whew...
 
Status
Not open for further replies.