Homework: Pyramid

Szabi Zsoldos -1 Tallied Votes 457 Views Share

This is intented for learning purposes, feel free to study the code

Create a drawing that looks like this (pyramid)
The number should be incremented with the modulus.

1.simple version
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    ***********
    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    
    2. modulus with array
    *
    ***
    *****
    *******
    *********
    **********
    *********
    *******
    *****
    ***
    *


    import java.util.ArrayList;
    import java.util.List;
    
    
    public class pyramidDrawing {
    
    	public static void main(String[] args) {
    		
    		simpleDrawing(10);
    		arrayDrawing(10);
    		
    	}
    	
    	
    	public static void simpleDrawing(int number) {
    		for(int i = 1; i <= number; i++) {
    			for(int j = 1; j <= i; j++) {
    				System.out.print("*");
    			}
    			System.out.println();			
    		}
    		
    		for(int x = 1; x <= (number+1) ; x++) {
    			System.out.print("*");
    		}
    		System.out.println();		
    		
    		for(int v = number; v >= 1; v--) {
    			for(int q = 1; q <= v; q++) {
    				System.out.print("*");
    			}
    			System.out.println();			
    		}		
    	}
    	
    	
    	public static void arrayDrawing(int number) {
    		List<Integer> numbers = new ArrayList<Integer>();
    		
    		for(int num = 1 ; num <= number; num++) {
    			numbers.add(num);
    		}
    		
    		for(int i = 0; i < numbers.size(); i++) {
    			if(i % 2 == 0) {
    				for(int j = 0; j <= i; j++) {
    					System.out.print("*");
    				}
    				System.out.println();
    			}
    		}
    		
    		for(int x = 1; x <= (numbers.size()) ; x++) {
    			System.out.print("*");
    		}
    		System.out.println();
    		
    		for(int v = numbers.size(); v >= 0; v--) {
    			if(v % 2 != 0) {
    				for(int q = 1; q <= v; q++) {
    					System.out.print("*");
    				}
    				System.out.println();
    			}
    		}		
    	}
    }
stultuske 1,116 Posting Maven Featured Poster

is there an actual question here, or is it just meant to be a 'how-to' post?

Szabi Zsoldos 26 Learner and helper guy

I posted it to the Tutorials for those who may need it for school :)

peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster

Then you should not posted at all because if they never learn basic they will fail. It is OK to help with attempt when someone tried and got catch in some circumstances that may not be obvious. However provide solution beforehand is plain bad.

JamesCherrill 4,490 Most Valuable Poster Moderator Featured Poster

WARNING: To anyone copying code for a homework assignments:
Your teacher can probably spot solutions copied from the web. If so you will get 0 marks, and may be penalised for cheating. YOu are also wasting your own time by cheating instead of learning. Write your own code!

Szabi: I'm sure your intentions were good, but in this forum we always perfer to teach people how to write good code. Your code may be a valid solution to the requirement, but in terms of commenting and clarity it's not a good example for a beginner, and not easy to learn from. Posts that just give solutions without explanations tend to pick up a lot of down-votes.

Szabi Zsoldos 26 Learner and helper guy

Got it, I will not post any snippets again :)

jwenting 1,807 duckman Team Colleague

snippets is fine, but entire solutions to common homework problems is not.

For example the following is helpful to a lot of people in real situations, but useless to a schoolkid trying to get away with not doing his homework:

String requestUri = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
requestURL.delete(requestURL.length()-requestUri.length(), requestURL.length()+1);
requestURL.append(request.getContextPath());
Szabi Zsoldos 26 Learner and helper guy

Yes, you're right, I did not think in that way at first but now, it's a different story :)

Be a part of the DaniWeb community

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