My homework assignment is to make a program that returns a shape like below if you enter a number ( I think it's 3 ). I'm really new at Java and am not sure how so if someone could help me it would be awesome.
``````
---
/   \
/     \
/       \
\       /
\     /
\   /
---
``````

This is what I have so far. Not to much should be needed.

``````import java.util.Scanner;

public class hw2
{
public static void printnc(int n, char c) {
for (int k = 0; k < n; k++)
System.out.print(c);
}

public static void printDiamond(int n)  {

printnc(n,' ');
printnc(n,'-');
System.out.println('-');

for (int k = 0; k < n; k++) {
printnc(n-k-1, ' ');
System.out.print('/');
printnc(3*k, ' ');
System.out.println('\\');
}
for (int k = 0; k < n; k++) {
printnc(k, ' ');
System.out.print('\\');
printnc(2*(n-k+2), ' ');
System.out.println('/');

printnc(n,' ');
printnc(n,'-');
System.out.println('-');

}
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("input the number, please:");
String line = scan.nextLine().trim();
int n = Integer.parseInt(line);
printDiamond(n);
}
}``````
Last edited by Seef at Sep 16, 2008,
it could be done way simpler than that man, if all you need to do is draw that when the scanner gets a 3

EDIT: give me a minute or two and i'll see what i can do (around my own java homework funnily enough, we're doing AI)

EDIT2: escape characters are a bit gay, lol. this does what you said you needed it too, whether its what they're looking for or not is another thing.

``````import java.util.Scanner;

public class ShapeThing {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

int input = s.nextInt();

if (input == 3) {

System.out.println("   ---");
System.out.println("  /   \\ ");
System.out.println(" /     \\ ");
System.out.println("/       \\ ");
System.out.println("\\       / ");
System.out.println(" \\     / ");
System.out.println("  \\   / ");
System.out.println("   ---");
}
else System.out.println("Please enter an integer");

}

}``````
Last edited by Diamond Dave at Sep 16, 2008,
What this guy said ^^
my MG15DFX has a button that simulates the sound of one of the expensive tube marshall amps

Fender Stratocaster HSS
LTD EC-400AT
Traynor YCV-50 Blue
Peavey Envoy 110

Wishlist: Hamer USA Explorer, Gibson Explorer
what that guy said about that guy ^^
Originally Posted by smb
I'm an arrogant bastard - I thought I was good before I'd plucked a note. I was right, of course.

Quote by MetalBass 77
sonsie knows all
and you should also add some exception handling to that as well to make it more robust but yeah you get the idea.

we had to do something similar when we were first starting out with java at uni where the number actually defined how many lines in the shape there were which was a bit trickier
I dont think the point of this assignment is to create the pattern for n=3, but for any general input value. Dont know about java, but I could do that in c++.
Quote by hitman_47
I dont think the point of this assignment is to create the pattern for n=3, but for any general input value. Dont know about java, but I could do that in c++.

Yeah like if I put in 4 it should print 4 / or - for each side. I need to do something with the multiplication and addition and stuff but I don't have Java on my computer right now to mess with it.

Dave - Yeah that was the first assignment where we had to write our names.
Here's the real deal, you lazy poogs.

``````
import java.util.Scanner;

class Diamond
{
public static void main(String[] args)
{
Diamond d = new Diamond();
Scanner scan = new Scanner(System.in);
System.out.println("input the number, please:");
String line = scan.nextLine().trim();
int n = Integer.parseInt(line);
d.printDiamond(n);
}

public void printDiamond(int n)
{
String tmp="";
// special "case", don't think my algoritm can handle this case so
//I hardcoded it, didn't try with my algoritm though, wrote this one before.
if(n == 1)
{
tmp += " _\n";
tmp += "/ \\\n";
tmp += "\\_/\n";
}
else
{
for(int i=1; i<=n; i++)
{
// first half of the diamond
if(i==1) // first row
{
tmp = appendSymbol(" ", n, tmp);
tmp = appendSymbol("_", n, tmp);
tmp = addNewRow(tmp);
}
/*
You can throw these inside themselves if you just want one line,
but after failing with my algoritms the first time
I seperated them and was too lazy to smack them into each other.
*/
tmp = appendSymbol(" ", (n-i), tmp);
tmp = appendSymbol("/", tmp);
tmp = appendSymbol(" ", (n + ((i-1) * 2)), tmp);
tmp = appendSymbol("\\", tmp);
tmp = addNewRow(tmp);
}

for(int i=1; i<=n; i++)
{
// second half
if(i==n) //last row
{
tmp = appendSymbol(" ", n-1, tmp);
tmp = appendSymbol("\\" , tmp);
tmp = appendSymbol("_", n, tmp);
tmp = appendSymbol("/", tmp);
tmp = addNewRow(tmp);
}
else
{    //same as above but "reversed"
tmp = appendSymbol(" ", ((i-n)+(n-1)), tmp);
tmp = appendSymbol("\\", tmp);
tmp = appendSymbol(" ", (((3*n)-2)-(2*i)+2), tmp);
tmp = appendSymbol("/", tmp);
tmp = addNewRow(tmp);
}
}
}
System.out.println(tmp);
}

public String addNewRow(String s)
{ //adds the new row escape sequence to the end of string s.
s+="\n";

return s;
}

public String appendSymbol(String a, int n, String s)
{  //appends string a x n times to string s (it adds a to the end of s)
/*
if a is errorneous, then s becomes null, if s is null the loop
does not start and s is set to "" and returned. (empty string, not null!)
*/
if(a==null)
s=null;
//if a is a empty string, then nothing will happen,
//so does not need to check that.
if(s!=null)
{
for(int i=0; i<n; i++)
{
s+=a;
}
}
else
s="";

return s;
}

public String appendSymbol(String a, String s)
{ //if you give only two strings it will do the above with n as 1.
//Can't remember the term for this specific technique.
if(a==null)
s=null;
if(s!=null)
{
s+=a;
}
else
s="";

return s;
}
}
``````
Quote by Prodigal
Here's the real deal, you lazy poogs.

