The Artima Developer Community
Sponsored Link

Java Answers Forum
Hey Help Me!

10 replies on 1 page. Most recent reply: Nov 27, 2006 4:43 AM by irfan siddique

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 10 replies on 1 page
lawrence deopante

Posts: 4
Nickname: enz
Registered: Sep, 2006

Hey Help Me! Posted: Sep 18, 2006 5:42 PM
Reply to this message Reply
Advertisement
i am a freshman computer science stud. and one of my new subject is java programming. we were ask to make a diamond program using for loop and the diamond should be made of (*). I write a diamond program, though its correct, my instructor told me to simplify this using other method. Pls Help Me! many tnx to you!

This is my program;

//Diamond program

public class Diamond {
public static void main(String[ ] args){


for (int a=1; a<6; a++){
System.out.print(" ");
for (int b=1; b<6-a; b++)
System.out.print(" ");
for (int c=a; c>=1; c--)
System.out.print("*");
for (int c=2; c<=a; c++)
System.out.print("*");
System.out.println();
}

for (int d=4; d>=1; d--) {
for(int e=6; e>=1+d; e--)
System.out.print(" ");
for( int f=d; f>=1; f--)
System.out.print("*");
for(int f=2; f<=d; f++)
System.out.print("*");
System.out.println();
}
}
}//end


Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: Hey Help Me! Posted: Sep 19, 2006 12:58 PM
Reply to this message Reply
public class nDiamonda {
public static void main(String[ ] args)
{
int i,j,k,n=24; //n is the number of lines and i,j are used for loops
k=(n%2==0)? n-1:n;
for( i = 0 ; i < n/2 ; i++ )
{
for( j = 0 ; j < k / 2 - i ; j++ )
System.out.print(" ");

for ( j = 0 ; j < i * 2 + 1 ; j++ )
System.out.print("*");
System.out.println();
}
if( n % 2 != 0)
{
for( i = 0 ; i < n ; i++ )
System.out.print("*");

System.out.println();
}
for( i = n/2-1 ; i >= 0 ; i-- )
{
for( j = 0 ; j < k/2 - i ; j++ )
System.out.print(" ");

for( j = 0 ; j < i*2+1 ; j++ )
System.out.print("*");

System.out.println();

}
}
}

anuj singhal

Posts: 4
Nickname: anujs
Registered: Sep, 2006

Re: Hey Help Me! Posted: Sep 20, 2006 2:50 AM
Reply to this message Reply
Will it help?

public class Diamond
{
// Size of the Diamond. Need to be an odd value.
private static int SIZE = 11;

public static void main(String argv[])
{
for(int i=0;i<SIZE;i++)
printLineItem(2*i + 1);
}

private static void printLineItem(int n)
{
int paddingLength = Math.abs(SIZE - n)/2;

String padding = "";
for(int i=0;i<paddingLength;i++)
padding += " ";

String result = padding;
for(int j=0;j<(SIZE - 2*paddingLength);j++)
result += "*";

System.out.println(result + padding);
}

}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Hey Help Me! Posted: Sep 20, 2006 3:29 PM
Reply to this message Reply
> i am a freshman computer science stud.

Computer science stud? Yeah, right. You wish.

If you were a computer science stud, you would have turned in this:
public class Diamond
{
    public static void main(String[] args)
    {
        System.out.println("   *\n  **\n ***\n****\n***\n**\n*"); // Q.E.D.
    }
}

lawrence deopante

Posts: 4
Nickname: enz
Registered: Sep, 2006

Re: Hey Help Me! Posted: Sep 26, 2006 5:32 PM
Reply to this message Reply
yah ryt... but the instruction is to use the FOR loop... and we are to use system.out.println to output a single char. :p

lawrence deopante

Posts: 4
Nickname: enz
Registered: Sep, 2006

Re: Hey Help Me! Posted: Sep 26, 2006 5:34 PM
Reply to this message Reply
tnx.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Hey Help Me! Posted: Sep 27, 2006 8:59 AM
Reply to this message Reply
Alternatively (given a spare coffee break);

It's a long way from being the simplest solution except that each method does approx one thing and all the variables have more or less maeningful names...
public class Testpad
{
    public static void main(String[] args){
        int size = 88;
        
        drawTopHalf(size);
        drawBottomHalf(size);
    }
 