``import java.util.Scanner;class Diamond {    public static void main(String[] args)     {        Diamond d = new Diamond();        Scanner scan = new Scanner(System.in);        System.out.println("input the number, please:");        String line = scan.nextLine().trim();        int n = Integer.parseInt(line);        d.printDiamond(n);    }        public void printDiamond(int n)    {        String tmp="";        // special "case", don't think my algoritm can handle this case so         //I hardcoded it, didn't try with my algoritm though, wrote this one before.        if(n == 1)         {            tmp += " _\n";            tmp += "/ \\\n";            tmp += "\\_/\n";        }        else        {            for(int i=1; i<=n; i++)            {                // first half of the diamond                if(i==1) // first row                {                    tmp = appendSymbol(" ", n, tmp);                    tmp = appendSymbol("_", n, tmp);                    tmp = addNewRow(tmp);                }                /*                 You can throw these inside themselves if you just want one line,                  but after failing with my algoritms the first time                  I seperated them and was too lazy to smack them into each other.                */                tmp = appendSymbol(" ", (n-i), tmp);                tmp = appendSymbol("/", tmp);                tmp = appendSymbol(" ", (n + ((i-1) * 2)), tmp);                tmp = appendSymbol("\\", tmp);                tmp = addNewRow(tmp);            }                        for(int i=1; i<=n; i++)            {                // second half                if(i==n) //last row                {                    tmp = appendSymbol(" ", n-1, tmp);                    tmp = appendSymbol("\\" , tmp);                    tmp = appendSymbol("_", n, tmp);                    tmp = appendSymbol("/", tmp);                    tmp = addNewRow(tmp);                }                else                 {    //same as above but "reversed"                    tmp = appendSymbol(" ", ((i-n)+(n-1)), tmp);                    tmp = appendSymbol("\\", tmp);                    tmp = appendSymbol(" ", (((3*n)-2)-(2*i)+2), tmp);                    tmp = appendSymbol("/", tmp);                    tmp = addNewRow(tmp);                }            }        }        System.out.println(tmp);    }        public String addNewRow(String s)     { //adds the new row escape sequence to the end of string s.        s+="\n";                return s;    }        public String appendSymbol(String a, int n, String s)     {  //appends string a x n times to string s (it adds a to the end of s)            /*            if a is errorneous, then s becomes null, if s is null the loop             does not start and s is set to "" and returned. (empty string, not null!)            */        if(a==null)                s=null;            //if a is a empty string, then nothing will happen,            //so does not need to check that.        if(s!=null)        {            for(int i=0; i<n; i++)            {                s+=a;            }        }        else             s="";                return s;    }        public String appendSymbol(String a, String s)     { //if you give only two strings it will do the above with n as 1.         //Can't remember the term for this specific technique.        if(a==null)            s=null;        if(s!=null)        {            s+=a;        }        else            s="";                    return s;    }}``

Wow! You explained it and everything! In the process of getting Java to try it out. Thanks!
Had to put this in code blocks to make it scale right, here's the print out from my app.

``````C:\diamondtest>java Diamond
input the number, please:
1
_
/ \
\_/

C:\diamondtest>java Diamond
input the number, please:
2
__
/  \
/    \
\    /
\__/

C:\diamondtest>java Diamond
input the number, please:
3
___
/   \
/     \
/       \
\       /
\     /
\___/

C:\diamondtest>java Diamond
input the number, please:
4
____
/    \
/      \
/        \
/          \
\          /
\        /
\      /
\____/

C:\diamondtest>java Diamond
input the number, please:
5
_____
/     \
/       \
/         \
/           \
/             \
\             /
\           /
\         /
\       /
\_____/

C:\diamondtest>java Diamond
input the number, please:
6
______
/      \
/        \
/          \
/            \
/              \
/                \
\                /
\              /
\            /
\          /
\        /
\______/

C:\diamondtest>java Diamond
input the number, please:
7
_______
/       \
/         \
/           \
/             \
/               \
/                 \
/                   \
\                   /
\                 /
\               /
\             /
\           /
\         /
\_______/

C:\diamondtest>java Diamond
input the number, please:
8
________
/        \
/          \
/            \
/              \
/                \
/                  \
/                    \
/                      \
\                      /
\                    /
\                  /
\                /
\              /
\            /
\          /
\________/

C:\diamondtest>``````
np bro, havn't coded in java for a few months so this was a nice paced "refresher".
Last edited by Prodigal at Sep 16, 2008,
Oh, and don't ask me to explain how I came up with the algorithms. :P
It was a real pain in the *bleep*.

If you want something else explained, ask away.
Is there anyway someone can fix the algorithms in the first code I posted? I can't figure out what algorithms do what.

EDIT - I'm using the terminal cus I can't figure Eclipse out. I'm getting closer but it's taking forever.

This is what I have

``````input the number, please:
3
---
/   \
/      \
/         \
\          /
---
\        /
---
\      /
---
``````

I need to bring the right side in and only have the --- once in the bottom.
Last edited by Seef at Sep 16, 2008,
Nobody? This should be easy.
What's wrong with my code? :P