    private static void drawTopHalf(int size){
        for (int slice = 0; slice <= size; slice++)
            drawSlice(slice, size);
    }
 
    private static void drawBottomHalf(int size){
        for (int slice = size - 1; slice > 0; slice--)
            drawSlice(slice, size);
    }
 
    private static void drawSlice(int slice, int size){
        drawLeadingSpaces(slice, size);
        drawBody(slice);
        System.out.println();
    }
 
    private static void drawLeadingSpaces(int slice, int size){
        for (int i = 0; i < size - slice; i++)
            System.out.print(" ");
    }
 
    private static void drawBody(int slice){
        int bodyWidth = 2 * slice - 1;
        for (int i = 0; i < bodyWidth; i++)
            System.out.print("*");
    }
}

Todd Blanchard

Posts: 316
Nickname: tblanchard
Registered: May, 2003

Dude, don't give them the answer Posted: Sep 28, 2006 11:20 PM
Reply to this message Reply
These people are your future co-workers. These questions are totally lame. Read a book.

Don't help people with elementary questions like this.

Wonder why your co-workers are morons that can't code? Its because they got all their homework answers off the internet.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Dude, don't give them the answer Posted: Sep 29, 2006 2:00 PM
Reply to this message Reply
Yep, all true but...

...I only knocked up an answer to see how long it would take me (answer - it took longer to find my penknife, open eclipse, configure a new project, type it out and run it than to work it out, about five minutes all told).

On the other hand, I did resist writing a JUnit tested version.

michael ppppp

Posts: 1
Nickname: mpea
Registered: Oct, 2006

Re: Hey Help Me! Posted: Oct 1, 2006 9:12 AM
Reply to this message Reply
// i made this method so you can input any number using:
// java dimond ..
// and it will make a dimond with length ..


public class dimond{

static String output = new String();
static String space = new String();
static int inputNo;
static int lineNo;

public static void main (String[] args){

int input = Integer.parseInt(args[0]); // get input
inputNo = input;
for(lineNo = 0; lineNo < input; lineNo++){
output = dimond(lineNo); // compute no of stars
space = spaces(lineNo); // compute no of spaces
System.out.print(space + output + "\n"); // writer ourput
}
}


public static String dimond(int x) {



if ( (x < 0) ){
output = "please input a valid number, eg \" java dimond 4 \" ";

}else {
int j;
output = "*";
if (x <= inputNo/2 ){
for (j =0; j < lineNo; j++) {
output = output + "**";
}
}
if (x > inputNo/2){
for (j = inputNo%2; j < inputNo - lineNo; j++) {
output = output + "**";
}
}
}
return output;
}


private static String spaces(int k){
int anum =0;
space = "";
if (k <= inputNo/2){
while (anum < (inputNo/2)- k ){
space += " ";
anum++;
}
k = 0;
}

if (k > inputNo/2){
if (inputNo%2 > 0){space += " ";} // when even numbers are put in
while (anum < inputNo/2 - (inputNo-k) ){
space += " ";
anum++;
}
k = 0;
}
return space;
}

}

irfan siddique

Posts: 1
Nickname: aerogram
Registered: Nov, 2006

Re: Hey Help Me! Posted: Nov 27, 2006 4:43 AM
Reply to this message Reply
public class Dimond
{
public static void main(String[] abc)
{
int dSize = 8;
int width = ((2* dSize) -1) ;
int middleRow = width / 2;
for( int thisRow = 0; thisRow < width; thisRow++ )
{
int numStars = width - (Math.abs((middleRow - thisRow)) * 2);
int numSpaces = (width - numStars) / 2;
int startPos = numSpaces;
int endPos = numSpaces + numStars - 1;
for ( int col = 0; col < width; col++ )
{
if( col < startPos || col > endPos )
System.out.print(" ");
else
System.out.print("*");
}
System.out.println("");
}
}
}

Flat View: This topic has 10 replies on 1 page
Topic: Hey Help Me! Previous Topic   Next Topic Topic: Compile Web & EJB Modules using ANT on Websphere Application Server V 6

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use